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

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

主站蜘蛛池模板: 犍为县| 曲阳县| 逊克县| 福州市| 偏关县| 水富县| 瓦房店市| 长治县| 德庆县| 永安市| 大竹县| 塔河县| 博兴县| 荣昌县| 瑞丽市| 鄯善县| 霍邱县| 西平县| 平和县| 米易县| 张家口市| 上高县| 萍乡市| 怀集县| 阜南县| 靖安县| 海兴县| 托克逊县| 洛浦县| 自贡市| 睢宁县| 泽州县| 武功县| 宁强县| 乳山市| 利辛县| 旅游| 涞水县| 岑溪市| 兖州市| 泉州市|