Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

// Player.java import java.util.Scanner; public class Player { private String name; private int score; public Player(String name) { this.name = name; score = 0; }

image text in transcribed

image text in transcribed

image text in transcribed

// Player.java

import java.util.Scanner;

public class Player {

private String name;

private int score;

public Player(String name) {

this.name = name;

score = 0;

}

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

public void addPoints(int n) {

score += n;

}

public int getScore() {

return score;

}

public void setScore(int score) {

this.score = score;

}

/**

* method to take one turn, and returns false when the turn is over

*

* @param cup

* - Cup object

*/

public boolean takeTurn(Cup cup) {

Scanner scanner = new Scanner(System.in);

String choice = "R";

int roundScore = 0;

/**

* Looping until player chooses to roll, or until double drat is met

*/

while (choice.equalsIgnoreCase("R")) {

System.out.println(name + "'s turn. (" + roundScore

+ " points accumulated this turn.)");

/**

* Rolling the cup

*/

cup.roll();

System.out.println(cup);

if (cup.getPointValue() == 0) {

System.out.println("!!! DOUBLE DRAT !!!");

System.out.println("TURN IS OVER");

return false;

} else {

roundScore += cup.getPointValue();

System.out.println("Roll/Bank (R/B)? ");

choice = scanner.nextLine();

if (choice.equalsIgnoreCase("B")) {

/**

* Adding accumulated points

*/

addPoints(roundScore);

}

}

}

return false;

}

@Override

public String toString() {

return name + " with score " + score;

}

}

// Cup.java

public class Cup {

private int pointValue;

private Die dice;

private int value1, value2, value3;

private static final int DOUBLE_DRAT = 0, UNIQUE = 20, TRIPLE = 30;

public Cup() {

pointValue = 0;

dice = new Die();

}

public void roll() {

/**

* rolling dice three times

*/

value1 = dice.roll();

value2 = dice.roll();

value3 = dice.roll();

if (value1 == value2 && value1 == value3) {

// triple

pointValue = TRIPLE;

} else if (value1 == value2 || value1 == value3 || value2 == value3) {

// double drat

pointValue = DOUBLE_DRAT;

} else {

// unique

pointValue = UNIQUE;

}

}

public int getPointValue() {

return pointValue;

}

@Override

public String toString() {

/**

* returning a properly formatted string

*/

return " Rolled: " + value1 + " " + value2 + " " + value3 + " ("

+ pointValue + " points)";

}

}

// Die.java

import java.util.Random;

public class Die {

private int faceValue;

private Random random;

public Die() {

//default value

faceValue=1;

//initializing random instance

random=new Random();

}

public int getFaceValue() {

return faceValue;

}

public void setFaceValue(int faceValue) {

this.faceValue = faceValue;

}

public int roll(){

/**

* setting a random value between 1 and 6 to the face value and returning it

*/

faceValue=random.nextInt(6)+1;

return faceValue;

}

@Override

public String toString() {

return ""+faceValue;

}

}

// DoubleDrat.java

public class DoubleDrat {

private static final int WIN_SCORE = 100; //winning score

private Player[] players;

private Cup cup;

public DoubleDrat(Player players[]) {

this.players = players;

cup = new Cup();//initializing empty cup

}

/**

* method to play the game

*/

public void playGame() {

// displaying score board

displayScoreBoard();

boolean gameOver = false; // keeps the track of the game

/**

* loops until the game is over

*/

while (!gameOver) {

/**

* looping through all players and their turns

*/

for (int i = 0; i

players[i].takeTurn(cup);

displayScoreBoard();// displaying score board

/**

* checking if current player has won

*/

if (players[i].getScore() >= WIN_SCORE) {

gameOver = true;// end of the loop

System.out

.println("Player " + players[i].getName()

+ " won with " + players[i].getScore()

+ " points!");

break;

}

}

}

}

/**

* method to display the score board

*/

public void displayScoreBoard() {

System.out.println("----------------------------------");

System.out.println("SCOREBOARD");

for (Player p : players) {

System.out.println(p);

}

System.out.println("----------------------------------");

}

}

// GameLauncher.java

import java.util.Scanner;

public class GameLauncher {

public static void main(String[] args) {

/**

* prompting and getting the number of players

*/

Scanner scanner=new Scanner(System.in);

System.out.println("how many players: ");

int numPlayers = Integer.parseInt(scanner.nextLine());

/**

* Initializing the players array

*/

Player[] players=new Player[numPlayers];

for (int i = 0; i

System.out.println("Player " + (i + 1) + " name:");

players[i] = new Player(scanner.nextLine());

}

/**

* Creating a DoubleDrat object and playing the game

*/

DoubleDrat doubleDrat=new DoubleDrat(players);

doubleDrat.playGame();

}

}

I have the ilustrations and the code, could you please answer these questions?

1. Explain why the Cup object must be passed to the Player objects takeTurn method as a parameter.

2. Where is the players array created (class/method), and how did you set its size?

3. If the game rules were changed to become TripleDrat, where 30 points is earned for doubles, and the turn ends with 0 points if the player rolls triples. Which method(s) must be changed to support this revision? Rewrite the logic of that method to support this change and show it here

Player name: String -score: int players+Player(String) +getName(): String +setName(String) : void +addPoints(int):void +getScore): int +setScore(int):void +takeTurn(Cup):Boolean +toString():String DoubleDrat +DoubleDrat() +playGame(): void +displayScoreboard):void theCup Die Cup GameLauncher faceValue: int 3 +Die() +Cup() +roll(): void +getPointValue():int +toString): String +getFaceValue(): int dice +setFaceValue(int):void +main(String[]):void +roll): int +toString(): String Player name: String -score: int players+Player(String) +getName(): String +setName(String) : void +addPoints(int):void +getScore): int +setScore(int):void +takeTurn(Cup):Boolean +toString():String DoubleDrat +DoubleDrat() +playGame(): void +displayScoreboard):void theCup Die Cup GameLauncher faceValue: int 3 +Die() +Cup() +roll(): void +getPointValue():int +toString): String +getFaceValue(): int dice +setFaceValue(int):void +main(String[]):void +roll): int +toString(): String

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Concepts

Authors: David Kroenke, David Auer, Scott Vandenberg, Robert Yoder

9th Edition

0135188148, 978-0135188149, 9781642087611

Students also viewed these Databases questions

Question

=+b. Would you need to edit down the copy for a smaller-space ad?

Answered: 1 week ago

Question

=+4. About the medium.

Answered: 1 week ago