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

Implementation

To fulfill this test, we need to check whether any horizontal line is filled by the same mark as the current player. Until this moment, we didn't care what was put on the board array. Now, we need to introduce not only which board boxes are empty, but also which player played them:

public String play(int x, int y) {
  checkAxis(x);
  checkAxis(y);
  lastPlayer = nextPlayer();
  setBox(x, y, lastPlayer);
  for (int index = 0; index < 3; index++) {
    if (board[0][index] == lastPlayer
&& board[1][index] == lastPlayer
&& board[2][index] == lastPlayer) { return lastPlayer + " is the winner"; } } return "No winner"; }
private void setBox(int x, int y, char lastPlayer) { if (board[x - 1][y - 1] != '\0') { throw new RuntimeException("Box is occupied"); } else { board[x - 1][y - 1] = lastPlayer; } }
主站蜘蛛池模板: 德庆县| 晋城| 潍坊市| 富民县| 靖西县| 百色市| 永安市| 黔江区| 白银市| 香格里拉县| 岳阳县| 万源市| 靖西县| 永善县| 湄潭县| 红河县| 长岭县| 新竹市| 祁阳县| 松潘县| 鄂伦春自治旗| 江孜县| 颍上县| 武冈市| 眉山市| 象山县| 章丘市| 牡丹江市| 大邑县| 措勤县| 台中县| 克山县| 旬阳县| 灌云县| 乌什县| 汉源县| 乾安县| 隆子县| 休宁县| 吉木萨尔县| 温泉县|