Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

2.4 Game (20 marks) The class Game implements the logic of the single player game of Rummy An object of the class Game has two

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

2.4 Game (20 marks) The class Game implements the logic of the single player game of Rummy An object of the class Game has two decks of cards, one is the main deck, whereas the second one represents the player's hand. An object of the class Game also has a die (reference to an object of the class Die). . The class declares one constructor. This constructor has one parameter, which specifies the number of ranks for this game. When a Game object is created, it also creates the main deck and the die A Game object has only one public instance method, which is called play. The method implements the logic of the game. Here is a description of the game . Step 0. The strange deck is created and shuffled. (In order to test your game more quickly you can reduce the number of ranks to less than 13 and more than 3). In your implementation, the created deck is an object of the class Deck. Top of the deck is considered the last card. * Step 1. The player is dealt 7 cards from the top of the strange deck. * Step 2. The following steps are repeated until the player runs out of the cards i.e. until the player's hand is empty: The player rolls a die * If the player gets a 1 The player can discard any one card she likes. After that, the current round is over and the game goes back to Step 2. * If instead the player gets 2, 3, 4, 5 or 6 The player is first delt, from the top of the deck, a number of cards corresponding to the value of the die, or size of the deck of cards, whichever is smaller. The player then keeps on discarding melds from her hand until she has no more melds. You program has to check that the set of cards that the player chooses indeed forms a valid meld before discarding them from the player's hand. Once she decides she is out of melds, the round is over and the game goes back to Step 2. Finally, once the player is out of cards (i.e. once the player's hand is empty), the total number of rounds is reported and the game is over. Note that if the deck is empty and the player has no more melds, no melds can ever be created again. Thus the player has to wait for 1 on the die. In order to avoid that frustration, your game should roll 1 i.e. set num to 1, in each round that starts with an empty deck. As usual, whenever you ask the player for some input you should make sure they give you the required kind of input. You may assume that the player will follow instructions and give you a correct type of data, but not the correct values. For example, if you are asking for an integer between 3 and 99, you may assume that the player will give you an integer, but not that she will give you an integer in the correct range. Thus you should keep on repeating the question until you get a valid answer. Similarly if you ask the player for a meld, you may assume that the player will give you a set of 3 cards, but you will need to test if these cards are indeed in the player's hand and that they form a meld. We provide you with several methods to help you with this part of the assignment. See 02/Utils.java. pVavaDicejava Sublime Text (UNREGISTERED) File Edit Selection Find View Goto Tools Project Preferences Help A1Q1.java A102 java AMQ1.java Cardjava Test java Garne.java 10 the variable is immutable. ** 12 import java. Lang.Math; 13 / Class Die // 14 public class Dice ( 15 16 17 18 19 20 21 public static final int MAXVALUE = 6; private int value; public Die ) ( roll ): 23 24 25 26 27 28 29 30 31 32 public void roll () this.value (int) (Math.random()((6 1) 1)) 1; public int getValue) f return this.value; public String toString t return "Die f""value:" this.value '; 34 35 Line 14, Column 17 Tab Size:4 ava Type here to search a 2:14 AM 2/1/2019 pVava Card.java Sublime Text (UNREGISTERED) File Edit Selection Find View Goto Tools Project Preferences Help A1Q01.java XA102.java AMQ1.java Dice- java Test java Garne.java 3 public class Card f 4 // Class Card static fina int DIAMOND = 0; static final int CLUB = 1; static final int HEART = 2; static final int SPADE3 18 private int suit; private int rank; 12 13 14 15 16 17 18 19 26 21 public Card (int suit, int rank) f this.suit suit; this, rank = rank; public int getSuit )f return this.suit; public int getRank ) f 23 24 25 26 27 28 29 39 31 32 return this.rank public boolean equals (Card card) t if ( (card instanceof Card)) f return false; if (this.suitcard.getSuit () && this.rankcard.getRank )) return true; else return false; 34 35 36 37 38 public String toString ) t return ""this.suit ","this.rank ""; Line 11, column 1 Tab Size:4 ava 2:14 AM 2/1/2019 Type here to search 11.. pVavaDeckjava Sublime Text (UNREGISTERED File Edit Selection Find View Goto Tools Project Preferences Help A1Q01.java import java.util.Arraylist; A102 java AMQ1.java Dice- java xGamejava Test java 2 import java.util.colLections; 4 public class Deck f private ArrayList cards; public Deck ) cards new ArrayList ): public Deck (int n) f cards new ArrayList ; for (int i-e;i 1) { for (int = e; this.cards.get(j).getsuit)) temp this.cards.get(i); this.cards.set (i, this.cards.get(j) this.cards.set (j,temp); 125 126 public void sortByRank ) ( 128 129 13e 131 132 133 134 135 136 137 138 139 148 141 Card temp; for (int i-e; icards.size ); i++) f for (intj e; j cards; public Deck ) cards new ArrayList ): public Deck (int n) f cards new ArrayList ; for (int i-e;i 1) { for (int = e; this.cards.get(j).getsuit)) temp this.cards.get(i); this.cards.set (i, this.cards.get(j) this.cards.set (j,temp); 125 126 public void sortByRank ) ( 128 129 13e 131 132 133 134 135 136 137 138 139 148 141 Card temp; for (int i-e; icards.size ); i++) f for (intj e; j<4><4>

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

Expert Performance Indexing In SQL Server

Authors: Jason Strate, Grant Fritchey

2nd Edition

1484211189, 9781484211182

More Books

Students also viewed these Databases questions

Question

Appreciate common obstacles to performance appraisals

Answered: 1 week ago

Question

Recognize traditional approaches to performance appraisals

Answered: 1 week ago