The project:
To make a tic tac toe game little more fun to play. There are 2 main types of mode one is classic version which is 3x3 grid playing field and it has two form of play Human V Human, Human V AI. this classic version is running fine no problem in AI moves.
2nd version is what I am calling is Grand Tic Tac Toe which is basically 9 x 9 grid playing field but it is actually 9 games of classic Tic Tac Toe being played simultaneously so we can say it is a set based game to win the grand version we have to play in all nine games and when we win one set that set is marked with your icon X or O so now that set is one point of grid so now we need to win more sets to make a line of three sets.
Code:
Android Studio is the platform used for writing the app. code is too long to be displayed here so i am attaching the github link of my project https://github.com/AUKNiazi/GrandTicTacToe
file which has the code for Grand Tic Tac Toe https://github.com/AUKNiazi/GrandTicTacToe/blob/master/java/com/example/auk01/grandtictactoe/TicTacToeGame.java
Problem: when a human player wins first set the AI will no longer move Randomly and will not block player’s wining moves. AI will start placing its move from the top most row left side first available point and will keep moving to its right and when the top row ends it will move to 2nd row first available left point and will keep placing its move to the next right.
screen recording of the problem:
it is also on the github page file called
tictactoe.mp4
// Return the best move for the computer to make. You must call setMove()
// to actually make the computer move to that location.
public int getComputerMove(){
int move;
// First see if there's a move O can make to win
for (int i = 0; i < getBOARD_SIZE(); i++)
{
if (mBoard[i] != PLAYER_ONE && mBoard[i] != PLAYER_TWO && mBoard[i] == EMPTY_SPACE)
{
char curr = mBoard[i];
mBoard[i] = PLAYER_TWO;
if ( checkForWinner() == 3)
{
setMove(PLAYER_TWO, i);
return i;
}
else
mBoard[i] = curr;
}
}
// See if there's a move O can make to block X from winning
for (int i = 0; i < getBOARD_SIZE(); i++)
{
if (mBoard[i] != PLAYER_ONE && mBoard[i] != PLAYER_TWO && mBoard[i] == EMPTY_SPACE)
{
char curr = mBoard[i];
mBoard[i] = PLAYER_ONE;
if ( checkForWinner() == 2)
{
setMove(PLAYER_TWO, i);
return i;
}
else
mBoard[i] = curr;
}
}
// Generate random move
do
{
move = mRand.nextInt(getBOARD_SIZE());
} while (mBoard[move] == PLAYER_ONE || mBoard[move] == PLAYER_TWO);
setMove(PLAYER_TWO, move);
return move;
}