IC210 Lab 10
A game of
tic-tac-toe using a user-defined type (Struct) for
the gameboard.
Pre-lab homework. First,
read over the given program, focusing especially on how main() uses
functions to get its job done, and how the struct is
used. Then answer the following
questions (type your answers and turn in hardcopy)
1.
What type of C++ object is used to store the game
board? Be as specific as possible
2.
Why does createBoard() use pass
by reference?
3.
Would the program still work if is_A_Draw() used
pass by reference instead of pass by value?
4.
Describe what the following statement (from main() ) does:
game.board[row][column] = turn;
5.
What are all the possible values that the
variable “winner” in main() possibly takes on during the course of the game?
Reminder: All
pre-lab homework is due at the beginning of the lab period, and late pre-lab
homework will earn a grade of 0.
1.
Compile and run the provided tic-tac-toe
game program that uses a user-defined type (struct TicTacToe)
to facilitate two human players playing a single round of the tic-tac-toe
game. Play a few rounds to get a feel
for how the game behaves (it already works just fine as long as the input
format and values are valid). Here’s an
example run (user input in red):
|1|2|3|
a| | | |
b| | | |
c| | | |
Player X's
move (input format: a1): a1
|1|2|3|
a|X| | |
b| | | |
c| | | |
Player O's
move (input format: a1): b1
|1|2|3|
a|X| | |
b|O| | |
c| | | |
Player X's move
(input format: a1): b2
|1|2|3|
a|X| | |
b|O|X| |
c| | | |
Player O's
move (input format: a1): c1
|1|2|3|
a|X| | |
b|O|X| |
c|O| | |
Player X's
move (input format: a1): c3
|1|2|3|
a|X| | |
b|O|X| |
c|O| |X|
***
Congratulations, the winner is X.
Shutting down,
have a fine Navy day and Beat Army!
Press any key
to continue
1.
Modify the program so that instead of quitting once
the game is over (after all, who ever plays just one game of tic-tac-toe!), the
users are prompted to see if they would like to continue playing another
game. When prompted, the users should
enter y if they want to pay again, or q to quit.
Write a function that is passed the TicTacToe
instance and resets the board if the user wants to play again.
2.
Modify the TicTacToe struct
so that it contains data members that keep track of the cumulative number of
wins for player X and player Y respectively, the number of draws, and whose
turn it is. These values should be printed
out each time a game is won.
3.
Modify the program so that when a player is prompted
for their input, they can either enter a cell location as described in 1 above
(and thereby continue playing the game) or they can enter sf to indicate that they would like to save the
game to the file tictactoe.txt and quit playing for the time being (so they can
go PT, eat chow, study for finals, ask some plebes their rates, etc). If the current player enters sf, your program should call
a function that stores the current board cell values, whose turn it is, the number of cumulative wins for X and O and the number
of draws in the output file tictactoe.txt. Note that you may want to make
things easier on yourself and write a character such as B instead of a blank
space for an as yet unused cell to the output file to facilitate reloading the
game in part 5. Remember to close the
file after you are done writing to it.
4. Modify
the game so that on initial startup the players are first prompted to determine
if they would like to play a (n)ew game (in which a blank game board and zero wins for both
X and O are initialized) or (c)ontinue if they would like to pick up where they left off
in a previous game. You can assume that
if the users enter c (for continue previous
game) the file tictactoe.txt is non-empty, and does actually contain the data
with which to pick up a previously running game. In this case, the game board
cell values, the number of wins, draws, and whose turn it is are all loaded
from the file tictactoe.txt and play is resumed by your program.
Going Beyond
1.
Modify your program so that you can play against the
computer. Start by making it pick moves
randomly. Then try to add a little bit
of intelligence.