ESc 014
Assignment 2 -- X's and O's
Due: Tuesday, April 16, 2002
Objectives
- Practice with two-dimensional arrays and nested loops
Your Mission
Your task is to write a program that will play 2-player tic-tac-toe.
Two users will take turns placing an X or an O on a 3 X 3 grid. The
first player to create three Xs or three Os in a row either
horizontally,
vertically, or diagonally wins. A tie results if all spaces are filled
without a winner.
The Algorithm
The basic procedure for playing a game of tic-tac-toe is as follows:
- Set up the game board by initializing each "slot" of the 3 X 3 grid
to a blank space.
- Repeat the following until there is either a winner or a tie
- Make it be the correct player's turn
(X or O. X will go first).
- Display the current state of the game board.
- Ask player for the row and column where they would
like to place an X (or O). If the player specifies a place
that is invalid
(it's off the grid or there is already a piece there),
then an error message should be displayed and the
player should be asked to re-enter his/her move. Play
should not continue until a valid move is entered.
- Place the appropriate symbol into the game board at
the specified place.
- Check to see if the player's move resulted in a winner
or a tie game.
- Once the game is completed, display the final state of
the game board and announce the winner (or say that it
ended in a tie).
Sample Output
Here is what your output should look like for the
first couple of moves of the game. It need not
be exactly like this, but it gives you an idea of
what needs to be included.
| | | |
_______
| | | |
_______
| | | |
_______
it is X's turn.
What row (1-3)? 1
What column (1-3)? 2
| |X| |
_______
| | | |
_______
| | | |
_______
it is O's turn.
What row (1-3)? 3
What column (1-3)? 3
| |X| |
_______
| | | |
_______
| | |O|
_______
it is X's turn.
What row (1-3)?
The Details
You are required to use a two-dimensional array for the game
board. You are also not allowed to "ignore" index 0. Use it!
You should also use nested loops whenever possible
for traversing the slots of the game board. This will
keep you from having to type the same code over and over. In order
to figure out whether you should be using a loop or not in a
certain place in your program, ask yourself the following: if
you wanted to change your program to play tic-tac-toe on a 4 X 4
grid (4-in-a-row to win), or a 10 X 10 grid (10-in-a-row to win),
how much work would be required to change your program? Loops
should allow you to make such changes very easily. You
will lose a significant number of points for inefficient
code that does not use loops effectively.
Remember to use functions to break up this problem into
smaller pieces. Here are some of the functions you may
consider using:
- a function to handle playing the game as a whole as described in
the
Algorithm section above. This function will in turn call other
functions
to handle other subtasks.
- a function for initializing the game board with empty spaces.
- a function to print the game board
- a function to check to see if there is a winner. There's a winner
if three Xs or three Os appear in either (1) one of the three rows,
(2) one of the three columns, or (3) one of the two diagonals.
- a function to check to see if there is a tie game. There's
a tie if all spaces are filled and there is no winner.
You may use other functions as well.
Extra Credit alert: I've given you the minimum requirements
for this program, but there is a lot of room for enhancement.
Here are some ideas that will net you extra credit:
- Get the two player's names and print them where appropriate
during the course of the game.
- Let the players decide who is X, who is O, and who goes first.
- Let the user decide how many games s/he wants to play, and
keep score of how many games each player has won.
Grading
This assignment is worth 50 points, divided as follows:
- 20 pts if your program plays the game correctly.
- 5 pts for appropriate determination of the winner or tie.
- 5 pts for appropriate error checking of the user's input.
- 5 pts for good testing of your code (don't forget to paste your
test
case results into the source code!)
- 5 pts for good documentation.
- 5 pts for good C++ language usage including nested loops.
- 5 pts for other design issues (good use of functions, no
convoluted
logic, etc.)
Remember to turn in both a paper and an electronic copy of your
project on BlackBoard. Name
your file with your FULL NAME along with the assignment number (e.g.
chrisfernandes_hw2.cpp)
Having trouble? Don't wait until the last minute! Come see me and get
your
$80 worth.
Administrative statement
Programming assignments are
individual projects. I encourage you to talk to others
about the
general nature of the project and ideas about how to pursue it. However,
the
technical work, the writing, and the inspiration behind these must be
substantially your own. If any person besides you contributes in any way
to the
project, you must credit their work on your homework. Similarly, if you
include
information that you have gleaned from other published sources, you must
cite
them as references. Looking at, and/or copying, other people's programs
or
written work is inappropriate, and will be considered cheating.
Return
to
Assignment Index