我一直在尝试下国际象棋,但被困在交换两颗棋子上
这是我的 ChessSquare 课
public class ChessSquare extends JButton
{
public int Yposition;
public int Xposition;
private String Type;
public ChessSquare( Icon s, int y , int x , String piece )
{
super(s);
Yposition = y;
Xposition = x;
Type = piece ;
}
public int getXposition()
{
return this.Xposition;
}
public int getYposition()
{
return this.Xposition;
}
public String getType()
{
return "";
}
}
然后我将其添加到 ChessBoard 类中,使用 ChessSquare 类作为 2D 数组
public class ChessBoard extends JFrame implements ActionListener
{
String type;
ChessSquare[][] s = new ChessSquare[8][8];
int xPos1,xPos2,yPos1,yPos2,i,j;
JPanel panel;
........
public void actionPerformed(ActionEvent e)
{
Boolean flag = false ;
if(!flag)
{
xPos1 = s.getYposition();
yPos1 = s.getXposition();
flag = true;
}
else
{
xPos2 = s.getYposition();
yPos2 = s.getXposition();
s[xPos1][yPos1] = s[xPos2][yPos2];
s[xPos2][yPos2] = s[xPos1][yPos1];
flag = false;
}
}
}
试图交换两件......但它不起作用?请帮忙...