Im doing an exercise I found on the internet to simulate the game "nim". The idea of the game is to get the other player to pick up the last item. The problem I am having is after player 1 makes a selection of which pile to pick from and how many items to pick up from that pile. Initially that works fine, however when it goes to player 2's turn, always picks he previous player pile regardless of the pile they pick, it deducts the amount they specify from the same pile as player 1. This only happened after I added the lower-case option in the if statement. Could someone explain why it always picks the previous players pile?
while(pileA > 0 || pileB > 0 || pileC > 0){
System.out.println(playerOne+", which pile would you like to pick from?");
String choice = input.next();
System.out.println("How many cards would you like to take from pile "+choice+"?");
int amount = input.nextInt();
if(choice.equals("A") || choice.equals("a")){
pileA = pileA - amount;
System.out.println("A: "+pileA+" B: "+pileB+" C: "+pileC);
}
else if(choice.equals("B") || choice.equals("b")){
pileB = pileB - amount;
System.out.println("A: "+pileA+" B: "+pileB+" C: "+pileC);
}
else if(choice.equals("C") || choice.equals("c")){
pileC = pileC - amount;
System.out.println("A: "+pileA+" B: "+pileB+" C: "+pileC);
}
else{
System.out.println("ERROR");
}
System.out.println(playerTwo+", which pile would you like to pick from?");
String choiceTwo = input.next();
System.out.println("How many cards would you like to take from pile "+choiceTwo+"?");
amount = input.nextInt();
if(choiceTwo.equals("A") || choice.equals("a")){
pileA = pileA - amount;
System.out.println("A: "+pileA+" B: "+pileB+" C: "+pileC);
}
else if(choiceTwo.equals("B") || choice.equals("b")){
pileB = pileB - amount;
System.out.println("A: "+pileA+" B: "+pileB+" C: "+pileC);
}
else if(choiceTwo.equals("C") || choice.equals("c")){
pileC = pileC - amount;
System.out.println("A: "+pileA+" B: "+pileB+" C: "+pileC);
}
else{
System.out.println("ERROR");
}
}
Aucun commentaire:
Enregistrer un commentaire