中国象棋2

使用Java编写中国象棋2

主程序:

public class Zgxq extends JFrame implements ActionListener, MouseListener, Runnable {
	Image imgArr[];
	final int SIZE = 70;
	JLabel play[] = new JLabel[32];
	// 工具栏
	JToolBar jmain;
	// 当前信息
	JLabel text;
	// 重新开始
	JButton anew;
	// 悔棋
	JButton repent;
	// 退出
	JButton exit;
	// 窗格
	Container con;
	// 规则
	ChessRule rule;
	/**
	 ** chessManClick=true chessManClick=false
	 * 
	 * @param args
	 */

	int chesscolor = 0;
	/**
	 * chessPlayClick=1黑棋走棋 chessPlayClick=2红棋走棋 默认红棋 chessPlayClick=3双方都不能走棋
	 * 
	 * @param args
	 */
	Thread tmain;
	// 把第一次的单击棋子给线程响应
	static int Man, i;
	int data[][] = { { 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 },
			{ 8, 0, 8, 0, 8, 0, 8, 0, 8 }, { 0, 9, 0, 0, 0, 0, 0, 9, 0 }, { 0, 0, 0, 0, 0, 0, 0, 0, 0 },
			{ 10, 11, 12, 13, 14, 13, 12, 11, 10 }, };

	public static void main(String[] args) {
		Zgxq a = new Zgxq();
		a.showUI();
	}

	public void showUI() {
		// 获行客格引用
		con = this.getContentPane();
		con.setLayout(null);
		// 创建工具栏
		jmain = new JToolBar();
		text = new JLabel("欢迎使用象棋对弈系统");
		text.setToolTipText("信息提示");
		anew = new JButton("新游戏");
		anew.setToolTipText("重新开始新的一局");
		repent = new JButton("悔棋");
		repent.setToolTipText("返回到上次走棋的位置");
		exit = new JButton("退出");
		exit.setToolTipText("退出象棋程序");
		jmain.setLayout(new GridLayout(0, 1));
		jmain.add(text);
		jmain.add(anew);
		jmain.add(repent);
		jmain.add(exit);
		jmain.setBounds(725, 50, 20, 10);
		con.add(jmain);
		// 注册按钮监听器
		anew.addActionListener(this);
		repent.addActionListener(this);
		exit.addActionListener(this);
		// 注册棋子监听器
		this.addMouseListener(this);

		for (int i = 0; i < 32; i++) {
			play[i] = new JLabel();

			con.add(play[i]);
			play[i].addMouseListener(this);
		}

		loadImage();
		this.setTitle("中国象棋");
		this.setLocation(500,100);//在屏幕中设置显示的位置
		this.setSize(10 * SIZE + 200, 11 * SIZE);
		this.setDefaultCloseOperation(EXIT_ON_CLOSE);
		this.setVisible(true);
	}
	// 图片
	ImageIcon icon = new ImageIcon("C:\\Users\\lenovo\\Desktop\\qiang\\test.jpg");

	public void paint(Graphics g) {
		// 重绘窗体(界面)
		super.paint(g);
		// 同时绘制棋盘
		drawChessTable(g);
		// 画棋子
		drawChesses(g);

	}
	// 初始化游戏数据

	public void drawChesses(Graphics g) {
		System.out.println("paint " + data[0][0]);
		for (int row = 0; row < data.length; row++) {
			for (int col = 0; col < data[row].length; col++) {
				// 获取棋子定义一个数组对象
				int chess = data[row][col];
				if (chess == 0) {
					// 空位
					continue;
				}
				// 获取相对应的图片对象
				Image img = imgArr[chess];

				// 计算绘制的坐标
				int x = (SIZE - 20) + SIZE * col;
				int y = (SIZE - 20) + SIZE * row;

				g.drawImage(img, x, y, null);
			}

		}
	}

	// 加载图片
	public void loadImage() {
		// 创建数组
		imgArr = new Image[15];
		for (int i = 0; i < 15; i++) {
			imgArr[i] = new ImageIcon(("img\\BA" + i + ".png")).getImage();
			System.out.println("img\\BA" + i + ".png");
		}

	}
	int x1, x2, y1, y2, row, col, row2, col2;

	public void drawChessTable(Graphics g) {

		// 画图片
		g.drawImage(icon.getImage(), 0, 0, 10 * SIZE, 11 * SIZE, null);
		// 画棋盘横线和纵线
		for (int i = 1; i < 11; i++) {
			for (int j = 1; j < 10; j++) {
				if (i < 6) {
					g.drawLine(SIZE, SIZE * i, 9 * SIZE, SIZE * i);
					g.drawLine(SIZE * j, SIZE, SIZE * j, 5 * SIZE);
				} else {
					g.drawLine(SIZE, i * SIZE, 9 * SIZE, SIZE * i);
					g.drawLine(SIZE * j, 6 * SIZE, SIZE * j, 10 * SIZE);
				}
				g.drawLine(4 * SIZE, SIZE, 6 * SIZE, 3 * SIZE);
				g.drawLine(6 * SIZE, SIZE, 4 * SIZE, 3 * SIZE);
				g.drawLine(4 * SIZE, 8 * SIZE, 6 * SIZE, 10 * SIZE);
				g.drawLine(6 * SIZE, 8 * SIZE, 4 * SIZE, 10 * SIZE);
				g.drawLine(SIZE, 5 * SIZE, SIZE, 6 * SIZE);
				g.drawLine(9 * SIZE, 5 * SIZE, 9 * SIZE, 6 * SIZE);
				g.setFont(new Font("", Font.BOLD, 50));
				g.drawString("楚 河", SIZE * 2, 5 * SIZE + 50);
				g.drawString("汉 界", 6 * SIZE, 5 * SIZE + 50);
			}

		}

	}

	@Override
	public void mouseReleased(MouseEvent e) {
		// TODO Auto-generated method stub

		x2 = e.getX();
		y2 = e.getY();
		col2 = (x2 - 35) / 70;
		row2 = (y2 - 35) / 70;
		System.out.println("col2" + col2);
		System.out.println("row2" + row2);

		this.repaint();

		ChessJun jun = new ChessJun();
		Chessma ma = new Chessma();
		Chessxiang xiang = new Chessxiang();
		Chessshi shi = new Chessshi();
		Chesspao pao = new Chesspao();
		Chessbing bing = new Chessbing();
		Chessjiang jiang = new Chessjiang();
		boolean canMove = false;
		System.out.println(chesscolor + "    chesscolor" + data[row][col] + "+");
		if(chesscolor==-1){
			return;
		}else if ((chesscolor == 0 && data[row][col] > 7) || (chesscolor == 1 && data[row][col] <= 7)) {
			// System.out.println(data[row2][col2]+"+++++");
			System.out.println(data[row][col] + "+");
			return;
		} else if ((0 < data[row][col] && data[row][col] <= 7 && 0 < data[row2][col2] && data[row2][col2] <= 7)
				|| (data[row][col] > 7 && data[row2][col2] > 7)) {
			return;
		} else {
			{
				if (data[row][col] == 1 || data[row][col] == 10) {
					canMove = jun.move(row, col, row2, col2, data);

				} else if (data[row][col] == 2 || data[row][col] == 11) {
					canMove = ma.move(row, col, row2, col2, data);
					System.out.println("=====  ma");
				} else if (data[row][col] == 3 || data[row][col] == 12) {
					canMove = xiang.move(row, col, row2, col2, data);

				} else if (data[row][col] == 4 || data[row][col] == 13) {
					canMove = shi.move(row, col, row2, col2, data);

				} else if (data[row][col] == 5 || data[row][col] == 14) {
					canMove = jiang.move(row, col, row2, col2, data);
				} else if (data[row][col] == 6 || data[row][col] == 9) {
					canMove = pao.move(row, col, row2, col2, data);
				} else if (data[row][col] == 7 || data[row][col] == 8) {
					canMove = bing.move(row, col, row2, col2, data);
				}
				System.out.println("当前 棋子 ::: " + data[row][col]);
				if (canMove) {
                    if(data[row2][col2]!=5&&data[row2][col2]!=14){                    	
					
                    data[row2][col2] = data[row][col];
					data[row][col] = 0;
					System.out.println("该步合法");
					this.repaint();
					chesscolor = (chesscolor + 1) % 2;
                    }else if(data[row2][col2]==5){
                    	data[row2][col2] = data[row][col];
    					data[row][col] = 0;
    					
    					Result winner=new Result();
    					winner.showresult();
    					chesscolor=-1;
    				  //  System.out.println("红方胜")	;
                    }else if(data[row2][col2]==14){
                    	data[row2][col2] = data[row][col];
    					data[row][col] = 0;
    					
    					Resultblack winner=new Resultblack();
    					winner.showresult();
    					chesscolor=-1;
                     //System.out.println("黑方胜");
                    }
				} else {
					System.out.println("该步不合法");
				}
			}
			

		}
	}

	@Override
	public void mousePressed(MouseEvent e) {
		// TODO Auto-generated method stub
		x1 = e.getX();
		y1 = e.getY();
		col = (x1 - 35) / 70;
		row = (y1 - 35) / 70;
		System.out.println("col" + col);
		System.out.println("row" + row);
	}

	@Override
	public void mouseClicked(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseEntered(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void mouseExited(MouseEvent e) {
		// TODO Auto-generated method stub

	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub

	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值