我认为我的java程序存在全局变量范围问题。这是HiQ游戏,我有一个板对象来存储板的状态,1是peg,0是空洞,-1是无效位置。游戏为可能的移动创建了32块板,并构建了可能的移动并进入深度优先搜索,以找到在3,3洞中找到一个钉子的路径。
问题在于,当我做一个移动并尝试更换板2并将板1留下所有钉子和1个孔时,程序不仅更改了板2来进行跳转,而且还修改了板1。此外,我的移动列表也已被覆盖。
请帮我创建我的电路板阵列,这样当我处理移动时它不会覆盖以前的电路板。我不知道我在这里做错了什么。谢谢!
董事会成员:
import java.util.LinkedList;
import java.util.Queue;
public class Board {
String PossibleMoves;
int[][] GraphState2;
Queue<String> qe = new LinkedList<String>();
public int leftDir = 0;
public int rightDir = 1;
public int upDir = 2;
public int downDir;
public static final int MAXROW=7;
public static final int MAXCOL=7;
Board(){
GraphState2 = new int[6][6];
}
public void setMove(String inMove){
qe.add(inMove);
}
public String getMove(){
if (qe.size() >= 1){
return qe.poll();
}
return "";
}
public void doMove(int row, int col, int toRow, int toCol, int removeRow, int removeCol){
GraphState2[row][col] = 0; //source peg
GraphState2[toRow][toCol] = 1; //destination
GraphState2[removeRow][removeCol] = 0; //jumped peg
}
public Boolean checkMove(){
if (qe.size() >= 1){
return true;
}
return false;
}
public void setBoard(int inBoard[][]){
GraphState2 = inBoard;
}
public int[][] getBoard(){
return GraphState2;
}
public Boolean findMove(int dir, int row, int col){
int row1, col1, row2, col2;
if (dir == leftDir){
row1 = row;
row2 = row;
col1 = col - 1;
col2 = col - 2;
if (col2 < 0) { return false; } // drop off board
if (GraphState2[row][col2] == 1) { return false; } // peg in destination
if (GraphState2[row][col1] == 0) { return false; } // no peg to jump
if (GraphState2[row][col2] == -1) {return false; } // invalid position
return true; // all checks are green, go go go
}
else if (dir == rightDir){
row1 = row;
row2 = row;
col1 = col + 1;
col2 = col + 2;
if (col2 >= MAXCOL) { return false; } // drop off board
if (GraphState2[row][col2] == 1) { return false; } // peg in destination
if (GraphState2[row][col1] == 0) { return false; } // no peg to jump
if (GraphState2[row][col2] == -1) { return false; } // invalid position
return true; // all checks are green, go go go
}
else if (dir == upDir){
row1 = row - 1;
row2 = row - 2;
col1 = col;
col2 = col;
if (row2 < 0) { return false; } // drop off board
if (GraphState2[row2][col] == 1) { return false; } // peg in destination
if (GraphState2[row1][col] == 0) { return false; } // no peg to jump
if (GraphState2[row2][col] == -1) { return false; } // invalid position
return true; // all checks are green, go go go
}
else{ //downDir
row1 = row + 1;
row2 = row + 2;
col1 = col;
col2 = col;
if (row2 >= MAXROW) { return false; } // drop off board
if (GraphState2[row2][col] == 1) { return false; } // peg in destination
if (GraphState2[row1][col] == 0) { return false; } // no peg to jump
if (GraphState2[row2][col] == -1) { return false; } // invalid position
return true; // all checks are green, go go go
}
}
}
HiQ主程序:
import java.util.*;
public class HiQDFS {
/**
* @param args
*/
public static void main(String[] args) {
int[][] GraphState = new int[7][7];
String currentMove = "";
GraphState[0][0] = -1;
GraphState[0][1] = -1;
GraphState[0][2] = 1;
GraphState[0][3] = 1;
GraphState[0][4] = 1;
GraphState[0][5] = -1;
GraphState[0][6] = -1;
GraphState[1][0] = -1;
GraphState[1][1] = -1;
GraphState[1][2] = 1;
GraphState[1][3] = 1;
GraphState[1][4] = 1;
GraphState[1][5] = -1;
GraphState[1][6] = -1;
GraphState[2][0] = 1;
GraphState[2][1] = 1;
GraphState[2][2] = 1;
GraphState[2][3] = 1;
GraphState[2][4] = 1;
GraphState[2][5] = 1;
GraphState[2][6] = 1;
GraphState[3][0] = 1;
GraphState[3][1] = 1;
GraphState[3][2] = 1;
GraphState[3][3] = 0;
GraphState[3][4] = 1;
GraphState[3][5] = 1;
GraphState[3][6] = 1;
GraphState[4][0] = 1;
GraphState[4][1] = 1;
GraphState[4][2] = 1;
GraphState[4][3] = 1;
GraphState[4][4] = 1;
GraphState[4][5] = 1;
GraphState[4][6] = 1;
GraphState[5][0] = -1;
GraphState[5][1] = -1;
GraphState[5][2] = 1;
GraphState[5][3] = 1;
GraphState[5][4] = 1;
GraphState[5][5] = -1;
GraphState[5][6] = -1;
GraphState[6][0] = -1;
GraphState[6][1] = -1;
GraphState[6][2] = 1;
GraphState[6][3] = 1;
GraphState[6][4] = 1;
GraphState[6][5] = -1;
GraphState[6][6] = -1;
String StartingBoard = "";
for (int i = 0; i < 7; i++){
for (int j = 0; j < 7; j++){
StartingBoard = StartingBoard + " " + GraphState[i][j];
}
StartingBoard = StartingBoard + "\n";
}
System.out.println(StartingBoard);
ArrayList<Board> board = new ArrayList<Board>();
board.add(new Board());
board.add(new Board());
board.add(new Board());
board.add(new Board());
board.add(new Board());
board.add(new Board());
board.add(new Board());
board.add(new Board());
board.add(new Board());
board.add(new Board());
board.add(new Board());
board.add(new Board());
board.add(new Board());
board.add(new Board());
board.add(new Board());
board.add(new Board());
board.add(new Board());
board.add(new Board());
board.add(new Board());
board.add(new Board());
board.add(new Board());
board.add(new Board());
board.add(new Board());
board.add(new Board());
board.add(new Board());
board.add(new Board());
board.add(new Board());
board.add(new Board());
board.add(new Board());
board.add(new Board());
board.add(new Board());
board.add(new Board());
Board currentBoard;
currentBoard = board.get(0);
currentBoard.setBoard(GraphState);
GraphState = currentBoard.getBoard();
boolean flag = false;
int counter = 0;
int row, col, toRow, toCol, removeRow, removeCol;
for (int depth = 0; depth < 32; depth++)
{
currentBoard = board.get(depth);
counter++;
System.out.println(depth);
if (flag == false){
for (int i = 0; i < 7; i++){
for (int j = 0; j < 7; j++){
if (GraphState[i][j] == 1){
if (currentBoard.findMove(0, i, j) == true){ //left move
currentMove = Integer.toString(i) + "," + Integer.toString(j) + " " + Integer.toString(i) + "," + Integer.toString(j-2) + " " + Integer.toString(i) + "," + Integer.toString(j-1);
currentBoard.setMove(currentMove);
}
if (currentBoard.findMove(1, i, j) == true){ //right move
currentMove = Integer.toString(i) + "," + Integer.toString(j) + " " + Integer.toString(i) + "," + Integer.toString(j+2) + " " + Integer.toString(i) + "," + Integer.toString(j+1);
currentBoard.setMove(currentMove);
}
if (currentBoard.findMove(2, i, j) == true){ //up move
currentMove = Integer.toString(i) + "," + Integer.toString(j) + " " + Integer.toString(i-2) + "," + Integer.toString(j) + " " + Integer.toString(i-1) + "," + Integer.toString(j);
currentBoard.setMove(currentMove);
}
if (currentBoard.findMove(3, i, j) == true){ //down move
currentMove = Integer.toString(i) + "," + Integer.toString(j) + " " + Integer.toString(i+2) + "," + Integer.toString(j) + " " + Integer.toString(i+1) + "," + Integer.toString(j);
currentBoard.setMove(currentMove);
}
}
}
}//end of load moves
}//end of if
if (currentBoard.checkMove()) {
currentMove = currentBoard.getMove();
flag = false;
row = Integer.parseInt(currentMove.substring(0, 1));
col = Integer.parseInt(currentMove.substring(2, 3));
toRow = Integer.parseInt(currentMove.substring(4, 5));
toCol = Integer.parseInt(currentMove.substring(6, 7));
removeRow = Integer.parseInt(currentMove.substring(8,9));
removeCol = Integer.parseInt(currentMove.substring(10,11));
Board nextBoard = board.get(depth+1);
nextBoard.setBoard(currentBoard.getBoard());
nextBoard.doMove(row, col, toRow, toCol, removeRow, removeCol);
} else {
depth--;
depth--;
flag = true;
}
GraphState = currentBoard.getBoard();
if (depth == 32 && GraphState[3][3] == 1) {
break;
}
}
}
}
答案 0 :(得分:0)
修好了。在Board类中将我的setBoard命令更改为:
public void setBoard(int inBoard[][]){
//GraphState = inBoard;
for(int i=0; i<inBoard.length; i++)
for(int j=0; j<inBoard[i].length; j++)
GraphState[i][j]=inBoard[i][j];
}
通过循环遍历所有元素,它创建了2d数组的副本而不是引用。在我用vb.net或c#编写的其他应用程序中,Haven之前遇到过这种情况,但他们通常使用的是1d数组。在那些需要任何类型图表的程序中不要做太多事情