/*
    No change from version 2
*/

public class TicTacToeBoard3 {
    private final char USER_MARK    = 'X';
    private final char MACHINE_MARK = 'O';

    private char [][] board;

    public TicTacToeBoard3 (){
        board = new char [3][3];
        for (int row = 0; row < 3; row++){
            for (int col =0; col < 3; col++){
                board[row][col] = ' ';
            }
        }
    }

    public String toString (){
        String result = "";
        result += row(0);
        result += horizontal();
        result += row(1);
        result += horizontal();
        result += row(2);
        return result;
    }

    private String row(int row){
        return board[row][0] + "|" + board[row][1] + "|" + board[row][2] + "\n";
    }

    private String horizontal (){
        return "------\n";
    }

    public void mark_cell(int row, int col, char mark) throws IllegalArgumentException {
        move_good(row, col, mark);
        board[row][col] = mark;
    }

    private void move_good(int row, int col, char mark) throws IllegalArgumentException {
        if (row < 0 || row > 2){
            throw new IllegalArgumentException(row + " is not a valid row number");
        } else if (col < 0 || col > 2) {
            throw new IllegalArgumentException(col + " is not a valid column number");
        } else if (mark != USER_MARK && mark != MACHINE_MARK) {
            throw new IllegalArgumentException(mark + " is not a valid mark");
        } else if (board[row][col] != ' ') {
            throw new IllegalArgumentException("The square " + row + "," + col + " is occupied");
        }
    }
}
