# Paste: hosey75432 part 2

Author: Brandon M. Gerson factor Mon, 12 Mar 2012 01:24:12
Plain Text |
```import java.util.*;

public class TicTacToeMethods
{
public String Markers (int n, int SquaresRemaining)
{
if ( SquaresRemaining%2 == 1 )
return "X";
else
return "O";
}

public static void PlayAgain (int n)
{
if ( n == 0 )
System.out.println ( "Thank you for playing Tic-Tac-Toe!" );
}

public int ComputerLogic ( String Square[] )
{
int y = 0; //initialize dummy variable

//default move
for ( int m = 1; m <= 9; m++ )
{
if ( Square[m] == null )
y = m;
}

//vertical blocker
for ( int i = 0; i <= 2; i++ )
{
if ( Square[i*3+1] == "X" && Square[i*3+2] == "X" && Square [i*3+3] == null )
y = 3 + 3*i;
if ( Square[i*3+1] == "X" && Square[i*3+3] == "X" && Square [i*3+2] == null )
y = 2 + 3*i;
if ( Square[i*3+2] == "X" && Square[i*3+3] == "X" && Square [i*3+1] == null )
y = 1 + 3*i;
}

//horizontal blocker
for ( int j = 0; j <= 2; j++ )
{
if ( Square [1+j] == "X" && Square [4+j] == "X" && Square [7+j] == null )
y = 7 + j;
if ( Square [1+j] == "X" && Square [7+j] == "X" && Square [4+j] == null )
y = 4 + j;
if ( Square [4+j] == "X" && Square [7+j] == "X" && Square [1+j] == null )
y = 1 + j;
}

//diagonal blocker
//first diagonal
if ( Square  == "X" && Square  == "X" && Square  == null )
y = 9;
if ( Square  == "X" && Square  == "X" && Square  == null )
y = 5;
if ( Square  == "X" && Square  == "X" && Square  == null )
y = 1;

//second diagonal
if ( Square  == "X" && Square  == "X" && Square  == null )
y = 7;
if ( Square  == "X" && Square  == "X" && Square  == null )
y = 5;
if ( Square  == "X" && Square  == "X" && Square  == null )
y = 3;

//victory

//horizontal victory
for ( int i = 0; i <= 2; i++ )
{
if ( Square[i*3+1] == "O" && Square[i*3+2] == "O" && Square [i*3+3] == null )
y = 3 + 3*i;
if ( Square[i*3+1] == "O" && Square[i*3+3] == "O" && Square [i*3+2] == null )
y = 2 + 3*i;
if ( Square[i*3+2] == "O" && Square[i*3+3] == "O" && Square [i*3+1] == null )
y = 1 + 3*i;
}

//vertical victory
for ( int j = 0; j <= 2; j++ )
{
if ( Square [1+j] == "O" && Square [4+j] == "O" && Square [7+j] == null )
y = 7 + j;
if ( Square [1+j] == "O" && Square [7+j] == "O" && Square [4+j] == null )
y = 4 + j;
if ( Square [4+j] == "O" && Square [7+j] == "O" && Square [1+j] == null )
y = 1 + j;
}

//diagonal victory
//first diagonal
if ( Square  == "O" && Square  == "O" && Square  == null )
y = 9;
if ( Square  == "O" && Square  == "O" && Square  == null )
y = 5;
if ( Square  == "O" && Square  == "O" && Square  == null )
y = 1;

//second diagonal
if ( Square  == "O" && Square  == "O" && Square  == null )
y = 7;
if ( Square  == "O" && Square  == "O" && Square  == null )
y = 5;
if ( Square  == "O" && Square  == "O" && Square  == null )
y = 3;

return y;

}

}```