Java gomoku极小极大
我正在尝试创建一个战略,将对人类或其他战略的游戏五子棋。我已经有了一些minimax函数,虽然我不太明白它是如何工作的,我还有一个getscore函数,它应该把最好的分数发回minimax?但问题是我的getScore函数无法识别一行/列/对角中是否有4或5个圆 这是我的密码:Java gomoku极小极大,java,minimax,gomoku,Java,Minimax,Gomoku,我正在尝试创建一个战略,将对人类或其他战略的游戏五子棋。我已经有了一些minimax函数,虽然我不太明白它是如何工作的,我还有一个getscore函数,它应该把最好的分数发回minimax?但问题是我的getScore函数无法识别一行/列/对角中是否有4或5个圆 这是我的密码: public class JarmoStrategyV1 implements ComputerStrategy { public static int Lrow = 0; public static
public class JarmoStrategyV1 implements ComputerStrategy {
public static int Lrow = 0;
public static int Lcol = 0;
public static int Drow = 0;
public static int Dcol = 0;
public static final int E = 0;
public static final int X = 1; // black
public static final int O = -1; // white
public static final int WINSCORE = 100;
private static final int WINCOUNT = 5;
public Location getMove(SimpleBoard board, int player) {
// let's operate on 2-d array
int[][] b = board.getBoard();
System.out.println(getScore(b, player));
for (int row = 0; row < b.length; row++) {
for (int col = 0; col < b[0].length; col++) {
if (b[row][col] == SimpleBoard.EMPTY) {
// first empty location
return new Location(row, col);
}
}
}
return null;
}
@Override
public String getName() {
return "Student name";
}
public static int minimax(int[][] board, int player, int depth) {
if (getScore(board, player) == WINSCORE) {
//
return WINSCORE;
}
if (depth == 2) {
return getScore(board, player);
}
int max = Integer.MIN_VALUE;
if (player == -1){
max = Integer.MAX_VALUE;
}
System.out.println(max);
List<Location> possibleMoves = getPossibleMoves(board);
for (Location loc : possibleMoves) {
board[loc.getRow()][loc.getColumn()] = player;
int newplayer = 0 - player;
if(newplayer == 1){
int value = minimax(board, newplayer,depth + 1);
if(value < max) {
max = value;
}
}
if (newplayer == -1){
int value = minimax(board, newplayer, depth + 1);
if (value > max) {
max = value;
}
}
board[loc.getRow()][loc.getColumn()] = E;
}
return max;
}
public static int getScore(int[][] board, int muutuja) {
//int yks = 0;
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
if (board[row][col] == muutuja) {
if (row <= (board.length - 5)) {
if (col <= board.length && getCount(board, row, col, 0, 1, muutuja, WINCOUNT) >= (WINCOUNT - 1) && getCount(board, row, (col + 4), 0, 1, E, 1 ) >= 1) return 1; // - 4 in a row
if (row >= 1 && getCount(board, row, col, 1, 0, muutuja, WINCOUNT) >= (WINCOUNT -1) && getCount(board, (row - 1), col, 1, 0, E, 1) == 1) return 1;
if (getCount(board, row, col, 1, 0, muutuja, WINCOUNT) >= WINCOUNT) return 100; // | 5 in a row
if (col <= WINCOUNT && getCount(board, row, col, 1, 1, muutuja, WINCOUNT) >= WINCOUNT) return 100; // \
if (col >= WINCOUNT && getCount(board, row, col, 1, -1, muutuja, WINCOUNT) >= WINCOUNT) return 100; // /
}
if (col <= WINCOUNT && getCount(board, row, col, 0, 1, muutuja, WINCOUNT) >= WINCOUNT) return 100; // -
}
}
}
return 0;
}
public static int getCount(int[][] board, int row, int col, int rowd, int cold, int player, int test) {
int count = 0;
for (int i = 0; i < test; i++) {
if (board[row + i * rowd][col + i * cold] == player) count++;
else break;
}
return count;
}
public static ArrayList<Location> getPossibleMoves(int[][] board) {
ArrayList<Location> availableMoves = new ArrayList<Location>();
for (int row = 0; row < board.length; row++) {
for (int col = 0; col < board[row].length; col++) {
if (board[row][col] == E) {
availableMoves.add(new Location(row, col));
}
}
}
return availableMoves;
}
}
公共类JarmoStrategyV1实现计算机策略{
公共静态int Lrow=0;
公共静态int Lcol=0;
公共静态int Drow=0;
公共静态int Dcol=0;
公共静态最终整数E=0;
公共静态final int X=1;//黑色
公共静态final int O=-1;//白色
公共静态最终int WINSCORE=100;
专用静态最终整数WINCOUNT=5;
公共位置getMove(SimpleBoard板,int播放器){
//让我们操作二维阵列
int[]b=board.getBoard();
System.out.println(getScore(b,player));
for(int row=0;row最大值){
最大值=最大值;
}
}
板[loc.getRow()][loc.getColumn()]=E;
}
返回最大值;
}
公共静态int getScore(int[]]板,int muutuja){
//int-yks=0;
对于(int row=0;row=1&&getCount(板、行、列、1、0、muutuja、WINCOUNT)>=(WINCOUNT-1)&&getCount(板、行、列、1、0、E、1)==1)返回1;
if(getCount(板、行、列、1、0、muutuja、WINCOUNT)>=WINCOUNT)返回100;/| 5行
如果(col=WINCOUNT)返回100;//\
如果(列>=WINCOUNT&&getCount(板,行,列,1,-1,muutuja,WINCOUNT)>=WINCOUNT)返回100;///
}
如果(col=WINCOUNT)返回100;//-
}
}
}
返回0;
}
公共静态int getCount(int[][]板、int行、int列、int行、int冷、int播放器、int测试){
整数计数=0;
对于(int i=0;i
getScore似乎存在某种问题,当我运行此代码时,我只能玩一段时间,直到我的Gomoku应用程序崩溃
如果您想自己尝试一下,那么可以通过Eclipse打开它。
下载项目文件:
将其导入eclipse项目/工作区。
您还必须构建lib文件夹中这两个jar文件的路径。
注意:我只能编辑gomoku.strategies包中的文件。堆栈跟踪显示异常。添加调试打印或使用调试器运行
java.lang.IllegalArgumentException: It's computer's turn, pass the player to makeMove()
at gomoku.Game.makeMove(Game.java:476)
这是一个错误,可能是因为程序运行超出了游戏边界,我认为java不理解发生了什么。