// Scott DeRuiter 7/24/2002 // TicTacToe.java // Play the game of Tictactoe public class TicTacToe { private char [][] board = { { '1', '2', '3' }, { '4', '5', '6' }, { '7', '8', '9' } }; private boolean player1turn, winner, done; private char choice; public TicTacToe ( ) { done = winner = false; player1turn = true; } public static void main ( String [] args ) { TicTacToe t = new TicTacToe ( ); t.RunGame ( ); } public void RunGame ( ) { do { PrintBoard ( ); AskAndChange ( ); CheckForWin ( ); player1turn = !player1turn; } while ( !done ); WinOrLoseMessage ( ); } public void PrintBoard ( ) { System.out.println ( "\n\n" ); for ( int i = 0; i < board.length; i++ ) { System.out.print ( " " ); for ( int j = 0; j < board[i].length; j++ ) System.out.print ( board [i][j] + " " ); System.out.println ( "\n" ); } System.out.println ( "\n\n" ); } public void AskAndChange ( ) { TextReader console = new TextReader ( ); boolean askagain = false; do { if ( player1turn ) System.out.print ( "\n\nPlayer 1, " ); else System.out.print ( "\n\nPlayer 2, " ); System.out.print ( "enter the place for your next move (1 - 9) -> " ); choice = console.readlnChar ( ); switch ( choice ) { case '1': case '2': case '3': case '4': case '5': case '6': case '7': case '8': case '9': askagain = Change ( choice ); break; default: askagain = true; } } while ( askagain ); } public boolean Change ( char choice ) { int row = ((int)choice - 49) / 3; int col = ((int)choice - 49) % 3; if ( board [row][col] == 'X' || board [row][col] == 'O' ) return true; else if ( player1turn ) board [row][col] = 'X'; else board [row][col] = 'O'; return false; } public void CheckForWin ( ) { char gamepiece = 'O'; if ( player1turn ) gamepiece = 'X'; CheckForRowWin ( gamepiece ); CheckForColWin ( gamepiece ); CheckForDiagWin ( gamepiece ); CheckForCatsGame ( ); } public void CheckForRowWin ( char gamepiece ) { int count = 0; for (int i = 0; i <= 2; i++) { count = 0; for (int j = 0; j <= 2; j++) if ( board[i][j] == gamepiece ) count++; if ( count == 3 ) { done = true; winner = true; } } } public void CheckForColWin ( char gamepiece ) { int count = 0; for (int j = 0; j <= 2; j++) { count = 0; for (int i = 0; i <= 2; i++) if ( board[i][j] == gamepiece ) count++; if ( count == 3 ) { done = true; winner = true; } } } public void CheckForDiagWin ( char gamepiece ) { if ( board[0][0] == board[1][1] && board[1][1] == board[2][2] ) { done = true; winner = true; } if ( board[0][2] == board[1][1] && board[1][1] == board[2][0] ) { done = true; winner = true; } } public void CheckForCatsGame ( ) { int count = 0; for (int i = 0; i <= 2; i++) for (int j = 0; j <= 2; j++) if ( board[i][j] == 'X' || board[i][j] == 'O' ) count++; if ( count == 9 ) done = true; } public void WinOrLoseMessage ( ) { player1turn = !player1turn; PrintBoard ( ); if ( !winner ) System.out.println ( "\n\nIt was a cat's game" ); else if (player1turn && winner) System.out.println ( "\n\nPLAYER 1 IS THE WINNER!!" ); else if (!player1turn && winner) System.out.println ( "\n\nPLAYER 2 IS THE WINNER!!" ); System.out.println ( "\n\n" ); } }