Advertisement
MrDoyle

Do-While) NIM

Nov 17th, 2020
674
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.10 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class Main {
  3.  
  4.     public static void main(String[] args) {
  5.         Scanner keyboard = new Scanner(System.in);
  6.  
  7.         System.out.print("Player 1, enter your name: ");
  8.         String player1Name = keyboard.next();
  9.  
  10.         System.out.print("Player 2, enter your name: ");
  11.         String player2Name = keyboard.next();
  12.  
  13.         int pileA = 3;
  14.         int pileB = 3;
  15.         int pileC = 3;
  16.         String Player = player1Name;
  17.         int pileReduce = 0;
  18.  
  19.         do {
  20.             // displays the amount in each pile
  21.             System.out.println("\nA: " + pileA + "\tB: " + pileB + "\tC: " + pileC);
  22.             System.out.print(Player + ", choose a pile: ");
  23.             String pileChoose = keyboard.next();
  24.  
  25.             while (!pileChoose.equals("A") && !pileChoose.equals("B")&& !pileChoose.equals("C")) { // Not choosing given piles
  26.                 System.out.println("Please only select pile A , B or C. Try again");
  27.                 pileChoose = keyboard.next();
  28.             }
  29.  
  30.             // choosing an empty pile
  31.             while (pileChoose.equals("A") && (pileA == 0) || (pileChoose.equals("B")&& (pileB == 0)) || (pileChoose.equals("C")&& (pileC == 0))){
  32.                 System.out.println("Nice try " + Player + ". That pile is empty. Choose again: ");
  33.                 pileChoose = keyboard.next();
  34.             }
  35.  
  36.             System.out.print("How many to remove from pile " + pileChoose + " : ");
  37.             pileReduce = keyboard.nextInt();
  38.  
  39.             if (pileReduce < 0){ // negative counter choice
  40.                 System.out.print("You must choose at least 1 counter. Try again: ");
  41.                 pileReduce = keyboard.nextInt();
  42.             } else if (pileChoose.equals("A")) {
  43.                 while ((pileA - pileReduce) < 0) { //neg counter number
  44.                     System.out.println("Pile " + pileChoose + " doesn´t have that many. Try again: ");
  45.                     pileReduce = keyboard.nextInt();
  46.                 }
  47.                 pileA = pileA - pileReduce;
  48.             }else if (pileChoose.equals("B")){
  49.                 while ((pileB - pileReduce) < 0){  //neg counter number
  50.                     System.out.println("Pile " + pileChoose + " doesn´t have that many. Try again: ");
  51.                     pileReduce = keyboard.nextInt();
  52.                 }
  53.                 pileB = pileB - pileReduce;
  54.             } else if (pileChoose.equals("C")){
  55.                 while ((pileC - pileReduce) < 0){ //neg counter number
  56.                     System.out.println("Pile " + pileChoose + " doesn´t have that many. Try again: ");
  57.                     pileReduce = keyboard.nextInt();
  58.                 }
  59.                 pileC = pileC - pileReduce;
  60.             }
  61.  
  62.             //switch players
  63.             if (Player == player1Name){
  64.                 Player = player2Name;
  65.             } else {
  66.                 Player = player1Name;
  67.             }
  68.  
  69.             } while (pileA >0 || pileB >0 || pileC > 0); //keep piles above zero
  70.  
  71.         //winning condition
  72.         System.out.println("\n" +Player + ", there are no counters left, so you WIN!");
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement