Skip to content

Instantly share code, notes, and snippets.

@tenifni
Last active August 17, 2016 20:10
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tenifni/d99c36f16c2367b7f5f5 to your computer and use it in GitHub Desktop.
Save tenifni/d99c36f16c2367b7f5f5 to your computer and use it in GitHub Desktop.
[Game] 21 Sticks (with Player distinction). Rules of game: start with 21 sticks, and two players take turns either taking one or two sticks. The player who takes the last stick loses. [simplified: no mis-numbering prevention] \
import java.util.Scanner;
//Player recognizable; Wrong number protection
public class Practive{
public static void main(String[] args){
int remain = 21;
Scanner scan = new Scanner(System.in);
System.out.println("The rules of this game are simple. You start with 21 sticks, then two players take turns alternatively");
System.out.println("either taking one or two sticks. The player who takes the last stick loses.");
System.out.println();
while (remain > 0){
System.out.println("Player A's turn: take 1 or 2 sticks. The number of reamining sticks is "+remain);
int inputA = scan.nextInt();
if (inputA >=2)
{
inputA = 2;
}
else if (inputA <=1)
{
inputA = 1;
}
remain = remain-inputA;
if (remain == 0){
System.out.println ("You lose!");
}
else {
System.out.println("Player B's turn: take 1 or 2 sticks. The number of reamining sticks is "+remain);
int inputB = scan.nextInt();
remain = remain-inputB;
if (remain ==0){
System.out.println ("You lose!");
}
}
if ( remain ==0){
System.out.println("Game's over.");
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment