In this lab exercise you will add functionality to the
OOP Implementation of Nim in C++
discussed in a web presentation. (See the
menu at left.)
Download this zipped folder of code files to your machine:
files.zip.
These code files need to be moved to your disk space on the Computer
Science server named
janus. (See
Using C++ at UMD on the
menu to the left for how to transfer, compile, and run programs.)
Run the program:
- The program will run with the message "All tests
succeeded".
- All calls to test methods in the main
function have been commented out.
- You can uncomment any test method call except
"testScoreKeeper()" to observe the behavior of any class
except ScoreKeeperInfo.
- You will implement the ScoreKeeperInfo class.
- The type ScoreKeeper is defined to
be ScoreKeeperInfo*.
- In NetBeans, click File and select New
Project....
-
In the New Project dialog,
- Select category C/C++
- Select project C/C++ Application.
- Click Next.
- In the New C/C++ Application dialog,
- Name the project "CS 1581 Nim C++"
- Uncheck the Create Main File box.
- Click Finish.
- Right click Header Files under the newly created project
and select Add Existing Item...
- In the Select Item dialog,
- browse to the files
folder and select all ten files with the ".H"
extension.
- Click Select to add them to the project.
- Right click Source Files under the project
and select Add Existing Item...
- In the Select Item dialog,
- browse to the files
folder and select all ten files with the ".cpp"
extension.
- Click Select to add them to the project.
Right click the
CS 1581 Nim C++ project node and choose
Clean
and Build.
You should see a number of messages and compiler invocations ending
with "
BUILD SUCCESSFUL".
Right click the CS 1581 Nim C++ project node and choose Run.
If you uncomment the "testGame()" call (see main
function at right) and run the program
you will observe one game set up and played to completion, followed by
a program exit.
Your job is to implement a ScoreKeeperInfo object that:
- Introduces the game and prompts for the user's name
- Flips a coin to determine if the user or computer
goes first in the first game
- Randomly generates a starting game state having
unique pile sizes chosen from [6..12]
- Plays a game to completion and
displays the current score (number of wins for each player)
- Asks the user if he/she wants to play another game.
- Alternates who goes first in succeeding games.
The script below shows one possible result of running
the testScoreKeeper() method.
For full credit your lab must exhibit the same functionality.
User input is shown in red.
Welcome to the Game of Nim.
What is your name? Ralph
I will flip a coin to start.
heads or tails? 1
heads or tails? tailsq
heads or tails? tails
It is heads
HAL 9000 will go first.
Pile 1: 11
Pile 2: 8
Pile 3: 10
HAL 9000 takes 1 coin(s) from pile 1
Pile 1: 10
Pile 2: 8
Pile 3: 10
Which pile will you remove from?
2
How many coins do you want to remove?
8
Ralph takes 8 coin(s) from pile 2
Pile 1: 10
Pile 2: 0
Pile 3: 10
HAL 9000 takes 1 coin(s) from pile 1
Pile 1: 9
Pile 2: 0
Pile 3: 10
Which pile will you remove from?
3
How many coins do you want to remove?
10
Ralph takes 10 coin(s) from pile 3
Pile 1: 9
Pile 2: 0
Pile 3: 0
HAL 9000 takes 9 coin(s) from pile 1
Pile 1: 0
Pile 2: 0
Pile 3: 0
HAL 9000 wins. Score:
Ralph: 0
HAL 9000: 1
Another game (yes or no)? sure
Another game (yes or no)? you bet
Another game (yes or no)? yes
Ralph will go first.
Pile 1: 10
Pile 2: 11
Pile 3: 9
Which pile will you remove from?
3
How many coins do you want to remove?
1
Ralph takes 1 coin(s) from pile 3
Pile 1: 10
Pile 2: 11
Pile 3: 8
HAL 9000 takes 1 coin(s) from pile 1
Pile 1: 9
Pile 2: 11
Pile 3: 8
Which pile will you remove from?
2
How many coins do you want to remove?
10
Ralph takes 10 coin(s) from pile 2
Pile 1: 9
Pile 2: 1
Pile 3: 8
HAL 9000 takes 1 coin(s) from pile 1
Pile 1: 8
Pile 2: 1
Pile 3: 8
Which pile will you remove from?
1
How many coins do you want to remove?
8
Ralph takes 8 coin(s) from pile 1
Pile 1: 0
Pile 2: 1
Pile 3: 8
HAL 9000 takes 7 coin(s) from pile 3
Pile 1: 0
Pile 2: 1
Pile 3: 1
Which pile will you remove from?
3
How many coins do you want to remove?
1
Ralph takes 1 coin(s) from pile 3
Pile 1: 0
Pile 2: 1
Pile 3: 0
HAL 9000 takes 1 coin(s) from pile 2
Pile 1: 0
Pile 2: 0
Pile 3: 0
HAL 9000 wins. Score:
Ralph: 0
HAL 9000: 2
Another game (yes or no)? no
All tests succeeded.
The ScoreKeeperInfo class is tested with the following:
The only public method in ScoreKeeperInfo should
be start(). Here is ScoreKeeperInfo.H:
Here is ScoreKeeperInfo.cpp:
ScoreKeeperInfo should have instance variables that
store:
- The Player representing the user
- The Player representing the computer
- Which Player is first to play
- The number of wins for the user
- The number of wins for the computer
You should develop and test ScoreKeeperInfo incrementally.
For example, here is a suggested implementation of the start()
method:
welcome, flipCoin, and playRepeatedly are
private methods that can be developed and tested separately in
order.
- Welcomes user and prompts for name
- Creates and stores Player references for user
and computer
- Initializes number of wins for each to zero
- Generates a random integer
in [0,1] representing heads or tails
- Prompts user for a guess, insisting on a response
in ["heads","tails"]
- Checks guess and sets first to play
- Announces who is first to play
- Creates a random start state — this should be
done by another private method, for example makeRandomState
- Creates a new Game reference and plays a game
- Announces winner, updates and displays score
- Prompts user for another game, insisting on a response
in ["yes","no"]
- If user wants another game, toggle (update) first to
play and repeat
- Use "rand() % 2" to generate a random
0 or 1
- Use "Utils::generateRandomInt(min, max)" to
generate a random integer in [min..max]
- See the testGame() method for examples of
creating and
manipulating Player, AutomatedPlayer, Game,
and GameState object references:
- When finished, re-zip your files folder
as files.zip.
- Submit files.zip to
.