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


3 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

Any thoughts or questions?