Question

I need java code for this . The assignment requires using objects

Exercise 2 of 2: Create a program that lets the player play a simplified game of Yahtzee. The game starts with the user throwing five 6-sided dice. The user can choose which dice to keep and which dice to re-roll. The user can re-roll a maximum of 2 times. After two times, the user has 5 dice with a certain value. The player then has to choose where they want their final combination of dice values applied. The following list is a simplified Yahtzee card, which we will use for this game: Ones, Twos, Threes, Fours, Fives, Sixes, 3 of a kind, 4 of a kind, Full House, Straight, Yahtzee For a Full House, the player needs 3 of a kind AND 2 of a kind. For a Straight, the player needs to have all 5 dice in consecutive order, i.e. either 1 ,2, 3, 4, 5, or 2, 3, 4, 5, 6. Yahtzee means 5 of a kind. If the player applies his dice score to something inapplicable, e.g. the player only has 2 of a kind, but applies it to 3 of a kind, or the player has no Ones, but applies his dice result to the Ones category, he scores zero in this category. Implementing the Bonus, Small Straight, and Chance (as usually available in most Yahtzee games) is not required for this homework exercise. Requirement: You wil need a random number generator for the dice roll. Make use of objects, which means either create a Dice class for the 5 dice, or implement a class that represents the 5 dice together (the choice is up to you.) Also, as mentioned, the player can re-throw his dice up to two times. The player can chose which dice to re-throw and which to keep.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Your Code. 1 import java.util.; public class YahtzeeClass public static void main(String[] args) int play-1, score -0, sum-0;die.roll; DiceArray[1] -die.get); 43 45 46 if (rerollw3) die.roll; DiceArray[2die.get); 48 49 50 51 52 53 54 if (reroll[w]4)85 86 try f result-Integer.parseInt (input (Prompt).trim); 87 ▼ t catch (Exception e) f result = 0; 89 90 return result; 91 s128class Winnings 129 130 131 132 133 134 135 136 137 private int score private int choice; public Winnings) f scoree; publicif { { if (wins[13]) t if (wins[14]) t (wins[11] System.out.println(12 - number of 4s); 0) 172 173 174 ▼ 175 176 if (wins[214 215 216 217 218 219 220 221 if ((DiceArray[0DiceArray[1] -1) && (DiceArray[1] DiceArray[2] 1) && (choice 3)) f winingsa1;256 257 258 259 260 else if ((winingsa2) &&(choice4) System.out.println(You have a small straight. score 30; else if ((winiSystem.out.println(Your get score points.); else System.out.println(You got nothin.; score 0; 297 298 299 300 301 302 3OUTPUT:

Result... compiled and executed in 48.575 sec(s) this is Die 1: 5 this is Die 2: 4 this is Die 3: 4 this is Die 4: 5 this isHow many dice do you want to reroll? (0-5)2 Which ones?4 Which ones?6 Die 1: 1 Die 2: 4 Die 3: 4 Die 4: 6 Die 5: 6 Which do yCOPY CODE:
import java.util.*;
  
public class YahtzeeClass
{
public static void main(String[] args)
{
int play = 1, score = 0, sum = 0;
int[] wins = new int[15];
while ((play == 1) && (sum < 15))
{
sum = 0;
int[] DiceArray = new int[] { 0, 0, 0, 0, 0 };
int roll = 0;
int x, y, w, z;
int roll_againA = 0, roll_againB = 03;
Die die = new Die();
for (x = 0; x < 5; x++) {
die.roll();
DiceArray[x] = die.get();
}

System.out.println("this is Die 1: " + DiceArray[0]);
System.out.println("this is Die 2: " + DiceArray[1]);
System.out.println("this is Die 3: " + DiceArray[2]);
System.out.println("this is Die 4: " + DiceArray[3]);
System.out.println("this is Die 5: " + DiceArray[4]);

do {
roll_againA = inputInt("How many dice do you want to reroll? (0-5)");
if (roll_againA > 0) {
int[] reroll = new int[roll_againA];
for (y = 0; y < roll_againA; y++) {
roll_againB = inputInt("Which ones?");
reroll[y] = roll_againB;
}
for (w = 0; w < roll_againA; w++) {
if (reroll[w] == 1) {
die.roll();
DiceArray[0] = die.get();
}
if (reroll[w] == 2) {
die.roll();
DiceArray[1] = die.get();
}
if (reroll[w] == 3) {
die.roll();
DiceArray[2] = die.get();
}
if (reroll[w] == 4) {
die.roll();
DiceArray[3] = die.get();
}
if (reroll[w] == 5) {
die.roll();
DiceArray[4] = die.get();
}
}
roll++;
System.out.println("Die 1: " + DiceArray[0]);
System.out.println("Die 2: " + DiceArray[1]);
System.out.println("Die 3: " + DiceArray[2]);
System.out.println("Die 4: " + DiceArray[3]);
System.out.println("Die 5: " + DiceArray[4]);

}
} while ((roll < 2) && (roll_againA > 0));
Winnings prize = new Winnings();
prize.checkWinnings(DiceArray, wins);
wins[prize.choice() - 1] = 1;
for (z = 0; z < 15; z++) {
sum += wins[z];
}
score += prize.score();
System.out.println("Your total score is: " + score);
if (sum < 15) {
play = inputInt("do you want to play again?(1=yes, 2=no)");
} else {
System.out.println("GAME OVER!");
}
}
}

static int inputInt(String Prompt) {
int result = 0;
try {
result = Integer.parseInt(input(Prompt).trim());
} catch (Exception e) {
result = 0;
}
return result;
}

static String input(String prompt) {
String inputLine = "";
System.out.print(prompt);
try {
java.io.InputStreamReader sys = new java.io.InputStreamReader(
System.in);
java.io.BufferedReader inBuffer = new java.io.BufferedReader(sys);
inputLine = inBuffer.readLine();
} catch (Exception e) {
String err = e.toString();
System.out.println(err);
}
return inputLine;
}
}

  
class Die {
private int value;
private Random rand;

public Die() {
value = 0;
rand = new Random();
}

public void roll() {
value = 1 + rand.nextInt(6);
}

public int get() {
return (value);
}
}

class Winnings {
private int score;
private int choice;

public Winnings() {
score = 0;
}

public void checkWinnings(int[] DiceArray, int[] wins) {
System.out.println("Which do you want to see if you have?");
if (wins[0] == 0) {
System.out.println("1 - yahtzee");
}
if (wins[1] == 0) {
System.out.println("2 - full house");
}
if (wins[2] == 0) {
System.out.println("3 - large straigt");
}
if (wins[3] == 0) {
System.out.println("4 - small straigt");
}
if (wins[4] == 0) {
System.out.println("5 - four of a kind");
}
if (wins[5] == 0) {
System.out.println("6 - three of a kind");
}
if (wins[6] == 0) {
System.out.println("7 - pair");
}
if (wins[7] == 0) {
System.out.println("8 - two pair");
}
if (wins[8] == 0) {
System.out.println("9 - number of 1's");
}
if (wins[9] == 0) {
System.out.println("10 - number of 2's");
}
if (wins[10] == 0) {
System.out.println("11 - number of 3's");
}
if (wins[11] == 0) {
System.out.println("12 - number of 4's");
}
if (wins[12] == 0) {
System.out.println("13 - number of 5's");
}
if (wins[13] == 0) {
System.out.println("14 - number of 6's");
}
if (wins[14] == 0) {
System.out.println("15 - chance");
}
choice = YahtzeeClass.inputInt("");

int x = 0, y = 0, winings = 0, winingsa = 0;
int ones = 0, twos = 0, threes = 0, fours = 0, fives = 0, sixes = 0;
Arrays.sort(DiceArray);
for (y = 0; y < 5; y++)
{
if (DiceArray[y] == 1)
{
ones++;
}
if (DiceArray[y] == 2)
{
twos++;
}
if (DiceArray[y] == 3)
{
threes++;
}
if (DiceArray[y] == 4)
{
fours++;
}
if (DiceArray[y] == 5)
{
fives++;
}
if (DiceArray[y] == 6)
{
sixes++;
}
}


if ((DiceArray[0] == DiceArray[1] - 1) && (DiceArray[1] == DiceArray[2] - 1)
&& (DiceArray[2] == DiceArray[3] - 1) && (DiceArray[3] == DiceArray[4] - 1)
&& (choice == 3)) {
winingsa = 1;
} else if ((ones > 0) && (twos > 0) && (threes > 0) && (fours > 0)) {
winingsa = 2;
} else if ((threes > 0) && (fours > 0) && (fives > 0) && (sixes > 0)) {
winingsa = 2;
} else if ((twos > 0) && (threes > 0) && (fours > 0) && (fives > 0)) {
winingsa = 2;
}

  
for (x = 0; x < 5; x++) {
if (x != 0) {
if ((DiceArray[0] == DiceArray[x])) {
winings++;
}
}
if ((x != 0) && (x != 1)) {
if ((DiceArray[1] == DiceArray[x])) {
winings++;
}
}
if ((x != 0) && (x != 1) && (x != 2)) {
if ((DiceArray[2] == DiceArray[x])) {
winings++;
}
}
if ((x != 0) && (x != 1) && (x != 2) && (x != 3)) {
if ((DiceArray[3] == DiceArray[x])) {
winings++;
}
}
}

if ((winingsa == 1) && (choice == 3)) {
System.out.println("You have a straight.");
score = 40;
} else if ((winingsa == 2) && (choice == 4)) {
System.out.println("You have a small straight.");
score = 30;
} else if ((winings == 10) && (choice == 1)) {
System.out.println("Yatzee!");
score = 50;
} else if ((choice == 6) && (winings >= 3)) {
System.out.println("You have three of a kind.");
score = DiceArray[0] + DiceArray[1] + DiceArray[2] + DiceArray[3] + DiceArray[4];
} else if ((choice == 7) && (winings > 0)) {
System.out.println("You have a pair.");
score = 5;
} else if ((winings == 2) && (choice == 8)) {
System.out.println("You have two pairs.");
score = 10;
} else if ((winings == 4) && (choice == 2)) {
System.out.println("You have a full house.");
score = 25;
} else if ((winings >= 6) && (choice == 5)) {
System.out.println("You have four of a kind.");
score = DiceArray[0] + DiceArray[1] + DiceArray[2] + DiceArray[3] + DiceArray[4];
} else if (choice == 9) {
System.out.println("You have " + ones + " ones.");
score = ones;
} else if (choice == 10) {
System.out.println("You have " + twos + " twos.");
score = twos * 2;
} else if (choice == 11) {
System.out.println("You have " + threes + " threes.");
score = threes * 3;
} else if (choice == 12) {
System.out.println("You have " + fours + " fours.");
score = fours * 4;
} else if (choice == 13) {
System.out.println("You have " + fives + " fives.");
score = fives * 5;
} else if (choice == 14) {
System.out.println("You have " + sixes + " sixes.");
score = sixes * 6;
} else if (choice == 15) {
score = DiceArray[0] + DiceArray[1] + DiceArray[2] + DiceArray[3] + DiceArray[4];
System.out.println("Your get " + score + " points.");
} else {
System.out.println("You got nothin'.");
score = 0;
}
}

public int score() {
return (score);
}

public int choice() {
return (choice);
}
}

Add a comment
Know the answer?
Add Answer to:
I need java code for this . The assignment requires using objects Exercise 2 of 2:...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Write a class called OneRoundOneRollYahtzee. The program should behave the same as the InputOrGenerateDiceRolls (which is...

    Write a class called OneRoundOneRollYahtzee. The program should behave the same as the InputOrGenerateDiceRolls (which is as follows: InputOrGenerateDiceRolls program should ask the user whether the user prefers 1) to roll his/her own dice, 2) use computer-generated dice rolls, or 3) quit the program. If the user prefers to roll his/her own dice, the program should prompt the user to enter 5 digits in the range from 1-6 separated by spaces in any order with duplicates allowed. However, if the...

  • Hi, I need help with this question to my coding assignment. I completed letter A in...

    Hi, I need help with this question to my coding assignment. I completed letter A in the remaining program I just need help with letter "B" and by the way this is C programming. If you can't do it, just explain to me in very simple terms what I would do. void updateScores(int scoreCard[CATEGORIES][COLS], int category,int dice [5]) { switch(category) { case ONE: printf("Scoring Ones...\n"); break; case TWO: printf("Scoring Twos...\n"); break; case THREE: printf("Scoring Threes...\n"); break; case FOUR: printf("Scoring Fours...\n");...

  • Please i need helpe with this JAVA code. Write a Java program simulates the dice game...

    Please i need helpe with this JAVA code. Write a Java program simulates the dice game called GAMECrap. For this project, assume that the game is being played between two players, and that the rules are as follows: Problem Statement One of the players goes first. That player announces the size of the bet, and rolls the dice. If the player rolls a 7 or 11, it is called a natural. The player who rolled the dice wins. 2, 3...

  • C# Code: Write the code that simulates the gambling game of craps. To play the game,...

    C# Code: Write the code that simulates the gambling game of craps. To play the game, a player rolls a pair of dice (2 die). After the dice come to rest, the sum of the faces of the 2 die is calculated. If the sum is 7 or 11 on the first throw, the player wins and the game is over. If the sum is 2, 3, or 12 on the first throw, the player loses and the game is...

  • Develop a C++ program which will play a simplified version of the dice game Yahtzee For...

    Develop a C++ program which will play a simplified version of the dice game Yahtzee For simplicity, this version of the game only uses four dice No prompting of the user for input values is required Simply use four variables called A, B, C, D to maintain dice roll values int A = 2; No input validation is required as well From the input you will determine if the player rolled 4 of a kind, 3 of a kind, 2...

  • Using Dr Java Objective: Create a game of video poker with a graphical user interface (GUI)....

    Using Dr Java Objective: Create a game of video poker with a graphical user interface (GUI). The player should start with $100, and then the system deals out 5 playing cards. After the player sees the cards they are then asked to wager $10, $20, $50, or $100. Next the user picks which cards they wish to throw away, and then the system deals more cards in their place. Once this has concluded money are awarded by these criteria: Nothing...

  • python code( using functions please)! Rules of the game: - This game requires a dice. The...

    python code( using functions please)! Rules of the game: - This game requires a dice. The goal of the game is to collect the correct number of coins to create a dollar. Players take coins based on a roll of the dice. The numbers on the dice correlate to the coin values as follows:  1—penny  2—nickel  3—dime  4—quarter  5— ( pick a random card from 1 to 4): o 1 = penny o 2 =...

  • 1. Problem Description Language: JAVA The game of Poker Dice is a bit like standard poker...

    1. Problem Description Language: JAVA The game of Poker Dice is a bit like standard poker but played with dice instead of cards. In this game, five fair dice are rolled. We will recognize one of seven different hands, in order of increasing value: None alike: Five distinct die values occur. Example: 1, 3, 4, 5, 6 One Pair: Four distinct die values occur; one die value occurs twice and the other three die values occur once each. Example: 1,...

  • III. Overview & Requirements: The following description has been adopted from Deitel & Deitel. One of...

    III. Overview & Requirements: The following description has been adopted from Deitel & Deitel. One of the most popular games of chance is a dice game called "craps," which is played in casinos and back alleys throughout the world. The rules of the game are straightforward: A player rolls two dice. Each die has six faces. These faces contain 1, 2, 3, 4, 5, and 6 spots. After the dice have come to rest, the sum of the spots on...

  • Write a Python (version 3.5.2) program that simulates how often certain hands appear in Yahtzee. In...

    Write a Python (version 3.5.2) program that simulates how often certain hands appear in Yahtzee. In Yahtzee, you roll five dice and then use those dice to make certain combinations. For example, the Yahtzee combination is formed by all five dice having the same value. A large straight occurs when all of the dice are in consecutive order (e.g., 1,2,3,4,5 or 2,3,4,5,6). If you haven’t played Yahtzee, you can find out more from various online sources such as Wikipedia. Once...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT