不能改变的事情:
enum CellLocation{TOP_LEFT,TOP_CENTER,TOP_RIGHT,CENTRE_LEFT, .. and so on}
enum CellState{OCCUPIED_BY_X,OCCUPIED_BY_O,EMPTY}
一个游戏板接口(interface),只包含一个方法:
CellState getCellState(CellLocation cellLocation);
一个Consultant接口(interface),也包含一个方法:
CellLocation suggest(GameBoard gameBoard);
我的工作是创建一个实现顾问接口(interface)的类并覆盖给定的建议方法(该方法还必须决定下一个玩家 -> Cellocation 输出必须具有 EMPTY 状态)
所以,我的问题是我不确定如何开始。我正在考虑在 suggest 方法中创建一个临时 map ,并填充所有数据,如下所示:
HashMap<CellLocation, CellState> temporaryBoard = new HashMap<>();
temporaryBoard.put(CellLocation.TOP_LEFT,gameBoard.getCellState(CellLocation.TOP_LEFT));
temporaryBoard.put(CellLocation.TOP_CENTRE,gameBoard.getCellState(CellLocation.TOP_CENTRE));
temporaryBoard.put(CellLocation.TOP_RIGHT,gameBoard.getCellState(CellLocation.TOP_RIGHT));
temporaryBoard.put(CellLocation.CENTRE_LEFT,gameBoard.getCellState(CellLocation.CENTRE_LEFT));
temporaryBoard.put(CellLocation.CENTRE_CENTRE,gameBoard.getCellState(CellLocation.CENTRE_CENTRE));
temporaryBoard.put(CellLocation.CENTRE_RIGHT,gameBoard.getCellState(CellLocation.CENTRE_RIGHT));
temporaryBoard.put(CellLocation.BOTTOM_LEFT,gameBoard.getCellState(CellLocation.BOTTOM_LEFT));
temporaryBoard.put(CellLocation.BOTTOM_CENTRE,gameBoard.getCellState(CellLocation.BOTTOM_CENTRE));
temporaryBoard.put(CellLocation.BOTTOM_RIGHT,gameBoard.getCellState(CellLocation.BOTTOM_RIGHT));
所以我可以很容易地使用它。我不确定我是否应该为自己制作一个临时游戏板。
任何想法将不胜感激。
最佳答案
问题并不完全清楚,但如果需要实现来建议最佳可能的移动,则可以使用 Minimax 来完成。算法,旨在执行可能的移动(从而为其他玩家创建一个可能移动较少的棋盘)并递归搜索游戏树,直到找到叶子。然后叶子在概念上被分配一个值 1
(玩家一的胜利)、0
(平局游戏)或 -1
(玩家的胜利)二)。对于非叶节点,某个着法的值是其值的最大值(或最小值,取决于哪个玩家的着法)。这个定义是递归计算的;然后,该算法会建议采用最大值或最小值的移动。
关于java - 井字棋建议走法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39881188/