分配新值时,2D数组不会更改

时间:2014-11-18 01:28:20

标签: java

我正在尝试在java控制台中制作一个跳棋游戏,当试图让一块棋子移动时,游戏板上的二维阵列不会改变。方法movePiece有三个参数,'移动代码'(1-4对应于玩家打算制作的动作,1对应于右,2为向上,3为向左,4为向右)和x和玩家想要移动的棋子的y值。该方法被调用并执行正常,但是当打印出电路板时(使用printBoard方法),它不会显示任何更改。

public class Board {
    private char gameBoard[][] =  {{'0','b','0','b','0','b','0','b'},{'b','0','b','0','b','0','b','0',},{'0','b','0','b','0','b','0','b'},{'0','0','0','0','0','0','0','0'},{'0','0','0','0','0','0','0','0'},{'r','0','r','0','r','0','r','0',},{'0','r','0','r','0','r','0','r'},{'r','0','r','0','r','0','r','0'}};;

    public char[][] getBoard() {
        return gameBoard;
    }

    public void movePiece(int move,int startingX, int startingY){
        int wantedX, wantedY;
        switch(move){
            case 1:
                wantedX = startingX + 1;
                wantedY = startingY + 1;

                break;
            case 2:
                wantedX = startingX - 1;
                wantedY = startingY + 1;
                break;
            case 3:
                wantedX = startingX - 1;
                wantedY = startingY - 1;
                break;
            case 4:
                wantedX = startingX + 1;
                wantedY = startingY - 1;
                break;
            default:
                wantedX = 0;
                wantedY = 0;
                break;
        }
        this.gameBoard[wantedX][wantedY] = this.gameBoard[startingX][startingY];
        this.gameBoard[startingX][startingY] = '0';
    }

    public int countPieces(char color){
        int count = 0;
        for(int i = 0; i < gameBoard.length; i++){
            for(int j = 0; j < gameBoard[i].length; i++){
                if(gameBoard[i][j] == color)
                    count++;
            }
        }
        return count;   
    }

    public void printBoard(){
        for(int i = 0; i < gameBoard.length; i++){
            for(int j = 0; j < gameBoard[i].length; j++){
                System.out.print(gameBoard[i][j]);
            }
            System.out.print("\n");
        }
    }
}




public class test {

    public static void main(String[] args){
        System.out.print("Beginning test\n");
        Board board = new Board();
        board.printBoard();
        System.out.println("Moving piece at (2,0) to (3,1)");
        board.movePiece(1, 2, 0);
        board.printBoard();
    }

}

0 个答案:

没有答案