• No results found

Project 3 - Yahtzee (Object-Oriented Programming)

N/A
N/A
Protected

Academic year: 2022

Share "Project 3 - Yahtzee (Object-Oriented Programming)"

Copied!
13
0
0

Loading.... (view fulltext now)

Full text

(1)

Project 3 - Yahtzee (Object-Oriented Programming)

CHS 0007 — Introduction to Computer Programming with Java University of Pittsburgh

Yahtzee (see http://en.wikipedia.org/wiki/Yahtzee for a rule overview) is a dice game in which players compete to achieve the highest possible total score. Each turn, a player rolls five dice: he/she may elect to re-roll some (or all) of the dice, or to keep the roll. A player may re-roll at most twice in any turn. Having rolled the dice, the player then chooses how best to score the roll: there are several categories (e.g., Fives, Three of a Kind, Small Straight), each with a different set of requirements for scoring. Over the course of the game, the player may use each category only once.

For this assignment, you will implement a couple classes where each class will contain a number of methods. These classes are needed to simulate the essential aspects of a single turn of Yahtzee.

When the code is completed, a user should be able to roll the dice (up to three times) and then see the points that each roll could provide in each of the scoring categories.

Part I: The Class Die (20 Points)

Yahtzee consists of five dice. So, the first class that we are going to implement is the class Die where an instance (object) can be used to represent a die. In general, we roll a die and look at its value. Thus, this class should contain two methods, roll() and getValue(). The roll() method should return a random number between 1 and 6 (inclusive). For the getValue() method, it simply returns the current value of the die. The filename of this class should be Die.java and its structure should look like the following:

public class Die {

public Die() {

}

public int roll() {

}

public int getValue() {

(2)

} }

You must come up with your own set of instance variables necessary to implement this class such that its instance will behave just like an actual die.

Once your class Die is implemented, you should test your class to ensure that it works properly.

For this project, our main class will be YahtzeeGame (in YahtzeeGame.java). So, let’s create this class with the following code to test our Die class:

public class YahtzeeGame {

public static void main(String[] args) {

Die d = new Die();

for(int i = 0; i < 10; i++)

System.out.println("roll(): " + d.roll());

System.out.println("getValue(): " + d.getValue());

} }

The output of the above program should look like the following:

roll(): 5 roll(): 5 roll(): 1 roll(): 6 roll(): 3 roll(): 6 roll(): 2 roll(): 5 roll(): 4 roll(): 4 getValue(): 4

Note that the result of each roll should be random and the result of the getValue() method should be the same as the last rolled. Be careful, once a die is constructed, at least it should have a current value which can be random. Otherwise, you may get an unexpected value when a user calls d.getValue() without calling d.roll() first.

Part II: The Class Yahtzee (50 Points)

We are going to look at an instance of the class Yahtzee as an object that consists of five dice. A user can roll some or all dice. A user can also look at current values of all five dice, check the score of each category as well as get the current score card. First, let’s focus on five dice and its related method.

(3)

For simplicity, we can use a one-dimensional array named dice of type Die to represent all five dice. A user can roll some or all dice as he/she wishes. He/She can also check the value of each individual die. Thus, the structure of the class Yahtzee (for now) should look like the following:

public class Yahtzee {

private Die[] dice;

public Yahtzee() {

dice = new Die[5];

for(int i = 0; i < 5; i++) dice[i] = new Die();

}

public void rollAllDice() {

}

public void rollADice(int dieNumber) {

}

public int getADie(int dieNumber) {

}

public String showDice() {

} }

From the above code, there are four methods related to dice as follows:

• rollAllDice(): When called, this method simply rolls all dices and return nothing

• rollADie(int dieNumber): This method allows user to roll a specific die. The dieNumber should be between 1 and 5 (inclusive). Do not forget that we use the variable dice of type array. In other words, die number 1 is associated with the variable dice[0], die number 2 is associated with dice[1], and so on. This method should return nothing.

• getADie(int dieNumber): This method simply returns the current value of a give die num- ber.

(4)

• showDice() returns a string representation of all dice and their value in the format shown below:

+---+---+---+---+---+---+

| Dice | 1 | 2 | 3 | 4 | 5 | +---+---+---+---+---+---+

| Face | 2 | 5 | 2 | 3 | 6 | +---+---+---+---+---+---+

Note that this method does not print anything on the console screen. It simply returns a String in the above format. In other words, you should see something like the above example if a user uses the following statement:

System.out.println(yahtzee.showDice());

where yahtzee is a reference variable of type Yahtzee.

As usual, you should test your class a couple times to ensure that those methods are implemented correctly. The following code (YahtzeeGame.java) can be used to test them:

public class YahtzeeGame {

public static void main(String[] args) {

Yahtzee yahtzee = new Yahtzee();

yahtzee.rollAllDice();

System.out.println(yahtzee.showDice());

System.out.println("Get value of die number 3 is " + yahtzee.getADie(3));

System.out.println(yahtzee.rollADie(3));

System.out.println(yahtzee.showDice());

System.out.println("After re-roll the die number 3: " + yahtzee.getDie(3));

} }

Scoring

The scoring of Yahtzee is quite complicate. To simplify this process, we are going to use another one-dimensional array of type int of size 6 elements named count.

private int[] count;

count[0] will store the number of the n dice rolls that have the value 1, count[1] will store the number of the n dice rolls that have the value 2, and so on. This count variable should be updated after a die is rolled. The following methods will help us easily update the variable count:

1. private int countUp(int value): This method returns the number of n dice rolls that have the value value. For example, if the dice rolls were 5, 1, 3, 6, 5, by countUp(5), it should return 2 since that are two dice with value 5.

(5)

2. public static void updateCount(): This method simply updates count[0] to count[5]

which should be called after each roll. With the countUp() method implemented, this meth- ods can be as simple as

public static void updateCount() {

for(int i = 0; i < 6; i++) count[i] = countUp(i + 1);

}

At this point, the structure of your class Yahtzee should look like the following:

public class Yahtzee {

private Die[] dice;

private int[] count;

public Yahtzee() {

dice = new Die[5];

for(int i = 0; i < 5; i++) dice[i] = new Die();

count = new int[6];

updateCount();

}

public void rollAllDice() {...}

public void rollADice(int dieNumber) {...}

public int getADie(int dieNumber) {...}

public String showDice() {...}

private int countUp(int value) {

}

private void updateCount() {

for(int i = 0; i < 6; i++) {

count[i] = countUp(i + 1);

System.out.println("Number of " + (i + 1) + "s rolled: " + count[i]);

}

(6)

} }

Note that the statement System.out.println() in the updateCount() method is for our debugging purpose only. It should be removed once you already verify that your methods countUp() and updateCount() work correctly. So, to verify that your countUp() and updateCount() methods are implemented correctly modify the main() method of the class YahtzeeGame as follows:

public class YahtzeeGame {

public static void main(String[] args) {

Yahtzee yahtzee = new Yahtzee();

yahtzee.showDice();

} }

The following is an example of an output of the above program:

Number of 1s rolled: 0 Number of 2s rolled: 2 Number of 3s rolled: 1 Number of 4s rolled: 0 Number of 5s rolled: 1 Number of 6s rolled: 1

+---+---+---+---+---+---+

| Dice | 1 | 2 | 3 | 4 | 5 | +---+---+---+---+---+---+

| Face | 2 | 5 | 2 | 3 | 6 | +---+---+---+---+---+---+

Make sure to verify that your count variable contain correct values. If you think your countUp()

and updateCount() methods are working properly, you can go ahead and remove the System.out.println() statement inside the updateCount() method.

The scoring of Yahtzee consists of two sections, the upper section and the lower section. We will look into each section separately.

The Upper Section

The upper section consists of six categories, Ones, Twos, Threes, Fours, Fives, and Sixes. For this scoring section, you simply total only the specified die face. For example, if you roll 5, 3, 1, 3, 1, the score of each category will be as follows:

• Ones is 2 since there are two dice with value 1 (1 + 1 = 2).

• Twos is 0 since there are no dice with value 2.

• Threes is 6 since there are two dice with value 3 (3 + 3 = 6).

(7)

• Fours is 0.

• Fives is 5 since there is one die with value 5.

• Sixes is 0.

With the variable count that has been properly updated, scoring of each category in this upper section will be as simple as count[value - 1] * value where value is the face value of each category. For example, from the above roll, count[0] will be 2 (there are two dice with value 1), the score of Ones is simply count[0] * 1. For this upper section, insert the following methods into your class Yahtzee:

public class Yahtzee {

:

public int getScoreOnes() {...}

public int getScoreTwos() {...}

public int getScoreThrees() {...}

public int getScoreFours() {...}

public int getScoreFives() {...}

public int getScoreSixes() {...}

}

where each method associate with each scoring category. After you implement the above methods, modify the main() method of your class YahtzeeGame to the following to verify these methods:

public class YahtzeeGame {

public static void main(String[] args) {

Yahtzee yahtzee = new Yahtzee();

yahtzee.showDice();

System.out.println(" Ones: " + yahtzee.getScoreOnes());

System.out.println(" Twos: " + yahtzee.getScoreTwos());

System.out.println(" Threes: " + yahtzee.getScoreThrees());

System.out.println(" Fours: " + yahtzee.getScoreFours());

System.out.println(" Fives: " + yahtzee.getScoreFives());

System.out.println(" Sixes: " + yahtzee.getScoreSixes());

} }

Again, Run the above program a couple times to verify that all scoring of this upper section is associated with the dice rolls.

Lower Section

The lower section consists of seven categories. The scoring of each category are as follows:

• Three of a Kind: If a roll contains at least three of the same die faces, simply total all the die faces and that is the score of this category.

(8)

• Four of a Kind: The scoring of this category is the same as the Three of a Kind (total all the die faces) except that you must have at least four of the same die faces.

• Full House: If you have three of a kind and a pair (e.g., 5, 3, 5, 5, 3), the score of this category is 25.

• Small Straight: If you have a sequence of 4 consecutive die faces (e.g., 2, 4, 3, 4, 5), 30 points.

• Large Straight: If you have a sequence of 5 consecutive die faces, 40 points.

• Chance: For this category, simply total all the die faces values.

• Yahtzee: This category is simply 5 of a kind. This score of this category is 50 points.

For this lower section, insert the following methods into your class Yahtzee:

public class Yahtzee {

:

public int getScoreThreeOfAKind() {...}

public int getScoreFourOfAKind() {...}

public int getScoreFullHouse() {...}

public int getScoreSmallStraight() {...}

public int getScoreLargeStraight() {...}

public int getScoreChance() {...}

public int getScoreYahtzee() {...}

}

where each method associate with each scoring category. As we mentioned earlier, use the variable count will help you implement scoring of this lower section. For example, for the Three of a Kind, you just have to search whether a count[i] where i is in between 0 to 5 (inclusive) is greater than or equal to 3. Once these method are implemented, use the following program to verify the above methods:

public class YahtzeeGame {

public static void main(String[] args) {

Yahtzee yahtzee = new Yahtzee();

yahtzee.showDice();

System.out.println("Three of a Kind: " + getScoreThreeOfAKind(count));

System.out.println(" Four of a Kind: " + getScoreFourOfAKind(count));

System.out.println(" Full House: " + getScoreFullHouse(count));

System.out.println(" Small Straight: " + getScoreSmallStraight(count));

System.out.println(" Large Straight: " + getScoreLargeStraight(count));

System.out.println(" Chance: " + getScoreChance(count));

System.out.println(" Yahtzee: " + getScoreYahtzee(count));

} }

(9)

The last method of this class Yahtzee is called getScoreCard() method. This method return a score card of the current roll in the form of a String (similarly to the showDice() method) which can be used with the statement System.out.println(). The format of the output string should look like the following:

+---+---+---+---+---+---+

| Dice | 1 | 2 | 3 | 4 | 5 | +---+---+---+---+---+---+

| Face | 6 | 1 | 2 | 4 | 5 | +---+---+---+---+---+---+

Ones: 1 Twos: 2 Threes: 0 Fours: 4 Fives: 5 Sixes: 6 Three of a Kind: 0 Four of a Kind: 0 Full House: 0 Small Straight: 0 Large Straight: 0 Chance: 18 Yahtzee: 0

Part III: Game Play (30 Points)

As we mention earlier, we will focus on only one turn of a player. In each turn, a player rolls five dice: he/she may elect to re-roll some (or all) of the dice, or to keep the roll. A player may re-roll at most twice in any turn.

Now we will implement the game play in the class YahtzeeGame (YahtzeeGame.java). So, first remove all statements inside the main() method related to debugging. What you have left should look like the following:

public class YahtzeeGame {

public static void main(String[] args) {

Yahtzee yahtzee = new Yahtzee();

} }

For our game, simply roll all five dice, show the current score card using the getScoreCard() method. The output on the console screen should look like the following:

+---+---+---+---+---+---+

| Dice | 1 | 2 | 3 | 4 | 5 |

(10)

+---+---+---+---+---+---+

| Face | 6 | 1 | 2 | 4 | 5 | +---+---+---+---+---+---+

Ones: 1 Twos: 2 Threes: 0 Fours: 4 Fives: 5 Sixes: 6 Three of a Kind: 0 Four of a Kind: 0 Full House: 0 Small Straight: 0 Large Straight: 0 Chance: 18 Yahtzee: 0

Then the game should ask the user to enter the die number(s) that they want to keep (separated) by a space. For example,

Enter die number(s) to keep (separated by a space):

You should use a Scanner object with the nextLine() method to read in a String. For example, in the above case, if a user wants to keep die numbers 1, 3, 4, and 5, user will enter 1 3 4 5 as shown below:

Enter die number(s) to keep (separated by a space): 1 3 4 5

Once the user press Enter, your program should re-roll all dices that are not on the given list. This can be done by calling the contains() method of the String class. For example, suppose the string

"1 3 4 5" is stored in the variable named userKeep of type String, userKeep.contains("1") will return true, and userKeep.contains("2") will return false. Re-roll the die number(s) that are not on the given list. When done, simply show the current roll with the score card as shown below:

Enter die number(s) to keep (separated by a space): 1 3 4 5 +---+---+---+---+---+---+

| Dice | 1 | 2 | 3 | 4 | 5 | +---+---+---+---+---+---+

| Face | 6 | 3 | 2 | 4 | 5 | +---+---+---+---+---+---+

Ones: 0 Twos: 2 Threes: 3 Fours: 4

(11)

Fives: 5 Sixes: 6 Three of a Kind: 0 Four of a Kind: 0 Full House: 0 Small Straight: 30 Large Straight: 40 Chance: 20 Yahtzee: 0

Enter die number(s) to keep (separated by a space):

Note that a user can re-roll one more time. However, if a user decide to end his/her turn, he/she must simply enter 1 2 3 4 5 to keep all dice as shown below:

Enter die number(s) to keep (separated by a space): 1 2 3 4 5 +---+---+---+---+---+---+

| Dice | 1 | 2 | 3 | 4 | 5 | +---+---+---+---+---+---+

| Face | 6 | 3 | 2 | 4 | 5 | +---+---+---+---+---+---+

Ones: 0 Twos: 2 Threes: 3 Fours: 4 Fives: 5 Sixes: 6 Three of a Kind: 0 Four of a Kind: 0 Full House: 0 Small Straight: 30 Large Straight: 40 Chance: 20 Yahtzee: 0

Similarly, if a user wants to re-roll all dice, simply press Enter without entering a number when asked.

The following is the console output of the previous example of a turn of a user:

+---+---+---+---+---+---+

| Dice | 1 | 2 | 3 | 4 | 5 | +---+---+---+---+---+---+

| Face | 6 | 1 | 2 | 4 | 5 | +---+---+---+---+---+---+

Ones: 1

(12)

Twos: 2 Threes: 0 Fours: 4 Fives: 5 Sixes: 6 Three of a Kind: 0 Four of a Kind: 0 Full House: 0 Small Straight: 0 Large Straight: 0 Chance: 18 Yahtzee: 0

Enter die number(s) to keep (separated by a space): 1 3 4 5 +---+---+---+---+---+---+

| Dice | 1 | 2 | 3 | 4 | 5 | +---+---+---+---+---+---+

| Face | 6 | 3 | 2 | 4 | 5 | +---+---+---+---+---+---+

Ones: 0 Twos: 2 Threes: 3 Fours: 4 Fives: 5 Sixes: 6 Three of a Kind: 0 Four of a Kind: 0 Full House: 0 Small Straight: 30 Large Straight: 40 Chance: 20 Yahtzee: 0

Enter die number(s) to keep (separated by a space): 1 2 3 4 5 +---+---+---+---+---+---+

| Dice | 1 | 2 | 3 | 4 | 5 | +---+---+---+---+---+---+

| Face | 6 | 3 | 2 | 4 | 5 | +---+---+---+---+---+---+

Ones: 0 Twos: 2 Threes: 3 Fours: 4 Fives: 5 Sixes: 6

(13)

Three of a Kind: 0 Four of a Kind: 0 Full House: 0 Small Straight: 30 Large Straight: 40 Chance: 20 Yahtzee: 0

You may notice that you may have only one method which is the main() method in YahtzeeGame.java and it is pretty short. This is one of the advantage of Object-Oriented Programming.

References

Related documents

Transmission or copying of this document is not permitted without prior written authorization. The wording &#34;Document Alcatel Business Systems&#34; must appear on all

Inhalation : Adverse symptoms may include the following: reduced foetal weight.. increase in foetal deaths

  4  Lumina Foundation. “New Jersey”. http://www.luminafoundation.org/state_work/new_jersey/. 

– In almost all surveyed States, an agreement concluded in a case of international child abduction needs to be incorporated in a judgment, turned into a court order, or be

Sama seperti lapisan masyarakat di Desa Beringin Agung, dapat disimpulkan bahwa masing-masing rumahtangga petani di Desa Pendahara cenderung memilih mencari ikan di sungai sebagai

United Methodist Church – 2210 Sixth Avenue East, Alexandria, MN 56308 Gary Taylor – email: [email protected].. Office

The mission of the Jacksonville University School of Aviation’s Aviation Management and Flight Operations major is to produce professional aviators possessing the leadership

Conclusions: Fifteen years after the screening programme started, this study supports an important decrease in breast cancer mortality due to the screening programme, with