C++ Script for Card Game \"War\" Demonstration C++ Card class The Card class is
ID: 3765206 • Letter: C
Question
C++ Script for Card Game "War" Demonstration
C++ Card class
The Card class is used to represent a single playing card.
Data Members
The class contains two data members.
An integer that holds the value of the face of the card
A character variable that holds the suit of the card.
The range of possible values for the integer data member (the face of the card) is 1 through 13 inclusive, with 1 representing an Ace, 11 representing a Jack, 12 representing a Queen, and 13 representing a King. All of the other values represent that specific card.
The possible values for the character data member are: 'C' to represent clubs, 'D' to represent diamonds, 'H' to represent hearts, and 'S' to represent spades.
Constructor
This class has one constructor, a default constructor (ie. one that takes no arguments). It should create a random card by using the random number generator.
For the face value (the integer), generate a random number between 1 and 13, inclusive.
For the suit value (the character), generate a random number between 1 and 4, inclusive. If the random number is 1, assign 'C' to the character data member. If the random number is 2, assign 'D' to the character data member. If the random number is 3, assign 'H' to the character data member. If the random number is 4, assign 'S' to the character data member.
Note: DO NOT set the seed value for the random number generator in the constructor. It has already been done in main.
Methods
void setCard( int newFace, char newSuit )
This method sets both the face and the suit for the Card object. It takes two arguments: an integer that represents the new face value for the card and a character that represents the new suit value for the card. It returns nothing.
No error checking is required. Simply take the passed-in values and assign them to the corresponding data members.
int getFace()
This accessor method returns the face value of the Card. It takes no arguments and returns an integer.
char getSuit()
This accessor method returns the suit value of the Card. It takes no arguments and returns a character.
void displayCard()
This method displays a text version of the Card. It takes no arguments and returns nothing.
For the face value, if the value is:
1, display "Ace"
11, display "Jack"
12, display "Queen"
13, display "King"
any other value, display the value
For the suit value, if the value is
'C' display "Clubs"
'D' display "Diamonds"
'H' display "Hearts"
'S' display "Spades"
So if a Card object has a face value of 6 and suit value of 'H',
should be displayed. Or if a Card object has a face value of 12 and suit value of 'C',
should be displayed.
Part 1: Testing the Card class
Before using the Card class as part of a larger project, it should be tested to make sure that the constructor and all of the methods work. A short program has been written that will test each one of the methods individually.
The test program can be downloaded from http://faculty.cs.niu.edu/~byrnes/csci240/pgms/240test10.cpp
The output that is produced by the test program:
If the output, using your Card class, matches the above output, move on to part 2 of this assignment. Otherwise, fix the constructor/methods until the output does match.
Part 2: Using the Card class
This is the part of the assignment that will be submitted for grading.
For this part of the program, create a simplified version of the card game War that uses the Card class and another pre-defined class named DeckOfCards.
The concept of our simplified version of War is, well, simple. It's a two-player game that uses a deck of 52 cards (no jokers). Each player will draw a card. If the face values of the cards are equal, both players will be awarded one point because the "war" was a draw. If player 1's card has the larger face value, player 1 will be awarded 2 points because they won the "war". If player 2's card has the larger face value, player 2 will be awarded 2 points because they won the "war". This process will continue until the deck of cards is empty (ie. all of the cards have been drawn from there deck and there are no more cards).
After all of the cards have been drawn, display the score for each player and declare a winner for the game. If the two players have the same score, declare the game a draw.
DeckOfCards class
This class has been pre-written and will be used to implement the game. The code can be found at: http://faculty.cs.niu.edu/~byrnes/csci240/pgms/assign10.cpp
Add the code for the Card class where indicated in the CPP file.
The DeckOfCards class is used to simulate a deck of 52 playing cards. It contains the following data members:
MAX_CARDS an integer symbolic constant that represents the maximum number of cards in a deck of cards. Its value is 52.
NUM_SUITS an integer symbolic constant that represents the number of different suits for the cards in the deck. Its value is 4.
CARDS_PER_SUIT an integer symbolic constant that represents the number of different face cards for each suit in the deck. Its value is 13.
deck an array of 52 Card objects. It represents the actual deck of cards.
topCard an integer that represents the subscript of the card that is on the top of the deck. It's initial value is -1 to represent that no cards have been removed from the deck.
The constructor and methods for the DeckOfCards class are as follows:
DeckOfCards(): the class has a single constructor that takes no arguments. It initializes all 52 elements of the deck array to the 52 cards in a standard deck of playing cards. It also initializes the topCard data member to -1 to indicate that no cards have been removed from the deck.
Card draw(): this method draws a card from the deck. It takes no arguments and returns a Card object: the card that is drawn from the deck.
void shuffle(): this method shuffles all 52 cards in the deck. It takes no arguments and returns nothing.
bool isEmpty(): this method determines if all of the cards have been drawn from the deck. It takes no arguments and returns a boolean value: true if the deck is empty (no more cards to draw) or false is the deck is not empty (there are still cards that can be drawn)
Implementing the game
Set the seed value that is used by the random number generator by using the srand() function. Pass the value time(NULL) or time(0) to the srand function.
Create a DeckOfCards object that will used for the game.
Create two Card objects. These will be used to hold the cards that are drawn by the two people playing the game.
Create and initialize two integers to hold the game scores for the two players.
Shuffle the deck of cards by calling the shuffle method for the DeckOfCards object that was created earlier.
Write a loop that will execute as long as the deck of cards is not empty. Inside of the loop:
Player 1 should draw a card
Player 2 should draw a card
Display the two cards that were drawn
If the face values of the two cards are equal, awarded one point to both players and declare the "war" a draw. If player 1's card has the larger face value, award player 1 2 points and declare that player 1 won the "war". If player 2's card has the larger face value, award player 2 2 and declare that player 2 won the "war".
Once the deck of cards is empty, display the scores for both players and declare a winner. If the scores are equal, declare the battle a "draw".
Programming Requirements
Each method that is required must have a documentation box like a function.
Hand in a copy of the source code from part 2 of the assignment using Blackboard.
Note
For ease, an Ace is considered the smallest/lowest card in the deck.
Output
A couple runs of the program might resemble the following but keep in mind that the random number generator is being used so your output will probably not match this output completely.
Run 1:
/********** Code the Card class methods between these lines **********/ /*************************************************************************/
//The constructor and methods for the pre-written DeckOfCards class follows
/***************************************************************
Constructor Use: This creates a DeckOfCards object Arguments: none Note: -1 is used to signal that no cards have been removed from the deck
***************************************************************/
DeckOfCards::DeckOfCards() { //An array of the 4 possible values for the card suit char suitVals[NUM_SUITS] = { 'C', 'D', 'H', 'S' }; int cardSub = 0; //subscript to process the deck of cards //Go through all 52 spots in the array of Cards and put a card //at the location for( int suitSub = 0; suitSub < NUM_SUITS; suitSub++ ) { //For each of the suits, put in all of the cards for the suit for( int faceVal = 1; faceVal <= CARDS_PER_SUIT; faceVal++ ) { //Put the card into the deck deck[ cardSub ].setCard( faceVal, suitVals[suitSub] ); //Move to the next card in the deck cardSub++; } } //Set the top card location to -1 to indicate the deck is brand new topCard = -1; }
/***************************************************************
Method: Card draw() Use: This method draws a card from the top of the deck Arguments: none Returns: a Card object (the card on the top of the deck)
***************************************************************/
Card DeckOfCards::draw() { //Increment to get the subscript of the current top card topCard++; //return the card that is currently on the top of the deck return deck[topCard]; }
/***************************************************************
Method: void shuffle() Use: This method shuffles the deck of cards Arguments: none Returns: nothing Note: this method uses the random_shuffle function that is part of the algorithm library to shuffle the 52 cards
***************************************************************/
void DeckOfCards::shuffle() { //Shuffle all 52 cards that are in the deck random_shuffle(deck, deck+MAX_CARDS); } /***************************************************************
Method: bool isEmpty() Use: This method determines if the deck of cards is empty( have all of the cards been drawn) Arguments: none Returns: boolean value: true if all of the cards have been drawn false if there are still cards in the deck
***************************************************************/ bool DeckOfCards::isEmpty() { return topCard + 1 == MAX_CARDS; }
Explanation / Answer
import java.util.*;
public class Deck
{
public static final int MAX_SIZE = 52;
private static ArrayList<Card> cards;
public Deck()
{
reset();
}
private void reset()
{
cards = new ArrayList<Card>();
addSuit(Suit.spade);
addSuit(Suit.heart);
addSuit(Suit.diamond);
addSuit(Suit.club);
}
private void addSuit(Suit suit)
{
for (int i = 1; i <= 13; i++)
cards.add(new Card(suit, i));
}
public boolean isEmpty()
{
return cards.isEmpty();
}
//methods
public void add(Card aCard)
{
cards.add(aCard);
}
public void clear()
{
cards.clear();
}
public static Card getTopCard()
{
return cards.remove(0);
}
public int size(){
return cards.size();
}
public Card deal(){
if( isEmpty())
return null;
else
return cards.remove(cards.size() -1);
}
public Card[] deal(int number){
if (number > cards.size())
return null;
else{
Card[] hand = new Card[number];
for(int i = 0; i < hand.length; i++)
hand[i] = deal();
return hand;
}
}
public void shuffle()
{
if (cards.size() < MAX_SIZE)
return;
Random gen = new Random();
Card[] array = new Card[MAX_SIZE];
while (cards.size() > 0){
Card card = cards.remove(cards.size() - 1);
int i = gen.nextInt(MAX_SIZE);
while (array[i] != null)
i = gen.nextInt(MAX_SIZE);
array[i] = card;
}
for (Card card : array)
cards.add(card);
}
public String toString()
{
String result = "";
for(Card card : cards)
result += card + " ";
return result;
}
}
Card:
public class Card implements Comparable
{
private Suit suit;
private int rank;
private boolean faceUp;
public Card(Suit suit, int rank)
{
this.suit = suit;
this.rank = rank;
faceUp = false;
}
public Card(int i, char rank2)
{
// TODO Auto-generated constructor stub
}
public boolean equals(Object other)
{
if ( this == other)
return true;
else if (! (other instanceof Card ))
return false;
else{
Card otherCard = (Card)other;
return rank == otherCard.rank;
}
}
public int compareTo(Object other)
{
if (! (other instanceof Card))
throw new IllegalArgumentException("Parameter must be a card ");
Card otherCard = (Card)other;
return rank - otherCard.rank;
}
public int getRank()
{
return rank;
}
public Suit getSuit()
{
return suit;
}
public boolean isFaceUp()
{
return faceUp;
}
public boolean isRed()
{
return suit == Suit.heart || suit == Suit.diamond;
}
public void turn()
{
faceUp = ! faceUp;
}
public String toString()
{
return rankToString() + " of " + suit;
}
private String rankToString()
{
if ( rank == 1)
return "Ace";
else if (rank == 11)
return "Jack";
else if (rank == 12)
return "queen";
else if (rank == 13)
return "king";
else
return "" + rank;
}}
Suit:
public class Suit implements Comparable
{
static public final Suit spade = new Suit(4, "spades");
static public final Suit heart = new Suit(3, "hearts");
static public final Suit diamond = new Suit(2, "diamonds");
static public final Suit club = new Suit(1, "clubs");
private int order;
private String name;
private Suit(int ord, String nm)
{
name = nm;
order = ord;
}
public int compareTo(Object other)
{
if(! (other instanceof Suit))
throw new IllegalArgumentException("Parameter must be a Suit");
Suit otherSuit = (Suit)other;
return order - otherSuit.order;
}
public String toString()
{
return name;
}
}
import java.util.*;
public class War
{
public static void main(String args[])
{
{
int halfDeck;
Scanner scanner = new Scanner (System.in);
Deck unplayedpile = new Deck();
Deck deck1 = new Deck();
Deck deck2 = new Deck();
unplayedpile.shuffle();
unplayedpile.add(Deck.getTopCard());
Card card1 = unplayedpile.getTopCard();
Card card2 = unplayedpile.getTopCard();
System.out.println(card1);
System.out.println(card2);
}}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.