當前位置:首頁 > IT技術(shù) > 移動平臺 > 正文

java實現(xiàn)中國象棋2:移動棋子
2021-12-01 23:05:54



java實現(xiàn)中國象棋2:移動棋子


我在“java實現(xiàn)中國象棋1”的博客中說了一下如何把棋子畫在棋盤上,使用一個 f l a g flag flag二維數(shù)組即可實現(xiàn)。因此如果我們想讓棋子移動,只需要改變改變 f l a g flag flag二維數(shù)組中的值即可。我先通過 m o u s e c l i c k ( ) mouseclick() mouseclick()函數(shù)獲取當前點擊的位置,再通過一個 g e t c r ( ) getcr() getcr()函數(shù)獲得當前點擊位置的行數(shù)和列數(shù),代碼如下:

// Listener.java
int x1,x2,y1,y2;
int c = -1;// 如果為0,則會在一開始選中flag[0][0]處的車,故設(shè)置為-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();// 獲得此時點擊處的位置
}

// 得到現(xiàn)在點擊的位置
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;
}

獲得了當前點擊的位置,就可以選中這個位置的棋子,然后獲得下次點擊的位置,把上次點擊的棋子放置到這次點擊的位置上,即可實現(xiàn)棋子的移動。所以我創(chuàng)建兩個變量 c u r c h e s s 和 b e f o r e c h e s s curchess和beforechess curchess和beforechess,都為一行三列的一維數(shù)組,第一位第二位第三位分別為 r , c , f l a g [ r ] [ c ] r,c,flag[r][ c ] r,c,flag[r][c],這樣就可以保存前一顆棋子和現(xiàn)在點擊的棋子或者空位。(我把空位也看做一個棋子,后面會添加紅棋走還是黑棋走,所以不擔心會出現(xiàn)空位把棋子吃了的現(xiàn)象)

//Listener.java


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

// 更新現(xiàn)在點中的棋子
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);復制數(shù)組
//%Arrays.copyOf(original, newLength);復制數(shù)組
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();
}

我想實現(xiàn)當我點擊棋子的時候,棋子能變大一些,以便于我知道我選中了哪個棋子的功能。所以我在DrawUI類中的Paint方法中添加以下代碼。

// 重繪
public void paint(Graphics g) {
super.paint(g);
g.drawImage(new ImageIcon(getClass().getResource("image\"+"棋盤.jpg")).getImage(), 90, 60, 625, 700, this);
// 根據(jù)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);
}
}
}
}

現(xiàn)在該實現(xiàn)棋子的移動功能了,現(xiàn)在要設(shè)置 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變?yōu)?
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) {// 如果此時點的地方?jīng)]有棋子,直接替換
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();
}
}
}
}
}

此時便可實現(xiàn)棋子走動的功能了。

關(guān)注微信公眾號:圖靈完備,回復中國象棋即可獲得圖片及代碼資源。


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

開通會員,享受整站包年服務(wù)立即開通 >