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

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).

主站蜘蛛池模板: 樟树市| 陆丰市| 方城县| 驻马店市| 林甸县| 广饶县| 永福县| 瑞丽市| 体育| 康平县| 策勒县| 金塔县| 乌兰县| 紫阳县| 蒙阴县| 胶州市| 邮箱| 永登县| 儋州市| 嵩明县| 大连市| 理塘县| 嵊州市| 维西| 永昌县| 大理市| 浮梁县| 文安县| 新源县| 胶州市| 赫章县| 成都市| 黄陵县| 乾安县| 广元市| 高邮市| 洛宁县| 合阳县| 肇源县| 平湖市| 勐海县|