CMSC 202 Project 2
WAR!!
A Two Player Card Game
Assigned |
Monday, March 2, 2008 |
Program Due |
8:00AM, Monday, March 23, 2008 |
Weight |
7% |
Updates |
|
Objectives
- To gain experience designing, coding, and testing classes
- To gain experience with the use of composition in an OO design.
- To gain experience implementing an API
- To gain experience using classes from a library
Project Policy
This project is considered an OPEN project. Please review the
OPEN project policy on the course website.
Project Description
Perhaps the simplest card game in the known universe is the kid's game of "War".
In this simple game, a standard 52-card deck is shuffled and cards are dealt to each of the two players who place each card on the top of their pile.
In each turn the players shows the face of their top card. The player with the higher ranking card wins his opponents card and puts both cards on the bottom of his pile. If the two cards are the same rank, WAR breaks out. Each player places three (3) cards face down while yelling "I DE-CLARE" as loud as possible, then turning over the next (4th) card while shouting "WAR!!". The player with the higher ranking 4th card wins the two (2) cards that originally tied, all six (6) cards that were face down, plus the two (2) cards that determined the winner of the WAR -- ten (10) cards in all. Of course, if both player's 4th cards are the same rank, there's another WAR. Play continues until one player has all the cards.
In this project, you will implement classes necessary to play a variation of WAR.
In this simplified variation, the two players will turn over all cards in their deck just once. At the end of the game, the player with the most cards will be declared the winner.
For each turn, each player's name, the card he shows, and the result of the turn should be displayed. At the end of the game, the number of cards won by each player, the number of WARs
won by each player and the total number of WARs during the game should be displayed.
Sample output from an actual run of this project is shown below.
Welcome to WAR!!
Please enter player1's name: Bob
Please enter player2's name: Mary
Please enter the RNG seed for shuffling: 123456
Turn 1
-------
Bob shows three of Spades
Mary shows three of Hearts
WAR!!
Turn 2
-------
Bob shows four of Spades
Mary shows jack of Diamonds
Mary wins 10 cards
Turn 3
-------
Bob shows eight of Spades
Mary shows eight of Hearts
WAR!!
....
Turn 19
-----
Bob shows five of Diamonds
Mary shows jack of Spades
Mary wins 2 cards
Turn 20
-----
Bob shows three of Diamonds
Mary shows ace of Hearts
Mary wins 2 cards
Game Over!!
There were 2 wars
Bob won 22 cards and 1 war(s)
Mary won 30 cards and 1 war(s)
Specification Details
The design of this project will be discussed in class, but some details are presented here.
You will be given Project2.java
that has main( )
already
written. You will also be given files that contains classes that model a Card, the Rank of
a Card, and the Suit of a Card. The public methods available in these files are provided in the
class API found below. You will be required to implement the supporting classes discussed in class.
WAR uses a single standard 52-card deck.
There are four (4) suits (hearts, diamonds, clubs, and spades). In each suit there
are thirteen (13) ranks. In order from highest to lowest the ranks
are ace, king, queen, jack, 10, 9, 8, 7, 6, 5, 4, 3, and 2. Suits are irrelevant in WAR.
So that all students play the same game (i.e. the cards are dealt in the same order), it is necessary that the deck of cards be initialized appropriately before shuffling. Initialize the deck of cards in this order starting from the top of the deck --
Ks, Qs, .... 2s, As, Kh, Qh, .... 2h, Ah, Kd, Qd, ... 2d, Ad, Kc, Qc, .... 2c, Ac
. Looping through the arrays returned by Suit.VALUES( )
and
Rank.VALUES( )
when creating Cards to add to the deck will result in the
desired ordering.
The algorithm below which shuffles an array of integers must be adapted and implemented
to shuffle the cards to start a new game. This algorithm is known as the Fisher-Yates algorithm, was first designed for computers by Richard Durstenfeld, and popularized by Donald Knuth.
- A[0], A[1]... A[n-1] is an array of integers
- Let N = n-1
- Pick a random index, k, between 0 and N (inclusive).
- Swap the values of A[k] and A[N].
- Decrease N by one.
- Repeat from step (c) until N is less than 1.
To choose a random index (step c), use the nextInt( )
method of the random number
generator class Random
found in the Java library. If you seed the random number generator with the value 123456, the shuffled deck and player cards should be as follow and
the result of playing the game should match the sample output above.
After shuffling, the first 10 cards at the top of the deck are
3d, Ah, 5d, Js, Td, Qs, 6c, 4d, Kc, and 9h.
After dealing the cards, player 1's first 5 cards starting from the top of his deck
are 3s, Ac, Qh, 7c, and 4s. Player 2's first 5 cards starting from the top of his deck are 3h, 3c, 2c, 8c, and Jd.
Provided Classes
- Project2.java implements
main( )
.
Be sure to import Project2.java into the proj2
package for this project.
A copy of main( )
is provided here for discussion purposes.
- Card.java defines one of the 52 playing cards used to play WAR.
Documentation for Card.java is provided in the file itself and in the Card API document
Be sure to import Card.java into the p2Util
package for this project.
- Suit.java defines Suit objects used to identify a card.
The four suits in a standard 52-card deck are Suit.CLUBS, Suit.DIAMONDS, Suit.HEARTS
, and Suit.SPADES
.
Do not submit Suit.java.
Documentation for Suit.java is provided in the file itself and in the Suit API document
Be sure to import Suit.java into the p2Util
package for this project.
- Rank.java defines Rank objects used to identify a card.
The ranks of the cards in a standard 52-card deck are
Rank.ACE, Rank.TWO, Rank.THREE, ... Rank.JACK, Rank.QUEEN,
and Rank.KING
.
Do not submit Rank.java.
Documentation for Rank.java is provided in the file itself and in the Rank API document
Be sure to import Rank.java into the p2Util
package for this project.
Requirements, Hints, and Tips
- Mr. Frey's public directory for this project is
/afs/umbc.edu/users/f/r/frey/pub/202/Proj2
- This project will have two Java packages.
Project2.java
and all the
.java
files you create will be contained in the proj2
package.
Card.java, Suit.java
and Rank.java
will be contained in the
p2Util
package.
- The provided classes can be copied from Mr. Frey's public directory and imported
into your Eclipse project. These files are to be used as is -- with no changes. but DO NOT SUBMIT these files. If these files are found in your submittal directory they will be deleted prior to grading your project.
Directions for importing .java
files are presented in
Lab4
and on this course webpage.
- You must implement
main
in your Deck class to perform unit testing.
See Testing Your Class in Main for help writing main
to test your classes.
- All public methods of the Deck class must provide complete method header comments including
pre- and post-condition comments
- Think about code reuse!
- In theory, a WAR may break out when the players don't have enough cards to complete the
WAR as described above. This will of course depend on the RNG seed you use for shuffling the Deck. This situation WILL NOT arise when using the seed value 123456 and we gurantee that it will
not happen when your project is graded, so there's no need to design or code for this case.
- To compile and run your program on Linux, copy all
.java
files into the same Linux
directory. Compile them using the command javac -d . *.java.
Execute your project using the command java proj2.Project2.
- Assuming that your deck is implemented as an array of Cards, you may find coding easier
if the highest array index is considered the "top" of the deck.
Grading
See the course website for a description of
how your project will be graded.
Project Submission
- submit all .java files you create..
DO NOT submit Project2.java, Card.java, Suit.java, Rank.java.
- submitls to verify they are in the remote directory
The order in which the files are listed doesn't matter. However, you must make
sure that all files necessary to compile and run your project are submitted.
You need not submit all files at the same time. You may resubmit
your files as often as you like, but only the last submittal will be graded and
will be used to determine if your project is late. For more information,
see the projects page on the course website.
You can check to see what files you have submitted by typing
submitls cs202 Proj2
More complete documentation for submit and related commands can be found
here.
Remember -- if you make any change to your program, no matter how
insignificant it may seem, you should recompile and retest your program before
submitting it. Even the smallest typo can cause compiler errors and a reduction
in your grade.
Avoid unpleasant surprises!