01: /**
02:    A 3 x 3 tic-tac-toe board.
03: */
04: public class TicTacToe
05: {
06:    /**
07:       Constructs an empty board.
08:    */
09:    public TicTacToe()
10:    {
11:       board = new String[ROWS][COLUMNS];
12:       // Fill with spaces
13:       for (int i = 0; i < ROWS; i++)
14:          for (int j = 0; j < COLUMNS; j++)
15:             board[i][j] = " ";
16:    }
17: 
18:    /**
19:       Sets a field in the board. The field must be unoccupied.
20:       @param i the row index 
21:       @param j the column index 
22:       @param player the player ("x" or "o")
23:    */
24:    public void set(int i, int j, String player)
25:    {
26:       if (board[i][j].equals(" "))
27:          board[i][j] = player;
28:    }
29: 
30:    /**
31:       Creates a string representation of the board, such as
32:       |x  o|
33:       |  x |
34:       |   o|
35:       @return the string representation
36:    */
37:    public String toString()
38:    {
39:       String r = "";
40:       for (int i = 0; i < ROWS; i++)
41:       {
42:          r = r + "|";
43:          for (int j = 0; j < COLUMNS; j++)         
44:             r = r + board[i][j];
45:          r = r + "|\n";
46:       }
47:       return r;
48:    }
49: 
50:    private String[][] board;
51:    private static final int ROWS = 3;
52:    private static final int COLUMNS = 3;
53: }