21 Sticks Game


SUBMITTED BY: knightley

DATE: Sept. 3, 2015, 9:12 a.m.

FORMAT: Java

SIZE: 1.3 kB

HITS: 454

  1. import java.util.Scanner;
  2. //Player recognizable; Wrong number protection
  3. public class Practive{
  4. public static void main(String[] args){
  5. int remain = 21;
  6. Scanner scan = new Scanner(System.in);
  7. System.out.println("The rules of this game are simple. You start with 21 sticks, and two players take turns");
  8. System.out.println("either taking one or two sticks. The player who takes the last stick loses.");
  9. System.out.println();
  10. while (remain > 0){
  11. System.out.println("Player A's turn: take 1 or 2 sticks. The number of reamining sticks is "+remain);
  12. int inputA = scan.nextInt();
  13. if (inputA >=2)
  14. {
  15. inputA = 2;
  16. }
  17. else if (inputA <=1)
  18. {
  19. inputA = 1;
  20. }
  21. remain = remain-inputA;
  22. if (remain == 0){
  23. System.out.println ("You lose.");
  24. }
  25. else {
  26. System.out.println("Player B's turn: take 1 or 2 sticks. The number of reamining sticks is "+remain);
  27. int inputB = scan.nextInt();
  28. remain = remain-inputB;
  29. if (remain ==0){
  30. System.out.println ("You lose");
  31. }
  32. }
  33. if ( remain ==0){
  34. System.out.println("Game is over");
  35. }
  36. }
  37. }
  38. }

comments powered by Disqus