How I Made My First Game TIC-TAC-TOE

My first Java game is a simple Tic-Tac-Toe game that is designed to be impossible to beat on hard difficulty.

Game concept
First, I designed class Board that implemets the A.I. for the game. It has the following priorities: look for moves to win the game > prevent the player from winning the game > take the mid tile > take the corner tiles. The last two priorities were added later in development to prevent certain strategies that resulted in the player having two winning moves.

The Board class comes with its own version of the game board that is printed out in the System.out and asks the player to input the coordinates of the tile he wants to select. However, this is somewhat awkward for the player and clunky, so I made a class that took care of the User Interface (UI). The class uses a JPanel with buttons that use an ActionListener. The two classes communicate with each other, with the UI class sending information about which buttons were pressed, while the AI class sends information about which tiles it has selected.
Originally the buttons printed out text, but I had them print out pictures to make it look better. I also added options for the player to choose if they wanted to play as “X” or “O,” if they wanted to continue after the game was over, and a difficulty option.

The “Medium” difficulty is handled by the original AI class, while the “Hard” difficulty is handled by a class that extends “Board” and changes the strategy method “move” in AI.


public class BoardHard extends Board {

 public int move(String o2) {
 if (this.board[1][1].equals(' ')) {
 this.board[1][1] = o2;
 return 4;

 // check corners 00 02 20 22
 if (checkCorner (0,0,o2)) {
    this.board[0][0] = o2;
    return 0;
 if (checkCorner (0,2,o2)) {
    this.board[0][2] = o2;
    return 2;
 if (checkCorner (2,0,o2)) {
    this.board[2][0] = o2;
    return 6;
 if (checkCorner (2,2,o2)) {
    this.board[2][2] = o2;
    return 8;
 for (int i = 0; i<3; i++)
    for (int j = 0; j<3; j++)
        if (this.board[i][j].equals(" ")) {
            this.board[i][j] = o2;
            return i*3 + j;
 return 0;

In “easy” mode, the computer selects its tile randomly. One problem I had while implementing it was that the computer would put its answer over tiles that it or the player already took, which was fixed by making sure that the tile was empty (equaled ” “) before returning a value.

Strategy Methods

The strategy methods give the AI instructions on which tile to select for its turn.

The winMove method:

public int winMove(String o2) {
 for (int i = 0; i<3; i++)
   for (int j = 0; j<3; j++)
     if (this.board[i][j] == ” “)
        if (isWinMove(i, j, o2, this.clone())) {
            this.board[i][j] = o2;
            return i*3 + j;
return -1;

It works by checking all empty spots for a move that will win it the game, returning the value of the tile that it wants. If it does not find a move that could win it the game, it sends -1, which the UI class ignores.

The winEnemyMove method:

public int winEnemyMove(String x2, String o2) {
 for (int i = 0; i < 3; i++)
    for (int j = 0; j < 3; j++)
       if (this.board[i][j] == ” “)
       if (isWinMove(i, j, x2, this.clone())) {
          this.board[i][j] = o2;
          return i*3 + j;
 return -1;

This method works by checking all empty spots for a move that could lead to the player winning, and sending the value of the tile it wants.  If it doesn’t find a way for the player to win, it sends meaningless information.

We use these methods including move() method to manipulate strategies

 * here we catch cheaters print the board and show who wins 
 if (bb.getBoardElement(i, j).equals(" ")) { 
     bb.setBoardElement(Board.X, i, j); 
     if (bb.isWin()) { 
 // Computer Lost 
        compWin = true; 
       } else { 
               int m = bb.winMove(Board.O); 
               if (m != -1) { 
               win = true; 
              } else { 
                      int mEnemyWin = bb.winEnemyMove(Board.X, Board.O); 
                     if (mEnemyWin == -1) { 
                     } else printButton(mEnemyWin); 
      if (bb.isBoardFull()) 
             win = false; 

The program was uploaded on GitHub using the following steps.

STS – Spring Tool Suite based on Eclipse
Install git plugin Help>Market Place>EGit

Create a gitHub account

Create a new repository

Clone the repository on your local computer
Be sure that you install git on your local machine.
from command line run
>git clone

Push project on
Run STS and put my java project under xo directory after that git commit and push project on gitHub


Posted on August 10, 2014, in Game, GitHub, Java and tagged . Bookmark the permalink. 1 Comment.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: