Reputation: 9
I'm having trouble figuring out how to get my loop to make the computer play after the human plays.
here's the code: (Note: Sorry for any indentation issues. this site doesn't like my copy and paste from textpad.)
import java.util.*;
class GameOfNim {
public static void main(String [] args) {
Scanner in = new Scanner (System.in);
System.out.println("Welcome to the game of Nim!");
System.out.println("------------------------------");
System.out.println("The rules are simple: ");
System.out.println("1. You must take one but at most half of the marbles.");
System.out.println("2. The player that takes the last marble loses!");
System.out.println("-------------------------------------------------------");
System.out.println("To start the game choose a difficulty where 0 = hard & 1 = easy: ");
int marbles, player, difficulty, pick;
marbles = (int)(Math.random()*90+10);
player = (int)(Math.random()+0.5);
difficulty = (int)(Math.random()+0.5);
pick = (int)(in.nextInt());
if(difficulty == 0) {
System.out.println("The Computer is now in Beast Mode!");
}else if(difficulty == 1) {
System.out.println("The Computer is now in easy mode.");
}
System.out.println("The pile of marbles has: " + marbles + " total marbles. Game On!");
{while(marbles>0) {
if(player==0) { //person
System.out.print("Pick some marbles: ");
pick = in.nextInt(); }
if (pick >= 1 && pick <= marbles/2) {
marbles = marbles - pick; System.out.println(marbles+"left"); }
else if(pick < 1 || pick > marbles/2 && marbles > 1) {
System.out.println("Error: Illegal Move.");
player = 0; continue; }
else { marbles = marbles - pick; System.out.println(marbles + "left."); }}
if(player == 1 && difficulty ==1) { System.out.println("Computer's turn to pick");
pick = (int)(Math.random()*marbles/2+1);
marbles = marbles - pick;
System.out.println("computer picks: " + pick + "marbles." + "There are: " + marbles + " marbles left.");}
else if(player == 1 && difficulty == 0) {
System.out.println("Computer's turn to pick");
if(marbles > 63) {pick = marbles - 63; marbles = marbles - pick;}
else if(marbles > 31) {pick = marbles - 32; marbles = marbles - pick;} else if(marbles > 15) {pick = marbles - 15; marbles = marbles - pick;} else if(marbles > 7) {pick = marbles - 7; marbles = marbles - pick;} else if(marbles > 3) {pick = marbles - 3; marbles = marbles - pick;}
else{ pick = (int)(Math.random()*marbles/2+1); marbles = marbles - pick;
System.out.println("Computer picks: " + pick + "marbles." + "There are: " + marbles + " marbles left."); }}
{if(player==0)//switching
player=1; //code
else player=0; } {if(player==0)
System.out.println("Computer: 'I give up! You win!");
else System.out.println("Computer: '...You are pathetic...'"); }}}}
Upvotes: 0
Views: 4310
Reputation: 245
The problem lies in this if statement:
if (player == 1 && difficulty == 1);
You never have player = 1, therefore it will never enter the if block.
RECOMMENDATION
I would recommend getting rid of the player variable completely.
Turn the game into its own function which returns a boolean variable based on who wins. With your continue statement if the player enters an invalid number of stones, there is no need to check whose turn it is in the function as it will naturally only progress to the computer's turn when the player has moved.
So your program will look like this:
public static boolean game(/*Suitable parameters(ie. number of stones etc.*/)) {
while (marbles > 0) {
//Play game
}
}
public static void main(String[] args) {
//Set up stuff
if (game(/*Suitable parameters(ie. number of stones etc.*/)) {
//Player wins
} else {
//Computer wins
}
}
Inside the game, probably the cleanest way to not have to worry about your player variable would be to have a while loop inside the game loop which only exits if the player makes a valid move:
while (marbles > 0) {
while (true) {
//Player makes move
if (validMove) {
//do move
break;
} else {
//Output error and then the loop repeats itself
}
}
//Computer move
}
Upvotes: 1
Reputation: 3360
{while(marbles>0) {
if(player==0) { //person
System.out.print("Pick some marbles: ");
pick = in.nextInt(); }
if (pick >= 1 && pick <= marbles/2) {
marbles = marbles - pick; System.out.println(marbles+"left"); }
After you pick the marble, you should change the player to computer.
{while(marbles>0) {
if(player==0) { //person
System.out.print("Pick some marbles: ");
pick = in.nextInt(); }
if (pick >= 1 && pick <= marbles/2) {
marbles = marbles - pick; System.out.println(marbles+"left");
player = 1;
}
Actually though I would prefer to put the player switch to end of the if else statements, so I could decide who is the winner. Just remember that you should change the player = 0 or 1 after your / computer "legal pick". That should be the game logic on how you change your turn.
{while(marbles>0) {
if(player==0) { //person
System.out.print("Pick some marbles: ");
pick = in.nextInt(); }
if (pick >= 1 && pick <= marbles/2) {
marbles = marbles - pick; System.out.println(marbles+"left"); }
else if(pick < 1 || pick > marbles/2 && marbles > 1) {
System.out.println("Error: Illegal Move.");
player = 0; continue; }
else { marbles = marbles - pick; System.out.println(marbles + "left."); }}
// change to computer turn when there's still marble left
if (marbles > 0) {
player = 1;
}
if(player == 1 && difficulty ==1) { System.out.println("Computer's turn to pick");
pick = (int)(Math.random()*marbles/2+1);
marbles = marbles - pick;
System.out.println("computer picks: " + pick + "marbles." + "There are: " + marbles + " marbles left.");}
else if(player == 1 && difficulty == 0) {
System.out.println("Computer's turn to pick");
if(marbles > 63) {pick = marbles - 63; marbles = marbles - pick;}
else if(marbles > 31) {pick = marbles - 32; marbles = marbles - pick;} else if(marbles > 15) {pick = marbles - 15; marbles = marbles - pick;} else if(marbles > 7) {pick = marbles - 7; marbles = marbles - pick;} else if(marbles > 3) {pick = marbles - 3; marbles = marbles - pick;}
else{ pick = (int)(Math.random()*marbles/2+1); marbles = marbles - pick;
System.out.println("Computer picks: " + pick + "marbles." + "There are: " + marbles + " marbles left."); }}
// change turn to player when there is marble left still
if (marbles > 0) {
player = 0;
}
Then use a if else statement after breaking your while loop turns to false when the marbles left 0, meaning end of game to see.
// when the game stop on whichever player....
if (player = 1){
System.out.println("Computer won");
else
System.out.println("You won");
Upvotes: 0