0

Java プログラムにグローバル変数のスコープの問題があると思います。これは HiQ ゲームで、ボードの状態を保存するボード オブジェクトがあります。1 はペグ、0 は空の穴、-1 は無効な位置です。ゲームは、可能な動きのために 32 枚のボードを作成し、可能な動きを構築し、穴 3、3 の 1 つのペグに到達するためのパスを見つけるために深さ優先検索に進みます。

問題は、移動してボード 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];
}

すべての要素をループすることで、参照ではなく 2 次元配列のコピーを作成します。私がvb.netまたはc#で書いた他のアプリでこれに遭遇したことはありませんが、通常は1次元配列を使用しています。あらゆる種類のグラフを必要とするプログラムでは多くのことをしないでください

于 2012-10-28T02:21:06.693 に答える