New:

Mike's Java TicTacToe Page


Suppose you wanted a tic tac toe game. And suppose you wanted a nice GUI interface, with colorful X's and O's. Now suppose you wanted to be able to play the game over the network.

And now, suppose you could do all that with less than 200 lines of code, and compile it into a program less than 7kb in size. Does that sound impossible? Enter the java platform! The file ttt.zip (7012 bytes) will take one (1) second to download at 56kbps. It contains the full source code and compiled class files for my Network TicTacToe.

  • System Requirements you need to have java runtime environment v1.2 or higher installed. Some trivial modifications (which I regret not making) would make it work with JRE 1.1, that is, on almost any machine on the planet.
  • To install download ttt.zip and unzip it into a directory of your choice. Make sure you have the java runtime environment v1.2 or higher properly installed. The JRE is available from Sun's website.
  • To run type java TicTacToe from the command prompt (from the same directory where you unzipped ttt.zip). This will run the game in Server mode. It will wait for a connection from another game. To connect to an already running game, type java TicTacToe host, where host is the IP address or hostname of the computer where you ran the server version. Then, play!
  • To run on one machine try using 127.0.0.1 or localhost as the host.
  • To play click in the squares. Enjoy!
This is a very simple implementation of Tic Tac Toe. It doesn't enforce any of the rules of the game, except that players take turns to put their symbol (X) or (O) in the squares on the board.
  • How it works. There are 4 java files that make up the game.
  • TicTacGame.java defines a class that encapsulates the position of the tic tac toe game. It includes methods to change the board, to see what is on the board, and to clear the board. Most importantly, it implements Serializable, which allows TicTacGame objects to be passed easily from program to program through the network.
    • Suggestions: add a method to check if the position is an impossible one (too many X's compared with O's, for example, or both X and O have 3 in a row. Add a method to check if X or O have one. Modify the given methods to check if the move being attempted is legal, either returning false if not, or throwing an Exception.
  • TicTacPanel.java defines a class that encapsulates the visible game board. It has a TicTacGame instance object, and uses the information in that object to determine how it should draw the screen. It also stores the player's symbol (X or O) as a char.
    • Suggestion: invent new ways to draw the X's and O's. Modify this file (and others as needed) to allow different sized boards.
  • TicTacToe.java defines the actual game program. It creates a Frame, then creates a TicTacPanel and adds it to the Frame. It also creates a cler button and adds it to the TicTacPanel. Then, depending on the number of arguments, it either creates a ServerSocket object, and obtains a socket by calling accept() on the ServerSocket, or if there is a command line argument, creates a Socket object directly by connecting to the given host. Once the Socket object is obtained, the program enters an infinite loop. If I got the Socket from a ServerSocket, I enter the loop at step 2, otherwise I enter at step 3. 1) Wait until the player has moved (exception - if the Socket has only just been obtained from the ServerSocket, don't wait). 2) get the OutputStream from the Socket, and create from it an ObjectOutputStream. Then, use writeObject(Object) on the ObjectOutputStream to send the TicTacGame over the network. 3) Get an InputStream from the Socket, and create from it an ObjectInputStream, and use readObject() on the ObjectInputStream, casting the object obtained to a TicTacGame, to replace the old TicTacGame object in the TicTacPanel. 4) Go to step 1
    • Suggestions: Put in some error checking in case of network failure, and catch Exceptions more gracefully. Expand the protocol to allow players to quit more gracefully or send messages to each other or decide between them who goes first.
  • TicTacAction.java defines a class that handles all the user input, whether from the mouse, or from the Clear button. As such, it implements ActionListener, MouseListener. I should have also made it implement WindowListener, instead of making the Window Listener an anonymous class in TicTacToe.java
    • Suggestion: fix that.
This game is released under the Gnu Public License. See the GNU website fo more details. I hope it helps you learn something about Networking or GUI's in Java.