2016-10-21 4 views
2

Jedes Mal, wenn ich das Programm laufen und geben Sie einen Haufen und Kreis Nummer, bekomme ich einen "TypeError: pile [StapelWahl] ist undefined" Fehler. Ich habe versucht, es mehrmals zu debuggen, aber ich kann es immer noch nicht richtig funktionieren.Ich habe Probleme bekommen dieses Javascript-Programm ausführen

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]' würden Sie nur geben Sie den ersten Buchstaben der 'Aufforderung() 'Antwort. – PHPglue

+0

'prompt()' gibt einen String zurück. Wenn Sie die Array-Notation für einen String ausführen, ist das wie 'String.charAt()'. Auch was ist der Punkt von 'numToRemove = Math.min (gameIns [1], Stapel [pileChoice] .circles.length); '? Die 'Math.min()' einer einzelnen Zahl ist sowieso diese Nummer. – PHPglue

+0

in der Zeile direkt vor 'pile [pileChoice]' fügen Sie diese 'console.log (pileChoice, pilees);' Dies kann Ihnen helfen, besser zu verstehen, was schief läuft, und dass Sie es wahrscheinlich auf 'piles [pileIdx] aktualisieren müssen ' –

Antwort

0

Ich denke, die Frage ist hier:

Sie haben

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

und dann später

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

Ich glaube, Sie für das bedeutete piles[pileIdx] zu sein? pileChoice wird ein Buchstabe sein, und Sie haben ein Array von Objekten, so dass Sie auf den richtigen Index zugreifen müssen.

1

In Ihrer userMove Funktion am Ende der while-Schleife Sie versuchen numToRemove mit der folgenden Zeile zu setzen:

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. 

Aber pileschoice ist entweder 'a', 'b' oder 'c', Recht? Deshalb haben Sie in Ihrem Switch den Parameter pileIdx berechnet. Sie müssen es stattdessen verwenden, da die Art, wie Sie Pfähle definiert haben, nicht mit 'a', 'b' oder 'c' indiziert werden kann.

0

Sie versuchen, piles[pileChoice] wie Pfähle ['a'], Pfähle ['b'] usw. in gameIns [0] zu referenzieren, aber das Stapel-Array ist nicht auf diese Weise formatiert, da Stapel ein Array von Objekten ist .

könnten Sie versuchen, die Art und Weise Sie Modell Pfählen überdenken oder eine Abhilfe wie die folgenden für das Erhalten der circles.length versuchen:

// 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);