7

I am creating the board game Tic Tac Toe in Java.

A cell will have three states: empty, X or O.

What is the best practice for representing this in Java? Should I create its own Cell class or just use integers (0/1/2) to represent the three states? If it had two states then I could use for example boolean to represent the two states, is there a similar already defined class for something with three states?

yoozer8
  • 7,361
  • 7
  • 58
  • 93
Alex
  • 627
  • 3
  • 12
  • 32
  • Note that what you're calling a cell is really just _the possible content values of a cell_. A cell is a location on the board, for TTT you have 9 cells. Naming matters, especially for code readability purposes. – Flater Nov 27 '18 at 13:14
  • 1
    `Bool` [of course](http://thedailywtf.com/articles/What_Is_Truth_0x3f_) ;-) – Emil Vikström Nov 27 '18 at 13:23

6 Answers6

11

I would use an enum for this:

enum CellState {
    EMPTY,
    X,
    O
}

And then in your code:

public static void main(String[] args) {
    CellState[][] cellStates = new CellState[3][3];
    cellStates[0][0] = CellState.X;

    // Do other stuff

}

I just defined the board structure as CellState[][] as example but this can be whatever.

Mark
  • 5,089
  • 2
  • 20
  • 31
3

About the most important thing when using an OO language is that objects model behaviour and, where possible, contain the data required to implement the behaviour. Behaviour is what the objects do, not what is done to them.

So unless there is a reason to in a requirement you haven't stated, the cell itself doesn't have any behaviour, it is just a place that the players mark.

So you could have a simple array of marks that both players update, with an enum with three values, or you could have each player update their own data of the marks they have made, in which case each player would have either a boolean array or a short bit mask to indicate their goes. In the latter case, each player then only changes the state of their own 'goes' and can implement the test for their winning condition rather than having shared state - the only sharing is they have to ask the other player whether the chosen cell is valid. It depends how strictly OO you want your design to be as to whether this is 'best practice' or not - for such a simple problem you could write in COBOL and the users would be as happy.

Lino
  • 19,604
  • 6
  • 47
  • 65
Pete Kirkham
  • 48,893
  • 5
  • 92
  • 171
  • As you said, in a properly-designed class, how the class stores the state of each cell should not matter, and should not be visible to, anything outside the class. – Jeff Dege Nov 27 '18 at 13:39
2

I would use an enum :

public enum CellState {
    EMPTY,
    X,
    O;
}

And a Cell class that has a field of type CellState

TheWildHealer
  • 1,546
  • 1
  • 15
  • 26
2

You could use an enum which contains the three values, like:

public enum CellState {
    EMPTY,
    X,
    O
}

And use it like in a way like this:

board.setCell(cellposition, CellState.X);
Sven Hakvoort
  • 3,543
  • 2
  • 17
  • 34
2

There are multiple approaches but in this case I prefer using an enum to represent your state.

public enum State {
    EMPTY,
    X,
    O
}

And then your cell class would look something like this.

public class Cell {
    private State state;

    public Cell(State state) {
        this.state = state;
    }

    public State getState {
        return state;
    }

    public void setState(State state) {
        this.state = state;
    }

}
David Baak
  • 944
  • 1
  • 14
  • 22
-8

The other way is just to use Boolean object and to use null as third state.

Boolean state = null; // => empty state
state = Boolean.TRUE // => X state
state = Boolean.FALSE // => O state
Tony
  • 72
  • 1
  • 8
  • 2
    Using nulls for anything other than faliure cases is a [bad idea](https://stackoverflow.com/questions/2707322/what-is-null-in-java). – Tejas Kale Nov 27 '18 at 13:30
  • Also `Boolean state;` is wrong, its not "empty" but rather uninitialized – Lino Nov 27 '18 at 17:20
  • It depends on the context like everything in the programming world. If he wants something quick, without having to add additional classes, enums, etc., which will bring additional logic to handle them and tests, he can just use this approach for his game. – Tony Nov 28 '18 at 16:53