Vous êtes sur la page 1sur 3

CS 103 Lab 8 - Stay Classy CS103

1 Introduction
In this lab you will use several C++ classes to play the game of War. This will be an
interesting experiment, as you will be able to compare your PA1 to this labs
implementation.

2 What you will learn


After completing this lab you should be able:
Complete class implementations (i.e. member functions)
Understand how to declare instances of classes and call their member
functions
See the difference between your implementation of PA1 without classes,
and this implementation with classes

3 Background Information and Notes


Review your lecture slides on C++ classes.
Classes: To model the game of war we have defined three classes: Card, Deck, and
WarPlayer.
The Card class models a single card with a suit and a value. To make
comparison easy well model the value of a 2 card = 0, the value of a 3
card = 1, , the value of a K = 11 and the value of an Ace = 12.
You will need to complete the toString() function of this class in card.cpp.
Otherwise all the code is complete.
The Deck class models the collection of 52 cards and provides operations to
shuffle, cut, get the top card, and print the cards that remain in the deck.
This class is complete and doesnt need to be modified by you.
The WarPlayer class models a single player of the War game. It contains an
array of Cards (up to 52) which represent the cards the player currently
holds in their pile and provides methods for adding a card to the bottom and
removing and returning a card from the top of their pile.
You will be asked to add a member function or two to this class, but you
do not need to modify the member functions already provided.

4 Procedure
4.1 [10 pts.] Finishing the Implementation of War
Download the code files.

Last Revised: 3/7/2014

CS 103 Lab 10 - Stay Classy CS103


$ wget http://ee.usc.edu/~redekopp/cs103/war.tar
$ tar xvf war.tar

This will extract the following files:


card.h and card.cpp
Class definition and implementation for a Card class
deck.h and deck.cpp
Class definition and implementation for a Deck or
cards
warplayer.h and
Class definition and implementation for a Player in
warplayer.cpp
the game of War
war.cpp
The main application where you will instantiate
Decks and Players and play the game.
Makefile
Makefile to compile the program and create ./war
Now make the following alterations:
1. Complete Card::toString() function by using a stringstream to produce the
string that the function returns. The format of the string should be the card
value (i.e. 2,3,10,J,K,Q,A) followed by a - then the suit {C=Clubs, D =
Diamonds, H = Hearts, S = Spades}. As an example, the 10 of Clubs should
be represented with a string 10-C, the Ace of hearts should be A-H. Be
sure to #include both <sstream> and <iostream>.
2. Add two member functions to the WarPlayer class.
a. One function should check if the player DOES HAVE any cards
remaining by examining the _size variable and return true (a
Boolean) if _size is not equal to 0, false otherwise. Use this function
in war.cpp to check if one of the players is out of cards.
b. The second function should just return the value of _size which is a
private data member. You can use this for debug purposes if desired.
c. Add prototypes for the functions in the warplayer.h class definition
and then add the implementations in warplayer.cpp
3. Modify war.cpp to model the gameplay
a. We have given some initial code to get you going. Notice the loop
that will play the game numSims times, where numSims is the value
of the 1st command line argument you provide.
b. We already create and shuffle the deck.
c. Youll need to write a loop to model a single game that continues
until one player does NOT have cards left. Follow the requirements
from the previous section.
d. Be sure to update the statistics: numBattles, numWars, p1Wins,
p2Wins.
e. Our code will print out the statistics at the end.
4. Compile your program and fix any compile errors
$ make

Last Revised: 3/7/2014

CS 103 Lab 10 - Stay Classy CS103


5. Test your program with just 1 simulation and the debug flag turned on to
see if things make sense (add some debug print statements to see variables
values or other info that would help you verify the correct operation).
$ ./war 1 1

6. Then test your program for a large number of simulations with the debug
flag turned off. Feel free to post your statistics (always list the number of
simulations you are using) on Piazza to verify your code. (Remember though
this is a random simulation so results may vary but should converge for a
large number of simulations).
$ ./war 10000 0

7. Show your TA/CP the functions you wrote and added. Demonstrate your
program running for just 1 simulation (i.e. 1 game) with the debug flag
turned on so we can see the deck and carts printed out (to make sure
Card::toString() works. Then demonstrate your program running for
10,000 simulations (with the debug flag turned off)

Last Revised: 3/7/2014

Vous aimerez peut-être aussi