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

TIC TAC TOE Game

This document contains a C++ implementation of a Tic Tac Toe game. It includes functions to draw the game board, place markers, check for a winner, and manage player turns. The main function initiates the game and handles player interactions.

Uploaded by

240944.EA
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)
18 views2 pages

TIC TAC TOE Game

This document contains a C++ implementation of a Tic Tac Toe game. It includes functions to draw the game board, place markers, check for a winner, and manage player turns. The main function initiates the game and handles player interactions.

Uploaded by

240944.EA
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>

using namespace std;

char board[3][3]= {{'1','2','3'},{'4','5','6'},{'7','8','9'}};


char currentMarker;
int currentPlayer;

void drawBoard(){
cout<<"\n";
for(int i = 0; i < 3; i++){
for(int j = 0; j <3; j++){
cout<<board[i][j]<<" ";
if(j<2) cout <<"|";
}
cout<<"\n";
if(i<2) cout<<"--|---|--\n";
}
cout<<"\n";
}

bool placeMarker(int slot){


int row = (slot - 1) / 3;
int col = (slot - 1) % 3;

if(board[row][col] != 'X' && board[row][col] != 'O'){


board[row][col] = currentMarker;
return true;
}
return false;

int checkWinner(){
//Check rows and columns
for(int i = 0; i < 3 ; i++){
if(board[i][0] == board[i][1] && board[i][1] == board[i][2]) return
currentPlayer;
if(board[0][i] == board[1][i] && board[1][i] == board[2][i]) return
currentPlayer;
}//check diagonals
if(board[0][0] == board[1][1] && board[1][1] == board[2][2]) return
currentPlayer;
if(board[0][2] == board[1][1] && board[1][1] == board[2][0]) return
currentPlayer;

return 0; //not winner yet

void swapPlayerAndMarker(){
currentPlayer = (currentPlayer == 1)? 2 :1;
currentMarker = (currentMarker == 'X')? 'O' : 'X';
}

void game(){
int winner = 0;
int slot;
cout << "Player 1, Choose your marker ( X or O ): ";
cin >> currentMarker;
currentPlayer = 1;

drawBoard();

for ( int i = 0; i < 9 && winner == 0; i++){


cout<<"Player"<<currentPlayer<<",Enter your slot";
cin >> slot;

if(slot < 1 || slot > 9 || !placeMarker(slot)){


cout<<"Invalid move. Try again!\n";
i--;//retry this return
continue;
}
drawBoard();
winner = checkWinner();

if(winner == 0){
swapPlayerAndMarker();
}
}

if(winner = 0){
cout<<"It's a tie!\n";
} else{
cout<<"Player " << winner << " wins!\n";
}
}

int main(){
cout<<"Wecome to the TIC TAC TOE game!\n";
game();
return 0;
}

You might also like