2016-10-21 27 views
2

我每次運行該程序,然後輸入一個樁圈數的問題,我得到一個「類型錯誤:樁[pileChoice]是未定義」的錯誤。我已經嘗試過多次調試,但仍然無法正常工作。我有我得到這個JavaScript程序執行

var piles = [ 
    {name: 'Pile A', circles: 'ooooo'}, 
    {name: 'Pile B', circles: 'ooooo'}, 
    {name: 'Pile C', circles: 'ooooo'} 
]; 

function boardPrint(){ 
    console.log("NIM"); 
    for(var i = 0; i < piles.length; i++) { 
    console.log(piles[i].name + ": " + piles[i].circles); 
    } 
} 

function getUserInput(){ 
    return prompt("Enter the letter for the pile (A-C) and the number of stones you want to remove (1-5). Example: A3").toLowerCase(); 
} 

function userMove(){ 
    var pileIdx = 0; 
    var valid = false; 
    var numToRemove = 0; 

    while(!valid) { 
    var gameIns = getUserInput(); // This will now get called multiple times until user enters valid input 
    var pileChoice = gameIns[0]; // This makes 'A' turn into 'a', which makes further logic easier. 

    // I rebuilt this part of the function to be a bit cleaner and to show you how switch statements could be used 
    switch(pileChoice){ 
     case 'a': 
     pileIdx = 0; 
     valid = true; 
     break; 
     case 'b': 
     pileIdx = 1; 
     valid = true; 
     break; 
     case 'c': 
     pileIdx = 2; 
     valid = true; 
     break; 
     default: 
     alert('Error! Invalid input.'); 
    } 
    numToRemove = Math.min(gameIns[1],piles[pileChoice].circles.length); // This way, they can't select a number that is greater than the number remaining in the pile. 
    } 

    piles[pileIdx].circles = piles[pileIdx].circles.slice(numToRemove); 
} 

function computerMove(move){ 
    // Task 1: pick a pile 

    var pileIdx = 0; 

    if(piles[0].circles.length > 0) { // tests for whether there are circles left in pile A 
    piles[0].circles = piles[0].circles.slice(pileIdx); // do something 

    } else if(piles[1].circles.length > 0) { 
    piles[1].circles = piles[1].circles.slice(pileIdx); // do something 

    } else if(piles[2].circles.length > 0) { 
    piles[2].circles = piles[2].circles.slice(pileIdx); // do something 
    } 

    // Task 2: pick a number to remove from the pile 

    // Optional: see how many piles are left and base your number to remove on that 
    //var pilesCount = 0; 

    // [some logic for counting piles] 

    // Otherwise, just remove all that are remaining from a pile 
    //var numToRemove = 0; 

    if (pilesCount > 1){ 
    // select a number to remove 
    } 
    else { 
    // select another number to remove 
    } 

    piles[pileIdx].circles = piles[pileIdx].circles.slice(numToRemove); 
} 

while(true) { 
    boardPrint(); 
    userMove(); 

    if (boardEmpty() === true) { 
    boardPrint(); 
    console.log("You win!"); 
    break; 
    } 

    boardPrint(); 
    computerMove(); 

    if (boardEmpty() === true) { 
    boardPrint(); 
    console.log("Computer wins!"); 
    break; 
    } 
} 

function boardEmpty() { 
    // Check if the board is empty 
} 
+0

'gameIns [0]'只會讓你的'提示的第一個字符()'迴應。 – PHPglue

+0

'prompt()'返回一個字符串。當你在一個String上運行數組符號時,它就像'String.charAt()'。另外,'numToRemove = Math.min(gameIns [1],樁[pileChoice] .circles.length)的意義是什麼?''?無論如何,單個數字的'Math.min()'就是這個數字。在該行右邊 – PHPglue

+0

以前'樁[pileChoice]'添加這個'的console.log(pileChoice,樁);'這可以幫助你更好地理解什麼錯誤,那你可能需要將其更新到'樁[pileIdx] ' –

回答

0

我認爲這個問題是在這裏:

var piles = [ 
    {name: 'Pile A', circles: 'ooooo'}, 
    {name: 'Pile B', circles: 'ooooo'}, 
    {name: 'Pile C', circles: 'ooooo'} 
]; 

再後來

numToRemove = Math.min(gameIns[1],piles[pileChoice].circles.length); 

我覺得你的意思該是piles[pileIdx]pileChoice將成爲一個字母,並且您有一個對象數組,因此您需要使用索引訪問正確的對象。

1

在你userMove功能,在while循環您嘗試設置numToRemove具有以下行的末尾:

numToRemove = Math.min(gameIns[1],piles[pileChoice].circles.length); // This way, they can't select a number that is greater than the number remaining in the pile. 

但pileschoice要麼是 'A', 'B' 或 'C',對?這就是爲什麼你在你的交換機中計算了pileIdx的原因。您需要使用它,因爲您定義堆的方式不能用'a','b'或'c'索引。

0

您正在嘗試在gameIns [0]中引用piles[pileChoice],如樁['a'],樁['b']等,但樁數組未被格式化,因爲樁是一組對象。

你可以嘗試重新思考的方式,你的模型樁或嘗試像得到circles.length以下解決方法:

// define pileCircleCount 
var pileCircleCount; 

// go over your piles array 
piles.forEach(function(pile) { 

// get the pile name value and retrieve the last char which is A/B/C 
// and convert it to lowercase 
var pileId = pile.name.substr(pile.name.length - 1).toLowerCase(); 

// now match it up with the pileChoice, if a match is found get its 
// circle length and set it to the pileCircleCount 
if(pileId === pileChoice) { 
    pileCircleCount = pile.circles.length; 
} 
}); 

numToRemove = Math.min(gameIns[1], pileCircleCount); 
相關問題