Tic Tac Toe Game
Tic Tac Toe Game
Tic Tac Toe Game
SOURCE CODE:
#include <iostream>
#include <cstdlib>
#include <ctime>
char board[9] = { ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ', ' ' };
void show_board();
void get_x_player_choice();
void get_o_player_choice();
void get_computer_choice();
char check_winner();
void computer_vs_player();
void player_vs_player();
int main() {
int mode;
switch (mode) {
case 1:
computer_vs_player();
break;
case 2:
player_vs_player();
break;
default:
break;
return 0;
void computer_vs_player() {
string player_name;
while (true) {
system("cls");
show_board();
if (count_board('X') == count_board('O')) {
get_x_player_choice();
} else {
get_computer_choice();
if (winner == 'X') {
system("cls");
show_board();
cout << player_name << " Won The Game." << endl;
break;
system("cls");
show_board();
break;
system("cls");
show_board();
break;
void get_computer_choice() {
srand(time(0));
int choice;
do {
choice = rand() % 9;
board[choice] = 'O';
void get_x_player_choice() {
while (true) {
int choice;
cout << "Please Select Your Choice From (1 - 9): " << endl;
} else {
board[choice] = 'X';
break;
void get_o_player_choice() {
while (true) {
int choice;
choice--;
cout << "Please Select Your Choice From (1 - 9): " << endl;
} else {
board[choice] = 'O';
break;
}
void player_vs_player() {
while (true) {
system("cls");
show_board();
if (count_board('X') == count_board('O')) {
get_x_player_choice();
} else {
get_o_player_choice();
if (winner == 'X') {
system("cls");
show_board();
cout << x_player_name << " Won The Game." << endl;
break;
system("cls");
show_board();
cout << o_player_name << " Won The Game." << endl;
break;
system("cls");
show_board();
break;
int total = 0;
if (board[i] == symbol)
total += 1;
return total;
char check_winner() {
return board[0];
return board[3];
return board[6];
return board[0];
return board[1];
if (board[2] == board[5] && board[5] == board[8] && board[2] != ' ')
return board[2];
return board[0];
return board[2];
return 'C';
else
return 'D';
void show_board() {
cout << "PLAYER - 1 [X]\t PLAYER - 2 [O]" << endl << endl;
cout << " " << board[0] << " | " << board[1] << " | " << board[2] << endl;
cout << " " << board[3] << " | " << board[4] << " | " << board[5] << endl;
cout << " " << board[6] << " | " << board[7] << " | " << board[8] << endl;