Starting from:

$30

Homework 3: Exes and Ohs

Homework 3:
Exes and Ohs

Overview
In this homework you will implement a simple Tic-Tac-Toe game using multidimensional arrays and functions
in C++. The general ?ow of your program will go as such:
1. Use a two-dimensional 3x3 array to represent the game board. Identify the smallest integral type in
C++ that you can use to hold the game's state, and make your board a 3x3 array of that type. (Hint:
there are ?ve fundamental integer types, one of which is a single byte in size.) The board will hold
one of three distinct values at each position: 0 if the space is empty; 1 if the space is taken by the
?X? player (player 1); -1 if the space is taken by the ?O? player (player 2). Since the board is initially
empty, it should be initialized to contain only 0 values. Note that 0 is di?erent than '0'.
A two-dimensional array can be declared as an array of arrays, as in std::array<std::array<int,
3, 3 board = {0};. You must use the std::array type in this homework, not the ?C-style? array
syntax, e.g., int board[3][3];.
2. Keep track of which player's turn it is (X goes ?rst). Output the current state of the board (see below)
and then ask the current player to choose a move. Moves need to be inputted in the form r,c where
r is a 0-based index for the row and c is a 0-based index for the column. In this format, 0,0 is the
upper left corner of the board and 1,1 is the ?middle? space.
Hint: you will need to use cin to scan these values from the user, but the comma gets in the way.
Use a ?dummy? variable appropriate for storing a comma, and cin that variable to remove the comma
from the keyboard bu?er.
In this step, you will use a function called GetMove, which takes two integer references and ?lls them
in with the inputs from the user. The main function will pass local variables to the function GetMove,
and when GetMove is done, main's variables should be updated with the input.
3. Check the square the user chose to make sure it is in bounds and currently empty (what value will be
in the array if it's empty?). If the square is occupied, loop and ask the user to choose a valid move
again. Warning: what will happen if you access the matrix at a square that is not in-bounds?
4. Once a valid square is chosen, update the game board by writing a 1 or -1 to the chosen space according
to whose turn it is.
Hint: you can do this without an if statement if you make an intelligent choice for how to keep track
of whose turn it is.
5. Change your variable for whose turn it is, then loop back to step 2. (Output the board; ask for a move;
verify the move; update the game.) Complete a total of nine game loops, one for each square in the
board.
Hint: if you want to quit your running program in the middle of a loop, press Control+C.
6. You must check for a winner in any of the 8 directions and stop the loop when a winner is detected.
Write the function CheckWinner and use it in your loop to stop the game when appropriate.
7. That's it!
Starting O?
1. Download the ?le TicTacToe.h from the course website. This header contains declarations for functions
you will need to write and use in your implementation, along with comments about how they should
1
work. You must follow the design laid out in this .h ?le.
2. Create a project (as in Lab 1) and move the .h ?le to the project's folder, then add it to the Header
?les of the project. Add a new ?le TicTacToe.cpp to the Source ?les of the project. In this ?le,
you will implement the methods declared in TicTacToe.h. Finally, add a Source ?le main.cpp and
implement the main function.
Output Formatting
User input is in italics.
When outputting the game board, make it look like this:
- 0 1 2
0 . . .
1 . X .
2 O . .
X's turn: 1,1
That space is already taken!
X's turn: 2, 0
That space is already taken!
X's turn: 1,0
- 0 1 2
0 . . .
1 X X .
2 O . .
O's turn:
Put the 0-based indices of each row/column around the perimeter. Use periods to show an ?empty? square.
Put a capital ?X? or ?O? depending on who is in each space.
Deliverables
Turn in the following when the homework is due:
1. A printed copy of your code, printed from Visual Studio or your IDE when possible. If you
cannot print from your editor, copy your code into Notepad or another program with a ?xed-width
(monospace) font and print from there.
2. Because the output of this program is lengthy and verbose, you do not need to print a copy of a full
run of the program. Simply print the output of the program after 2 moves: one for X and one for O.
2

More products