This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center




7年前关闭。





第一次问周围

我正在做拼贴画的国际象棋游戏(还很早就开始了),我在移动棋子时遇到了问题。

public void choosePiece(int x, int y){
    GUI.recolour();
    if(active==null){
        if(gameArray[x][y].isWhite()==whiteActive){
            active=gameArray[x][y];
            GUI.displayMoves(showMoves(active));
        }
    }else{
        if(gameArray[x][y]==null){
            gameArray[x][y]=active;
            gameArray[x][y].setCoordinades(x,y);
            gameArray[active.getX()][active.getY()]=null;
            active=null;
        }else if(gameArray[x][y].isWhite()==whiteActive){
            active=gameArray[x][y];
            GUI.displayMoves(showMoves(active));
        }else{
            if(whiteActive)
                blackDied.add(gameArray[x][y]);
            else
                whiteDied.add(gameArray[x][y]);
            gameArray[x][y]=active;
            gameArray[x][y].setCoordinades(x,y);
            gameArray[active.getX()][active.getY()]=null;
            active=null;
        }
        guiRefresh();
    }
}


用户应该单击板上的一块(JButton网格),然后GUI类将使用所述JButton的坐标来调用GameHandler类。

该代码应该从JButton网格中获取那些x; y坐标,并检查是否没有活动的块。如果是这样,则将gameArray [x] [y](从一个棋子阵列中选出一个棋子)作为活动棋子,并显示可用的移动,将JButton的背景更改为JButton网格中的绿色。

如果已经有一个活动部件,则应检查x; y目标是否为空或另一个部件:

在第一种情况下,它将把活动部件放置在空白处。
如果目标x; y中还有另一块,它将检查该块是否来自其他颜色。

如果颜色相同,它将更改活动的棋子并显示其移动。
如果是另一种颜色,它将“杀死”该棋子,并将其替换为活动棋子。

请注意,除该部件可访问的那些以外的所有其他空按钮均未启用。
最后的guiRefresh()方法遍历整个gameArray,并要求GUI在x,y位置放置一个表示X类型块的按钮。我使用了从8x8按钮矩阵添加按钮的网格布局。

嗯..听起来像是对我有用..但事实并非如此:S照原样,当A吃掉B时,B从棋盘上消失了,但是A从未被放置而不是B ..它保持不变..但如果再次单击A时,显示可能移动的绿色按钮看起来就像A实际在B中一样。

最佳答案

如果setCoordinates更改了getX()getY()返回的值,则以下语句的顺序错误:

gameArray[x][y].setCoordinades(x,y);
gameArray[active.getX()][active.getY()]=null;

10-05 21:21