Starting from:

$30

Programming Project: Game


Programming Project: Game
CPSC 298-6 Programming in C++

Introduction
In this programming project, you'll implement a computer game called "Battle Boat" as a
C++ class. A framework for the game has been implemented for you but is incomplete.
You'll need to finish the implementation.
"Battle Boat" is similar to the classic game "Battleship" but much simpler.
Battleship game played by crewmembers of USS George H.W. Bush
The opponent has only one boat, albeit a stealthy, autonomous, robotic boat, that is
hidden in an ocean represented by a two-dimensional grid. The game is a command line
game and all output is rendered in ASCII (American Standard Code for Information
Interchange) characters. For example, the grid is shown below, with the tilde character ~
representing waves. Each grid square is flanked by a vertical bar character to help the
user see the squares.
You control another boat with a magazine of torpedoes and must hunt the Battle Boat.
2
You fire a torpedo at the battle boat by selecting a grid square as your aim point. For
example, in the sequence below the user enters grid location 2, 2, indicating row 2 and
column 2 in the grid as the aim point.
An asterisk, indicating a torpedo explosion is rendered at the aim point. The game reports
the torpedo missed its target, the wily Battle Boat.
If the torpedo comes close to the battle boat, where close means one of the eight grid
squares surrounding the Battle Boat, the game reports the torpedo was "close." For
example, if the Battle Boat is hidden in location 2, 2 and the user fires the torpedo at grid
location 2, 3, the game reports row 2 and column 3 is close to the Battle Boat.
The eight squares surrounding 2, 2 are shown numbered in the figure below.
Your boat has a limited magazine capacity; the number of torpedoes remaining is shown
after each turn.
If the torpedo aim point grid coordinates correspond exactly with the coordinates of the
Battle Boat, the Battle Boat is hit and sunk and you win the game.
3
The game reports the hit and the explosion and announces victory.
If, on the other hand, all torpedoes are expended without hitting the Battle Boat, the game
ends (with a victory for the Battle Boat and a loss for the player).
The hidden location of the Battle Boat, represented as a 'V' character is displayed to the
player.
The locations of all the torpedo explosions, represented by asterisk characters, remain
displayed until the end of the game. This allows the user to remember the grid squares
they've targeted previously as they hunt the Battle Boat.
The game consists of a sequence of turns where each turn involves the player firing a
torpedo and the game reporting the results. The sequence of turns continues until either
the Battle Boat is sunk or until all torpedoes have been expended.
When a game ends, the player is prompted as to whether they wish to play another game.
If they answer yes (y), the game is reset and a new sequence of turns begins.
The section below describes the (partial) implementation of the game program. It
provides very helpful information on certain details that may make the assignment easier
for you. However, reading it is not essential to completing the assignment and you can
skip ahead to the assignment section if you'd prefer.
4
Implementation
Though Battle Boat is a simple game, it's implementation resembles that of large
commercial computer games. For example, Battle Boat uses a main game loop in which
the user undertakes some action (firing a torpedo in this case) and the game assesses the
results of that action - is it a hit, a close miss or a complete miss; has the player run out
of torpedoes?
The Game Class
Battle Boat is implemented as a single C++ class called Game. The class is declared in a
header file, Game.h and implemented in an implementation file, Game.cpp. There is also
a main function in source file main.cpp.
The game grid is maintained in a two-dimensional character array, which is a member
variable of the Game class. The grid is rendered by a displayGrid method which adds
vertical bars around the grid elements to improve clarity.
Class Constants
The class Game includes a number of constants that give the default size of the grid (4 by
5), and the default number of torpedoes (equal to the number of grid squares divided by
2).
Member Variables
The number of rows and columns doesn't deviate from the default in this implementation
of the game and is maintained by member variables m_nGridRows and m_nGridColums,
respectively.
Similarly, the location within the two-dimensional grid of the stealthy Battle Boat is
maintained in member variables m_iRowBoat and m_iColumnBoat. Another variable,
m_nTorpedoes, stores the number of torpedoes remaining.
5
Public Member Functions
The Game class exposes only two public member functions, the constructor, Game and
play.
Private Member Functions
The play member function contains the main game loop, in which all activity occurs. play
calls on several private member functions to carry out all of its actions.
6
The private functions include one that is used only for test purposes, placeBoat. In actual
games, placeBoatRandomly is used. However, for testing, you may want to place the
BattleBoat in a specific location; placeBoat lets you do that.
Conditionally-compiled Code
placeBoat is conditionally compiled when the macro TEST is defined:
7
To define the macro TEST, insert the following line near the top of your C++
implementation file, Game.cpp:
There is also a DIAG macro which may be defined to activate diagnostic output.
For example, member function placeBoatRandomly will display the random grid location
of the Battle Boat if DIAG is defined.
To define the macro DIAG, insert the following line near the top of your C++
implementation file, Game.cpp:
Both lines are presented, but commented out in Game.cpp, so you actually only need to
uncomment these lines to activate them.
std::tuple Template Class
The Game class's implementation makes use of a C++ Standard Template Library (STL)
template class called std::tuple, which is similar to the std::vector class you've already
encountered. The sd::tuple class holds a collection of items, which may be of different
types.
In the Game class, the tuple template class is used to store a set of two integers,
corresponding to the row and column numbers of a grid location.
8
Like std::vector, tuple is a template class and the data types of the two elements it stores
must be specified in angle brackets as shown. In the case shown below, the two values
are initialized in the tuple's constructor to iRow and iColumn, two variables declared and
assigned to earlier.
The contents of the tuple can be extracted using the std::tie function, which ties the
content of the tuple to other variables.
For instance, member function fireTorpedo returns a tuple containing two integers, the
row and column number of the location where a torpedo has been fired. The std::tie
function receives this tuple (via the assignment statement) and "ties" the two integer
values to local variables iRow and iColumn, which have been previously declared (int
iRow; int iColumn;).
The tuple template class makes it easy to pass an ordered pair of values representing grid
coordinates around.
The main Function
The game program's main function declares a single instance of the Game class and later
calls it's public member function play to execute the game loop.
The main function calls the play public member function of the Game object, game in a
while loop which repeats so long as the user wishes to play another game.
The Assignment
The Battle Boat game is implemented in three source files, main.cpp Game.h and
Game.cpp. These files are presented in incomplete versions that you must finish.
Instructions for finishing the game are give in TODO comments spread throughout the
code.
The TODO comments appear similar to the following:
9
You may leave these comments in after you've implemented the functionality (it makes it
easier on the instructor to find your changes).
Diagnostic Mode
Once you've implemented all the changes and have a complete, working game, place the
game in diagnostic mode by defining the DIAG macro. You need only uncomment the
line shown in Game.cpp to define the macro.
Once uncommented, as shown, diagnostic output code will be enabled.
In particular, the following line in placeBoatRandomly will be compiled in
This causes the location of the Battle Boat, normally kept secret, to be displayed at the
beginning of the program.
Building
To compile and link the Battle Boat game under Linux, execute the following g++
command:
g++ -o BattleBoat Game.cpp main.cpp
To execute type ./BattleBoat
10
These commands are illustrated in the screen capture below.
11
Execute Two Games
Using this location given in the diagnostic ouput, execute a game where you have at least
one miss, one "close" and then sink the Battle Boat.
The game output should appear similar to the following:
Capture this output and submit it with your source code.
When prompted to "Play another game?", enter y and execute a game where the player
does not sink the Battle Boat. The player must expend all torpedoes and not hit the Battle
Boat, either with a "miss" or a "close."
12
Beginning of Game
End of Game
Submit screen captures of the beginning and end of the game that results in missing the
Battle Boat.
Appendix A shows a complete game where the Battle Boat is not sunk.
13
Appendix A: Complete Game Output Resulting in
Missing the Battle Boat
The game output for a game resulting a loss is shown in the following screen captures.
14

More products