Was writing a tic tac toe app for android recently as was familiar with the rules of the game :-).

The last time I wrote a tic tac toe game was in college time, and then most focus was on the gui design. The lousiest algorithm in the background.

One of the algorithm was to check the state of the game i.e win or draw etc. Gave it a thought this time and made some optimization in the row, col, diagonal checks. Its mainly decided in the first nested loop if we need to check a particular column or diagonal. So, we avoid checking of columns or diagonals saving time. This makes big impact when the board size is more and a significant number of the cells are not filled.

Next time I will post the nextMove implementation, for the computer to decide its next move.

Here is the java code for the gamestate algorithm.

int gameState(int values[][], int boardSz) {

boolean colCheckNotRequired[] = new boolean[ boardSz ];

boolean diag1CheckNotRequired = false;

boolean diag2CheckNotRequired = false;

boolean allFilled = true;

int x_count = 0;

int o_count = 0;

/* Check rows */

for (int i = 0; i < boardSz; i++) {

x_count = o_count = 0;

for (int j = 0; j < boardSz; j++) {

if(values[i][j] == x_val)x_count++;

if(values[i][j] == o_val)o_count++;

if(values[i][j] == 0)

{

colCheckNotRequired[j] = true;

if(i==j)diag1CheckNotRequired = true;

if(i + j == boardSz - 1)diag2CheckNotRequired = true;

allFilled = false;

//No need check further

break;

}

}

if(x_count == boardSz)return X_WIN;

if(o_count == boardSz)return O_WIN;

}

/* check cols */

for (int i = 0; i < boardSz; i++) {

x_count = o_count = 0;

if(colCheckNotRequired[i] == false)

{

for (int j = 0; j < boardSz; j++) {

if(values[j][i] == x_val)x_count++;

if(values[j][i] == o_val)o_count++;

//No need check further

if(values[i][j] == 0)break;

}

if(x_count == boardSz)return X_WIN;

if(o_count == boardSz)return O_WIN;

}

}

x_count = o_count = 0;

/* check diagonal 1 */

if(diag1CheckNotRequired == false)

{

for (int i = 0; i < boardSz; i++) {

if(values[i][i] == x_val)x_count++;

if(values[i][i] == o_val)o_count++;

if(values[i][i] == 0)break;

}

if(x_count == boardSz)return X_WIN;

if(o_count == boardSz)return O_WIN;

}

x_count = o_count = 0;

/* check diagonal 2 */

if( diag2CheckNotRequired == false)

{

for (int i = boardSz - 1,j = 0; i >= 0 && j < boardSz; i--,j++) {

if(values[j][i] == x_val)x_count++;

if(values[j][i] == o_val)o_count++;

if(values[j][i] == 0)break;

}

if(x_count == boardSz)return X_WIN;

if(o_count == boardSz)return O_WIN;

x_count = o_count = 0;

}

if( allFilled == true)

{

for (int i = 0; i < boardSz; i++) {

for (int j = 0; j < boardSz; j++) {

if (values[i][j] == 0) {

allFilled = false;

break;

}

}

if (allFilled == false) {

break;

}

}

}

if (allFilled)

return DRAW;

return INPROGRESS;

}

## No comments:

## Post a Comment