TicTacToe建议搬家

时间:2016-10-05 18:23:48

标签: java algorithm tic-tac-toe

无法改变的事情:

enum CellLocation{TOP_LEFT,TOP_CENTER,TOP_RIGHT,CENTRE_LEFT, .. and so on}
enum CellState{OCCUPIED_BY_X,OCCUPIED_BY_O,EMPTY}

游戏板界面,只包含一种方法:

CellState getCellState(CellLocation cellLocation);

Consultant接口,它还包含一个方法:

CellLocation suggest(GameBoard gameBoard);

我的工作是创建一个实现顾问接口的类并覆盖给定的suggest方法(该方法还必须决定下一个播放器 - > Cellocation输出必须具有EMPTY状态)

所以,我的问题是我不知道如何开始。我正在考虑在suggest方法中创建一个临时地图,并填写所有数据,如下所示:

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

所以我可以轻松使用它。我不确定我是否应该为自己制作一个临时游戏板。

任何想法都会受到赞赏。

1 个答案:

答案 0 :(得分:1)

问题并不完全清楚,但如果要求实施建议尽可能最佳的移动,可以使用Minimax算法来完成,该算法旨在执行可能的移动(从而创建一个板对另一个玩家的移动较少)并且反复搜索游戏树直到找到一个叶子。然后在概念上为叶子分配值1(玩家1的胜利),0(绘制游戏)或-1(玩家2的胜利)。对于非叶节点,某个移动的值是其值的最大值(或最小值,取决于哪个移动者移动)。这个定义是递归评估的;然后,算法会建议具有最大值或最小值的移动。