TicTacToe algorithm - Programmers Heaven

#### Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

#### Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

# TicTacToe algorithm

Posts: 18Member
I having a huge problem with this algorithm! Every time I click on a square the whole game is played out for me. I have to use this algorithm on a 2-dimensional array and it just goes bananas, if anyone can help I would greatly appreciate it.

void makeMove()
{
int maxSoFar = -3;
int tentRow = -1;
int tentCol = -1;
for(int row = 0; row < 3; row++)
for(int col = 0; col < 3; col++)
{
if(board[row][col] == BLANK)
{
board[row][col] = EX;
int temp = value(board, MAX);
if(temp > maxSoFar)
{
maxSoFar = temp;
tentRow = row;
tentCol = col;
}
board[row][col] = BLANK;
}
//check for win

if(board[0][0] == EX && board[0][1] == EX && board[0][2] == EX )currView = XWIN;
if(board[1][0] == EX && board[1][1] == EX && board[1][2] == EX )currView = XWIN;
if(board[2][0] == EX && board[2][1] == EX && board[2][2] == EX )currView = XWIN;
if(board[0][0] == EX && board[1][0] == EX && board[2][0] == EX )currView = XWIN;
if(board[0][1] == EX && board[1][1] == EX && board[2][1] == EX )currView = XWIN;
if(board[0][2] == EX && board[1][2] == EX && board[2][2] == EX )currView = XWIN;
if(board[0][0] == EX && board[1][1] == EX && board[2][2] == EX )currView = XWIN;
if(board[0][2] == EX && board[1][1] == EX && board[2][0] == EX )currView = XWIN;
repaint();
}
board[tentRow][tentCol] = EX;

}
public int value(int[][]a, int Player)
{
if(a[0][0] == EX && a[0][1] == EX && a[0][2] == EX)return +1;
if(a[1][0] == EX && a[1][1] == EX && a[1][2] == EX )return +1;
if(a[2][0] == EX && a[2][1] == EX && a[2][2] == EX )return +1;
if(a[0][0] == EX && a[1][0] == EX && a[2][0] == EX )return +1;
if(a[0][1] == EX && a[1][1] == EX && a[2][1] == EX )return +1;
if(a[0][2] == EX && a[1][2] == EX && a[2][2] == EX )return +1;
if(a[0][0] == EX && a[1][1] == EX && a[2][2] == EX )return +1;
if(a[0][2] == EX && a[1][1] == EX && a[2][0] == EX )return +1;
//do for all wins and ties!
//next lines is very important
if(Player == MAX)
{
int minSoFar = +5;
for(int row = 0; row < 3; row++)
for(int col = 0; col < 3; col++)
{
if(a[row][col]==BLANK)
{
a[row][col]=OH;
int temp = value(a,MIN);
if(tempminSoFar)minSoFar = temp;// temp>maxSoFar)MaxSoFar = temp;
}
}
return minSoFar; //returnminSoFar
}
else return 50;

}