import java.util.Random; import java.util.Scanner; public class Pig1 { public static int rollDie() { Random rng = new Random(); int num = rng.nextInt(6) + 1; return num; } public static int askLimit() { Scanner in = new Scanner(System.in); System.out.print("What do you want to play to? "); int limit = in.nextInt(); while (limit < 10 || limit > 500) { System.out.print("Please enter a number between 10 and 500: "); limit = in.nextInt(); } return limit; } // return true if user wants to roll public static boolean getUserDecision(int roundScore) { Scanner in = new Scanner(System.in); System.out.println("You have " + roundScore + " points so far this round."); System.out.print("Do you want to roll or stay? "); String response = in.next(); while (!response.equals("roll") && !response.equals("stay")) { System.out.println("Please enter \"roll\" or \"stay\""); System.out.print("Do you want to roll or stay? "); response = in.next(); } return response.equals("roll"); } public static boolean getAIDecision(int userScore, int compScore, int roundScore) { int target; int difference = compScore - userScore; // if we're within 10 points, play normally if (difference > -10 && difference < 10) { target = 10; } // if we're more than 10 points behind, play aggressibely else if (difference <= -10) { target = 20; } // if we're more than 10 points ahead, play safely else { target = 5; } if (roundScore >= target) { return false; } else { return true; } } public static void sleep() { try { Thread.sleep(500); } catch (InterruptedException e) { throw new RuntimeException(e); } } // play one round for user public static int computerRound(int userScore, int compScore) { int roundScore = 0; System.out.println("Computer has " + compScore + " points."); System.out.println("You have " + userScore + " points."); boolean rolling = getAIDecision(userScore, compScore, roundScore); while (rolling) { int die = rollDie(); System.out.println("The computer rolled a " + die + "."); sleep(); if (die == 1) { System.out.println("Computer busted!"); return 0; } else { // add die to score roundScore += die; } // ask if they want to roll again rolling = getAIDecision(userScore, compScore, roundScore); } return roundScore; } // play one round for user public static int userRound(int userScore, int compScore) { int roundScore = 0; System.out.println("You have " + userScore + " points."); System.out.println("Computer has " + compScore + " points."); boolean rolling = getUserDecision(roundScore); while (rolling) { int die = rollDie(); System.out.println("You rolled a " + die + "."); if (die == 1) { System.out.println("You busted!"); return 0; } else { // add die to score roundScore += die; } // ask if they want to roll again rolling = getUserDecision(roundScore); } return roundScore; } public static void game() { int userScore = 0; int computerScore = 0; int limit = askLimit(); while (userScore < limit && computerScore < limit) { System.out.println("Your turn!"); int points = userRound(userScore, computerScore); userScore += points; if (userScore >= limit) { break; } System.out.println("Computer turn"); points = computerRound(userScore, computerScore); computerScore += points; System.out.println("Computer got " + points + " points on its turn"); } if (userScore > computerScore) { System.out.println("Congratulations, you won!"); } else { System.out.println("Sorry you lost!"); } } public static void main(String[] args) { game(); } }