Currently stuck on this part of a project. I am struggling with creating the String for creating the dice values in the boxes for the "showDice" string. I have already completed the first part which was creating the class for the Die. This is Javascript.
For this part, we will create the class that allows you to roll and score the dice used in a game of Yahtzee. This will be the longest part of the project, so I suggest that you don’t leave this one until the last minute.
We will grade Part 2 for programming style. You should make sure you review the section on style guidelines before you start this section.
Setting up the Class Yahtzee
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 methods.
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. S/he 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 { }
rollAllDice(); public void rollADie(int
dieNumber) |
From the above code, there are four methods related to dice as follows:
rollADie(int dieNumber):This method allows the 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.
rollAllDice(): When called, this method simply rolls all die and return nothing. This method should make use of the rollADie method
getADie(int dieNumber): This method simply returns the current value of a given die number.
showDice() returns a string representation of all dice and their value in the format shown below:
+------+---+---+---+---+---+ |
Note: 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.
Code
class Die
{
private int face;
public Die()
{
face=1;
}
public void rollDie()
{
face=(int )(Math. random() * 6 + 1);
}
public int getFace() {
return face;
}
}
public class Yahtzee {
private Die[] dice;
public Yahtzee()
{
dice = new Die[5];
for(int i = 0; i < 5; i++)
{
dice[i] = new
Die();
}
rollAllDice();
}
public void rollADie(int dieNumber)
{
dice[dieNumber].rollDie();
}
public void rollAllDice()
{
for(int i = 0; i < 5; i++)
{
dice[i].rollDie();
}
}
public int getADie(int dieNumber)
{
return
dice[dieNumber].getFace();
}
public String showDice()
{
String
str="+------+---+---+---+---+---+\n";
str+="| Dice |";
for(int i=1;i<=5;i++)
str+=" "+(i)+"
|";
str+="\n+------+---+---+---+---+---+\n";
str+="| Dice |";
for(int i=0;i<5;i++)
str+="
"+dice[i].getFace()+" |";
str+="\n+------+---+---+---+---+---+\n";
return str;
}
}
YahtzeeMain class for main method
public class YahtzeeMain {
public static void main(String[] args) {
Yahtzee yahtzee=new
Yahtzee();
yahtzee.rollAllDice();
System.out.println(yahtzee.showDice());
}
}
output
If you have any query regarding the code please ask me in the comment i am here for help you. Please do not direct thumbs down just ask if you have any query. And if you like my work then please appreciates with up vote. Thank You.
Get Answers For Free
Most questions answered within 1 hours.