import java.util.Random; import java.util.Scanner; public class Pig2 { // returns true if the user wants to roll public static boolean askHuman() { Scanner in = new Scanner(System.in); int number = 0; while (number != 1 && number != 2) { System.out.println("Would you like to roll again?"); System.out.println("1. Yes"); System.out.println("2. No"); System.out.print(": "); number = in.nextInt(); } return number == 1; } // return true for rolling, false for stay public static boolean askComputer(int playerScore, int computerScore, int roundPoints, int rolls) { // if we have done less than three rolls, for sure roll if (rolls < 4) { return true; } int difference = computerScore - playerScore; // if we are now ahead, stop rolling if (difference + roundPoints > 0) { return false; } // at this point, flip a coin and decide based off that Random rng = new Random(); int num = rng.nextInt(1, 3); if (num == 1) { return true; } else { return false; } } public static int dieRoll() { Random rng = new Random(); return rng.nextInt(1, 7); } public static int turn(int playerScore, int computerScore, boolean humanPlayer) { boolean rolling = true; int roundPoints = 0; int rolls = 0; while (rolling) { int roll = dieRoll(); rolls++; System.out.println("The roll is " + roll + "."); roundPoints += roll; if (roll == 1) { roundPoints = 0; rolling = false; if (humanPlayer) { System.out.println("Sorry you busted!"); } else { System.out.println("The computer busted"); } } else { if (humanPlayer) { System.out.println("So far this round you have " + roundPoints + " points."); rolling = askHuman(); } else { System.out.println("Computer has " + roundPoints + " points so far this round."); rolling = askComputer(playerScore, computerScore, roundPoints, rolls); try { Thread.sleep(1000); } catch (InterruptedException e) { // ignore } } } } if (humanPlayer) { System.out.println("You finish this round with " + roundPoints + " points."); } else { System.out.println("Computer finishes this round with " + roundPoints + " points."); } return roundPoints; } public static void game() { int playerScore = 0; int computerScore = 0; boolean playerTurn; // pick first player randomly Random rng = new Random(); if (rng.nextInt(1, 3) == 1) { playerTurn = true; } else { playerTurn = false; } while (playerScore < 100 && computerScore < 100) { System.out.println("Player has " + playerScore + " points."); System.out.println("Computer has " + computerScore + " points."); if (playerTurn) { System.out.println("It is your turn."); } else { System.out.println("It is the computer turn."); } // do the turn int points = turn(playerScore, computerScore, playerTurn); // add in points if (playerTurn) { playerScore += points; } else { computerScore += points; } // switch turns playerTurn = !playerTurn; System.out.println(); } // check who won if (playerScore >= 100) { System.out.println("Congratulations! You won with " + playerScore + " points!"); } else { System.out.println("Sorry, the computer won wit " + computerScore + " points :("); } } public static void main(String[] args) { game(); } }