0% found this document useful (0 votes)
56 views2 pages

Tic Tac Toe

This C++ program implements a Tic-Tac-Toe game between two players. It uses a 3x3 character array to represent the game board and tracks whose turn it is with the 'X' and 'O' characters. Functions are used to display the board, get player input, place markers on the board, and check for a win or draw condition after each turn. The main function calls these functions, initializes the game, and displays the outcome.

Uploaded by

Zwe Lin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
56 views2 pages

Tic Tac Toe

This C++ program implements a Tic-Tac-Toe game between two players. It uses a 3x3 character array to represent the game board and tracks whose turn it is with the 'X' and 'O' characters. Functions are used to display the board, get player input, place markers on the board, and check for a win or draw condition after each turn. The main function calls these functions, initializes the game, and displays the outcome.

Uploaded by

Zwe Lin
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

#include<iostream>

#include<stdlib.h>
using namespace std;

char board[3][3]={{'1','2','3'},{'4','5','6'},{'7','8','9'}};
int choice;
int row,column;
char turn ='X';
bool draw = false;

void display_board()
{
cout<<"PLAYER - 1 [X]\tPLAYER - 2[O]\n";
cout<<"\t\t | | \n";
cout<<"\t\t "<<board[0][0]<<" | "<<board[0][1]<<" | "<<board[0][2]<<" \n";
cout<<"\t\t____|____|____\n";
cout<<"\t\t | | \n";
cout<<"\t\t "<<board[1][0]<<" | "<<board[1][1]<<" | "<<board[1][2]<<"\n";
cout<<"\t\t____|____|____\n";
cout<<"\t\t | | \n";
cout<<"\t\t "<<board[2][0]<<" | "<<board[2][1]<<" | "<<board[2][2]<<"\n";
cout<<"\t\t | | \n";
}
void player_turn()
{
if(turn =='X'){
cout<<"\n\tPlayer - 1[X]turn : ";
}
else if(turn == 'O'){
cout<<"\n\tPlayer - 2[O]turn : ";
}
cin>>choice;

switch(choice){
case 1: row=0;column=0;break;
case 2: row=0;column=1;break;
case 3: row=0;column=2;break;
case 4: row=1;column=0;break;
case 5: row=1;column=1;break;
case 6: row=1;column=2;break;
case 7: row=2;column=0;break;
case 8: row=2;column=1;break;
case 9: row=2;column=2;break;
default:
cout<<"Invalid Move";
}
if(turn=='X'&&board[row][column]!='X'&&board[row][column]!='O'){
board[row][column]='X';
turn = 'O';
}
else if(turn == 'O'&&board[row][column]!='X'&&board[row][column]!='O'){
board[row][column]='O';
turn = 'X';
}
else{
cout<<"Box already filled!n Please choose another!!\n\n";
player_turn();
}
display_board();
}

bool gameover()
{
for(int i=0;i<3;i++)
if(board[i][0]==board[i][1]&&board[i][0]==board[i][2]||board[0]
[i]==board[1][i]&&board[0][i]==board[2][i])
return false;

if(board[0][0]==board[1][1]&&board[0][0]==board[2][2]||board[0][2]==board[1]
[1]&&board[0][2]==board[2][0])
return false;

for(int i=0;i<3;i++)

for(int j=0;j<3;j++)

if(board[i][j]!='X'&&board[i][i]!='O')
return true;

draw = true;
return false;

int main()
{
cout<<"\t\t\tTICK--TAC--TOE--GAME\t\t\t";
cout<<"\n\t\t\t\tFOR 2 PLAYERS\n\t\t\t";
display_board();
while(gameover()){

player_turn();
gameover();
}
if(turn=='X'&&draw==false){
cout<<"\n\nCongratulations! Player with 'O' has won the game";
}
else if(turn == 'O'&&draw == false){
cout<<"\n\Congratulations! Player with 'X' has won the game";
}
else
cout<<"\n\nGAME DRAW!!!\n\n";
}

You might also like