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

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

主站蜘蛛池模板: 普安县| 威信县| 资中县| 沛县| 大厂| 汉源县| 全椒县| 基隆市| 永福县| 安图县| 桓仁| 南陵县| 治多县| 许昌县| 津南区| 栖霞市| 通辽市| 新津县| 通辽市| 湘潭县| 抚州市| 龙里县| 阿合奇县| 崇左市| 延安市| 涪陵区| 兰考县| 大方县| 石屏县| 武强县| 湖口县| 滨州市| 潢川县| 日照市| 台东市| 甘德县| 陇南市| 拉萨市| 镇安县| 简阳市| 乳山市|