0

我认为我的 java 程序存在全局变量范围问题。这是 HiQ 游戏,我有一个棋盘对象来存储棋盘的状态,1 是钉子,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;
        }
        }


}

}
4

1 回答 1

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# 编写的其他应用程序中之前没有遇到过这种情况,但它们通常使用一维数组。不要在那些需要任何类型图表的程序中做太多事情

于 2012-10-28T02:21:06.693 回答