Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * File: Yahtzee.java
- * ------------------
- * This program plays the game Yahtzee.
- * Names: Disha Dasgupta and Amanda Tu
- * Section Leader: Joshua Spayd
- */
- import acm.io.*;
- import acm.program.*;
- import acm.util.*;
- public class Yahtzee extends GraphicsProgram implements YahtzeeConstants {
- /* These constants specify the scores for any special category where the score is the same
- * fixed number every time (Full House, Yahtzee, Small Straight, and Large Straight). */
- public static final int FULL_HOUSE_SCORE = 25;
- public static final int YAHTZEE_SCORE = 50;
- public static final int SMALL_STRAIGHT_SCORE = 30;
- public static final int LARGE_STRAIGHT_SCORE = 40;
- public static final int BONUS_THRESHOLD = 63;
- public static final int BONUS_AMOUNT = 35;
- public static void main(String[] args) {
- new Yahtzee().start(args);
- }
- public void run() {
- IODialog dialog = getDialog();
- nPlayers = dialog.readInt("Please enter the number of players.");
- playerNames = new String[nPlayers];
- for (int playerNum = 1; playerNum <= nPlayers; playerNum++) {
- playerNames[playerNum - 1] = dialog.readLine("Enter name for player " + playerNum);
- }
- display = new YahtzeeDisplay(getGCanvas(), playerNames);
- scoreCardMatrix = new int [N_CATEGORIES + 1][nPlayers];
- for(int round = 1; round <= 2; round++) {
- for (int turn = 1; turn <= nPlayers; turn++) {
- player = turn;
- playGame();
- }
- }
- displayConcludingScores();
- }
- private void playGame() {
- display.printMessage(playerNames [player - 1] + ", please click 'Roll Dice' button to roll the dice");
- display.waitForPlayerToClickRoll(player);
- runFirstRoll();
- for(int i = 2; i <= 3; i++) {
- runSubsequentRoll();
- }
- display.printMessage(playerNames [player - 1] + ", select a category for this roll");
- chooseCategoryToCheck();
- }
- private void runFirstRoll() {
- for (int i = 0; i <= N_DICE - 1; i++) {
- diceResult[i] = rgen.nextInt(1,6);
- }
- display.displayDice(diceResult);
- }
- private void runSubsequentRoll() {
- display.printMessage(playerNames [player - 1] + ", please select the dice you wish to re-roll and click 'Roll Again'");
- display.waitForPlayerToSelectDice();
- rerollSelectedDice();
- display.displayDice(diceResult);
- }
- private void rerollSelectedDice() {
- for (int dice = 0; dice <= N_DICE - 1; dice++) {
- if (display.isDieSelected(dice) == true) {
- diceResult[dice] = rgen.nextInt(1,6);
- }
- }
- }
- private void chooseCategoryToCheck() {
- category = display.waitForPlayerToSelectCategory();
- if(category == ONES || category == TWOS || category == THREES || category == FOURS || category == FIVES || category == SIXES) {
- checkUpperSectionCategories();
- } else {
- checkLowerSectionCategories();
- }
- }
- private void checkUpperSectionCategories() {
- resetAllCounters();
- for(int categoryNum = ONES; categoryNum <= SIXES; categoryNum++){
- if(category == categoryNum) {
- for(int i = 0; i <= diceResult.length - 1; i++) {
- if(diceResult[i] == categoryNum) {
- score += diceResult[i];
- }
- }
- }
- }
- display.updateScorecard(category, player, score);
- updateOngoingScore();
- }
- private void checkLowerSectionCategories() {
- resetAllCounters();
- checkThreeOfAKind();
- checkFourOfAKind();
- checkFullHouse();
- checkSmallStraight();
- checkLargeStraight();
- checkYahtzee();
- checkChance();
- updateOngoingScore();
- }
- private void checkThreeOfAKind() {
- if(category == THREE_OF_A_KIND){
- readAllDiceValues();
- if (onesCounter >= 3 || twosCounter >= 6 || threesCounter >= 9 || foursCounter >= 12 || fivesCounter >= 15 || sixesCounter >= 18) {
- score = onesCounter + twosCounter + threesCounter + foursCounter + fivesCounter + sixesCounter;
- }
- display.updateScorecard(category, player, score);
- }
- }
- private void checkFourOfAKind() {
- if(category == FOUR_OF_A_KIND){
- readAllDiceValues();
- if (onesCounter >= 4 || twosCounter >= 8 || threesCounter >= 12 || foursCounter >= 16 || fivesCounter >= 20 || sixesCounter >= 24) {
- score = onesCounter + twosCounter + threesCounter + foursCounter + fivesCounter + sixesCounter;
- }
- }
- }
- private void checkFullHouse() {
- if (category == FULL_HOUSE) {
- readAllDiceValues();
- if (onesCounter >= 2) {
- if (twosCounter >= 6 || threesCounter >= 9 || foursCounter >= 12 || fivesCounter >= 15 || sixesCounter >= 18) {
- score = FULL_HOUSE_SCORE;
- }
- }
- if (twosCounter >= 4){
- if (onesCounter >= 3 || threesCounter >= 9 || foursCounter >= 12 || fivesCounter >= 15 || sixesCounter >= 18) {
- score = FULL_HOUSE_SCORE;
- }
- }
- if (threesCounter >= 6){
- if (onesCounter >= 3 || twosCounter >= 6 || foursCounter >= 12 || fivesCounter >= 15 || sixesCounter >= 18) {
- score = FULL_HOUSE_SCORE;
- }
- }
- if (foursCounter >= 8){
- if (onesCounter >= 3 || twosCounter >= 6 || threesCounter >= 9 || fivesCounter >= 15 || sixesCounter >= 18) {
- score = FULL_HOUSE_SCORE;
- }
- }
- if (fivesCounter >= 10){
- if (onesCounter >= 3 || twosCounter >= 6 || threesCounter >= 9 || foursCounter >= 12 || sixesCounter >= 18) {
- score = FULL_HOUSE_SCORE;
- }
- }
- if (sixesCounter >= 12){
- if (onesCounter >= 3 || twosCounter >= 6 || threesCounter >= 9 || foursCounter >= 12 || fivesCounter >= 15 || twosCounter >= 6) {
- score = FULL_HOUSE_SCORE;
- }
- }
- display.updateScorecard(category, player, score);
- }
- }
- private void checkSmallStraight() {
- if (category == SMALL_STRAIGHT) {
- readAllDiceValues();
- if (onesCounter >= 1 && twosCounter >= 2 && threesCounter >= 3 && foursCounter >= 4) {
- score = SMALL_STRAIGHT_SCORE;
- }
- if (twosCounter >= 2 && threesCounter >= 3 && foursCounter >= 4 && fivesCounter >= 5) {
- score = SMALL_STRAIGHT_SCORE;
- }
- if (threesCounter >= 3 && foursCounter >= 4 && fivesCounter >= 5 && sixesCounter >= 6) {
- score = SMALL_STRAIGHT_SCORE;
- }
- }
- display.updateScorecard(category, player, score);
- }
- private void checkLargeStraight() {
- if (category == LARGE_STRAIGHT) {
- readAllDiceValues();
- if (onesCounter == 1 && twosCounter == 2 && threesCounter == 3 && foursCounter == 4 && fivesCounter == 5) {
- score = LARGE_STRAIGHT_SCORE;
- }
- if (twosCounter == 2 && threesCounter == 3 && foursCounter == 4 && fivesCounter == 5 && sixesCounter == 6) {
- score = LARGE_STRAIGHT_SCORE;
- }
- }
- display.updateScorecard(category, player, score);
- }
- private void checkYahtzee() {
- if(category == YAHTZEE) {
- for (int i = 1; i < N_DICE; i++) {
- if(diceResult[i] == diceResult[i-1]) {
- score = YAHTZEE_SCORE;
- }
- }
- }
- }
- private void checkChance() {
- if(category == CHANCE){
- readAllDiceValues();
- score = onesCounter + twosCounter + threesCounter + foursCounter + fivesCounter + sixesCounter;
- display.updateScorecard(category, player, score);
- }
- }
- private void readAllDiceValues() {
- for (int i = 0; i < N_DICE; i++) {
- if(diceResult[i] == 1) {
- onesCounter++;
- } else if (diceResult[i] == 2) {
- twosCounter+=2;
- } else if (diceResult[i] == 3) {
- threesCounter+=3;
- } else if (diceResult[i] == 4) {
- foursCounter+=4;
- } else if (diceResult[i] == 5) {
- fivesCounter+=5;
- } else if (diceResult[i] == 6) {
- sixesCounter+=6;
- }
- }
- }
- private void resetAllCounters() {
- score = 0;
- onesCounter = 0;
- twosCounter = 0;
- threesCounter = 0;
- foursCounter = 0;
- fivesCounter = 0;
- sixesCounter = 0;
- }
- private void updateOngoingScore() {
- scoreCardMatrix [category][player - 1] = score;
- scoreCardMatrix [TOTAL][player - 1] += score;
- display.updateScorecard(category, player, score);
- display.updateScorecard(TOTAL, player, scoreCardMatrix [TOTAL][player - 1]);
- }
- private void calculateUpperSectionScore() {
- for (int player = 1; player <= nPlayers; player++) {
- for(int i = ONES; i <= SIXES; i++) {
- scoreCardMatrix [UPPER_SCORE][player - 1] = scoreCardMatrix [i] [player - 1];
- display.updateScorecard(UPPER_SCORE, player, scoreCardMatrix [UPPER_SCORE][player - 1]);
- }
- }
- }
- private void calculateLowerSectionScore() {
- for (int player = 1; player <= nPlayers; player++) {
- for(int i = THREE_OF_A_KIND; i <= CHANCE; i++) {
- scoreCardMatrix [LOWER_SCORE][player - 1] = scoreCardMatrix [i] [player - 1];
- display.updateScorecard(LOWER_SCORE, player, scoreCardMatrix [LOWER_SCORE][player - 1]);
- }
- }
- }
- private void calculateUpperBonus() {
- if(scoreCardMatrix [UPPER_SCORE][player - 1] >= BONUS_THRESHOLD) {
- scoreCardMatrix [UPPER_BONUS][player - 1] = BONUS_AMOUNT;
- display.updateScorecard(UPPER_BONUS, player, scoreCardMatrix [UPPER_BONUS][player - 1]);
- }
- }
- private void calculateFinalScore() {
- for (int player = 1; player <= nPlayers; player++) {
- scoreCardMatrix [TOTAL][player - 1] += scoreCardMatrix [UPPER_BONUS] [player - 1];
- display.updateScorecard(TOTAL, player, scoreCardMatrix [TOTAL][player - 1]);
- }
- }
- private void determineWinner() {
- int highestValue = scoreCardMatrix [TOTAL][0];
- for (int player = 1; player <= nPlayers; player++) {
- if (scoreCardMatrix [TOTAL][player - 1] > highestValue) {
- highestValue = scoreCardMatrix [TOTAL][player - 1];
- }
- }
- display.printMessage("You win with a score of: " + highestValue);
- }
- private void displayConcludingScores () {
- calculateUpperSectionScore();
- calculateLowerSectionScore();
- calculateUpperBonus();
- calculateFinalScore();
- determineWinner();
- }
- /* Private instance variables */
- private int nPlayers;
- private String[] playerNames;
- private YahtzeeDisplay display;
- private RandomGenerator rgen = new RandomGenerator();
- private int[] diceResult = new int[N_DICE];
- private int category;
- private int score = 0;
- private int player;
- private int onesCounter = 0;
- private int twosCounter = 0;
- private int threesCounter = 0;
- private int foursCounter = 0;
- private int fivesCounter = 0;
- private int sixesCounter = 0;
- private int scoreCardMatrix [][];
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement