// 6/30/10 M. Brenner public class Board extends Observable { static public final int EMPTY = 0, CROSS = 1, CIRCLE = 2; static private final int COLS = 3, ROWS = 3; static private Board instance_; private int board_[][]; static public Board getinstance() { if (instance_ == null) { instance_ = new Board(); } return instance_; } private Board() { board_ = new int[ROWS][COLS]; } public void clear() { int col, row; for (row = 0; row < ROWS; row++) { for (col = 0; col < COLS; col++) { board_[row][col] = EMPTY; } } changed(); } private void changed() { setChanged(); notifyObservers(); } public int getelement (int row, int col) { return board_[row][col]; } public boolean isempty (int row, int col) { return board_[row][col] == EMPTY; } public void mark (int row, int col, int symbol) { if (board_[row][col] == EMPTY) { board_[row][col] = symbol; changed(); } } public void replay() { clear(); } public String toString() { char chr; int col, row; StringBuffer text; text = new StringBuffer ("Board\n "); for (row = 0; row < ROWS; row++) { for (col = 0; col < COLS; col++) { chr = '_'; switch (board_[row][col]) { case CROSS: chr = 'X'; break; case CIRCLE: chr = 'O'; break; } text.append (" " + chr + " "); } text.append ("\n "); } return text.toString(); } }