Tic Tac Toe in C++

This program is a game program, Tic Tac Toe. Most of us have played this game in school days, we will be making a C++ program on it now. This program is quite lengthy, so I’ll first explain each variable and function.

Variables used:
(global) int gameover: This variable is initialized to 0 and its value is altered to 1 when the game is over. The program stops execution at gameover = 1.

int pos[9]
: This array contains the positions 1 – 9. This array is just used in one function (void print_board_start())

(global) char set[9] : This array is initialized to values 1 to 9. The values of 1 to 9 are altered to either X or O depending on user input. For instance: Player 1 (X) selects position 5 to mark as ‘X’. The function input_board_x() sets the value of set[5-1] = X. Similar process happens with Player 2(O).

(global) int taken[9]: This array to check if a position is taken. For instance, if a user selects position 5, taken[5-1] will be set to 1. While taking input from user, the program first checks if taken[x_pos - 1] = 1. If it is, it would tell the Player 1 that the position is already taken and asks to re-enter the position.

count: To count the number of moves. This variable cannot exceed 8 as there can only be 9 moves in a tic tac toe game.

void print_board_start(): This objective of this function is basically to print the starting board without any ‘X’ or ‘O’.

print_board_start()

void input_board_x()/void input_board_y(): These two functions alter the values of the positions of set[] to ‘X’ or ‘O’ depending on the users’ choice.

check_gameover();: This function tests if the game is over. If it is, then the value of gameover is set to 1.

check_duplicate();: Checks if user has entered a position that is already taken.

Program:

I know, this explanation is not so good. If you have any queries regarding the program, please let us know via comments.
Also, if you have a better program, feel free to let us know about it via comments.

Thank You.

Tested in Microsoft Visual Studio 2010.

The following two tabs change content below.
  • Sriram

    Nice implementation, if i want to exit from the game at any stage, can i ? i guess, there should be a gateway to exit from the game without completing it.

  • Vlad

    You could use another variable and call it say ‘char exit’ and initialize it to ‘A’. Then display an instruction to the user at the beginning saying “To exit the game type ‘x’.” Then simply check if the next user input is ‘x’ or no. You can implement the same by checking the condition in the while loop.

  • priyansh

    there are 14 errors in program so please debug the errors

  • doak

    Priyansh could you specify the errors and IDE you are using?

    As mentioned, the program is tested in Visual Studio 2010 and it works fine.

  • Jason

    I have to make a tic tac to game that has 4 rows and 4 columns. The players only have to get 3 in a row though. How do I use this source code but integrate 4 rows and 4 columns into this?