How to describe a state with three items

  • A+
Category:Languages

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?

 


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.

Comment

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: