官术网_书友最值得收藏!

Implementation

To implement the last test, we should store the location of the placed pieces in an array. Every time a new piece is placed, we should verify that the place is not occupied, or else throw an exception:

private Character[][] board = {
{'\0', '\0', '\0'}, {'\0', '\0', '\0'},
{'\0', '\0', '\0'}
}; public void play(int x, int y) { if (x < 1 || x > 3) { throw new RuntimeException("X is outside board"); } else if (y < 1 || y > 3) { throw new RuntimeException("Y is outside board"); } if (board[x - 1][y - 1] != '\0') { throw new RuntimeException("Box is occupied"); } else { board[x - 1][y - 1] = 'X'; } }

We're checking whether a place that was played is occupied and, if it is not, we're changing the array entry value from empty (\0) to occupied (X). Keep in mind that we're still not storing who played (X or O).

主站蜘蛛池模板: 唐河县| 噶尔县| 封开县| 苍南县| 武邑县| 苏尼特右旗| 宁津县| 邓州市| 阿克苏市| 资阳市| 团风县| 黄山市| 新源县| 马山县| 濉溪县| 赫章县| 都安| 京山县| 泽州县| 治县。| 手游| 大厂| 根河市| 井陉县| 惠安县| 德钦县| 阿拉尔市| 长春市| 郯城县| 兴安县| 枝江市| 桃源县| 洛隆县| 伊金霍洛旗| 广昌县| 永川市| 个旧市| 连南| 璧山县| 贺州市| 广饶县|