class eotcatcit
{
//variables which should be constants (not to mention bytes, 1 and 2)
private static int P1 = -1;
private static int P2 = 1;
public static void main(String argv[])
{
//start 3x3 array for the board
int[][] arr = new int[3][3];
//unnecessary for loops, since declaring an array like you did starts it with 0
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
arr[i][j] = 0;
//check if player 1 can win
System.out.println(findOutcome(arr, P1));
}
static int findOutcome(int[][] arr, int whomoves)
{
//check if this board configuration is a win
int test = testWin(arr);
boolean winflag = false;
//Check if any moves coming from this move is a win
if(test != 0)
return test;
// if any move has a winning outcome for current player,
// then current player wins. Otherwise current player loses.
// Also, if no moves are legal for current player, current player loses.
for(int i = 0; i < 3; i++)
for(int j = 0; j < 3; j++)
{
if(arr[i][j] == 0){
arr[i][j] = whomoves;
winflag = (findOutcome(arr, -whomoves) == whomoves);
arr[i][j] = 0;
}
//If this board configuration is a win, return it (this will effectively end the program)
if(winflag)
return whomoves;
}
//If it's not a win, return the opposite, and the program keeps going
return -whomoves;
}
//this sub checks if a win happened
static int testWin(int[][] arr)
{
int i;
for(i = 0; i < 3; i++){
if(arr[i][0] != 0 && arr[i][0] == arr[i][1] && arr[i][1] == arr[i][2])
return -arr[i][0];
if(arr[0][i] != 0 && arr[0][i] == arr[1][i] && arr[1][i] == arr[2][i])
return -arr[i][0];
}
if(arr[0][0] != 0 && arr[0][0] == arr[1][1] && arr[1][1] == arr[2][2])
return -arr[0][0];
if(arr[2][0] != 0 && arr[2][0] == arr[1][1] && arr[1][1] == arr[0][2])
return -arr[2][0];
return 0;
}
}