Created
January 29, 2019 09:13
-
-
Save techtide/4af037d5823b12e0b1dbdca842e5529f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Pig.java | |
* A game for AP CS. | |
* By Max and Arman. | |
*/ | |
public class Pig { | |
private static int currentPlayer = 1; | |
private static int player1Score = 0 ; | |
private static int player2Score = 0; | |
private static int tempPointsAccumualted = 0; | |
public static void main(String[] args) { | |
System.out.println("Pig\n\n"); | |
System.out.println("In this game you roll a die to accumulate and then score points.\n"); | |
System.out.println("On your turn, you can keep rolling as long as you want, accumulating more points. \nYou can stop at any time, scoring the points you've accumulated this turn.\nIf you ever roll a 1, you lose all of the points you've accumulated this turn (but not those you've previously scored)."); | |
System.out.println("The first payer to score 100 or more points wins.\n\n"); | |
while(true) { | |
printScorePrompt(); | |
System.out.println("You have accumulated " + tempPointsAccumualted + " points."); | |
System.out.print("Type s to stop, anything else to roll: "); | |
String prompt = StdIn.readString(); | |
if(prompt.equals("s") && currentPlayer == 1) { | |
player1Score += tempPointsAccumualted; | |
currentPlayer = 2; | |
checkIfWinner(); | |
tempPointsAccumualted = 0; | |
} else if(prompt.equals("s") && currentPlayer == 2) { | |
player2Score += tempPointsAccumualted; | |
currentPlayer = 1; | |
checkIfWinner(); | |
tempPointsAccumualted = 0; | |
} else { | |
rollDieAndPrint(); | |
} | |
} | |
} | |
private static void rollDieAndPrint() { | |
int roll = StdRandom.uniform(1, 6); | |
if(roll != 1) { | |
tempPointsAccumualted += roll; | |
System.out.println("You rolled a " + roll + "."); | |
} else if(roll == 1) { | |
System.out.println("You rolled a " + roll + ".\nYou do not score any points."); | |
tempPointsAccumualted = 0; | |
if(currentPlayer == 1){ | |
currentPlayer = 2; | |
System.out.println("switched: " + currentPlayer); | |
} | |
else if (currentPlayer == 2){ | |
System.out.println("switched: " + currentPlayer); | |
currentPlayer = 1; | |
} | |
} | |
} | |
private static void checkIfWinner() { | |
if(player1Score >= 100 || player2Score >= 100) { | |
System.out.println("\nGame Over. Player 1: " + player1Score + "; Player 2: " + player2Score + "."); | |
System.exit(0); | |
} | |
} | |
private static void printScorePrompt() { | |
System.out.println("---------------"); | |
System.out.println("Player " + currentPlayer); | |
System.out.println("---------------"); | |
System.out.println("Player 1: " + player1Score + "; Player 2: " + player2Score); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment