我收到了一个要解决的使用Enums的项目,我有些困惑。这是一个井字游戏,其枚举将3x3网格表中的单元格位置存储在其中,而枚举则存储了单元格的状态(空,X或O)。另外,我有一个GameBoard接口,它返回CellStategetCellState (CellLocation cellLocation);

我的任务是仅编写Consultant接口,并且无法更改提供给我的代码中的任何内容。
我在每个步骤之前都在努力检查Board的状态。

我的想法是X开始游戏,我在步骤中使用for循环,并在每一步中检查if nrOfSteps%2==0。如果是,我将cellState设置为玩家O。检查我们是否处于获胜位置,我们是否获胜或是否有平局。如果没有,我建议采取行动。
例如。

if (nrOfSteps%2!=0){
    cState =CellState.OCCUPIED_BY_X;
      if (isWon(cState, board)){
        throw new IllegalStateException("Player X won!");
      } else if (draw(board, locations)){
        throw new IllegalStateException("No more empty fields!");
      } else
        for (int remainingCells = 0; remainingCells <9; remainingCells++) {
            if (board.equals(locations[remainingCells]) &&
            cState==CellState.EMPTY){
            availableCells[index] = locations[remainingCells];
            index++;
        }
        CellLocation cellToTake = availableCells[(int)(Math.random()*
        (availableCells.length-1))];
        return cellToTake;
}


现在,我的问题是我已经尝试对isWon方法进行以下操作(部分代码目前仅验证第一行):

private boolean isWon(CellState player, GameBoard board){
if (board.equals(CellLocation.TOP_LEFT) && cState==player &&
    board.equals(CellLocation.TOP_CENTRE) && cState==player &&
    board.equals(CellLocation.TOP_RIGHT) && cState==player){
  return true;
}

return false;
}


但是我已经意识到,正如我在上面的代码中检查的那样,电路板的当前状态不能仅等于一个单元。并且显然不能等于3个不同的“仅一个单元” -s。而且我什至不确定是否可以通过以下方式检查棋盘上是否只有一个单元格被玩家X占用:

board.equals (CellLocation.TOP_LEFT) && cState==player


有人可以提示我如何将CellStateCellLocation合并到一个查询中吗?我应该使用数组吗?例如CellState[][]

最佳答案

您可以计算矩阵中单元格的总和(针对每一列,每一行和每一条对角线)。像这样:

import java.io.IOException;
import java.util.Arrays;
import java.util.Random;

public class CrossAndZeros {
    private static CellState winner;
    private static Enum[][] field = new Enum[3][3];

    public static void main(String[] args) throws IOException {

        for (int i = 0; i < field.length; i++) {
            for (int j = 0; j < field[i].length; j++) {
                field[i][j] = CellState.values()[new Random().nextInt(3)];
            }
        }

        for (Enum[] enums : field) {
            System.out.println(Arrays.toString(enums));
        }
        System.out.println();

        System.out.println("Winner is found: " + isWinnerFound());

        System.out.println(winner == null ? "No winner, GAME OVER" : winner);
    }

    private static boolean isWinnerFound() {
        int[] result = calculate();
        int count = 0;

        for (int win : result) {
            if (win == 3) {
                winner = CellState.OCCUPIED_BY_X;
                return true;
            } else if (win == -12) {
                winner = CellState.OCCUPIED_BY_O;
                return true;
            } else if (win == -9 || win == -2 || win == -3) { // This means that the line is spoilt
                count++;
            }
        }
        return count == 8; // If all the lines are spoilt, the game is over
    }

    private static int[] calculate() {
        int[] result = new int[8];

        for (int i = 0; i < field.length; i++) {
            for (int j = 0; j < field[i].length; j++) {
            result[i] += getCellOwner(field[j][i]); // a column
            result[i + 3] += getCellOwner(field[i][j]); // a row
        }
        result[field.length * 2] += getCellOwner(field[i][i]); // diagonal
        result[field.length * 2 + 1] += getCellOwner(field[i][field.length - i - 1]); // diagonal

        }

        System.out.println(Arrays.toString(result));

        return result;
    }

    private static int getCellOwner(Enum cell) {
        switch ((CellState) cell) {
            case OCCUPIED_BY_O:
                return -4;
            case OCCUPIED_BY_X:
                return 1;
            case EMPTY:
            default:
                return 0;
        }
    }

    public enum CellState {
        /**
         * this cell is occupied by player X
         */
        OCCUPIED_BY_X,

        /**
         * this cell is occupied by player O
         */
        OCCUPIED_BY_O,

        /**
         * this cell is Empty
         */
        EMPTY
    }
}

09-11 13:02