import java.applet.*; import java.awt.*; public class TicTacToe extends Applet { private int[][] moves; private int turn; private boolean endGame; public void init() { turn = 1; moves = new int[3][3]; endGame = false; } public void start() { turn = 1; endGame = false; for( int i=0; i<3; i++ ) for( int j=0; j<3; j++ ) moves[i][j] = 0; } public void stop() { start(); } private int whichTile( int x, int maxX ) { if( x < maxX / 3 ) return 0; else if( x < 2 * maxX / 3 ) return 1; else return 2; } private boolean checkEndGame() { // if no remaining squares, end of game endGame = true; for( int i=0; i<3; i++ ) for( int j=0; j<3; j++ ) if( moves[i][j] == 0 ) endGame = false; for( int i=0; i<3; i++ ) { if( moves[i][0] == moves[i][1] && moves[i][0] == moves[i][2] && moves[i][0] > 0 ) { moves[i][0] += 2; moves[i][1] += 2; moves[i][2] += 2; endGame = true; } if( moves[0][i] == moves[1][i] && moves[0][i] == moves[2][i] && moves[0][i] > 0 ) { moves[0][i] += 2; moves[1][i] += 2; moves[2][i] += 2; endGame = true; } } if( moves[0][0] == moves[1][1] && moves[0][0] == moves[2][2] && moves[0][0] > 0 ) { moves[0][0] += 2; moves[1][1] += 2; moves[2][2] += 2; endGame = true; } if( moves[0][2] == moves[1][1] && moves[0][2] == moves[2][0] && moves[0][2] > 0 ) { moves[0][2] += 2; moves[1][1] += 2; moves[2][0] += 2; endGame = true; } return endGame; } public boolean mouseDown( Event e, int x, int y ) { Dimension d = size(); if( endGame ) { start(); repaint(); return true; } int tileX = whichTile(x,d.width); int tileY = whichTile(y,d.height); if( moves[tileY][tileX] == 0 ) { moves[tileY][tileX] = turn; turn = (turn%2)+1; drawOneTile( getGraphics(), tileY, tileX ); } else { // error, flash the screen repaint(); } // is this the end of the game? if( checkEndGame() ) repaint(); return true; } private Color myColor( int turn ) { if( turn == 0 ) return Color.black; else if( turn == 1 || turn == 3 ) return Color.red; else if( turn == 2 || turn == 4 ) return Color.green; else return Color.black; } private void drawOneTile( Graphics g, int i, int j ) { Dimension d = size(); // draw X or O if( moves[i][j] == 1 ) { g.setColor( myColor(moves[i][j]) ); g.drawLine( j*d.width/3, i*d.height/3, (j+1)*d.width/3, (i+1)*d.height/3 ); g.drawLine( j*d.width/3, (i+1)*d.height/3, (j+1)*d.width/3, i*d.height/3 ); } else if( moves[i][j] == 3 ) { g.setColor( myColor(moves[i][j]) ); g.fillRect( j*d.width/3, i*d.height/3, d.width/3, d.height/3 ); g.setColor( Color.black ); g.drawLine( j*d.width/3, i*d.height/3, (j+1)*d.width/3, (i+1)*d.height/3 ); g.drawLine( j*d.width/3, (i+1)*d.height/3, (j+1)*d.width/3, i*d.height/3 ); } else if( moves[i][j] == 2 ) { g.setColor( myColor(moves[i][j]) ); g.drawOval( j*d.width/3, i*d.height/3, d.width/3, d.height/3 ); } else if( moves[i][j] == 4 ) { g.setColor( myColor(moves[i][j]) ); g.fillOval( j*d.width/3, i*d.height/3, d.width/3, d.height/3 ); } } public void paint( Graphics g ) { Dimension d = size(); g.setColor( Color.black ); g.fillRect( 0, 0, d.width, d.height ); // draw red/green/black squares of the board // black squares are "unused" // red is for player "X" // green is for player "O" for( int i=0; i<3; i++ ) { for( int j=0; j<3; j++ ) { drawOneTile(g, i, j); } } // draw white separating lines between the tiles for( int i=1; i<3; i++ ) { g.setColor( Color.white ); g.drawLine( i*d.width/3, 0, i*d.width/3, d.height ); g.drawLine( 0, i*d.height/3, d.width, i*d.height/3 ); } } }