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’.

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:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 |
#include <iostream> #include <conio.h> using namespace std; int gameover= 0; int pos[9]={1,2,3,4,5,6,7,8,9}; char set[9]={'1','2','3','4','5','6','7','8','9'}; int taken[9]={0,0,0,0,0,0,0,0,0}; char cPlayer1 = 'X'; char cPlayer2 = 'O'; void print_board_start() //Prints board at the start of the program { cout<<"Welcome to Tic Tac Toe."<<endl; cout<<"|"<<pos[0]<<"|"<<pos[1]<<"|"<<pos[2]<<"|"<<endl; cout<<"-------"<<endl; cout<<"|"<<pos[3]<<"|"<<pos[4]<<"|"<<pos[5]<<"|"<<endl; cout<<"-------"<<endl; cout<<"|"<<pos[6]<<"|"<<pos[7]<<"|"<<pos[8]<<"|"<<endl; cout<<"-------"<<endl; } void input_board_x(int user_pos_x) //Sets the value for Player 1(X). { set[user_pos_x-1]= 'X'; } void input_board_y(int user_pos_y) //Sets position for Player 2(O) { set[user_pos_y-1]= 'O'; } void final_result_board() //Prints board after the game is won. { cout<<"|"<<set[0]<<"|"<<set[1]<<"|"<<set[2]<<endl; cout<<"-------"<<endl; cout<<"|"<<set[3]<<"|"<<set[4]<<"|"<<set[5]<<endl; cout<<"-------"<<endl; cout<<"|"<<set[6]<<"|"<<set[7]<<"|"<<set[8]<<endl; cout<<"-------"<<endl; } int check_gameover() // Checks if the game is over { //***---Checking if game is over.***---// //First possibilty if(set[0] == set[1] && set[2] == set[0]) { cout<<"GAME OVER!"<<endl; gameover = 1; if(set[0] == cPlayer1) { cout<<"Player 1 wins!"<<endl; } else cout<<"Player 2 wins!"<<endl; } //Second possiblity if(set[0] == set[3] && set[6] == set[0]) { cout<<"GAME OVER!"<<endl; gameover = 1; if(set[0] == cPlayer1) { cout<<"Player 1 wins!"<<endl; } else cout<<"Player 2 wins!"<<endl; } //Third possibility if(set[0] == set[4] && set[8] == set[0]) { cout<<"GAME OVER!"<<endl; gameover = 1; if(set[0] == cPlayer1) { cout<<"Player 1 wins!"<<endl; } else cout<<"Player 2 wins!"<<endl; } //Fourth possibility if(set[1] == set[4] && set[7] == set[1]) { cout<<"GAME OVER!"<<endl; gameover = 1; if(set[1] == cPlayer1) { cout<<"Player 1 wins!"<<endl; } else cout<<"Player 2 wins!"<<endl; } //Fifth possibility if(set[2] == set[5] && set[2] == set[8]) { cout<<"GAME OVER!"<<endl; gameover = 1; if(set[2] == cPlayer1) { cout<<"Player 1 wins!"<<endl; } else cout<<"Player 2 wins!"<<endl; } //Sixth possibilty if(set[2] == set[4] && set[4] == set[6]) { cout<<"GAME OVER!"<<endl; gameover = 1; if(set[2] == cPlayer1) { cout<<"Player 1 wins!"<<endl; } else cout<<"Player 2 wins!"<<endl; } //Seventh possibilty if(set[6] == set[7] && set[8] == set[6]) { cout<<"GAME OVER!"<<endl; gameover = 1; if(set[2] == cPlayer1) { cout<<"Player 1 wins!"<<endl; } else cout<<"Player 2 wins!"<<endl; } return gameover; } int check_duplicate(int i) //Checks if any of the two players add a duplicate data. { while(taken[i-1] == 1) { cout<<i<<" is already taken, please select another spot "; cin>>i; } return i; } int main() { int count = 0; int x_pos,y_pos; print_board_start(); cout<<"Player 1 : X"<<endl<<"Player 2 : 0"<<endl; while(gameover==0 && count<=8) { count++; cout<<"Enter the position for X :"<<endl; cin>>x_pos; while(x_pos>9 || x_pos <1 ) { cout<<"Invalid Move! Please select position from 1 to 9: "; cin>>x_pos; } if(taken[x_pos-1] == 1) { x_pos=check_duplicate(x_pos); } input_board_x(x_pos); //Setting the position of X. taken[x_pos -1] = 1; ////Setting the user entered position to taken. check_gameover(); //Check if the game is over. if(gameover == 1) //If game ends, this part of code will terminate the while loop { break; } if(count == 9) //If there have been 9 moves till now, loops stop executing { break; } count++; final_result_board(); //Prints current game progress cout<<"Enter the position for 0 : "<<endl; cin>>y_pos; while(y_pos>9 || y_pos <1 ) //If user enters invalid move. That is more than 9 and less than 0. { cout<<"Invalid Move! Please select position from 1 to 9: "; cin>>y_pos; } if(taken[y_pos-1] == 1) { y_pos=check_duplicate(y_pos); } input_board_y(y_pos); //Setting the position of Y. taken[y_pos -1] = 1; //Setting the user entered position to taken. check_gameover(); // Checks if the game is over final_result_board(); //Prints current game progress if(gameover == 1) //If game ends, this part of code will terminate the while loop { break; } } if(gameover == 0) //If there is no result, i.e gameover stays at 0 after 9 iteration. { cout<<"Draw"<<endl; } final_result_board(); getch(); } |
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.
Vlad
Latest posts by Vlad (see all)
- Code jam “Tic-Tac-Toe-Tomek” solution in java - April 19, 2013
- Code jam “Minimum Scalar product” solution in java - March 18, 2013
- Code jam Store Credit solution in java - March 10, 2013