MORE JAVA

Oct 28, 2004 13:52

//Andy Brabson

import TerminalIO.*;

public class TicTacToe
{
public static void main(String[] args)
{
KeyboardReader reader = new KeyboardReader();
char[] board = new char[10];
char win = ' ';

while(win != 'y')
{
printBoard(board);
updateBoardX(board);
printBoard(board);
updateBoardO(board);

if((board[1] != ' ' && board[1] == board[5] && board[5] == board[9]) ||
(board[1] != ' ' && board[1] == board[4] && board[4] == board[7]) ||
(board[1] != ' ' && board[1] == board[2] && board[2] == board[3])
)
{
if(board[1] == 'X')
{
System.out.println("The X's win. ");win = 'y';
}
else if(board[1] == 'O')
{
System.out.println("The O's win. ");win = 'y';
}
}

else if(board[2] != ' ' && board[2] == board[5] && board[5] == board[8])
{
if(board[2] == 'X')
{
System.out.println("The X's win. ");win = 'y';
}
else if(board[2] == 'O')
{
System.out.println("The O's win. ");win = 'y';
}
}

else if((board[3] != ' ' && board[3] == board[6] && board[6] == board[9])||
(board[3] != ' ' && board[3] == board[5] && board[5] == board[7]))
{
if(board[3] == 'X')
{
System.out.println("The X's win. ");win = 'y';
}
else if(board[3] == 'O')
{
System.out.println("The O's win. ");win = 'y';
}
}
else if(board[4] != ' ' && board[4] == board[5] && board[5] == board[6])
{
if(board[4] == 'X')
{
System.out.println("The X's win. ");win = 'y';
}
else if(board[4] == 'O')
{
System.out.println("The O's win. ");win = 'y';
}
}
else if(board[7] != ' ' && board[7] == board[8] && board[8] == board[9])
{
if(board[7] == 'X')
{
System.out.println("The X's win. ");win = 'y';
}

else if(board[7] == 'O')
{
System.out.println("The O's win. ");win = 'y';
} }
}

resetBoard(board);

}
public static void resetBoard(char[] board)
{
for (int i = 1; i<10; i++)
board[i] = ' ';

}
public static void printBoard(char[] board)
{
System.out.println(board[1] + "|" + board[2] + "|" + board[3] );
System.out.println("------");
System.out.println(board[4] + "|" + board[5] + "|" + board[6] );
System.out.println("------");
System.out.println(board[7] + "|" + board[8] + "|" + board[9] );
}
public static void updateBoardX(char[] board)
{
KeyboardReader reader = new KeyboardReader();

int pos = 0;

pos = reader.readInt("Where would you like to put your X? (1,2,3,4,5,6,7,8,9) ");
board[pos] = 'X';

}
public static void updateBoardO(char[] board)
{
KeyboardReader reader = new KeyboardReader();

int pos = 0;

pos = reader.readInt("Where would you like to put your O? (1,2,3,4,5,6,7,8,9) ");
board[pos] = 'O';
}
}
Previous post Next post
Up