This is the tic tac toe game made using the C++ programming language. The game has the option to play as 1 or 2 players.
The game has a score feature, which keeps on increasing with wins. Also, the game features a menu for easy transition from one option to another.
Here is the source code of the Tic Tac Toe game in C++:
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 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 | #include <iostream> #include<list> #include <cstdlib> #include<string> #include <ctime> using namespace std; typedef struct{ int *row; }WinList; class Player { private: string name; int score; public: Player() :Player {""}{} Player(string n) :score{0}, name{n}{} void won(){ //increment the score score++; } int getScore(){ return this->score;} string getName(){ return this->name;} }; class Game { private: char board[9]; int emptyIndex[9]; int gameOn, againstComputer; int emptyCount; WinList winlist[8]; void displayBoard(){ cout <<endl; cout << " | | "<<endl; cout << " "<< board[0] <<" | "<<board[1]<<" | "<<board[2]<<endl; cout << " | | "<<endl; cout << "-----------"<<endl; cout << " | | "<<endl; cout << " "<< board[3] <<" | "<<board[4]<<" | "<<board[5]<<endl; cout << " | | "<<endl; cout << "-----------"<<endl; cout << " | | "<<endl; cout << " "<< board[6] <<" | "<<board[7]<<" | "<<board[8]<<endl; cout << " | | "<<endl; cout <<endl; } void computerInput(){ int pos; pos = rand()%10; if(emptyIndex[pos] == 1){ if(emptyCount < 0) return; computerInput(); } else { cout<< "Computer choose: " << pos+1 << endl; emptyIndex[pos] =1; emptyCount-=1; board[pos] = 'O'; } } void playerInput(Player &player){ int pos; cout << endl; cout << "\t" << player.getName() <<" Turn: "; cout <<"\t Enter the position " << endl; cin >> pos; pos -=1; if(emptyIndex[pos] == 1){ cout << "-----Position not empty-------"<< endl; playerInput(player); } else { emptyIndex[pos] =1; emptyCount-=1; player.getName().compare("Player I") == 0 ? board[pos] ='X': board[pos] ='O'; } } void checkWin(Player &p1,Player &p2){ int i,j,k; bool flag = false; char first_symbol; for(i=0; i<8; i++){ first_symbol = board[winlist[i].row[0]]; if((first_symbol != 'X') && (first_symbol != 'O')){ flag = false; continue; } flag = true; for(j=0;j<3;j++){ if(first_symbol != board[winlist[i].row[j]]){ flag = false; break; } } if(flag){ gameOn = 0; if(first_symbol == 'X'){ cout << "-----------------------"<< endl; cout << "\t Player I WON"<< endl; cout << "-----------------------"<< endl; p1.won(); } else { p2.won(); if(againstComputer){ cout << "-----------------------"<< endl; cout << "\t Computer WON"<< endl; cout << "-----------------------"<< endl; } else { cout << "-----------------------"<< endl; cout << "\t Player II WON"<< endl; cout << "-----------------------"<< endl; } } displayScore(p1,p2); break; } } } void play(Player &p1,Player &p2){ char rematch ='\0'; int hand = 0; gameOn =1; displayBoard(); while((emptyCount > 0) && (gameOn != 0)){ if(againstComputer) hand == 1 ? computerInput(): playerInput(p2); else hand == 1 ? playerInput(p1): playerInput(p2); hand= !hand; displayBoard(); checkWin(p1,p2); } if (emptyCount <=0){ cout << " -----------------------"<< endl; cout << "\t No WINNER"<< endl; cout << " -----------------------"<< endl; } cout<< endl; cout << "Rematch Y/N: "; cin >> rematch; if((rematch == 'Y')||(rematch == 'y')){ init(); play(p1,p2); } } void displayScore(Player &p1, Player &p2){ cout << endl; cout << "\t SCORE: \t"; if(againstComputer) cout<<" Player I: " <<p1.getScore()<<" \t Computer: "<<p2.getScore()<< endl; else cout<<" Player I: " <<p1.getScore()<<" \t Player II: "<<p2.getScore()<< endl; } public: Game(): emptyCount{0}, gameOn{1}, againstComputer{0}{ init(); winlist[0].row = new int[3]{0,1,2}; winlist[1].row = new int[3]{3,4,5}; winlist[2].row = new int[3]{6,7,8}; winlist[3].row = new int[3]{0,3,6}; winlist[4].row = new int[3]{1,4,7}; winlist[5].row = new int[3]{2,5,8}; winlist[6].row = new int[3]{0,4,8}; winlist[7].row = new int[3]{2,4,6}; } void init(){ gameOn = 1; emptyCount =0; srand(time(0)); for(size_t i=0; i<10; i++){ emptyIndex[i] = 0; board[i] = (i+1) +'0'; emptyCount++; } emptyCount--; } void onePlayerGame(){ //Creating Player Player p("Player I"); Player c("Computer"); cout << " -----------------------"<< endl; cout << "\t Player I: X \t Computer: O"<< endl; cout << " -----------------------"<< endl; cout << endl; againstComputer = 1; play(c,p); } void twoPlayerGame(){ //Creating Player Player p("Player I"); Player c("Player II"); cout << " -----------------------"<< endl; cout << "\t Player I: X \t Player II: O"<< endl; cout << " -----------------------"<< endl; cout << endl; againstComputer = 0; play(c,p); } }; int main() { int ch; while(1){ cout<< " ----------MENU----------" << endl; cout << "\t 1. 1 Player game" <<endl; cout << "\t 2. 2 Player game" <<endl; cout << "\t 3. To exit " <<endl; cout <<" ------------------------" << endl; cout << endl; cout <<"\t Select an option" << endl; cin >> ch; switch(ch){ case 1:{ Game *game = new Game; game->init(); game->onePlayerGame(); } break; case 2:{ Game *game = new Game; game->init(); game->twoPlayerGame(); } break; case 3: return 0; default: cout << "OOPs Invalid Option! TRY AGAIN"; } } return 0; } |
Running the Game:
The C++ program has two classes Player
and Game
. We create instances of these classes depending on the user choice.
Player
class is used to create a player object which has a separate score, whereas the Game
class has various member methods that run the game.
In 1 player game, the player plays against the computer.
To implement this functionality, we select a random number from 1 to 9 and check if that particular position is empty. If so, we select that position on the board for the computer, else we repeat the process of generating a random number to continue the game.
That’s all for the tic tac toe game code in C++. If the code doesn’t work for you or you have any problem then comment below.