当前位置:首页 > IT技术 > 移动平台 > 正文

java实现中国象棋2:移动棋子
2021-12-01 23:05:54



java实现中国象棋2:移动棋子


我在“java实现中国象棋1”的博客中说了一下如何把棋子画在棋盘上,使用一个 f l a g flag flag二维数组即可实现。因此如果我们想让棋子移动,只需要改变改变 f l a g flag flag二维数组中的值即可。我先通过 m o u s e c l i c k ( ) mouseclick() mouseclick()函数获取当前点击的位置,再通过一个 g e t c r ( ) getcr() getcr()函数获得当前点击位置的行数和列数,代码如下:

// Listener.java
int x1,x2,y1,y2;
int c = -1;// 如果为0,则会在一开始选中flag[0][0]处的車,故设置为-1
int r = -1;

public void mouseClicked(MouseEvent e) {
System.out.println("点击");
x1 = e.getX();
y1 = e.getY();
if (x1 > init.x0 - init.size / 2 && y1 > init.y0 - init.size / 2
&& x1 < init.x0 + init.size / 2 + init.column * init.size
&& y1 < init.y0 + init.row * init.size + init.size / 2) {
x2 = ((x1 - init.x0 + init.size / 2) / init.size) * init.size + init.x0;
y2 = ((y1 - init.y0 + init.size / 2) / init.size) * init.size + init.y0;
getcr();// 获得此时点击处的位置
}

// 得到现在点击的位置
public void getcr() {
x2 = ((x1 - init.x0 + init.size / 2) / init.size) * init.size + init.x0;
y2 = ((y1 - init.y0 + init.size / 2) / init.size) * init.size + init.y0;
// 当前点击的位置
c = (x2 - init.x0) / init.size;
r = (y2 - init.y0) / init.size;
}

获得了当前点击的位置,就可以选中这个位置的棋子,然后获得下次点击的位置,把上次点击的棋子放置到这次点击的位置上,即可实现棋子的移动。所以我创建两个变量 c u r c h e s s 和 b e f o r e c h e s s curchess和beforechess curchess和beforechess,都为一行三列的一维数组,第一位第二位第三位分别为 r , c , f l a g [ r ] [ c ] r,c,flag[r][ c ] r,c,flag[r][c],这样就可以保存前一颗棋子和现在点击的棋子或者空位。(我把空位也看做一个棋子,后面会添加红棋走还是黑棋走,所以不担心会出现空位把棋子吃了的现象)

//Listener.java


int[] curchess = new int[3];
int[] beforechess = new int[3];

// 更新现在点中的棋子
public void recurchess() {
if (r != -1) {
curchess[0] = r;
curchess[1] = c;
curchess[2] = flag[r][c];
}
}

// 更新上一次点中的棋子
public void rebec() {
//System.arraycopy(src, srcPos, dest, destPos, length);复制数组
//%Arrays.copyOf(original, newLength);复制数组
beforechess[0] = curchess[0];
beforechess[1] = curchess[1];
beforechess[2] = curchess[2];
}

public void mouseClicked(MouseEvent e) {
System.out.println("点击");
x1 = e.getX();
y1 = e.getY();
if (x1 > init.x0 - init.size / 2 && y1 > init.y0 - init.size / 2
&& x1 < init.x0 + init.size / 2 + init.column * init.size
&& y1 < init.y0 + init.row * init.size + init.size / 2) {
x2 = ((x1 - init.x0 + init.size / 2) / init.size) * init.size + init.x0;
y2 = ((y1 - init.y0 + init.size / 2) / init.size) * init.size + init.y0;
getcr();// 获得此时点击处的位置
rebec();// 更新前一颗棋子
ui.repaint();
recurchess();
}

我想实现当我点击棋子的时候,棋子能变大一些,以便于我知道我选中了哪个棋子的功能。所以我在DrawUI类中的Paint方法中添加以下代码。

// 重绘
public void paint(Graphics g) {
super.paint(g);
g.drawImage(new ImageIcon(getClass().getResource("image\"+"棋盘.jpg")).getImage(), 90, 60, 625, 700, this);
// 根据flag画棋子
for (int i = 0; i < init.row; i++) {
for (int j = 0; j < init.column; j++) {
if (ls.flag[i][j] > 0) {
g.drawImage(new ImageIcon(getClass().getResource("image\"+(Integer.toString(ls.flag[i][j])) + ".png")).getImage(), init.y0 + j * init.size - init.chesssize / 2,init.x00 + i * init.size - init.chesssize / 2,init.chesssize, init.chesssize, this);
}
}
}

// 将选中的棋子放大
if(ls.r != -1) {
if(ls.flag[ls.r][ls.c] > 0) {
if(ls.chessflag == 1&ls.flag[ls.r][ls.c] > 10 | ls.chessflag == 2&ls.flag[ls.r][ls.c] < 10) {
int newexsize = 8;
g.drawImage(new ImageIcon(getClass().getResource("image\"+(Integer.toString(ls.flag[ls.r][ls.c])) + ".png")).getImage(), init.y0 + ls.c * init.size - (init.chesssize+newexsize) / 2,init.x00 + ls.r * init.size - (init.chesssize+newexsize) / 2,init.chesssize+newexsize, init.chesssize+newexsize, this);
}
}
}
}

现在该实现棋子的移动功能了,现在要设置 c h e s s f l a g chessflag chessflag,如果为1,那就是红方走;如果为2,那就是黑方走。在中国象棋中红方先走,因此我初始化 c h e s s f l a g chessflag chessflag为1。棋子移动分三种情况:1、红方或者黑方走空位;2、红方吃黑方;3、黑方吃红方。

// Listener.java


int chessflag = 1;

// 更新黑方红方
public void rechessflag() {
if (chessflag == 1) {
chessflag = 2;
} else if (chessflag == 2) {
chessflag = 1;
}
}

public void walk(){
flag[r][c] = beforechess[2];
flag[beforechess[0]][beforechess[1]] = 0;
curchess = new int[3]; // 走完一步后curchess变为0
beforechess = new int[3];
c = -1;
r = -1;
rechessflag();
ui.repaint();
}
public void mouseClicked(MouseEvent e) {
System.out.println("点击");
x1 = e.getX();
y1 = e.getY();
if (x1 > init.x0 - init.size / 2 && y1 > init.y0 - init.size / 2
&& x1 < init.x0 + init.size / 2 + init.column * init.size
&& y1 < init.y0 + init.row * init.size + init.size / 2) {
x2 = ((x1 - init.x0 + init.size / 2) / init.size) * init.size + init.x0;
y2 = ((y1 - init.y0 + init.size / 2) / init.size) * init.size + init.y0;
// 当前点击的位置
getcr();// 获得此时点击处的位置
rebec();// 更新前一颗棋子
ui.repaint();
recurchess();
if (r != -1) {
if (curchess[2] == 0 & chessflag == 1 & beforechess[2] > 10
| curchess[2] == 0 & chessflag == 2 & beforechess[2] < 10) {// 如果此时点的地方没有棋子,直接替换
System.out.println("走空位");
walk();
} else if (beforechess[2] > 10 & curchess[2] < 10 & chessflag == 1 & flag[r][c] < 10){
if (curchess[2] != 0) {// 如果手中有棋子
System.out.println("红棋吃黑棋");
walk();
}
} else if (beforechess[2] < 10 & curchess[2] > 10 & beforechess[2] > 0 & chessflag == 2 & flag[r][c] > 10) {
if (curchess[2] != 0) {// 如果手中有棋子
System.out.println("黑棋吃红棋");
walk();
}
}
}
}
}

此时便可实现棋子走动的功能了。

关注微信公众号:图灵完备,回复中国象棋即可获得图片及代码资源。


本文摘自 :https://blog.51cto.com/u