當前位置:首頁 > IT技術 > 編程語言 > 正文

java實現(xiàn)中國象棋5:代碼合集
2021-12-01 23:08:33



文章目錄


前言

做完了中國象棋的幾部分,可能前面有些地方敘述不清,故寫一篇代碼合集,可以對照發(fā)現(xiàn)是否有寫錯的地方。共分為5個文件,分別為??DrawUI.java??,??Listener.java??,??init.java??,??BlackWin.java??,??RedWin.java??.其中前三個文件在前面的文章中均有介紹,后兩個是勝利后分別出來的彈窗,因簡單故不再多加敘述。

請不要直接復制粘貼,這樣沒有任何意義!

另:代碼與棋盤圖片是配套的,如果使用自己的棋盤圖片不行的話就改一下init里面的數據。

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

DrawUI

DrawUI.java文件

package 中國象棋; // 自己的包的名字

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Image;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.WindowConstants;

public class DrawUI extends JPanel {

private static final long serialVersionUID = 1L;
Listener ls = new Listener();
public void initui() {
// 創(chuàng)建面板
JFrame jf = new JFrame();
// 設置面板屬性
jf.setSize(1240, 860);
jf.setTitle("中國象棋");
jf.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
jf.getContentPane().setBackground(Color.WHITE);
jf.setLocationRelativeTo(null);
jf.setResizable(false);// 設置窗體不可放縮
// 添加JPanel
JPanel jp = new JPanel();
jp.setPreferredSize(new Dimension(450, 1));
jp.setBackground(Color.white);
jp.setLayout(null);
jf.add(jp, BorderLayout.EAST);
// 添加JLabel
JLabel jl = new JLabel("中國象棋") {
private static final long serialVersionUID = 1L;
Image jli = new ImageIcon(this.getClass().getResource("image\"+"中國象棋.png")).getImage();
public void paint(Graphics g) {
g.drawImage(jli, 0, 0,400, 204, null);
}
};
jl.setBounds(0,0, 400, 204);
jp.add(jl);
// 把this添加到JFrame中
this.setBackground(Color.white);
jf.add(this);
// 添加按鈕
String[] ShapeBtn = { "開始游戲", "重新開始", "悔棋" };
for (int i = 0; i < ShapeBtn.length; i++) {
String name = ShapeBtn[i];
JButton jbt = new JButton(name) {
private static final long serialVersionUID = 1L;
Image jbti = new ImageIcon(this.getClass().getResource("image\"+name+".png")).getImage();
public void paint(Graphics g) {
g.drawImage(jbti, 0, 0,250, 100, null);
}
};
jbt.setBounds(100, 260+150*i, 250, 100);
jbt.addActionListener(ls);
jp.add(jbt);
}
// 給畫板添加監(jiān)聽器
jf.addMouseListener(ls);
jf.setVisible(true);
Graphics g = jf.getGraphics();
ls.setG(g);
ls.setUI(this);
}

// 重繪
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);
}
}
}
}

public static void main(String args[]) {
DrawUI ui = new DrawUI();
ui.initui();
}
}
Listener

Listener.java文件

package 中國象棋; // 自己的包的名字

import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;


public class Listener extends MouseAdapter implements ActionListener {
Graphics g;
String action;
int x1, y1, x2, y2, index, beindex;
int r = -1;
int c = -1;
int chessflag = 1;// 紅方先走為1
DrawUI ui;
int[][] lianbiao = new int[99999][6];// 棋子初始位置,現(xiàn)在的位置,棋子的編號,棋子占的位本來的棋子的編號
int[] curchess = new int[3];
int[] beforechess = new int[3];
int[][] flag = new int[10][9]; // 初始化棋盤

// 將畫布傳遞過來
public void setG(Graphics g) {
this.g = g;
}

public void setUI(DrawUI ui) {
this.ui = ui;
}

// 得到現(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;
}

// 更新悔棋列表
public void setLb() {
lianbiao[index][0] = beforechess[0];
lianbiao[index][1] = beforechess[1];
lianbiao[index][2] = r;
lianbiao[index][3] = c;
lianbiao[index][4] = beforechess[2];
lianbiao[index][5] = flag[r][c];
index++;
}

// 更新現(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);復制數組
//%Arrays.copyOf(original, newLength);復制數組
beforechess[0] = curchess[0];
beforechess[1] = curchess[1];
beforechess[2] = curchess[2];
}

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

public void walk(){
setLb();// 把此棋子的前后位置保存下來
flag[r][c] = beforechess[2];
flag[beforechess[0]][beforechess[1]] = 0;
ifwin();
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();// 獲得此時點擊處的位置
System.out.println("grtcr"+flag[r][c]);

rebec();// 更新前一顆棋子
ui.repaint();
recurchess();
if (r != -1) {
if (curchess[2] == 0 & chessflag == 1 & beforechess[2] > 10 & ifwalk(beforechess[2]) == 1
| curchess[2] == 0 & chessflag == 2 & beforechess[2] < 10 & ifwalk(beforechess[2]) == 1) {// 如果此時點的地方沒有棋子,直接替換
System.out.println("走空位");
walk();
} else if (beforechess[2] > 10 & curchess[2] < 10 & chessflag == 1 & flag[r][c] < 10
& ifwalk(beforechess[2]) == 1) {
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 & ifwalk(beforechess[2]) == 1) {
if (curchess[2] != 0) {// 如果手中有棋子
System.out.println("黑棋吃紅棋");
walk();
}
}
}
}
}

public int ifwalk(int who) {
int ifflag = 0;
// 將的走法
if (who == 5) {
if (r < 3 & c < 6 & c > 2) {
if(beforechess[0] == curchess[0] & Math.abs(beforechess[1] - curchess[1]) == 1
| beforechess[1] == curchess[1] & Math.abs(beforechess[0] - curchess[0]) == 1){
ifflag = 1;
}
}
}
// 帥的走法
else if (who == 55) {
if (r > 6 & c < 6 & c > 2) {
if (beforechess[0] == curchess[0] & Math.abs(beforechess[1] - curchess[1]) == 1
| beforechess[1] == curchess[1] & Math.abs(beforechess[0] - curchess[0]) == 1) {
ifflag = 1;
}
}
}
// 車的走法
else if (who == 1 | who == 11) {
if (beforechess[0] == curchess[0] | beforechess[1] == curchess[1]) {
if (findnumb(beforechess[0], beforechess[1], curchess[0], curchess[1]) == 0) {
ifflag = 1;
}
}
}
// 馬的走法
else if (who == 2 | who == 22) {
if(beforechess[0] > 0) {
if (beforechess[0] - curchess[0] == 2 & Math.abs(beforechess[1] - curchess[1]) == 1
& flag[beforechess[0] - 1][beforechess[1]] == 0) {
ifflag = 1;// 向上走日
}
}
if(beforechess[0] < 9) {
if (beforechess[0] - curchess[0] == -2 & Math.abs(beforechess[1] - curchess[1]) == 1
& flag[beforechess[0] + 1][beforechess[1]] == 0) {
ifflag = 1;// 向下走日
}
}
if(beforechess[1] < 8) {
if (beforechess[1] - curchess[1] == -2 & Math.abs(beforechess[0] - curchess[0]) == 1
& flag[beforechess[0]][beforechess[1] + 1] == 0) {
ifflag = 1;// 向右走日
}
}
if(beforechess[1] > 0) {
if (beforechess[1] - curchess[1] == 2 & Math.abs(beforechess[0] - curchess[0]) == 1
& flag[beforechess[0]][beforechess[1] - 1] == 0) {
ifflag = 1;// 向左走日
}
}
}
// 象的走法
else if (who == 3 | who == 33) {
if(beforechess[0] > 0&beforechess[1] > 0) {
if (beforechess[0] - curchess[0] == 2 & beforechess[1] - curchess[1] == 2
& flag[beforechess[0] - 1][beforechess[1] - 1] == 0) {
ifflag = 1;// 向左上角走田
}
}
if(beforechess[0] < 9&beforechess[1] > 0) {
if (beforechess[0] - curchess[0] == -2 & beforechess[1] - curchess[1] == 2
& flag[beforechess[0] + 1][beforechess[1] - 1] == 0) {
ifflag = 1;// 向左下角走田
}
}
if(beforechess[0] > 0&beforechess[1] < 8) {
if (beforechess[0] - curchess[0] == 2 & beforechess[1] - curchess[1] == -2
& flag[beforechess[0] - 1][beforechess[1] + 1] == 0) {
ifflag = 1;// 向右上角走田
}
}
if(beforechess[0] < 9&beforechess[1] < 8) {
if (beforechess[0] - curchess[0] == -2 & beforechess[1] - curchess[1] == -2
& flag[beforechess[0] + 1][beforechess[1] + 1] == 0) {
ifflag = 1;// 向右下角走田
}
}
}
// 士的走法
else if (who == 4) {
if (r < 3 & c < 6 & c > 2) {
if(Math.abs(beforechess[1] - curchess[1])==1 & Math.abs(beforechess[0] - curchess[0])==1) {
ifflag = 1;
}
}
}
// 仕的走法
else if(who == 44) {
if (r > 6 & c < 6 & c > 2) {
if(Math.abs(beforechess[1] - curchess[1])==1 & Math.abs(beforechess[0] - curchess[0])==1) {
ifflag = 1;
}
}
}
// 炮的走法
else if(who == 6|who == 66) {
if (beforechess[0] == curchess[0] | beforechess[1] == curchess[1]) {
if (findnumb(beforechess[0], beforechess[1], curchess[0], curchess[1]) == 1&curchess[2]!=0|findnumb(beforechess[0], beforechess[1], curchess[0], curchess[1]) == 0&curchess[2]==0) {
ifflag = 1;
}
}
}
// 卒的走法
else if(who == 7) {
if(beforechess[0]<5&beforechess[1]==curchess[1]&beforechess[0]-curchess[0]==-1|beforechess[0]>4&beforechess[1]==curchess[1]&beforechess[0]-curchess[0]==-1|beforechess[0]>4&beforechess[0]==curchess[0]&Math.abs(beforechess[1]-curchess[1])==1) {
ifflag = 1;
}
}
// 兵的走法
else if(who == 77) {
if(beforechess[0]>4&beforechess[1]==curchess[1]&beforechess[0]-curchess[0]==1|beforechess[0]<5&beforechess[1]==curchess[1]&beforechess[0]-curchess[0]==1|beforechess[0]<5&beforechess[0]==curchess[0]&Math.abs(beforechess[1]-curchess[1])==1) {
ifflag = 1;
}
}
System.out.println("ifflag="+ifflag);
return ifflag;
}

// 找到某一起點到終點中含有的棋子數
public int findnumb(int r1, int c1, int r2, int c2) {
int numb = 0;
if (r1 == r2) {
for (int i = Math.min(c1, c2) + 1; i < Math.max(c1, c2); i++) {
if (flag[r1][i] > 0) {
numb++;
}
}
} else if (c1 == c2) {
for (int i = Math.min(r1, r2) + 1; i < Math.max(r1, r2); i++) {
if (flag[i][c1] > 0) {
numb++;
}
}
}
return numb;
}

public void ifwin() {
if(chessflag == 1 & curchess[2] == 5) {
System.out.println("紅方勝利");
new RedWin().init();
}else if(chessflag == 2 & curchess[2] == 55) {
System.out.println("黑方勝利");
new BlackWin().init();
}
}

public void renew() {
flag = new int[][] { { 1, 2, 3, 4, 5, 4, 3, 2, 1 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 6, 0, 0, 0, 0, 0, 6, 0 }, { 7, 0, 7, 0, 7, 0, 7, 0, 7 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 77, 0, 77, 0, 77, 0, 77, 0, 77 }, { 0, 66, 0, 0, 0, 0, 0, 66, 0 },
{ 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 11, 22, 33, 44, 55, 44, 33, 22, 11 } };
chessflag = 1;
r = -1;
x1=0;y1=0;x2=0;y2=0;index=0;beindex=0;
chessflag = 1;// 紅方先走為1
lianbiao = new int[99999][6];// 棋子初始位置,現(xiàn)在的位置,棋子的編號,棋子占的位本來的棋子的編號
curchess = new int[3];
beforechess = new int[3];
}

public void Regret_Chess(){
r = -1;
if (index > 0) {
flag[lianbiao[index - 1][0]][lianbiao[index - 1][1]] = lianbiao[index - 1][4];
flag[lianbiao[index - 1][2]][lianbiao[index - 1][3]] = lianbiao[index - 1][5];
rechessflag();
index--;
}
}

public void actionPerformed(ActionEvent e) {
// 獲取按鈕上的文字
action = e.getActionCommand();

if (action.equals("開始游戲")) {
System.out.println("開始游戲");
renew();
ui.repaint();
} else if (action.equals("重新開始")) {
System.out.println("重新開始");
renew();
ui.repaint();
} else if (action.equals("悔棋")) {
System.out.println("悔棋");
Regret_Chess();
ui.repaint();
}
}

}
init

init.java文件

package 中國象棋; // 自己的包的名字

public interface init {

public int x00 = 70;
public int x0 = 105;
public int y0 = 100;
public int row = 10;
public int column = 9;
public int chesssize = 67;
public int size = 76;
}
BlackWin

Black.java文件

package 中國象棋; // 自己的包的名字

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;

public class BlackWin extends JFrame {

private static final long serialVersionUID = 1L;
private Listener ls;

public void init() {
// 設置窗口屬性
this.setSize(400, 100);
this.setDefaultCloseOperation(3);
this.setLayout(new BorderLayout());
this.setLocationByPlatform(true);
this.setResizable(false);

// 設置窗口居中顯示
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setLocation(screenSize.width / 2 - getSize().width / 2, screenSize.height / 2 - getSize().height / 2);

// 添加JPanel
JPanel jp = new JPanel();

// 添加靜態(tài)文本并設置其屬性
JLabel jl = new JLabel("游戲結束,黑方勝利");
jl.setSize(200, 30);
jl.setFont(new Font("微軟雅黑", Font.BOLD, 16));
jl.setHorizontalAlignment(SwingConstants.CENTER);

// 添加按鈕
JButton jbt1 = new JButton("退出");
JButton jbt2 = new JButton("返回");
Listener re = new Listener();
// 設置當點擊退出時退出游戲
jbt1.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
System.exit(0);
}
});

// 設置當點擊確定時重新開始游戲
jbt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
re.renew();
setVisible(false); //關閉窗口
dispose(); //消除進程
}
});

// 把JPanel和文本添加到窗體上,并把按鈕添加到JPanel上
this.add(jl, BorderLayout.CENTER);
this.add(jp, BorderLayout.SOUTH);
jp.add(jbt1);
jp.add(jbt2);

// 創(chuàng)建監(jiān)聽器對象
ls = new Listener();

// 添加按鈕監(jiān)聽器
jbt1.addActionListener(ls);
jbt2.addActionListener(ls);

// 設置窗體可見
this.setVisible(true);

// 獲取畫布命名為g
Graphics g = this.getGraphics();

// 設置監(jiān)聽器監(jiān)聽的的畫布是g畫布
ls.setG(g);
}

public static void mian(String[] args) {
BlackWin ot = new BlackWin();
ot.init();
}
}
RedWin

Red.java文件

package 中國象棋; // 自己的包的名字

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.FlowLayout;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.SwingConstants;


public class RedWin extends JFrame{

/**
*
*/
private static final long serialVersionUID = 1L;
private Listener ls;

public void init() {
this.setSize(400, 100);
this.setDefaultCloseOperation(3);
this.setLayout(new BorderLayout());
this.setLocationByPlatform(true);
this.setResizable(false);

Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setLocation(screenSize.width / 2 - getSize().width / 2, screenSize.height / 2 - getSize().height / 2);

ls = new Listener();
Listener re = new Listener();
JPanel jp = new JPanel();
jp.setLayout(new FlowLayout());
JLabel jl = new JLabel("游戲結束,紅方勝利");
JButton jt1= new JButton("退出");
JButton jt2= new JButton("返回");
jl.setFont(new Font("微軟雅黑",Font.BOLD,16));
jl.setHorizontalAlignment(SwingConstants.CENTER);
jt1.addActionListener(new ActionListener() {
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
System.exit(0);
}
});

jt2.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent e) {
re.renew();
setVisible(false); //關閉窗口
dispose(); //消除進程
}
});

this.add(jp, BorderLayout.SOUTH);
this.add(jl, BorderLayout.NORTH);
jp.add(jt1);
jp.add(jt2);
jt1.addActionListener(ls);
jt2.addActionListener(ls);
this.setVisible(true);
Graphics g = this.getGraphics();
ls.setG(g);
}

public static void mian(String[] args) {
RedWin ot = new RedWin();
ot.init();
}
}


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

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