In this article, we will discuss the following main topics. I encourage you to watch YouTube video to understand the source code in detail.
- Technique to switch players.
- Logic to check winner player.
- Calculate computer player best move.
Technique to switch players
I have assigned the value 0 to the first player and the value 1 to the second player, regardless of who plays first. If the human chooses to play first, the value 0 is assigned to the human variable, and the value 1 is assigned to the computer variable, and vice versa. Throughout the project, the system checks the current player against the human or computer variable to determine the player type. The first player always uses "X," while the second player uses "O." However, the value assigned to a box depends on the player type: the value of a box chosen by the human is 3, and the value of a box chosen by the computer is 5.
#define firstPlayer 0 //Flag for first player
#define secondPlayer 1 //Flag for second player
int human,computer; //Variable to identify human or computer
//code to initialize player
void initGame(int choice){
initBoard();
if(choice == 1){
human = firstPlayer;
computer = secondPlayer;
} else if(choice == 2){
human = secondPlayer;
computer = firstPlayer;
}
initPlayer(firstPlayer,secondPlayer);
//getch();
}
Logic to check winner player
As you know, the value 3 is assigned to a box chosen by the human player, and the value 5 is assigned to a box chosen by the computer. To determine the winner, we will check the sum of the values of the boxes. If the total sum of a row, column, or diagonal is 9, the human player wins the game. Conversely, if the total sum is 15, the computer wins the game. We will calculate the sum of the values for each row, column, and both diagonals individually and compare the results to identify the winner.
//This function check the winner
int checkWinner(){
int total=0;
//Check Column
for(int i=0;i<3;i++){
total = box[i].value + box[i + 3].value + box[i + 6].value;
if (total == 9 || total == 15)
return 1;
}
//Check rows
for(int i=0;i<9;i=i+3){
total = box[i].value + box[i + 1].value + box[i + 2].value;
if (total == 9 || total == 15)
return 1;
}
//Check first diagonal of the board
total = box[0].value + box[4].value + box[8].value;
if(total == 9 || total == 15)
return 1;
//Check second diagonal of the board
total = box[2].value + box[4].value + box[6].value;
if(total == 9 || total == 15)
return 1;
return 0;
}
Calculate computer's next best move
We have already discussed the algorithm for the computer player to determine its next move. I have implemented this algorithm exactly as described in the source code. The computer player prioritizes selecting a winning position first. If a winning position is not available, it chooses a position that results in a draw. If neither a winning nor a draw position is available, the computer selects the next best move from a set of predefined steps.
//This function check the winner
int checkWinner(){
int total=0;
//Check Column
for(int i=0;i<3;i++){
total = box[i].value + box[i + 3].value + box[i + 6].value;
if (total == 9 || total == 15)
return 1;
}
//Check rows
for(int i=0;i<9;i=i+3){
total = box[i].value + box[i + 1].value + box[i + 2].value;
if (total == 9 || total == 15)
return 1;
}
//Check first diagonal of the board
total = box[0].value + box[4].value + box[8].value;
if(total == 9 || total == 15)
return 1;
//Check second diagonal of the board
total = box[2].value + box[4].value + box[6].value;
if(total == 9 || total == 15)
return 1;
return 0;
}
//This function will return game winning move
//or game draw move based on prameters
//flag == 0, winning move
//flag == 1, draw move
int getWinDrawMove(int flag){
int total=-1;
if(flag==0){
total=10;
} else if(flag==1){
total=6;
}
//Check Column
for(int i=0;i<3;i++){
if (total==(box[i].value+box[i + 3].value+box[i + 6].value)){
if(box[i].value==0)
return i;
else if (box[i + 3].value==0)
return i+3;
else if (box[i + 6].value==0)
return i+6;
}
}
//Check rows
for(int i=0;i<9;i=i+3){
if ((box[i].value+box[i + 1].value+box[i + 2].value)==total){
if(box[i].value==0)
return i;
else if (box[i+1].value==0)
return i+1;
else if (box[i+2].value==0)
return i+2;
}
}
//Check first diagonal of the board
if((box[0].value+box[4].value+box[8].value) == total){
if(box[0].value==0)
return 0;
else if (box[4].value==0)
return 4;
else
return 8;
}
//Check second diagonal of the board
if((box[2].value+box[4].value+box[6].value) == total){
if(box[2].value==0)
return 2;
else if (box[4].value==0)
return 4;
else
return 6;
}
return -1;
}
//Get location of boxes 5,2,4,6,8 respectively
int getMake2()
{
if(box[4].value == 0)
return 4;
if(box[1].value == 0)
return 1;
if(box[3].value == 0)
return 3;
if(box[5].value == 0)
return 5;
if(box[7].value == 0)
return 7;
return -1;
}
//Get location of boxes 1,3,7,9 respectively
int getMake4()
{
if(box[0].value == 0)
return 0;
if(box[2].value == 0)
return 2;
if(box[6].value == 0)
return 6;
if(box[8].value == 0)
return 8;
return -1;
}
//This function return best next move for computer player
int getNextMove(){
int boxNum;
if(p.numOfTurn >= 3){
//Get winning position
boxNum = getWinDrawMove(1);
if(boxNum!=-1)
return boxNum;
//Get Draw position
boxNum = getWinDrawMove(0);
if(boxNum!=-1)
return boxNum;
}
boxNum = getMake2();
if(boxNum!=-1)
return boxNum;
boxNum = getMake4();
if(boxNum!=-1)
return boxNum;
return -1;
}
No comments:
Post a Comment