Java Class 构造函数更改输入参数 - MiniMax
Java Class Constructor changes input parameter - MiniMax
我正在为 Connect 4 编写 MiniMax 代码。基本上我有一个 class SimpleMiniMax,它有一个 minimax 方法(接受 grid 的输入),它应该是 return [=] 的一个实例22=] 移动。方法 minimax 调用方法 maxMove,它接受网格的输入(与第一个方法调用相同)。这里的 maxMove 方法为 AI 生成 Moves,然后运行 mimMove。问题是,当它为 AI 生成 Move 时,它会传递当前 Grid 并获得 Move back.But 在此过程中,它也会更改当前 Grid。因此,当它经历所有可能性时,它会继续添加 AI 移动到同一个网格。所以在7种可能之后(连4格是标准的6行7列)AI填满7个位置显然赢了。
我尝试将输入参数复制到一个局部变量中并传递它,但值仍然流过。我知道传递给 Java 方法的参数是按值传递的(只读)。我不确定我在这里做错了什么。
这是简短的代码。
public class SimpleMiniMax {
private static Boolean noMoreMoves = false;
public Move minimax(int[][] gridCopy_minimax){
Move nextMove = null;
noMoreMoves = false;
return maxMove(gridCopy_minimax, 1);
}
private Move maxMove(int[][] gridCopy_max, int depth){
int gameValue = 0;
Move maxMove = null,
tempMove = null,
thisMove = null;
if(depth==0 || noMoreMoves){
return null;
} else {
for (int i = 0; i < 7; i++) {
try {
thisMove = new Move(gridCopy_max, i, Grid.getAI());
// Issue here---
// After this call the value of gridCopy_max carries the Ai move so when its
// called for next loop run it builds from there on.
tempMove = minMove(thisMove.newGrid, depth-1 );
if(tempMove==null) tempMove = thisMove;
if (maxMove==null && tempMove != null) maxMove = tempMove;
if(tempMove != null && tempMove.moveValue >= maxMove.moveValue ){
maxMove = tempMove;
}
} catch (Exception e) {
// TODO: handle exception
if(i==6){
noMoreMoves = true;
break;
}
}
}
}
return maxMove;
}
Class Move
public class Move {
public int[][] newGrid = new int[6][7];
public int moveX = 9,
moveY,
moveValue;
public Move(int[][] CurrGrid, int y, int player_in) throws Exception{
newGrid = CurrGrid;
int i = 0;
for (i = 0; i < 6 ; i++) {
if(CurrGrid[i][y] != 0){
moveX = i-1;
break;
}
}
if(i<0){
throw new Exception("No More Moves");
}
if(moveX==9){
moveX = 5;
}
moveY = y;
newGrid[moveX][y] = player_in;
this.moveValue(player_in);
System.out.println("Generating Move" + player_in + "|" + moveX + "|" + moveY);
}
你复制到局部变量的是一个指向数组的指针。对于引用参数,Java 按值传递引用本身,它可以是 null 或指向对象的指针。
我建议在修改之前将参数引用的数组的内容复制到本地创建的数组中。
我正在为 Connect 4 编写 MiniMax 代码。基本上我有一个 class SimpleMiniMax,它有一个 minimax 方法(接受 grid 的输入),它应该是 return [=] 的一个实例22=] 移动。方法 minimax 调用方法 maxMove,它接受网格的输入(与第一个方法调用相同)。这里的 maxMove 方法为 AI 生成 Moves,然后运行 mimMove。问题是,当它为 AI 生成 Move 时,它会传递当前 Grid 并获得 Move back.But 在此过程中,它也会更改当前 Grid。因此,当它经历所有可能性时,它会继续添加 AI 移动到同一个网格。所以在7种可能之后(连4格是标准的6行7列)AI填满7个位置显然赢了。
我尝试将输入参数复制到一个局部变量中并传递它,但值仍然流过。我知道传递给 Java 方法的参数是按值传递的(只读)。我不确定我在这里做错了什么。
这是简短的代码。
public class SimpleMiniMax {
private static Boolean noMoreMoves = false;
public Move minimax(int[][] gridCopy_minimax){
Move nextMove = null;
noMoreMoves = false;
return maxMove(gridCopy_minimax, 1);
}
private Move maxMove(int[][] gridCopy_max, int depth){
int gameValue = 0;
Move maxMove = null,
tempMove = null,
thisMove = null;
if(depth==0 || noMoreMoves){
return null;
} else {
for (int i = 0; i < 7; i++) {
try {
thisMove = new Move(gridCopy_max, i, Grid.getAI());
// Issue here---
// After this call the value of gridCopy_max carries the Ai move so when its
// called for next loop run it builds from there on.
tempMove = minMove(thisMove.newGrid, depth-1 );
if(tempMove==null) tempMove = thisMove;
if (maxMove==null && tempMove != null) maxMove = tempMove;
if(tempMove != null && tempMove.moveValue >= maxMove.moveValue ){
maxMove = tempMove;
}
} catch (Exception e) {
// TODO: handle exception
if(i==6){
noMoreMoves = true;
break;
}
}
}
}
return maxMove;
}
Class Move
public class Move {
public int[][] newGrid = new int[6][7];
public int moveX = 9,
moveY,
moveValue;
public Move(int[][] CurrGrid, int y, int player_in) throws Exception{
newGrid = CurrGrid;
int i = 0;
for (i = 0; i < 6 ; i++) {
if(CurrGrid[i][y] != 0){
moveX = i-1;
break;
}
}
if(i<0){
throw new Exception("No More Moves");
}
if(moveX==9){
moveX = 5;
}
moveY = y;
newGrid[moveX][y] = player_in;
this.moveValue(player_in);
System.out.println("Generating Move" + player_in + "|" + moveX + "|" + moveY);
}
你复制到局部变量的是一个指向数组的指针。对于引用参数,Java 按值传递引用本身,它可以是 null 或指向对象的指针。
我建议在修改之前将参数引用的数组的内容复制到本地创建的数组中。