Scanner input = new Scanner (System.in); int numSticks = 21; int sticksTaken = 0; String playAgain = "y"; String answer = " "; boolean playersTurn = false; while(playAgain.equalsIgnoreCase("y")){ System.out.println("Don't end up with the last stick"); System.out.println("Would you like to go first? y/n"); answer = input.next(); if(answer.equalsIgnoreCase("y")){ playersTurn = true; } while(numSticks >= 0){ if(playersTurn == true){ System.out.println("There are " + numSticks + " sticks"); System.out.println("How many sticks would you like to take away (1 or 2) "); sticksTaken = input.nextInt(); if(sticksTaken > 2){ sticksTaken = 2; } if(sticksTaken < 1){ sticksTaken = 1; } numSticks = numSticks-sticksTaken; if(numSticks == 1){ System.out.println("Congratulation you won "); break; } if(numSticks <= 0){ System.out.println("Sorry you lose"); break; } playersTurn = false; } else { if((numSticks-2) %3==0 || numSticks-2==0){ numSticks = numSticks-1; System.out.println("The computer took 1 stick"); } else { numSticks = numSticks-2; System.out.println("The computer took 2 sticks"); } if(numSticks == 1){ System.out.println("Sorry you lose "); break; } playersTurn = true; } } System.out.println("Do you want to play again? "); playAgain = input.next(); if(playAgain.equalsIgnoreCase("n")){ System.out.println("Thanks for playing"); break; } }
JAVA - Stick Game!
Click 'Run The Code!', bottom right of the editor, to start the game!
Enter Answers in box below the console (after click on 'Run The Code!')
Be patient, while it compiles the code after starting the game!