I am implementing a tic-tac-toe AI in jquery. I am almost done, but I seem to have a problem with my check_winner() function. It is basicly the same thing as the c version. It checks rows, cols, diagonals and returns -1 for player win, 1 for ai win and 0 for no winner yet. I have been going over it for days and I cannot find anything wrong. When playing the game it will return 0 when I have 3 in a row, it will return 1 when the ai does not have 3 in a row. I can't figure it out. Here is the code:
/**
* check for winner
*/
function check_winner(hyp_board)
{
var first_cell, winner, i ,j;
for (i = 0; i < grid_size; i++){
// check each row
first_cell = hyp_board[i][0];
for (j = 1; j < grid_size; j++){
winner = hyp_board[i][j];
if (first_cell != winner){
winner = 0;
break;
}
}
if (winner != 0) return winner;
// check each colum
first_cell = hyp_board[0][i];
for (j = 1; j < grid_size; j++){
winner = hyp_board[j][i];
if (first_cell != winner){
winner = 0;
break;
}
}
if (winner != 0) return winner;
// check diagonals
first_cell = hyp_board[0][0];
for (j = 1; j < grid_size; j++){
winner = hyp_board[j][j];
if (first_cell != winner){
winner = 0;
break;
}
}
if (winner != 0) return winner;
first_cell = hyp_board[0][grid_size - 1];
for (j = 1; j < grid_size; j++){
winner = hyp_board[j][grid_size - 1 - j];
if (first_cell != winner){
winner = 0;
break;
}
}
return winner;
}
}
Please help.