cs106a – Assignment #5 – Extra Task #2

The complete specification of assignment #5 can be found as part of the stream at iTunes.

Incorporate the bonus scores for multiple Yahtzees in a game

As long as you have not entered a 0 in the Yahtzee box, the rules of the game give you a bonus chip worth 100 points for each additional Yahtzee you roll during the same game.

Start by a new array as instance variable which indicates if additional Yahtzees are allowed – meaning it was not set to 0 before – and initialize it:

	private boolean[] allowAdditionalYahtzees;

	private void playGame() {
	        ...
		allowAdditionalYahtzees = new boolean[nPlayers];
		for (int player = 0; player < nPlayers; player++) {
			allowAdditionalYahtzees[player] = true;
		}		
	        ...
	}

After the score for the chosen category has been calculated, check if the current category is the Yahtzee one and disable the Yahtzee bonus when the Yahtzee score equals zero. For any other category, if the bonus is still possible, and the Yahtzee has already been scored, add the bonus if the current roll is another Yahtzee:

	private void updateScore(int player) {
                ....
		int score = calculateScore(category);

		if (category == YAHTZEE) {
			if (score == 0) {
				allowAdditionalYahtzees[player] = false;
			}			
		} else {
			if (allowAdditionalYahtzees[player] 
			    && usedCategories[player][YAHTZEE] 
			    && (calculateOfAKindValues(5) > 0)) {
				score += SCORE_ADDITONAL_YAHTZEES;
			}
		}
                ....
	}

… and again use a constant for the bonus value:

private static final int SCORE_ADDITONAL_YAHTZEES = 100;

The code for this assignment is available on github.

Facebooktwittergoogle_plusredditpinterestlinkedintumblrmailFacebooktwittergoogle_plusredditpinterestlinkedintumblrmail

Flattr this!

Leave a Reply

Your email address will not be published. Required fields are marked *