CSC 145 Project 5 - Using the Card and Deck Class

Total Page:16

File Type:pdf, Size:1020Kb

CSC 145 Project 5 - Using the Card and Deck Class

CSC 145 Project 5 - Using the Card and Deck Class

In this project you will build a simple card game that uses pre-defined Card and Deck classes to emulate a deck of 52 playing cards.

Introduction - A standard deck of 52 playing cards has four suits (Clubs, Spades, Hearts, Diamonds) with 13 cards in each suit (Ace, 2, . . . , 10, Jack, Queen, King). We first define a Card class from which we can instantiate individual cards.

The attributes of the Card class include strings for the suit and rank and an integer indicating the numeric value of the card based on some card game. In this example we will assume that the value of each card matches the rank with Jack = 11, Queen = 12, King = 13 and Ace = 14. The Card constructor accepts the three attributes as parameters in the order of suit, rank, and value.

Note: All instance classes include a default constructor that instantiates a reference type for the class with none of its attributes declared.

We also define a Deck class that can be used to instantiate an entire card deck. The Deck class constructor instantiates 52 cards using the Card class. A card-playing program can create a deck of cards without calling upon the Card class directly. The attributes of the Deck class are an array of 52 cards and an integer indicating the current top card in the deck.

In addition to the attributes the Deck class includes two methods. One for shuffling the deck and the other for dealing a card from the deck.

Shuffle - The shuffle( ) method swaps the positions of pairs of cards in the deck randomly choose the pair of cards to swap. This random pair swap operation is repeated 1000 times. Also the value of topcard is reset to 0.

Deal - The dealCard( ) method checks the current topcard and if its value is < 52 it returns a reference to the corresponding card in the deck and then increment the value of topcard by 1.

The source code for both the Card class and the Deck class are provided with this project. They should be download into the same folder as the game program you are building and compiled separately. To ensure you have properly installed these classes you can run a simple test program such the following: Program - For this project we will implement a simple card game called War. In this game two players are each dealt a card. The player whose card has the highest rank wins the round. Play is repeated until all the cards have been dealt. The player who wins the most rounds wins the game.

Your task is to design and implement this game using the Card and Deck classes provided.

Declarations - You will need to declare a card type variable to "hold" the cards dealt to each player, and you will need variables of integer data type to keep count of the number of wins by each player. Finally you will want to instantiate a deck of cards as part of your declarations.

Initialization - Before entering game play the counts are initialized to zero (actually can be part of the declaration as shown above). Also the deck of cards must be shuffled. This randomizes the order of the cards in the deck and resets the topcard index to 0.

The Game Loop - Since the players don't make any decisions during the play we don't need to worry about user interaction with the game (not much of a game). There are 52 cards in a deck which means there are 26 rounds in the game. We can implement the game in a for-loop in which one card is dealt each player, the values are compared and the win is awarded to the player whose card has the higher value. In this version of the game ties are awarded to neither player.

Your task is to design and implement the remainder of the War program. Use the following specifications as a guide:

1. In each pass of the game loop two cards are dealt, one to each player.

p1_card = the_deck.dealCard();

2. The player with the higher value card wins the round.

3. The win count of the winning player is incremented by one (1).

4. The rank and suit of each player's card should be displayed.

System.out.println("Player 1 " + p1_card.rank + " " + p1_card.suit);

5. The program should indicate the winner of each round

6. Rate of play may be slowed by including a delay between rounds (optional).

Results - After the completion of all rounds, the program should display a message indicated the game winner showing the win counts for each player. CSC 145 - Project 5

Name ______Score ______

Questions - Review your code as well as the Card and Deck classes as you answer the following:

1. Your program instantiates a deck of cards. Where are the individual cards instantiated?

______

2. What is the return type of the dealCard( ) method in the Deck class? ______

3. What does the dealCard( ) method return when there are no more cards in the deck? ______

4. In the shuffle( ) method, what happens if both k and m are the same values? ______

______

5. The shuffle( ) method swaps a randomly selected pair of cards in the deck 1000 times. Assuming that the any card is equally likely to be chosen for swapping, give a rough estimate of the probability that a particular card will be in its original position after shuffle( ) completes.

______

______

6. In the card game BlackJack, face cards have a value of 10, while aces can be 1 or 11. How might you modify the Deck Class to support a BlackJack program?

______

______

7. There are 4 suits and 13 different ranks of cards in each suit. The Deck class breaks the index value i into a suitnum and a ranknum in the following manner:

suitnum = i%4; ranknum = i/4 + 1;

Alternatively we could use the following statements to produce the same values:

suitnum = i/13; ranknum = i%13 + 1;

Explain how this is possible. ______

______

8. Write a Java statement that displays the rank and suit of the 42nd card in a_deck without using the dealCard( ) method.

Submission - Submit a hard-copy of the source code of your program with this worksheet.

Recommended publications