Java needed please Sequences, also known as Straight Shooter. This game is playe
ID: 3869499 • Letter: J
Question
Java needed please
Sequences, also known as Straight Shooter.
This game is played with six dice. This is essentially a standard dice version of an old game called Hearts or Hearts Due which is played with special dice that have the letters H, E, A, R, T and S printed on the die's faces instead of spots.
Play: Each player in turn rolls the six dice and scores points for any sequence of consecutive numbers thrown beginning with 1. In the event of two or more of the same number being rolled only one counts.
However, a throw that contains three 1s cancels out a player's score and they must start from zero again.
A total of scores is kept and the first player to reach 100 points, wins the game. 1: 5 points 1, 2: 10 points 1, 2, 3: 15 points 1, 2, 3, 4: 20 points 1, 2, 3, 4, 5: 25 points 1, 2, 3, 4, 5, 6: 35 points 1, 1, 1: Cancels player's total score
nb: this is a non-interactive game when played on computer. You will need two arrays of Die references, one for each player (human and computer). Use the Die class you used in a previous lab this semester. Create an object-oriented design. Die class, Game class, Driver class
After each roll, print the faces of the dice and display the points for that roll along with the total accumulated points.
Once a game is won by a player, print the winner’s name and then ask the user if he/she wants to play again. Keep a running total of games won and lost by each of the two players (human and computer)
If you finish early, go back and re-write a version Die class so the sides have the letters (‘H’,’E’,’A’,’R’,’T’,’S’) instead of the dots.
Explanation / Answer
Given below is the code for the question along with output . Please do rate the answer if it helped. Thank you.
Die.java
package game.hearts;
import java.util.Random;
//a die class for HEARTS game
public class Die {
public static char SIDES[] = {'H' , 'E', 'A', 'R', 'T', 'S'};
private static Random RANDOM = new Random(System.currentTimeMillis());
private int side; // the current side showing up after a roll
public Die()
{
}
public void roll()
{
side = RANDOM.nextInt(SIDES.length);
}
public char getFace()
{
return SIDES[side];
}
}
Player.java
package game.hearts;
public class Player {
private String name;
private Die[] dice;
private int score;
private static int MAX_SCORE = 100;
public Player(String name)
{
this.name = name;
dice = new Die[6];
for(int i = 0; i < dice.length; i++)
dice[i] = new Die();
}
public String getName()
{
return name;
}
public int getScore()
{
return score;
}
public void roll()
{
for(int i = 0; i< dice.length; i++)
dice[i].roll();
}
/*public String getRolledSequence()
{
String str = "";
for(int i = 0; i < dice.length; i++)
str += dice[i].getFace();
return str;
}
*/
//removes any duplicate dice face, and gives them in arranged sequence of HEARTS
public String getArrangedSequence()
{
String str = "";
for(int i = 0; i < Die.SIDES.length; i++)
{
for(int j = 0; j < dice.length; j++)
if(dice[j].getFace() == Die.SIDES[i])
{
str += dice[j].getFace();
}
}
return str;
}
public int getPoints()
{
int points = 0;
//check for 3 Hs i.e 3 1s, which will cancel out the points
String seq = getArrangedSequence();
if(!seq.contains("HHH"))
{
if(seq.equals("HEARTS"))
points = 30;
else if(seq.startsWith("HEART"))
points = 25;
else if(seq.startsWith("HEAR"))
points = 20;
else if(seq.startsWith("HEA"))
points = 15;
else if(seq.startsWith("HE"))
points = 10;
else if(seq.startsWith("H"))
points = 5;
}
return points;
}
public void addToScore(int points)
{
score += points;
}
public boolean isWinner()
{
return score >= MAX_SCORE;
}
}
Driver.java
package game.hearts;
public class Driver {
public static void main(String[] args) {
Player[] players = new Player[2];
players[0] = new Player("Human");
players[1] = new Player("Computer");
int turn = 0;
while(!players[0].isWinner() && !players[1].isWinner()) //as long as no one has won
{
players[turn].roll();
players[turn].addToScore(players[turn].getPoints());
System.out.printf("%15s",players[turn].getName() + ": ");
System.out.print(players[turn].getArrangedSequence());
System.out.printf(" points = %3d" , players[turn].getPoints());
System.out.printf(" score = %3d " , players[turn].getScore());
turn = 1 - turn;
}
if(players[0].isWinner())
{
if(players[1].isWinner())
System.out.println("Its a Tie and both score " + players[0].getScore() + "points ");
else
System.out.println(players[0].getName() + " wins with " + players[0].getScore() + " points ");
}
else
System.out.println(players[1].getName() + " wins with " + players[1].getScore() + " points ");
}
}
output
Human: EARRTT points = 0 score = 0
Computer: HHHETT points = 0 score = 0
Human: HHEART points = 5 score = 5
Computer: HATTTS points = 5 score = 5
Human: HEATSS points = 15 score = 20
Computer: EEAARS points = 0 score = 5
Human: EERTTT points = 0 score = 20
Computer: HHEEAR points = 5 score = 10
Human: EATTSS points = 0 score = 20
Computer: HEEEAR points = 10 score = 20
Human: EAASSS points = 0 score = 20
Computer: EEAAAR points = 0 score = 20
Human: HEERRS points = 10 score = 30
Computer: HRRTTS points = 5 score = 25
Human: HEAARS points = 15 score = 45
Computer: HARTSS points = 5 score = 30
Human: HHTTSS points = 5 score = 50
Computer: HHAART points = 5 score = 35
Human: HHEATS points = 5 score = 55
Computer: AARTSS points = 0 score = 35
Human: HEEARS points = 10 score = 65
Computer: EAARTT points = 0 score = 35
Human: AATSSS points = 0 score = 65
Computer: EEATTS points = 0 score = 35
Human: HEARTS points = 30 score = 95
Computer: HHHART points = 0 score = 35
Human: ERRRTS points = 0 score = 95
Computer: EERTTS points = 0 score = 35
Human: HHARRT points = 5 score = 100
Human wins with 100 points
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.