The Tic Tac Toe Game in Java Code



This is a small program that shows how to create a TicTacToe game in Java.
The purpose of the game is to get three in a row before your opponent does, either vertically, horizontally or diagonally.

The TicTacToe program is made up of two Java classes. One is called TicTacToe and that is the actual game class that contains much of the logic for the game.
The other class called Main is the class that starts the game, displays the rules and loops for as many times as there are moves by the users.

The Main class looks like this:

Below is the code for the TicTacToe class:

This is what a game could look like in the console when played:







Search for more Java info on this site here:
Custom Search


5 thoughts on “The Tic Tac Toe Game in Java Code

  1. when we compile Main.java we arre getting following errors.

    C:\JavaGames>javac Main.java
    Main.java:11: error: cannot find symbol
    TicTacToe game = new TicTacToe();
    ^
    symbol: class TicTacToe
    location: class Main
    Main.java:11: error: cannot find symbol
    TicTacToe game = new TicTacToe();
    ^
    symbol: class TicTacToe
    location: class Main
    2 errors

    We were successfully able to compile TicTacToe.java file but when we compile Main.java that is when we gets above error. We just copied your above code and created two java file, Main.java and TicTacToe.java. No changes made in any of files. Do we suppose to?

    Can you please help.

    Thanks
    Fly

      • after compiling both d file at d same time we got d same error
        Main.java:11: error: cannot find symbol
        TicTacToe game = new TicTacToe();
        ^
        symbol: class TicTacToe
        location: class Main
        Main.java:11: error: cannot find symbol
        TicTacToe game = new TicTacToe();
        ^
        symbol: class TicTacToe

        • That is strange, I checked it again myself and it works. Maybe there are differences between versions of the compiler. Try to specify TicTacToe.java as first argument to javac and Main.java as second. Or try to compile them one at a time starting with TicTacToe.java

          As you can see from the error message, the main class is dependant on the TicTacToe.class being present i.e. compiled first.

Please type any questions here.