1. Engineering
  2. Computer Science
  3. java 80 or greater thanks 2 game of rummy we...

Question: java 80 or greater thanks 2 game of rummy we...

Question details

(JAVA 8.0 or greater, thanks!)

2 Game of Rummy

We revisit the single player game Rummy introduced in ITI 1120/1520. We use the same set of rules, but we take this opportunity to apply the object-oriented programming concepts that we have just learnt.

In the game of Rummy, the main goal is to build melds, which consists of sets: two, three or four of a kind of the same rank; or progression, three or more cards in a sequence of consecutive ranks, of the same suit. So the set ♡10, ♢10, ♣10 forms three of a kind. And the set/sequence ♢7, ♢8, ♢9, ♢10, ♢11 forms a progression. The goal of the player is to get rid of all of the cards in as few rounds of the game as possible.

One of the first changes that we make will be to create a data type (a class) to represent the cards. Just like in ITI 1120/1520, the number of suits is fixed (4), but the number of ranks can be any value in the range from 3 to 99.

Next, we also create a data type to represent a deck of cards. Using arrays to store the cards would require you to write a large amount of code since the arrays have a fixed size and support a limited number of operations. Since you are already familiar with the concept of lists, the class Deck will use an object of the class ArrayList to store cards. The fact that one can use a class without knowing the details of its implementation is one of the greatest strengths of object-oriented programming. In the second part of the semester, we will learn about the implementation of lists.

Our implementation of the game consists of 6 classes: Die, Card, Deck, Game, Run, and Utils. In the following sections, we describe the requirements for each class.

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.

2.5 Run

We provide you with a class called Run. It contains the main method for this application.

2.6 Test

We provide you with a class called Test. It contains a small number of tests to help you better understand the requirements. Those tests are by no means exhaustive.

2.7 Utils

The class Utils contains a number of methods to assist you to solve this assignment. Namely, you can use Utils.readCard() and Utils.readCards() to obtain one or several cards from the user, respectively.

Solution by an expert tutor
Blurred Solution
This question has been solved
Subscribe to see this solution