CIS071
Lab10 - Tic Tac Toe Game DUE: (Wednesday, Nov 15, 10AM)
Tic Tac Toe
About Tic Tac
Toe
The object of Tic Tac Toe is to get three in a row. You play on a 3 x 3
game board. The first player is known as X and the second is O. Players
alternate placing Xs and Os on the game board until either opponent has three
in a row or all nine squares are filled. X always goes first, and in the event
that no one has three in a row after the board is filled, the game is called a tie.
Introduction
You are given program tictactoe_partial.c (check the course web site) that compiles and works, but is not completely functional. This program can:
-
declare two dimensional char array
tictactoe with 3 rows and 3 columns
-
populate its elements with symbol ‘.’ (using fill_board function)
-
display the board (using print_board function)
-
ask players to enter ‘X’ or ‘O’ to the
desired position on the board (using enter_symbol function)
-
decide if there is a winner
(using if_winner function)
There are two major problems with
the given code:
-
function enter_symbol does not prevent overwriting previous moves. For example, if position (1,1) is already taken by player 1 (i.e., its content is ‘X’), player 2
can place symbol ‘O’ on that place.
-
function if_winner is not completed. Currently, it always returns 0, indicating
that there is no winner
Objective
TASK 0: Compile tictactoe_partial.c
and run the program. Try to understand how the program works by using it. Look
carefully at the code and try to understand how and why it works.
(4 points) TASK 1: The given code is
provided without comments. Write comments throughout the code to explain how it
works. It is not needed to put a comment on each line. Comments should explain
major lines and blocks of code. You will be graded on how well you understood
and commented the code.
(4 points) TASK 2: Modify function enter_symbol in program tictactoe_partial.c, so that it
prevents overwriting the previous moves. So, this function should first ask
user to select position of the new entry and then check if this position is
already occupied. If the position is occupied, the function should ask user to select
valid coordinates again. Hint: this could be done using the do-while
loop controlled by a flag that is set to 1 if the entry is valid.
(4 points) TASK 3: Modify function if_winner in program tictactoe_partial.c, so that it
could determine if there is a winner given the current content of the tictactoe array. If there is no winner, the function should return 0, if winner is
player 1, it should return 1, and if the winner is player 2, it should return
2. Hint: you should think about how a winner is determined – there is a
winner if the board has 3 Xs or 3 Os in a horizontal, vertical, or diagonal
row.
Grading:
You could get 12 points in this lab,
where 10 of them are regular points, and 2 are extra points.
Deliverables:Submit your program with comments