Paste: hosey75432 part 2
Author: | Brandon M. Gerson |
Mode: | factor |
Date: | 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 [1] == "X" && Square [5] == "X" && Square [9] == null )
y = 9;
if ( Square [1] == "X" && Square [9] == "X" && Square [5] == null )
y = 5;
if ( Square [5] == "X" && Square [9] == "X" && Square [1] == null )
y = 1;
//second diagonal
if ( Square [3] == "X" && Square [5] == "X" && Square [7] == null )
y = 7;
if ( Square [3] == "X" && Square [7] == "X" && Square [5] == null )
y = 5;
if ( Square [5] == "X" && Square [7] == "X" && Square [3] == 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 [1] == "O" && Square [5] == "O" && Square [9] == null )
y = 9;
if ( Square [1] == "O" && Square [9] == "O" && Square [5] == null )
y = 5;
if ( Square [5] == "O" && Square [9] == "O" && Square [1] == null )
y = 1;
//second diagonal
if ( Square [3] == "O" && Square [5] == "O" && Square [7] == null )
y = 7;
if ( Square [3] == "O" && Square [7] == "O" && Square [5] == null )
y = 5;
if ( Square [5] == "O" && Square [7] == "O" && Square [3] == null )
y = 3;
return y;
}
}
New Annotation