Lab 2 Part 1: Let's Play Tic-Tac-Toe

Deadlines

The deadline for odd-numbered groups is Tuesday 28 September 2004, 23:59:59 hours.

The deadline for even-numbered groups is Tuesday 05 October 2004, 23:59:59 hours.
Submission for even-numbered groups will not be available on Wednesday 29 September 2004.

Your file must be named tictactoe.c, incorrect filename will result in zero marks. This part of the lab is worth one mark.

You must be familiar with 2-dimensional arrays before trying out this lab. If you have not programmed with arrays before, get some practice with 1-dimensional arrays first, then get some practice with 2-dimensional arrays. After that, you will be ready to try out this lab. Remember, this is not a practice lab, so do not expect it to be easy.

Overview

Tic-Tac-Toe is a popular board game where two players take turns to place their marker on one of the 3 x 3 squares of the board. Whichever player manages to line up (either horizontally, vertically or diagonally) 3 of his/her markers first wins the game. The game is a tie if the board is filled before either player gets a winning line.

For example, below is a game (player 1 uses 'X' as marker, player 2 uses 'O' as marker) where player 1 wins after 5 rounds.

X

O

 
 

X

O

   

X

The game below is a tie.

X

O

O

O

X

X

X

X

O

Since a Tic-Tac-Toe board can be easily represented by a 3x3 2D array in C, we are going to implement the Tic-Tac-Toe game for this lab.

 

Getting Started

Suppose we label the rows and columns for a Tic-Tac-Toe board with numbers ranging from 0 to 2 as follows:

 

0

1

2

0

X

 

 
1

 

 

O

2    

 

Then a Tic-Tac-Toe board corresponds to the following C declaration:

char Board[3][3];

To place the 'X' and 'O' markers as the above example, we can do something like this:

Board[0][0] = 'X';

Board[1][2] = 'O';

 

Bits and Pieces

Now that you have a basic idea of simulating a board. Implement the following functions which would help you in defining the final program. The purpose, input and output of the functions are given to help you.

void ClearBoard(char Board[][3]);
  
 Purpose:    Clear the board of rows x columns (i.e. fill them with space)
    Input:      Board (the 2d array that represents the tic-tac-toe board)
    Output:     None. See remark.
    Remark:     Since the board  is directly manipulated/changed by this
                function, no return type is required. Take a look at pg137 of
                textbook for details.

void PrintBoard(Char Board[][3])
   
Purpose:    Print the board using the following format

    0    1    2

0        O  
1   X   
2
   

                Each column is separated by a tab. Print the markers if a
                square is filled, otherwise just print a space.

    Input:      Board (the 2d array that represents the tic-tac-toe board)
    Output:     None. Directly display on screen. 


int IsValid(char Board[][], int x, int y)
   
Purpose:    Check whether square(x,y) of Board is valid (i.e. empty)
                for placing a new marking.
    Input:      Board (the 2d array that represents the tic-tac-toe board)
                x (row number)
                y (column number)
    Output:     1 == true (valid)
                0 == false (invalid)
    Remark:     Don't forget to check whether x and y is in the range of 0 to
                2.

int CheckWin (char Board[][])
   
Purpose:    Check whether there is a line of same markings (vertically,
                horizontal and diagonal).
    Input:      Board (the 2d array that represents the tic-tac-toe board)
    Output:     1 == true (there is a line)
                0 == false (no winning line)
    Remark:     Need to do Eight checking:
                (3 horizontal, 3 vertical, 2 diagonal)

Putting it together

After the above pieces are in place, you are now ready to define the function that actually plays the Tic-Tac-Toe game. The basic steps is given as follows:

    Extra Information:
    Player 1 uses 'X' as marker and play first.
    Player 2 uses 'O' as marker.

  Basic Steps:

        1. Print the current turn number and current player.       
        2. Ask the current player for a coordinate i.e. row and column
            number for placing a marker.
        3. Repeat Step 2 for invalid coordinate.
        4. Place the marker on board.
        5. Print the board
        6. If the board has a winning line or the game is tie, then
           end the game, else go to step 1 by alternating the player.
  Remark:

        1. You have to figure a way to check for tie game.
            Big Hint:
Use turn number.......
           

 

Skeleton Program

#include <stdio.h>

void ClearBoard(char[][]);
void PrintBoard(char[][]);
int IsValid(char[][3], int, int);
int CheckWin (char[][]);

int main()
{
    //here are some of the declarations
    char Board[3][3];
    int turn = 1,player = 1;

    // fill in the main code


    return 0;
}

void ClearBoard(char Board[][3])
{
}

void PrintBoard(char Board[][3])
{

}

int IsValid(char Board[][3], int x, int y)
{
}

int CheckWin (char Board[][3])
{
}
 

Sample Game 1

Note: User Input is in bold.

*******************
Turn 1: Player 1 ('X')
*******************

Enter Row: 1
Enter Column: 1
    0    1    2
0
1        X
2

*******************
Turn 2: Player 2 ('O')
*******************

Enter Row: 1
Enter Column: 0
    0    1    2
0
1   O    X
2

*******************
Turn 3: Player 1 ('X')
*******************

Enter Row: 0
Enter Column: 0
    0    1    2
0   X
1   O    X
2

*******************
Turn 4: Player 2 ('O')
*******************

Enter Row: 2
Enter Column: 2
    0    1    2
0   X
1   O    X
2             O

*******************
Turn 5: Player 1 ('X')
*******************

Enter Row: 0
Enter Column: 2
    0    1    2
0   X         X
1   O    X
2             O

*******************
Turn 6: Player 2 ('O')
*******************

Enter Row: 2
Enter Column: 0
    0    1    2
0   X         X
1   O    X
2   O         O

*******************
Turn 7: Player 1 ('X')
*******************

Enter Row: 0
Enter Column: 1
    0    1    2
0   X    X    X
1   O    X
2   O         O

Congratulation Player 1. You Won!
 

Sample Game 2

*******************
Turn 1: Player 1 ('X')
*******************

Enter Row: 3
Enter Column: 6
Invalid Input. Try Again.

Enter Row: 1
Enter Column: 1
    0    1    2
0
1        X
2

*******************
Turn 2: Player 2 ('O')
*******************

Enter Row: 1
Enter Column: 1
Invalid Input. Try Again.

Enter Row: 0
Enter Column: 0
    0    1    2
0   O
1        X
2

*******************
Turn 3: Player 1 ('X')
*******************

Enter Row: 2
Enter Column: 0
    0    1    2
0   O
1        X
2   X

*******************
Turn 4: Player 2 ('O')
*******************

Enter Row: 0
Enter Column: 2
    0    1    2
0   O         O
1        X
2   X

*******************
Turn 5: Player 1 ('X')
*******************

Enter Row: 0
Enter Column: 1
    0    1    2
0   O    X    O
1        X
2   X

*******************
Turn 6: Player 2 ('O')
*******************

Enter Row: 2
Enter Column: 1
    0    1    2
0   O    X    O
1        X
2   X    O

*******************
Turn 7: Player 1 ('X')
*******************

Enter Row: 1
Enter Column: 0
    0    1    2
0   O    X    O
1   X    X
2   X    O

*******************
Turn 8: Player 2 ('O')
*******************

Enter Row: 1
Enter Column: 2
    0    1    2
0   O    X    O
1   X    X    O
2   X    O

*******************
Turn 9: Player 1 ('X')
*******************

Enter Row: 2
Enter Column: 2
    0    1    2
0   O    X    O
1   X    X    O
2   X    O    X

Game is a tie. Bye Bye.