There are two parts to this homework: a written component worth 11 points, and programming component worth 14 points. Submission instructions are available here. Make sure to note the different deadline and late policy for this homework above. Since there is no class on that day, written homeworks can be submitted to the CS building before it closes at 5pm, to 608 CEPSR before it closes at 5:30pm, or electronically along with your programming assignment.
Note that problems assigned from Schneider/Gersting or Lewis/Loftus are the exercise problems at the end of each chapter, not the practice problems or self-review questions. (The practice/self-review problems are optional, and solutions for them are provided in the book. For obvious reasons, the solutions for the exercises are not. ;-))
As described below, you will submit the design part of this assignment as the written problem #2, and the implementation part of this assignment electronically (including commented source, README, and a typescript).
The Game
The game of War is a classic card game, one that's popular with kids for an important reason: it's deterministic. That is, there is no thinking or strategy involved, just luck and probabilities. Here's a summary of the game on Wikipedia, but to make everyone's implementation consistent, the rules we'll follow are outlined here.
Your Design (and Our Requirements)
As we promised earlier, you have the opportunity to design your program to conform to the above rules -- that is, you decide what variables and methods go in each class. There are a few additional requirements that we will impose upon you, however:
Card
class, a Player
class that holds a pile of cards, and a
Game
class that actually runs the game. The classes
have their own requirements (although by no means do these requirements
imply the exact design):Card
must contain both value and suit, and should
implement the Comparable
interface to enable comparing
two cards as per the rules of War (i.e., ignore suits, just use
values; suits are just there to get an idea of which cards a player
has). Card
must also contain a toString
method that produces a human-readable representation of a card.
Something like "6C
", meaning the six of clubs, is
sufficient.Player
uses the Java ArrayList
class
to contain a variable number of Card
objects.
Don't use a "regular" Java array (i.e., Card[]
).
Player
must also contain a toString
method: in this case, it'll print out the number of cards and the
value of each card in the ArrayList
.Game
must have a main
method to run
the game. Upon startup, Game must generate a deck of cards and
deal them randomly to the two players. It then must
display the initial hands, play the game as per the rules above, and
add a user interface: after a battle, Game
should show
the updated hands and prompt the user with three options --
[Enter]
to play another battle, a number to automatically
play the desired number of battles, or q
to quit the
game. (Note that you do not need to prompt for
sub-battles as part of a war.) Once the game ends or is
quitted, you should print out the final winner, if any, the number
of battles fought, and the number of wars fought.Before you actually implement this design, you must first document it. This will serve as question #2 in your written section. The written portion must have two parts:
main
method that runs
the game once the deck has been dealt). You should try to be as
precise as possible. For full credit, make sure to set up the
loops for both battles (2 points) and wars (2 points) so that they run
as per the rules above.You may find that, during implementation, your design will change a little bit. This is normal, but if you do make changes, include a few sentences at the end of the design document pointing out your changes and why you made them.
Your Implementation
Here's a point breakdown of the implementation.
Card
class
toString
)Comparable
interfacePlayer
class
toString
)ArrayList
Game
classYou may find the following hints helpful when designing and implementing the game.
ArrayList
. Pick one at random,
and assign it to the first player. Pick another one at random, and
assign it to the second player. Repeat until the ArrayList
is empty.ArrayList
, and throw in the first two cards along
with the 6 "face-down" war cards. Then, the battle code can pick
two more cards, determine the winner, and give the winner both the two
cards as well as the war chest. This mechanism is also convenient
as it allows for multiple wars without additional work.Sample Execution
Here's an execution of our game. Your game should look similar to this one, but since the design is up to you, it doesn't have to be completely identical. However, make sure to design and implement the requirements as stated above.
$ java Game
Player A initial hand: 26 cards:
[2C|2S|2D|QD|3H|KH|3D|QC|6D|JC|8C|QS|4S|JD|4D|QH|7C|5H|5C|2H|4H|AC|9C|8D|10S|4C]
Player B initial hand: 26 cards:
[5S|AH|7S|9D|9H|10D|KS|8H|6H|KC|JH|7D|5D|7H|KD|9S|AD|6C|AS|3C|10C|6S|3S|10H|8S|JS]
*** Playing battle 1
Player A has 2C, player B has 5S
Player B wins
*** Battle results:
Player A has 25 cards:
[2S|2D|QD|3H|KH|3D|QC|6D|JC|8C|QS|4S|JD|4D|QH|7C|5H|5C|2H|4H|AC|9C|8D|10S|4C]
Player B has 27 cards:
[AH|7S|9D|9H|10D|KS|8H|6H|KC|JH|7D|5D|7H|KD|9S|AD|6C|AS|3C|10C|6S|3S|10H|8S|JS|2C|5S]
Hit enter for next battle, q to quit, or a number of battles:
*** Playing battle 2
Player A has 2S, player B has AH
Player B wins
*** Battle results:
Player A has 24 cards:
[2D|QD|3H|KH|3D|QC|6D|JC|8C|QS|4S|JD|4D|QH|7C|5H|5C|2H|4H|AC|9C|8D|10S|4C]
Player B has 28 cards:
[7S|9D|9H|10D|KS|8H|6H|KC|JH|7D|5D|7H|KD|9S|AD|6C|AS|3C|10C|6S|3S|10H|8S|JS|2C|5S|2S|AH]
Hit enter for next battle, q to quit, or a number of battles:
*** Playing battle 3
Player A has 2D, player B has 7S
Player B wins
*** Battle results:
Player A has 23 cards:
[QD|3H|KH|3D|QC|6D|JC|8C|QS|4S|JD|4D|QH|7C|5H|5C|2H|4H|AC|9C|8D|10S|4C]
Player B has 29 cards:
[9D|9H|10D|KS|8H|6H|KC|JH|7D|5D|7H|KD|9S|AD|6C|AS|3C|10C|6S|3S|10H|8S|JS|2C|5S|2S|AH|2D|7S]
Hit enter for next battle, q to quit, or a number of battles:
*** Playing battle 4
Player A has QD, player B has 9D
Player A wins
*** Battle results:
Player A has 24 cards:
[3H|KH|3D|QC|6D|JC|8C|QS|4S|JD|4D|QH|7C|5H|5C|2H|4H|AC|9C|8D|10S|4C|QD|9D]
Player B has 28 cards:
[9H|10D|KS|8H|6H|KC|JH|7D|5D|7H|KD|9S|AD|6C|AS|3C|10C|6S|3S|10H|8S|JS|2C|5S|2S|AH|2D|7S]
Hit enter for next battle, q to quit, or a number of battles:
*** Playing battle 5
Player A has 3H, player B has 9H
Player B wins
*** Battle results:
Player A has 23 cards:
[KH|3D|QC|6D|JC|8C|QS|4S|JD|4D|QH|7C|5H|5C|2H|4H|AC|9C|8D|10S|4C|QD|9D]
Player B has 29 cards:
[10D|KS|8H|6H|KC|JH|7D|5D|7H|KD|9S|AD|6C|AS|3C|10C|6S|3S|10H|8S|JS|2C|5S|2S|AH|2D|7S|3H|9H]
Hit enter for next battle, q to quit, or a number of battles:
*** Playing battle 6
Player A has KH, player B has 10D
Player A wins
*** Battle results:
Player A has 24 cards:
[3D|QC|6D|JC|8C|QS|4S|JD|4D|QH|7C|5H|5C|2H|4H|AC|9C|8D|10S|4C|QD|9D|KH|10D]
Player B has 28 cards:
[KS|8H|6H|KC|JH|7D|5D|7H|KD|9S|AD|6C|AS|3C|10C|6S|3S|10H|8S|JS|2C|5S|2S|AH|2D|7S|3H|9H]
Hit enter for next battle, q to quit, or a number of battles:
*** Playing battle 7
Player A has 3D, player B has KS
Player B wins
*** Battle results:
Player A has 23 cards:
[QC|6D|JC|8C|QS|4S|JD|4D|QH|7C|5H|5C|2H|4H|AC|9C|8D|10S|4C|QD|9D|KH|10D]
Player B has 29 cards:
[8H|6H|KC|JH|7D|5D|7H|KD|9S|AD|6C|AS|3C|10C|6S|3S|10H|8S|JS|2C|5S|2S|AH|2D|7S|3H|9H|3D|KS]
Hit enter for next battle, q to quit, or a number of battles:
*** Playing battle 8
Player A has QC, player B has 8H
Player A wins
*** Battle results:
Player A has 24 cards:
[6D|JC|8C|QS|4S|JD|4D|QH|7C|5H|5C|2H|4H|AC|9C|8D|10S|4C|QD|9D|KH|10D|QC|8H]
Player B has 28 cards:
[6H|KC|JH|7D|5D|7H|KD|9S|AD|6C|AS|3C|10C|6S|3S|10H|8S|JS|2C|5S|2S|AH|2D|7S|3H|9H|3D|KS]
Hit enter for next battle, q to quit, or a number of battles:
*** Playing battle 9
Player A has 6D, player B has 6H
WAR!
Player A has 4S, player B has 5D
Player B wins
Giving player B the warchest!
*** Battle results:
Player A has 19 cards:
[JD|4D|QH|7C|5H|5C|2H|4H|AC|9C|8D|10S|4C|QD|9D|KH|10D|QC|8H]
Player B has 33 cards:
[7H|KD|9S|AD|6C|AS|3C|10C|6S|3S|10H|8S|JS|2C|5S|2S|AH|2D|7S|3H|9H|3D|KS|6D|6H|JC|KC|8C|JH|QS|7D|4S|5D]
Hit enter for next battle, q to quit, or a number of battles:
*** Playing battle 11
Player A has JD, player B has 7H
Player A wins
*** Battle results:
Player A has 20 cards:
[4D|QH|7C|5H|5C|2H|4H|AC|9C|8D|10S|4C|QD|9D|KH|10D|QC|8H|JD|7H]
Player B has 32 cards:
[KD|9S|AD|6C|AS|3C|10C|6S|3S|10H|8S|JS|2C|5S|2S|AH|2D|7S|3H|9H|3D|KS|6D|6H|JC|KC|8C|JH|QS|7D|4S|5D]
Hit enter for next battle, q to quit, or a number of battles: 1000
*** Playing battle 12
Player A has 4D, player B has KD
Player B wins
*** Playing battle 13
Player A has QH, player B has 9S
Player A wins
*** Playing battle 14
Player A has 7C, player B has AD
Player B wins
*** Playing battle 15
Player A has 5H, player B has 6C
Player B wins
{And so on; removed for brevity}
*** Playing battle 1037
Player A has 9D, player B has 4S
Player A wins
*** Playing battle 1038
Player A has 3S, player B has QC
Player B wins
*** Playing battle 1039
Player A has 10C, player B has 7D
Player A wins
*** Battle results:
Player A has 25 cards:
[9C|KH|6C|8C|5S|10S|3C|AD|7C|6S|4D|KC|3D|QS|5H|JS|2C|JC|5D|10H|6D|9D|4S|10C|7D]
Player B has 27 cards:
[10D|2D|QH|8D|AH|JH|AC|2S|KS|4H|QD|7H|KD|5C|7S|8H|9S|2H|JD|6H|AS|3H|9H|4C|8S|3S|QC]
Hit enter for next battle, q to quit, or a number of battles: 10000
{Yet more games ensue; removed for brevity}
*** Playing battle 2690
Player A has 3D, player B has AC
Player B wins
*** Playing battle 2691
Player A has KH, player B has 3S
Player A wins
*** Playing battle 2692
Player A has 8D, player B has 8C
WAR!
Player B doesn't have enough cards for war, A wins
Over 2692 battles and 67 wars:
Player A is the winner!
$