HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Simple Tic Tac Toe game

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
simpletoetictacgame

Problem

I'm just starting to learn Java and I have written this simple Tic Tac Toe game. Please review it and give me some feedback, advice, guidance etc.

Main method:

import java.util.Scanner;

    public class Main {

public static void main(String[] args) {
        char player='o';
        int row,column;
        Scanner k =  new Scanner (System.in);
        Game g = new Game ();

        g.initializeGame();
        System.out.println("Game ready !\n");

while(true)
{   
player=g.changePlayer(player);
System.out.print("\n"+player +" ,choose your location (row, column):");
row=k.nextInt();
column=k.nextInt();

    while (g.checkIfLegal(row,column))
    {
    System.out.println("That slot is already taken or out of bounds, please try again (row, column).");
    g.displayBoard();
    row=k.nextInt();
    column=k.nextInt();
    }
g.changeBoard(player,row,column );
g.displayBoard();

if(g.checkIfWinner())
    {
    System.out.println("\nThe winner is "+ player+" !");
    break;

    }
if(g.checkIfTie())
    {
        System.out.println("\nThe game is a tie !");
        break;
    }
}

    }

    }


Game class:

```
public class Game {

char [][] table = new char[3][3];

public void initializeGame()
{
for (int i = 0; i 2 || column>2) || (row<0 || column <0) )
return true;

else if(table[row][column]=='x' || table[row][column]=='o')
return true;

return false;
}

public void changeBoard(char player, int row, int column)
{
table[row][column]=player;

}

public void displayBoard()
{

System.out.println(" 0 " + table[0][0] + "|" + table[0][1] + "|" + table[0][2]);
System.out.println(" --+-+--");
System.out.println(" 1 " + table[1][0] + "|" + table[1][1] + "|" + table[1][2]);
System.out.println(" --+-+--");
System.out.println(" 2 " + table[2][0] + "|" + table[2][1] + "|" + table[2][2]);
System.out.println(" 0 1 2 ");
}

public char changePlayer(char player) {
char newTurn='e';
if (player

Solution

Avoid magic symbols

The character literals 'x', 'o', ' ' appear many times throughout the code.
It would be better to put these in constants, for example:

static final char EMPTY = ' ';
static final char PLAYER_X = 'x';
static final char PLAYER_O = 'o';


Simplify changePlayer

In changePlayer, the newTurn variable is pointless.
The player is either 'o' or 'x', so you can simplify to:

public char changePlayer(char player) {
    return player == 'o' ? 'x' : 'o';
}


Simplify checkIfLegal

The parentheses are unnecessary in the boundary check condition.

And instead of checking if a cell is 'x' or 'o',
it would be simpler to check if it's ' '.

Like this:

public boolean checkIfLegal(int row, int column) {
    if (row > 2 || column > 2 || row < 0 || column < 0) {
        return true;
    } else if (table[row][column] != ' ') {
        return true;
    }

    return false;
}


Not intuitive checkIfLegal

The return value of checkIfLegal is kind of counter-intuitive.
Judging by the name,
my first thought is to return true if legal.
But that's not the case.
A more natural name would be isIllegal.

Formatting

Your code, nicely formatted, would look below.
I simply used my IDE to auto-reformat. You can do this too, next time.

```
public class Game {

char[][] table = new char[3][3];

public void initializeGame() {
for (int i = 0; i 2 || column > 2) || (row < 0 || column < 0)) {
return true;
} else if (table[row][column] == 'x' || table[row][column] == 'o') {
return true;
}

return false;
}

public void changeBoard(char player, int row, int column) {
table[row][column] = player;

}

public void displayBoard() {

System.out.println(" 0 " + table[0][0] + "|" + table[0][1] + "|" + table[0][2]);
System.out.println(" --+-+--");
System.out.println(" 1 " + table[1][0] + "|" + table[1][1] + "|" + table[1][2]);
System.out.println(" --+-+--");
System.out.println(" 2 " + table[2][0] + "|" + table[2][1] + "|" + table[2][2]);
System.out.println(" 0 1 2 ");
}

public char changePlayer(char player) {
char newTurn = 'e';
if (player == 'o') {
newTurn = 'x';
}
if (player == 'x') {
newTurn = 'o';
}
return newTurn;
}

public boolean checkIfWinner() {
if (table[0][0] == table[1][0] && table[1][0] == table[2][0] && (table[0][0] == 'x' || table[0][0] == 'o')) {
return true;
} else if (table[0][1] == table[1][1] && table[1][1] == table[2][1] && (table[0][1] == 'x' || table[0][1] == 'o')) {
return true;
} else if (table[0][2] == table[1][2] && table[1][2] == table[2][2] && (table[0][2] == 'x' || table[0][2] == 'o')) {
return true;
} else if (table[0][0] == table[0][1] && table[0][1] == table[0][2] && (table[0][0] == 'x' || table[0][0] == 'o')) {
return true;
} else if (table[1][0] == table[1][1] && table[1][1] == table[1][2] && (table[1][0] == 'x' || table[1][0] == 'o')) {
return true;
} else if (table[2][0] == table[2][1] && table[2][1] == table[2][2] && (table[2][0] == 'x' || table[2][0] == 'o')) {
return true;
} else if (table[0][0] == table[1][1] && table[1][1] == table[2][2] && (table[0][0] == 'x' || table[0][0] == 'o')) {
return true;
} else if (table[2][0] == table[1][1] && table[1][1] == table[0][2] && (table[2][0] == 'x' || table[2][0] == 'o')) {
return true;
} else {
return false;
}
}

public boolean checkIfTie() {
for (int i = 0; i < 3; i++) {
for (int p = 0; p < 3; p++) {
if (table[i][p] == ' ') {
return false;
}
}
}

return true;
}

}

public class Main {

public static void main(String[] args) {
char player = 'o';
int row, column;
Scanner k = new Scanner(System.in);
Game g = new Game();

g.initializeGame();
System.out.println("Game ready !\n");

while (true) {
player = g.changePlayer(player);
System.out.print("\n" + player + " ,choose your location (row, column):");
row = k.nextInt();
column = k.nextInt();

while (g.checkIfLegal(row, column)) {
System.out.println("That slot is already taken or out of bounds, please try again (row, column).");
g.displayBoard();
row = k.nextInt();
column = k.nextInt();
}
g.changeBoard(player, row, column);
g.displayBoard();

if (g.checkIfWinner()) {
System.out.println("\nThe winner is " + player + " !");
break;

}
if (g.checkI

Code Snippets

static final char EMPTY = ' ';
static final char PLAYER_X = 'x';
static final char PLAYER_O = 'o';
public char changePlayer(char player) {
    return player == 'o' ? 'x' : 'o';
}
public boolean checkIfLegal(int row, int column) {
    if (row > 2 || column > 2 || row < 0 || column < 0) {
        return true;
    } else if (table[row][column] != ' ') {
        return true;
    }

    return false;
}
public class Game {

    char[][] table = new char[3][3];

    public void initializeGame() {
        for (int i = 0; i < 3; i++) {
            for (int p = 0; p < 3; p++) {
                table[i][p] = ' ';
            }
        }
    }


    public boolean checkIfLegal(int row, int column) {
        if ((row > 2 || column > 2) || (row < 0 || column < 0)) {
            return true;
        } else if (table[row][column] == 'x' || table[row][column] == 'o') {
            return true;
        }

        return false;
    }


    public void changeBoard(char player, int row, int column) {
        table[row][column] = player;

    }


    public void displayBoard() {

        System.out.println("  0  " + table[0][0] + "|" + table[0][1] + "|" + table[0][2]);
        System.out.println("    --+-+--");
        System.out.println("  1  " + table[1][0] + "|" + table[1][1] + "|" + table[1][2]);
        System.out.println("    --+-+--");
        System.out.println("  2  " + table[2][0] + "|" + table[2][1] + "|" + table[2][2]);
        System.out.println("     0 1 2 ");
    }


    public char changePlayer(char player) {
        char newTurn = 'e';
        if (player == 'o') {
            newTurn = 'x';
        }
        if (player == 'x') {
            newTurn = 'o';
        }
        return newTurn;
    }


    public boolean checkIfWinner() {
        if (table[0][0] == table[1][0] && table[1][0] == table[2][0] && (table[0][0] == 'x' || table[0][0] == 'o')) {
            return true;
        } else if (table[0][1] == table[1][1] && table[1][1] == table[2][1] && (table[0][1] == 'x' || table[0][1] == 'o')) {
            return true;
        } else if (table[0][2] == table[1][2] && table[1][2] == table[2][2] && (table[0][2] == 'x' || table[0][2] == 'o')) {
            return true;
        } else if (table[0][0] == table[0][1] && table[0][1] == table[0][2] && (table[0][0] == 'x' || table[0][0] == 'o')) {
            return true;
        } else if (table[1][0] == table[1][1] && table[1][1] == table[1][2] && (table[1][0] == 'x' || table[1][0] == 'o')) {
            return true;
        } else if (table[2][0] == table[2][1] && table[2][1] == table[2][2] && (table[2][0] == 'x' || table[2][0] == 'o')) {
            return true;
        } else if (table[0][0] == table[1][1] && table[1][1] == table[2][2] && (table[0][0] == 'x' || table[0][0] == 'o')) {
            return true;
        } else if (table[2][0] == table[1][1] && table[1][1] == table[0][2] && (table[2][0] == 'x' || table[2][0] == 'o')) {
            return true;
        } else {
            return false;
        }
    }


    public boolean checkIfTie() {
        for (int i = 0; i < 3; i++) {
            for (int p = 0; p < 3; p++) {
                if (table[i][p] == ' ') {
                    return false;
                }
            }
        }

        return true;
    }


}


public class Main {

    public static void main(String[] args) {
        char player = 'o';
        int row, column;
     

Context

StackExchange Code Review Q#95876, answer score: 7

Revisions (0)

No revisions yet.