CIS 1057 - Homework 11

Handed out: 11/09/10
Due: by 10am on 11/15/10

The assignment consists of implementing the game Tic-Tac-Toe.

This program will allow two people (or a person and the computer) to play the game of Tic-Tac-Toe. Tic-Tac-Toe is a board game with a 3 X 3 board of squares, all of which are initially empty. There are two players, known as `X' and `O', who take turns making moves. In this version of the game, the `X' player will always move first. On a player's turn, the player must place exactly one piece on an empty square of the board. The `X' player always places pieces labeled `X', and the `O' player always places pieces labeled `O'. The game ends when either player fills three consecutive positions of the board, in a straight line that is either vertical, horizontal, or diagonal. The player to first accomplish this wins. If the whole board fills with neither player able to get three of his or her pieces in a row, then the game is a draw.

Your program should begin by asking if you want the 'O' player to be the computer or the user and then act accordingly. Your program should then draw an empty Tic-Tac-Toe board on the screen using Text graphics (an example is shown below). It should then prompt the `X' player to enter a move, and redraw the board with the newly placed piece in the correct position. After every move, the program should test to see if the player won or the board is filled up, in which case the game is over and the program can print the winner (or that it is a draw).
You may assume that when the user is asked to enter a row and a column to place a piece, then the user always enters two integers. However, you should not assume that the user enters integers that correspond to valid board positions or the player may pick a spot that corresponds to a spot on the edges of the board, or a spot that is already occupied. In this case, the program should keep prompting the user for a new spot until the s/he enters a valid position. Write your program in a file called TicTacToe.c, and email it to your TA. Please comment liberally, to help in grading the assignment.

Below is an example with a few moves assuming that the 'O' player is the user.

 1  2 3
1  | |
 -------
2  | |
 -------
3  | |

It is the X player's turn.
Please enter a row and column: 2 3

 1  2 3
1  | |
 -------
2  | |X
 -------
3  | |

It is the O player's turn.
Please enter a row and column: 1 3

 1  2 3
1  | |O
 -------
2  | |X
 -------
3  | |
You will receive full credit whether or not your program acts as the 'O' player.