Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to describe a state with three items

Tags:

java

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?

like image 349
Alex Avatar asked Nov 27 '18 11:11

Alex


People also ask

What does 3 state mean in electronics?

Three-state logic From Wikipedia, the free encyclopedia In digital electronics three-state, tri-state, or 3-state logic allows an output or input pin/pad to assume a high impedance state, effectively removing the output from the circuit, in addition to the 0 and 1 logic levels.

What three objects describe me?

The three of my closest objects that describe me are my iPod, my trunk and my Rubik’s Cube. The first object of mine that I would use to define me would be my iPod. As we all know, an iPod contains music that can lighten up different situations and people.

How can you classify the three states of matter?

Matter can be classified as solid, liquid and gas on the basis of inter-molecular forces and the arrangement of particles. These three forms of matter can be converted from one state of matter to another state by increasing or decreasing pressure and temperature. For example, Ice can be converted from solid state to liquid state by...

What is 3-state logic?

] In digital electronics three-state, tri-state, or 3-state logic allows an output port to assume a high impedance state, effectively removing the output from the circuit, in addition to the 0 and 1 logic levels .


Video Answer


1 Answers

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.

like image 185
Mark Avatar answered Oct 22 '22 13:10

Mark