Last active
August 17, 2016 20:10
-
-
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] \
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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