And here are the Extensions
- Add a high score feature. Save the top ten highest scores and names to a file and make it persistent between runs of the program. Read the file when you start and print out the hall of fame. If a player gets a score that preempts one of the early high scores, congratulate them and update the file before you quit so it is recorded for next time.
- 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.
- Beef up your Yahtzee to the variant called “Triple Yahtzee.” In this variant, each player manages three simultaneous scorecard columns, as if they were playing for three players at once. The player can assign a roll to any one of their three columns. All entries are scored normally with respect to categories and validity, but the values in the second column are doubled, and the third column values are tripled. The player’s score consists of the sum of all three columns. This would make for a three-dimensional
array (an array of players who have any array of columns which are an array of score entries)—pretty tricky! Game play continues for 3 * 13 rounds, until all players have
filled in all entries in all three columns. The player with the highest total score is the winner.
Top scores
The way that I approached, displaying and saving the top scores was to use a BufferedReader, which allows to read a line by line of a file, so once the file was opened I could read each line of the top 10 scores, with the scores and the name of the top person being separated by a comma e.g. 10,genux so whilst I was reading in the top scores I could find the comma and then find out the lowest value which I could pass back to the main game, here is the java code for that
System.out.println("Top 10 scores :"); while (reader.ready()) { scoresTop10 = reader.readLine(); commaValue = scoresTop10.indexOf(","); lowestValue = Integer.parseInt(scoresTop10.substring(0,commaValue)); System.out.println("score : " + lowestValue +" name : "+scoresTop10.substring(commaValue+1)); } reader.close();
the reasons was so that once that game had finished I could see if the highest score was higher than the lowest score of the top 10 scores and if so update the scores file. To update the scores file I read in the scores into a two arrays, one being the score values and the other being the string of the names, but whilst reading in the scores, I inserted into the array where the new score and person name (that had played the game and scored a score that could be placed into the highest scores file), once done this then just wrote out to the file the new top 10 highest scores (by only writing out 10 of the array), this source code starts of with me inserting the new “score” and “username” into the scores/names array at the correct place, and the code below that uses the PrintWriter to out to the file.
... if (scoreChecker < score && !updated) { scores[insertNumber] = score; names[insertNumber] = username; insertNumber++; updated = true; } scores[insertNumber] = scoreChecker; names[insertNumber] = scoresTop10.substring(commaNumber+1); ... PrintWriter fileWriter = new PrintWriter(new FileWriter("top10.scores")); for (int i =0; i < 10; i++) fileWriter.println(scores[i]+","+names[i]); fileWriter.close(); }
Bonus Yahtzee
The bonus of yahtzee for multiple yahtzees (100 points extra), I did by when I was updating the scores within the function I checked to see if the score was above 0 (which would mean that there was a score for this yahtzee) and also that the previous score was above 0 as well (e.g. there was a previous yathzee) then increase the score by 100).
here is the source code for that part
else if (category == YahtzeeConstants.YAHTZEE && scoresUpdate[boardX][category] > 0 && score >0) { scoresUpdate[boardX][category] += (YAHTZEE_BONUS_CLIP*(boardX+1)); score = scoresUpdate[boardX][category]; return false; } else
Triple Yahtzee – x score cards
To allow for X amount of score cards ( did limit it to 3) but you could have 2 score cards and all that I did was to ask the user when they started the game to see how many score cards they wanted and then altered the scoring array to that amount. When it did come to adding the score to the board, I just questioned the user which score card to use, and then used that score card and times (*) the value accordingly. I did have to alter a couple of lines in functions to have the 3 dimensional array instead of 2 and also when updating the score cards had to alter it to place into the correct one, but could use most of the functions untouched due to how they was programmed.
I have included the score code in the zip file attached and also the PDF of the assignment requirements.
Tags: Yahtzee