Tic Tac Toe game in CPP
#include<iostream> using namespace std; char board[3][3] = {{'1','2','3'} ,{'4','5','6'} ,{'7','8','9'}}; int choice; char a[10];//NAME PLAYER 1 char b[10];//NAME PLAYER 2 char row,column; char turn = 'X'; bool draw = false;//1ST FALSE BECAUSE GAME NOT DRAW int count; void display_table(){ system("cls"); cout<<"\t\t\t\t\t\tTic Tac Toe\n\n"<<endl; cout<<"\t\t\t"<<a<<"[X]"<<"\t\t\t\t\t"<<b<< "[O]\n\n"<<endl;//Tic Tac Toe game in CPP cout<<"\t\t\t\t\t\t "<<" |"<<" "<<" |"<<" "<<endl; cout<<"\t\t\t\t\t\t "<<board[0][0]<<" |"<<" "<<board[0][1]<<" |"<<" "<<board[0][2]<<" "<<endl; cout<<"\t\t\t\t\t\t__|"<<"_|"<<"__"<<endl; cout<<"\t\t\t\t\t\t "<<" |"<<" "<<" |"<<" "<<endl; cout<<"\t\t\t\t\t\t "<<board[1][0]<<" |"<<" "<<board[1][1]<<" |"<<" "<<board[1][2]<<" "<<endl; cout<<"\t\t\t\t\t\t__|"<<"_|"<<"__"<<endl; cout<<"\t\t\t\t\t\t "<<" |"<<" "<<" |"<<" "<<endl; cout<<"\t\t\t\t\t\t "<<board[2][0]<<" |"<<" "<<board[2][1]<<" |"<<" "<<board[2][2]<<" "<<endl; cout<<"\t\t\t\t\t\t "<<" |"<<" "<<" |"<<" "<<endl; } void player_turn_circle(){ if(turn == 'X'){ cout<<a<<" Turn"<<endl; } else if(turn == 'O'){ cout<<b<<" Turn"<<endl; } 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'){//Tic Tac Toe game in CPP 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!try again "<<endl; player_turn_circle();//AGAIN TURN } display_table();//AGAIN DISPLAY TABLE PRINT } bool gameover(){ for(int i=0; i<3; i++)//ROW AND COIUMN CHECK FOR WIN 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;//FALSE STOP 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;// CHECK diagonally for(int i=0; i<3; i++)//GAMEOVER CHECK BOX NOT FILLED for(int j=0; j<3; j++) if(board[i][j] != 'X' && board[i][j] != 'O') return true; draw = true; return false; } int main() { cout<<"\t\t\t\tIf you are 1st player ,your symbol will be [x]"<>a; cout<<"Enter 2nd player's name"<>b; while(gameover()){ display_table(); player_turn_circle(); gameover(); } //GEME WIN SECTION if(turn == 'X' && draw == false){ cout<<"Winner winner chicken dinner!!!!! player2 win$$$$"<<endl;//Tic Tac Toe game in CPP } else if(turn == 'O' && draw == false){ cout<<"Winner winner chicken dinner player1 win$$$$"<<endl; } else cout<<"GAME DRAW!!"; }
Tic Tac Toe game in CPP || C++ Project/Tic Tac Toe game code in CPP
Thanks for sharing. I read many of your blog posts, cool, your blog is very good.