Let's have a look on another approach of making Tic Tac Toe using Obj oriented language...
Here is the Block Diagram -:
Algorithm - :
Algorithm - :
- Step 1: START
- Step 2: Check
wining move for system (X). If there is such move, take it and go to step 7.
Else go to Step 3.
- Step 3: Check wining
move for user (O). It there is such move, then block the player and go to step
7. Else go to step 4.
- Step 4: If
there are corner spaces (i.e.1,3,7,9), take it and go to step 7. Else go to
step 5.
- Step 5: If
there is center position, take it and go to step 7. Else go to step 6.
- Step 6: If
there are side positions (i.e. 2,4,6,8), take it and go to step 7. Else go to
step 8. (Since there are no spaces left to move)
- Step 7: Ask
user to take a move and go to step 2.
We can create a two-player Tic Tac Toe game in C++ language
//Importing the inbuild libraries in CPP
#include <iostream>
#include <stdlib.h>
using namespace std;
//Array for the board
char board[3][3] = {{'1','2','3'},{'4','5','6'},{'7','8','9'}};
//Variable Declaration
int choice;
int row,column;
char turn = 'X';
bool draw = false;
//Function to show the current status of the gaming board
void display_board(){
//Rander Game Board LAYOUT
cout<<"PLAYER - 1 [X]t PLAYER - 2 [O]nn";
cout<<"tt | | n";
cout<<"tt "<<board[0][0]<<" | "<<board[0][1]<<" | "<<board[0][2]<<" n";
cout<<"tt_____|_____|_____n";
cout<<"tt | | n";
cout<<"tt "<<board[1][0]<<" | "<<board[1][1]<<" | "<<board[1][2]<<" n";
cout<<"tt_____|_____|_____n";
cout<<"tt | | n";
cout<<"tt "<<board[2][0]<<" | "<<board[2][1]<<" | "<<board[2][2]<<" n";
cout<<"tt | | n";
}
//Function to get the player input and update the board
void player_turn(){
if(turn == 'X'){
cout<<"ntPlayer - 1 [X] turn : ";
}
else if(turn == 'O'){
cout<<"ntPlayer - 2 [O] turn : ";
}
//Taking input from user
//updating the board according to choice and reassigning the turn Start
cin>> choice;
//switch case to get which row and column will be update
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'){
//updating the position for 'X' symbol if
//it is not already occupied
board[row][column] = 'X';
turn = 'O';
}else if(turn == 'O' && board[row][column] != 'X' && board[row][column] != 'O'){
//updating the position for 'O' symbol if
//it is not already occupied
board[row][column] = 'O';
turn = 'X';
}else {
//if input position already filled
cout<<"Box already filled!n Please choose another!!nn";
player_turn();
}
/* Ends */
display_board();
}
//Function to get the game status e.g. GAME WON, GAME DRAW GAME IN CONTINUE MODE
bool gameover(){
//checking the win for Simple Rows and Simple Column
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;
//checking the win for both diagonal
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;
//Checking the game is in continue mode or not
for(int i=0; i<3; i++)
for(int j=0; j<3; j++)
if(board[i][j] != 'X' && board[i][j] != 'O')
return true;
//Checking the if game already draw
draw = true;
return false;
}
//Program Main Method
int main()
{
cout<<"tttT I C K -- T A C -- T O E -- G A M Ettt";
cout<<"nttttFOR 2 PLAYERSnttt";
while(gameover()){
display_board();
player_turn();
gameover();
}
if(turn == 'X' && draw == false){
cout<<"nnCongratulations!Player with 'X' has won the game";
}
else if(turn == 'O' && draw == false){
cout<<"nnCongratulations!Player with 'O' has won the game";
}
else
cout<<"nnGAME DRAW!!!nn";
}
Output -:
Perfectly explained💯💯
ReplyDeleteThanks a lot
DeleteThanks for the code
ReplyDeletewelcome
DeleteImpressive work👍
ReplyDeleteGood content..keep writing
ReplyDeleteThanks for the code
Thanks a lot
DeleteGood work
ReplyDeletethanks bro
DeleteThanks for the code
ReplyDeleteNice work buddy!
ReplyDeleteThanks a lot
Deletegreat content!!
ReplyDeleteVery helpful
ReplyDeleteThanks
DeleteImpressive work!
ReplyDeleteThanks
DeleteGood work ! 👍
ReplyDeletecode worked
ReplyDeleteThanks a lot
DeleteNice work
ReplyDeleteGood work....
ReplyDeleteThanks
DeleteNice work
ReplyDeleteThanks a lot
DeleteGreat information
ReplyDeleteThanks man
DeleteNice Work!!
ReplyDeleteThanks bhai
DeleteHelpful!!
ReplyDeletethanks a lot
DeleteNice👍
ReplyDeletethank you
DeleteGood job!!
ReplyDeletethank you
DeleteGreat work 👍
ReplyDeleteHelped a lot
thanks
DeleteGreat content and good explanation bro!
ReplyDeletethanks bro
DeleteGreat work👍
ReplyDeletethanks bro
DeleteAmazing content!
ReplyDeletethanks a lot
DeleteGood work bro best explantion!
ReplyDeleteThanks Bro
Delete