Java代写:COMP10062 Deck of Cards

用Java代写纸牌游戏,练习Array的基础用法。

Deck of Cards

The Assignment

This assignment is about using arrays. You will define two classes - one to represent a playing card with a rank and a suit, and one to represent a deck of cards (these are the model classes). Then you will repeatedly shuffle and deal hands, and create a histogram of the results. The view class for this assignment uses a main method and Standard Input and Output.

The Deck of Cards (model)

A Card object has a rank and a suit, both of which are positive, non-zero integers. Some cards are trump cards and some are not. The rank and suit are specified when a Card is created and cannot be changed. A Card can report its rank and suit, and its value. The value of a card is its rank multiplied by its suit. It has a toString() method that reports its rank and suit. That’s all it can do.

A DeckOfCards object holds a set of Card objects in an array. The size of the deck is determined by two integers passed to the constructor when it is created. The first integer specifies the maximum rank, and the second specifies the number of suits. The deck then contains one card of each rank/suit combination. So if there are 3 suits and the maximum rank is 6, there will be 18 cards in the deck (3 x 6 = 18). This constructor creates the Card objects and stores them in the array. A DeckOfCards can shuffle itself (which it does by randomly swapping pairs of cards in the array), can report its size, the minimum and maximum card value in the deck, and can “deal” the top n cards by placing them in an array and returning them. It has a toString() method that that reports the size of the deck, the minimum and maximum values, and the top card. It also has a histogram method, described below. That’s all it can do.

The Histogram Method

The histogram method of DeckOfCards accepts a parameter that specifies the number of cards in a hand. Then it shuffles and deals the cards 100,000 times by calling its own shuffle and deal methods. For each deal, it adds up the total value of all the cards in the hand and uses an array of counters to record the number of times each total comes up. For example, if the sum of the values of the cards dealt is 12, you should add 1 to array element 12. Then it returns the histogram array. See HistogramExample.java in this weeks examples for some code that might help.

Design and Implementation

The exact details of the implementation and interface of these two classes is up to you, but you must respect the specifications given above, and you must create and hand in a UML class diagram to represent Card, DeckOfCards and the association relationship between the two classes. This UML diagram can be hand drawn, or it you can use UMLet or draw.io. If you have another piece of software you would like to use, check with your instructor.

The Main Method (View)

The main method should start by asking the user to enter the number of suits and maximum rank. Then it should create the deck of cards and print it to the screen. Then it should present a menu in a loop that allows them to shuffle once, deal one hand, or shuffle and deal 100,000 hands. They choose the size of the hands dealt.

If they choose to deal a single hand, show the result by printing the cards they got. If choose 100,000 hands, call the histogram method described above, and print the non-zero elements of the array, as shown in the example output.

Handing In

You have approximately 1 week to complete this assignment. See the due date and time on the drop box. Hand in a zipped version of your .java (not .class) files and your class diagram to the drop box.

Evaluation

Your assignment will be evaluated for performance (20%), class diagram (20%), structure (40%), and documentation (20%) using the rubric in the drop box.

Example Output

Below is an example output of the program. User input is boldfaced and red. You are free to make your interface look however you like.

How many suits? 2
How many ranks? 3

Deck of 6 cards: low = 1 high = 6 top = Card S1R1
1=shuffle, 2=deal 1 hand, 3=deal 100000 times, 4=quit: 1

Deck of 6 cards: low = 1 high = 6 top = Card S2R1
1=shuffle, 2=deal 1 hand, 3=deal 100000 times, 4=quit: 2
How many cards? 3

Card S1R1 Card S2R1 Card S1R3
Deck of 6 cards: low = 1 high = 6 top = Card S1R1
1=shuffle, 2=deal 1 hand, 3=deal 100000 times, 4=quit: 3
How many cards? 3

 5:  5001
 6:  9981
 7: 14801
 8:  9968
 9: 20099
10:  9918
11: 15098
12: 10078
13:  5056

Optional Extra: Here’s a nicer way to display the histogram, courtesy of Mark
Yendt. Scale the numbers down and print asterisks to represent the quantities.

 5:  5001  *****
 6:  9981  **********
 7: 14801  ***************
 8:  9968  **********
 9: 20099  ********************
10:  9918  **********
11: 15098  ***************
12: 10078  **********
13:  5056  *****                       or you could use graphics
Deck of 6 cards: low = 1 high = 6 top = Card S2R1
1=shuffle, 2=deal 1 hand, 3=deal 100000 times, 4=quit: 4

BYE!