CS
305j - Programming Project 8
The Game of War
Practice with: Writing your own classes
This assignment is a pair assignment. You may work with one other
person on this project using the pair programming technique (as
described on the course webpage - read, and follow, the rules
carefully). You are not required to work with a partner; you may work
alone. If you choose to work with a partner, you are allowed to work
with any CS 305j student - your partner does not need to be in your
discussion section. If you work as a pair, only one of you should
submit your solution. Once you start working on the assignment, you are
not allowed to switch partners! If you do not want to work with your
partner after starting the assignment, you must both complete the
assignment individually, and you must both email the instructor to
explain why you are not completing the assignment as a pair. Failure to
follow the pair programming rules (and include the necessary
documentation in your files) will result in significant point
deductions.
This project will count twice as much as your other projects.
As
for all CS 305j projects, your file comment header should look like
this:
/**
* author: <Your Name Here>
* date: <Submission Date>
* CS 305j Assignment 2
* On my honor, <Your Name>, (or <Your Name>,
and <Your Partner's Name>) this programming assignment is
my (our) own work.
*
* EID: <Your EID> (include for both partners, if applicable)
* Section: <Unique Number>, <Tuesday discussion time>
(include for both partners, if applicable)
*
* <Brief Description - what does the program do?>
*
* Slip Days I am using on this project: <Your Slip Days>
* Slip Days I have used this semester: <Your Total, including for
this project>
*/
Note: This is a lengthy
assignment. You will not be able to complete it in a week or less! Do
not procrastinate.
There are 3 basic parts to the assignment. Each is described in detail
below. Do these parts in the order in which they are described, and
make sure that each part is working correctly before you continue on to
the next part.
1. The Card class
For this class you will be simulating one card - a card in a deck of
playing cards. A playing card has two properties: a suit and a value.
We encode each of these as an integer using the following scheme:
Suit
Spades - 3
Hearts - 2
Diamonds - 1
Clubs - 0
(Note: This is a good opportunity to use a
constant - you are required to do so. See
the "getSuitAsString()" method below.)
Value
Ace - 1
2-10 will be represented by the actual value
Jack - 11
Queen - 12
King - 13
1) You will need to write two constructors
and
accessor
methods for each property. One constructor creates a random, valid
card. The
second constructor will accept two parameters. The first parameter is
the suit
and the second parameter is the card value.
2) Also override the clone(), toString(),
and
equals() methods from the Object class.
3) Write a compareValue() method that takes
another Card object and returns true if the current object has greater
value and false otherwise. The header for this method is
public boolean compareValue(Card other) - it returns true if this
card's value is greater than other's value.
4) You should also include the following
methods
in your
class:
public String getSuitAsString() {
// Return a String representing the card's suit.
// (If the card's suit is invalid, "??" is returned.)
switch ( suit ) {
case SPADES: return "Spades";
case HEARTS: return "Hearts";
case DIAMONDS: return "Diamonds";
case CLUBS: return "Clubs";
default: return "??";
}
}
public String getValueAsString() {
// Return a String representing the card's value.
// If the card's value is invalid, "??" is returned.
switch ( value ) {
case 1: return "Ace";
case 2: return "2";
case 3: return "3";
case 4: return "4";
case 5: return "5";
case 6: return "6";
case 7: return "7";
case 8: return "8";
case 9: return "9";
case 10: return "10";
case 11: return "Jack";
case 12: return "Queen";
case 13: return "King";
default: return "??";
}
}
public String toString() {
// Return a String representation of this card, such as
// "10 of Hearts" or "Queen of Spades".
return getValueAsString() + " of " + getSuitAsString();
}
2. The Deck class
The Deck class represents a standard deck of
52 playing cards. It has three instance variables:
1. An array of 52 cards (ie Card objects)
2. An integer index into the array called top. If top is 4, that
means that cards 0 to 3 have already been dealt. The next card to deal
is at position 4 in the array.
3. the number of cards currently in the deck, deckSize.
Your Deck class should contain the following public methods:
1. A constructor that takes no arguments, and initializes a deck in
order by suit and rank. Ie the constructor creates 52 new Card objects,
and puts them in order in the deck: ace of clubs, two of clubs, ...
king of clubs, ace of diamonds, two of diamonds, ... etc.
2. void shuffle(): this method shuffles the deck. Remember to ignore
the cards which have been dealt when shuffling. To shuffle the
deck, consider repeatedly (say 500 times or so) picking two cards in
the deck at random and swapping their positions.
3. Card dealCard(): deal the card on top of the deck and return it.
4. Accessor methods for instance variables top and deckSize.
5. Overwritten version of toString() that returns a string
representation of each card currently in the deck.
6. Overwritten version of clone() that returns a copy of this deck.
3. Playing the game of War
The card game War has the following rules:
- Shuffle a deck of cards, and deal them out into two hands of
26
cards each.
- Play rounds until one player is out of cards. A round
consists of
the following:
- Both players turn over their top card. The card with the
largest value wins the round, and the winning player adds both cards to
the bottom of his/her hand (winning card on top). Suit doesn't matter -
only the value of the cards.
- If the cards have the same value, a "war" begins. The
equal-value cards stay on the table, and both players put out one card
face-down and the next face-up. Whichever player has the higher-valued
card face-up wins the war, and takes all six cards. If the face-up
cards have equal value, the war continues, and goes on until there is
no longer a tie between the face-up cards.
- If a player runs out of cards during war, that player
loses the
game. If both players run out of cards at the same time, the game is a
draw.
For this project, you will implement the game of War. Your program
should play War until one player wins, or until 5000 rounds have been
played. If you stop after 5000 rounds, your program should display how
many cards each player has at that point, and if the game ends because
one player won, your program should indicate who won.
You will need to use your Card and Deck classes. I suggest you add a
compareTo() method to your Card class so that you can compare two
cards. This method should have this signature:
- public int compareTo(Object c)
- This method returns -1 if card is less than c, 0 if equal,
or 1
if card is greater than c
- Aces are high
You will also need some other classes:
- Hand - methods you may want to add to your Hand class are:
- size() - returns the number of cards currently in the hand
- isEmpty() - returns whether the hand is empty or not, ie
true
or false
- addCard(Card c) - add c to the hand as described above
- getNext() - remove the top card from the hand and return it
- War - You need a class that uses helper methods to play a
game of
war. You need to shuffle the deck of cards, deal two hands, and
simulate a game of war. You may want to write a method that takes two
hands as arguments and plays war until one player/hand wins or until
the game ends. Keep track of the number of rounds and the number of
wars in the game, and display those when the game is over.
The main method in your program should be contained in the War class.
Submit Card.java, Deck.java, Hand.java and War.java (and other classes,
as described in your documentation, if you choose) by the due date.
Remember that slip days
cannot be used on this project and plan
accordingly.
You are allowed, but not required, to work with a partner on this
project. If you choose to work with a partner, follow all the rules
that have been described previously about pair projects. Only one
partner should submit the project.