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)
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.
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.
main( )
.proj2
package for this project.
main( )
is provided here for discussion purposes.
p2Util
package for this project.
Suit.CLUBS, Suit.DIAMONDS, Suit.HEARTS
, and Suit.SPADES
.
p2Util
package for this project.
Rank.ACE, Rank.TWO, Rank.THREE, ... Rank.JACK, Rank.QUEEN,
and Rank.KING
.
p2Util
package for this project.
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..java
files are presented in
Lab4
and on this course webpage.
main
in your Deck class to perform unit testing.
See Testing Your Class in Main for help writing main
to test your classes..java
files into the same Linux
directory. Compile them using the command javac -d . *.java.
Execute your project using the command java proj2.Project2.
You can check to see what files you have submitted by typing
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!