Java 从返回枚举的接口检索数据
我已经收到了一个项目,以解决使用枚举,我有点卡住了。这是一个tic-tac-toe游戏,它的枚举存储3x3网格表中单元格的位置,枚举存储单元格的状态(空、X或O)。另外,我还有一个用于Java 从返回枚举的接口检索数据,java,enums,Java,Enums,我已经收到了一个项目,以解决使用枚举,我有点卡住了。这是一个tic-tac-toe游戏,它的枚举存储3x3网格表中单元格的位置,枚举存储单元格的状态(空、X或O)。另外,我还有一个用于GameBoard的接口,它返回CellState:getCellState(CellLocation-CellLocation) 我的任务是只编写Consultant接口,我无法更改提供给我的代码中的任何内容。 在每一步之前,我都在努力检查板的状态 我的想法是,X开始游戏,我使用for循环进行步骤,在每个步骤中,
GameBoard
的接口,它返回CellState
:getCellState(CellLocation-CellLocation)代码>
我的任务是只编写Consultant
接口,我无法更改提供给我的代码中的任何内容。
在每一步之前,我都在努力检查板的状态
我的想法是,X开始游戏,我使用for
循环进行步骤,在每个步骤中,我检查nrOfSteps%2==0
。如果是,我将cellState
设置为玩家O。检查我们是否处于获胜位置,我们是否赢了,或者是否有平局。如果没有,我将建议采取行动。
例如
但我已经意识到,在我检查上述代码时,电路板的当前状态不能仅等于一个单元。很明显,它不能等于3个不同的“只有一个细胞”——s。我甚至不确定我是否可以检查棋盘是否只有一个单元格被玩家X占用:
board.equals (CellLocation.TOP_LEFT) && cState==player
有人能告诉我如何将CellState
和CellLocation
合并到一个查询中吗?我应该使用数组吗?e、 g.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
}
}
import java.io.IOException;
导入java.util.array;
导入java.util.Random;
公共类CrossAndZero{
私人国家赢家;
私有静态枚举[]字段=新枚举[3][3];
公共静态void main(字符串[]args)引发IOException{
for(int i=0;i
单元位置枚举:
public enum CellLocation {
/** The Cell in the top row and leftmost column */
TOP_LEFT,
/** The Cell in the top row and centre column */
TOP_CENTRE,
/** The Cell in the top row and rightmost column */
TOP_RIGHT,
/** The Cell in the centre row and leftmost column */
CENTRE_LEFT,
/** The Cell in the centre row and centre column */
CENTRE_CENTRE,
/** The Cell in the centre row and rightmost column */
CENTRE_RIGHT,
/** The Cell in the bottom row and leftmost column */
BOTTOM_LEFT,
/** The Cell in the bottom row and centre column */
BOTTOM_CENTRE,
/** The Cell in the bottom row and rightmost column */
BOTTOM_RIGHT;
}
CellState枚举:
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;
}谢谢你的帮助。我已经尝试了你的代码,并对其进行了一些修改,因为我必须编写的接口将有一个返回null的“publiccelllocation suggest(GameBoard-GameBoard)”方法。所以它没有一个main方法,而为我预先编写的JUnit测试就是针对这个方法的。我将不得不从这里委派任务。但这样我就无法将for循环中的枚举转换为cellLocation,从而只返回一个位置。它显示了getCellOwner方法的一个错误,表示无法将其解析为变量。您可以显示您的CellState和CellLocation类吗?我将其作为aswer发布,因为它不允许我将其发布到注释中。谢谢。我已经更改了答案。谢谢,对不起,你的回答很有帮助,但我没有办法完成作业或在这里回答。再次感谢。
public enum CellLocation {
/** The Cell in the top row and leftmost column */
TOP_LEFT,
/** The Cell in the top row and centre column */
TOP_CENTRE,
/** The Cell in the top row and rightmost column */
TOP_RIGHT,
/** The Cell in the centre row and leftmost column */
CENTRE_LEFT,
/** The Cell in the centre row and centre column */
CENTRE_CENTRE,
/** The Cell in the centre row and rightmost column */
CENTRE_RIGHT,
/** The Cell in the bottom row and leftmost column */
BOTTOM_LEFT,
/** The Cell in the bottom row and centre column */
BOTTOM_CENTRE,
/** The Cell in the bottom row and rightmost column */
BOTTOM_RIGHT;
}
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;