Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.Scanner;
- public class Main {
- public static void main(String[] args) {
- Scanner keyboard = new Scanner(System.in);
- System.out.print("Player 1, enter your name: ");
- String player1Name = keyboard.next();
- System.out.print("Player 2, enter your name: ");
- String player2Name = keyboard.next();
- int pileA = 3;
- int pileB = 3;
- int pileC = 3;
- String Player = player1Name;
- int pileReduce = 0;
- do {
- // displays the amount in each pile
- System.out.println("\nA: " + pileA + "\tB: " + pileB + "\tC: " + pileC);
- System.out.print(Player + ", choose a pile: ");
- String pileChoose = keyboard.next();
- while (!pileChoose.equals("A") && !pileChoose.equals("B")&& !pileChoose.equals("C")) { // Not choosing given piles
- System.out.println("Please only select pile A , B or C. Try again");
- pileChoose = keyboard.next();
- }
- // choosing an empty pile
- while (pileChoose.equals("A") && (pileA == 0) || (pileChoose.equals("B")&& (pileB == 0)) || (pileChoose.equals("C")&& (pileC == 0))){
- System.out.println("Nice try " + Player + ". That pile is empty. Choose again: ");
- pileChoose = keyboard.next();
- }
- System.out.print("How many to remove from pile " + pileChoose + " : ");
- pileReduce = keyboard.nextInt();
- if (pileReduce < 0){ // negative counter choice
- System.out.print("You must choose at least 1 counter. Try again: ");
- pileReduce = keyboard.nextInt();
- } else if (pileChoose.equals("A")) {
- while ((pileA - pileReduce) < 0) { //neg counter number
- System.out.println("Pile " + pileChoose + " doesn´t have that many. Try again: ");
- pileReduce = keyboard.nextInt();
- }
- pileA = pileA - pileReduce;
- }else if (pileChoose.equals("B")){
- while ((pileB - pileReduce) < 0){ //neg counter number
- System.out.println("Pile " + pileChoose + " doesn´t have that many. Try again: ");
- pileReduce = keyboard.nextInt();
- }
- pileB = pileB - pileReduce;
- } else if (pileChoose.equals("C")){
- while ((pileC - pileReduce) < 0){ //neg counter number
- System.out.println("Pile " + pileChoose + " doesn´t have that many. Try again: ");
- pileReduce = keyboard.nextInt();
- }
- pileC = pileC - pileReduce;
- }
- //switch players
- if (Player == player1Name){
- Player = player2Name;
- } else {
- Player = player1Name;
- }
- } while (pileA >0 || pileB >0 || pileC > 0); //keep piles above zero
- //winning condition
- System.out.println("\n" +Player + ", there are no counters left, so you WIN!");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement