General Project Description
You have just recently started an internship with GamesCo - a local interactive
game company and your supervisor wants you to create an electronic version of
the old BattleShip game. You will be responsible for creating the interface
that users will see, all of the game logic, a networking component that will
allow users to play each other head-to-head, and an artificial-intelligence
component that will allow users to play the computer.
Each phase of this project will add on a different component so that by the
end of the five phases, you will have a completely operational BattleShips
game.
Project Description
For the first phase of the project, your boss wants you to focus on the
representation of the board and interactivity with the human player. Also,
for simplicity, he requires that you include a component that reads gameboards
from a file.
The following components of functionality are required, in the following order:
- Print an introduction to the user describing what the current
application does
- Prompt for a filename from which to read a board configuration
- Read in the board from that file and store it
- Loop
- Print the board [as read from the file]
- Print a "tracking" board that does not have the ships on it, but
will keep track of hits and misses for the user
- Ask the user which square they would like to attack
- Report whether the attack was a hit or a miss
- Mark the result on both boards
- Repeat the above until all ships are sunk
- Report that the user has won [even though no game was played]
Board File
- All boards (at this stage) are 10x10 boards with a single character
representing each position on the board.
- Each row of a board is a set of 10 characters with spaces between them
for asthetic reasons (makes the board look more "square").
- Empty positions are denoted by the character 'o' [lowercase Oh]
- Ship positions are denoted by the character 'S' [uppercase S]
The following guarantees can be assumed about the board files we will
use to test your application:
- If a board file exists, it is guaranteed to be correctly formatted.
- Board files include ONLY 'o', 'S', and ' ' [space] characters.
- Board files MAY contain extra lines AFTER the board itself, but not before
or within. These lines will contain only whitespace characters.
- There will always be 5 non-overlapping ships (although they may not look
distinct on the board).
- These 5 ships will always have the following sizes: 2, 3, 3, 4, and 5
- There may be extra spaces at the end of any row, but only a single space
will be between each column.
- Rows and columns in the file will contain neither labels nor headers.
User Interaction
Users are expected to supply the following input when requested (but values
and types are NOT guaranteed)
- Filenames consist of a single sequence of non-whitespace characters
- Rows are lettered 'A'-'J' [10 characters].
- Columns are numbered 0-9 [10 numbers].
The following are requirements for the user-interaction and will be tested:
- If the user enters a non-existent file, your program must reprompt for
an additional filename until an existing filename is supplied.
- If the user enters a non-existent square (for the board), your program
must reprompt for an additional square until a valid square is supplied.
- If the user enters either an invalid row OR column, reprompt for a new
square (NOT just the row or the column - reprompt for BOTH row and column)
- If the user enters a valid square they have already guessed, DO NOT
reprompt, the user has simply wasted their turn. Updated!
- If the user enters characters after the row and column, an error stating
that the extra characters were ignored must be printed to the user and then
the square must be validated (and additional error messages produced as
described above). Updated!
The following are guarantees for the user-interaction:
Updated [Review all below]!
- The row and column will be on a single input line.
- The row and column will be represented by single characters (one each).
- There may be one or more spaces between the row and column.
- The row and column may or may not be followed by additional characters.
[extra characters provided as input must be ignored with an error message]
- We will be using text files and scripts to test your application - so
focus on keys that exist in text files, you can safely not test for things
like CTRL-character and F-keys. You can safely assume that only "printable"
characters (alphabetic, digit, and punctuation) will be provided as input.
Design and Implementation Requirements
Functions
This project is designed to give you ample opportunity to explore, experience,
and use functions with a variety of different styles of parameters. You MUST
use functions in this project whenever possible. If your main function is
longer than 25-50 lines or so - it is probably too long - you need to break
it apart. However, if your functions are all less than 5 lines, or several
look very similar, you may need to rethink/combine them.
The proper use and implementation of functions is an important
part of this project (and your grade for this project). In particular,
- Your program must make adequate use of functions in keeping with
top-down design principles.
- The names of the functions are left to you, but poor naming
will lead to losing points, use descriptive names.
- Parameters must be passed to functions using the appropriate
technique (by value or by reference).
- The proper use of const and default values with
parameters are also necessary.
- Values must be returned from functions appropriately.
- All non-class functions called from main() implemented in
Proj1Aux.cpp.
- These functions must be prototyped in Proj1Aux.h.
- All constants must be defined in Proj1Aux.h.
- Function header comments must list the pre- and post-conditions.
- Functions must handle each pre-condition that is not met.
Output
You project is required to produce reasonable output that is nicely formatted
and readable for the user. In addition, your project is required to print the
board to the user in the following format:
0 1 2 3 4 5 6 7 8 9
A o o H o o o o o o o
B o o H o o o o o o o
C o o o o o o o o o S
D o o o o o o H o o S
E o o o o o M H M o S
F o o o o o o S o o S
G S S S o o o S o o o
H S o o o o o S o o o
I S o o o o o o o o o
J H o o o o o o o o o
The following must be true about the board-display from your project,
variations are not acceptable:
- Columns must be vertically aligned with a single space between each.
- Columns (with the exception of the first) must have a "header" that is
the digit that corresponds to that column.
- Columns must be numbers 0 to 9.
- Rows must be horizontally aligned with NO lines between them.
- Rows must begin with the letter that corresponds to that row with a single
space following it.
- Rows must be lettered 'A' to 'J'.
- A ship on the board is denoted by 'S' [uppercase S]
- A hit on the board is denoted by a 'H' [uppercase H]
- A miss on the board is denoted by a 'M' [uppercase M]
Example Input and Output
Example files have been provided in Ms. Wortman's public directory for this
project (location listed below).
Example Program Execution
The following is an example of a single execution of a project. The middle
has been extracted for simplicity (and length). Your output should be similar
but need not be identical.
linux1[96]% Proj1
Welcome to Battleship Tester!
This application tests the Human component of
the game and ensures that boards can be read in
from a file.
What file would you like to read the board from?
boards/b1.txt
0 1 2 3 4 5 6 7 8 9
A o o S o o o o o o o
B o o S o o o o o o o
C o o o o o o o o o S
D o o o o o o S o o S
E o o o o o o S o o S
F o o o o o o S o o S
G S S S o o o S o o o
H S o o o o o S o o o
I S o o o o o o o o o
J S o o o o o o o o o
0 1 2 3 4 5 6 7 8 9
A o o o o o o o o o o
B o o o o o o o o o o
C o o o o o o o o o o
D o o o o o o o o o o
E o o o o o o o o o o
F o o o o o o o o o o
G o o o o o o o o o o
H o o o o o o o o o o
I o o o o o o o o o o
J o o o o o o o o o o
Where would you like to attack? (row column) (ex: A 1)
K 0
Sorry, you have chosen an illegal move.
Please try again. (row column)
A B
Sorry, you have chosen an illegal move.
Please try again. (row column)
A 0
You have missed!
0 1 2 3 4 5 6 7 8 9
A M o S o o o o o o o
B o o S o o o o o o o
C o o o o o o o o o S
D o o o o o o S o o S
E o o o o o o S o o S
F o o o o o o S o o S
G S S S o o o S o o o
H S o o o o o S o o o
I S o o o o o o o o o
J S o o o o o o o o o
0 1 2 3 4 5 6 7 8 9
A M o o o o o o o o o
B o o o o o o o o o o
C o o o o o o o o o o
D o o o o o o o o o o
E o o o o o o o o o o
F o o o o o o o o o o
G o o o o o o o o o o
H o o o o o o o o o o
I o o o o o o o o o o
J o o o o o o o o o o
Where would you like to attack? (row column) (ex: A 1)
A 1
You have missed!
0 1 2 3 4 5 6 7 8 9
A M M S o o o o o o o
B o o S o o o o o o o
C o o o o o o o o o S
D o o o o o o S o o S
E o o o o o o S o o S
F o o o o o o S o o S
G S S S o o o S o o o
H S o o o o o S o o o
I S o o o o o o o o o
J S o o o o o o o o o
0 1 2 3 4 5 6 7 8 9
A M M o o o o o o o o
B o o o o o o o o o o
C o o o o o o o o o o
D o o o o o o o o o o
E o o o o o o o o o o
F o o o o o o o o o o
G o o o o o o o o o o
H o o o o o o o o o o
I o o o o o o o o o o
J o o o o o o o o o o
Where would you like to attack? (row column) (ex: A 1)
A 2
You have hit a ship!
0 1 2 3 4 5 6 7 8 9
A M M H o o o o o o o
B o o S o o o o o o o
C o o o o o o o o o S
D o o o o o o S o o S
E o o o o o o S o o S
F o o o o o o S o o S
G S S S o o o S o o o
H S o o o o o S o o o
I S o o o o o o o o o
J S o o o o o o o o o
0 1 2 3 4 5 6 7 8 9
A M M H o o o o o o o
B o o o o o o o o o o
C o o o o o o o o o o
D o o o o o o o o o o
E o o o o o o o o o o
F o o o o o o o o o o
G o o o o o o o o o o
H o o o o o o o o o o
I o o o o o o o o o o
J o o o o o o o o o o
[...]
[cut for length]
[...]
Where would you like to attack? (row column) (ex: A 1)
I 0
You have hit a ship!
0 1 2 3 4 5 6 7 8 9
A M M H o o o o o o o
B o o H o o o o o o o
C o o o o o o o o o H
D o o o o o o H o o H
E o o o o o o H o o H
F o o o o o o H o o H
G H H H o o o H o o o
H H o o o o o H o o o
I H o o o o o o o o o
J S o o o o o o o o o
0 1 2 3 4 5 6 7 8 9
A M M H o o o o o o o
B o o H o o o o o o o
C o o o o o o o o o H
D o o o o o o H o o H
E o o o o o o H o o H
F o o o o o o H o o H
G H H H o o o H o o o
H H o o o o o H o o o
I H o o o o o o o o o
J o o o o o o o o o o
Where would you like to attack? (row column) (ex: A 1)
J 0
You have hit a ship!
Congratulations, you have won the game!
Example Board Files
The following are three example boards to test your program, but I would
recommend creating your own boards by hand that will test the boundaries
of the board (i.e. first and last rows and columns, four corners, etc.)
o o S o o o o o o o
o o S o o o o o o o
o o o o o o o o o S
o o o o o o S o o S
o o o o o o S o o S
o o o o o o S o o S
S S S o o o S o o o
S o o o o o S o o o
S o o o o o o o o o
S o o o o o o o o o
o o o S S S S o o o
o o o o o o S o o o
o o S S S S S o o S
o o o o o o o o o S
o o o o o o o o o S
o o o o o o o o o o
o o o o o o o o o o
o o o S S S S o o o
o o o o o o o o o o
o o o o o o o o o o
o o o o o o o o o o
o o o o o o o o o o
S S S S S S S o o o
o o o o o o o S S S
o o o o o o o o o o
o o o o o S S S S o
o o o o o S S S o o
o o o o o o o o o o
o o o o o o o o o o
o o o o o o o o o o
Data Structures
The following are requirements of the data structures that you must use
in your project
- Your program must use vectors instead of arrays.
- Your program must use cin/cout/getline instead of the C-style input and
output.
- Your program must use C++ strings instead of C-style strings (if you use
strings at all).
- Your program must use const variables rather than "magic numbers"
or "magic strings". Do not use #defines, use const variables instead. For
this project, examples of magic strings would be the different representations
for hit ('H') or miss ('S'), the size of the board (10x10), etc.
if (currentSquare == 'S')
is unacceptable, use:
if (currentSquare == SHIP)
where SHIP is defined elsewhere.
- Although not required, you may find the use of one or more
structs helpful in organizing your data. You may use classes if you already
understand them, but their use is not required or recommended as you may be
required to rewrite them completely for Project 2. As you design your structs
or classes, think about collecting data and functions that are all related
to a physical object. For example, the board could be stored in a struct
or class, but what functionality should the board support?
- Your program must provide adequate error checking of user input.
- Your program will be tested with a variety of good and BAD inputs.
- Get your project working with good input first. Then go back and put
in the error checking and error handling.
Tips for Success
Here are some tips for having a simpler development process:
- Write your code in small portions: write 5 lines, save, compile, test.
- Write one function at a time, save, compile, test.
- Write code that will thoroughly test each function as you write it.
- Break down the project into smaller pieces (modules), write one module
at a time or focus on one component of functionality at a time.
- NEVER write the entire project and then hope that it works, it won't.
- Start with the board, get each of the following working one at a time,
thoroughly test each before moving on to the next:
- Use vectors to create the board representation
- Print the board (a totally empty board)
- Read the board in from a hard-coded file
- Ask the user for a filename, read in the board
- Add the ability to set a square in the board to either hit or miss
- Ask the user for a single square to attack, mark that square
- Ask the user for multiple squares to attack, mark those squares
- Add the ability to detect if all ships have been sunk
You do not need to follow these steps to the letter, they are simply a
recommended pattern for developing your project. Don't be afraid to write
code that you will eventually delete!!
General Tips
- TEST your program thoroughly - the makefile provided has a 'make test'
target that you can use to ensure that your application will work with our
test suite. HOWEVER, you need to test your application using other files that
you create yourself. Be sure to examine the guarantees closely and be sure
that your project handles all of the "bad" cases.
- Be sure to name your files as noted above, otherwise, you will have to
modify the makefile we provide.
- Because your program will be tested using Unix redirection, DO NOT prompt
for any input not specified in the project description.
- Use incremental development, develop one function at a time, write code
that will thoroughly test it, run and test it, then move on to another function.
Don't be afraid to write testing code that you will eventually get rid of.
- This project is approximately 150 - 250 lines of code... don't
procrastinate.
- Ms Wortman's public directory for this project is
/afs/umbc.edu/users/d/a/dana3/pub/CMSC202/p1.
- Do not cheat, you will be caught. Do not look at another student's code -
it is very tempting to "borrow" an algorithm. Do not show your code to
another student. Use the Tutors, TA's, and Instructors for help.
- Make sure you have completed Project 0.
- Check the discussion board before emailing a TA or Instructor - your
question has probably already been answered.
Project Design Assignment
Your project design document for project 1 must be named p1design.txt.
Be sure to read the
Project Makefile
The "make" utility is used to help control projects with large numbers of files.
It consists of targets, rules, and dependencies. You have already learned about
make files in lab. For this project, the makefile will be provided for
you. You will be responsible for providing makefiles for all future projects.
Copy the file makefile from Ms. Wortman's public directory to your
directory.
When you want to compile and link your program, simply type
the command make or make Proj1
at the Linux prompt.
This will compile all necessary .cpp files and create the
executable named Proj1.
The make utility can also be used for compiling a single file without
linking. For example, to compile Proj1.cpp, type make Proj1.o.
In addition to compiling and linking your files, make can be used
for maintaining your directory. Typing make clean will remove any
extraneous files in your directory, such as .o files and core files.
Typing make cleanest will remove all .o files, core files, Proj1
executable and backup files created by the editor. More information about
these commands can be found at the bottom of the makefile.
Grading
The grade for this project will be broken down as follows. A more detailed
breakdown will be provided in the grade form you receive
with your project grade.
85% - Correctness
This list may not be comprehensive, but everything on this list will be
verified by the graders.
- Your project produces all required output.
- The output produced is correct.
- The output is in an acceptable format.
- Your program follows good top-down design principles.
- All function parameters are passed using the appropriate method.
- const variables are used wherever appropriate.
- All unmet function pre-conditions are handled.
- Use of efficient method for formatting hyphenated numbers.
- All project requirements are met.
15% - Coding Standards
Your code adheres to the