Tic tac toe game in java – download source code

Hello everyone, and welcome to this coding blog. I'll show you how to make a
tic tac toe game in Java in this post. I'll also provide you access to the
source code.

Tic tac toe game in java langugae

Tic toe game in java

Tic tac toe, as we all know, has nine locations where you can put a 'X' and an'O'.
So, to preserve the status of these places, we can employ an array of size 9.
We know that if three 'X's are joined in a line, the player who made the final move with a 'X' is the winner.

We must output the current status of the game after each turn.
We can do this by displaying a board like the one below or by creating a graphical user interface (GUI).
I'm going to make a cmd (command line) game. you can use swing's graphics api to build an UI.

            |   |
        -------------
            |   |
        -------------
            |   |
            

A two-player game will be implemented.
The player with the 'X' will make the first move. We'll do some checks with each move to see if it's a win or a tie. I have previously written a post on tic tac toe win detection. You should read it to gain a better understanding.

download the source code from here:
Tic tac toe java source code.

We have two files in the src folder: Game.java and TTT.java.
These two define the Game and TTT classes, respectively.
The TTT class acts as a controller, while the Game class manages the game's state.

Methods declared in TTT class are-

  run()
  showMSG()
  processUserCMD()
  processUserCMD(String cmd)
  rename()
  start()
  restart()
  
  and one static main() to 
  instantiate TTT object and call the 
  run()
  

And methods inside Game class

  processGame(String cmd)
  drawBoard()
  update()
  check()
  isFinshed()
  setName()
  

TTT class takes commands in an infinite loop from the command-line interface
(console). For this, Two overloaded methods are responsible for taking and
processing the command as shown below.

   private void processUserCMD() {
        while (true) {
            String cmd = scanner.nextLine();
            // remove leading and trailing spaces, if any exists.
            cmd = cmd.trim();
            processUserCMD(cmd);
        }
    }
    
   
    private void processUserCMD(String cmd) {
        switch (cmd) {
            case "start":
                start(); break;
            case "restart":
                restart(); break;
            case "rename":
                rename(); break;
            case "exit":
                System.out.println("Exited from the game!");
                System.exit(0);
            default:
                if (game != null) {
                    game.processGame(cmd);
                    if (game.isFinished()) {
                        game = null;
                    }
                }
        }
    }
    

In the main method, an object of TTT is instantiated and we call the run()
method. run() method subsequently calls the showMSG() and processUserCMD().
showMSG() print out all the available commands to the console.
processUserCMD() method takes and process the commands in a loop.

Available commands are rename (to rename the name of players), start (To
start a new game), restart( to restart the game) and exit ( to exit from the
program).

Commands are processed in a switch case block and each command calls its
corresponding method. In the default case, we check if the game is live if
it is then we process the game. This design is very simple you can easily
understand by taking a look at the code.

Share this Post

Leave a Reply

Your email address will not be published. Required fields are marked *