Java实践(十二)——中国象棋

本文介绍了使用Java实现中国象棋的实践,包括机机对弈(红方先手)、棋谱的保存与读取功能。在对弈过程中,遵循象棋规则,支持鼠标操作。完成棋局后,可保存棋谱,并提供演示棋谱功能。参考代码涵盖主类、棋子坐标、规则及棋谱制作和演示相关类。
摘要由CSDN通过智能技术生成

一、实践目的:

1.鼠标点击、拖动等事件的应用与区别

2.棋谱文件的保存与读取

3.完善象棋的规则。

二、实践内容:

中国象棋历史悠久,吸引了无数的人研究,现对中国象棋的对战和实现棋谱的制作做如下的设计和说明,供大家参考学习。

1、机机对弈,红方先手。在符合规则的情况下拖动棋子到目的地,松鼠标落子。


人人对弈图

2、制作棋谱,选择制作棋谱菜单后,对弈开始,并记录了下棋过程。


选择“制作棋谱”菜单




棋谱制作完毕红方胜出

一方胜出后弹出胜利消息对话框。点击确定后,选择“保存棋谱”菜单,弹出保存文件对话框。


保存棋谱对话框

3.演示棋谱,选择演示棋谱菜单后,弹出打开对话框,选择保存好的棋谱,开始演示。


演示棋谱对话框



演示棋谱过程(自动和手动两种)

三、参考代码:

1.象棋主类 文件ChineseChess.java

package cn.edu.ouc.chineseChess;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.LinkedList;

/**
 * 象棋主类
 * 
 * @author cnlht
 */
public class ChineseChess extends JFrame implements ActionListener {
	ChessBoard board = null;
	Demon demon = null;
	MakeChessManual record = null;
	Container con = null;
	JMenuBar bar;
	JMenu fileMenu;
	JMenuItem 制作棋谱, 保存棋谱, 演示棋谱;
	JFileChooser fileChooser = null;
	LinkedList 棋谱 = null;

	public ChineseChess() {
		bar = new JMenuBar();
		fileMenu = new JMenu("中国象棋");
		制作棋谱 = new JMenuItem("制作棋谱");
		保存棋谱 = new JMenuItem("保存棋谱");
		保存棋谱.setEnabled(false);
		演示棋谱 = new JMenuItem("演示棋谱");
		fileMenu.add(制作棋谱);
		fileMenu.add(保存棋谱);
		fileMenu.add(演示棋谱);
		bar.add(fileMenu);
		setJMenuBar(bar);
		setTitle(制作棋谱.getText());
		制作棋谱.addActionListener(this);
		保存棋谱.addActionListener(this);
		演示棋谱.addActionListener(this);
		board = new ChessBoard(45, 45, 9, 10);
		record = board.record;
		con = getContentPane();
		JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, true,
				board, record);
		split.setDividerSize(5);
		split.setDividerLocation(460);
		con.add(split, BorderLayout.CENTER);
		addWindowListener(new WindowAdapter() {
			public void windowClosing(WindowEvent e) {
				System.exit(0);
			}
		});
		setVisible(true);
		setBounds(60, 20, 690, 540);
		fileChooser = new JFileChooser();
		con.validate();
		validate();
	}

	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == 制作棋谱) {
			con.removeAll();
			保存棋谱.setEnabled(true);
			this.setTitle(制作棋谱.getText());
			board = new ChessBoard(45, 45, 9, 10);
			record = board.record;
			JSplitPane split = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT,
					true, board, record);
			split.setDividerSize(5);
			split.setDividerLocation(460);
			con.add(split, BorderLayout.CENTER);
			validate();
		}
		if (e.getSource() == 保存棋谱) {
			int state = fileChooser.showSaveDialog(null);
			File saveFile = fileChooser.getSelectedFile();
			if (saveFile != null && state == JFileChooser.APPROVE_OPTION) {
				try {
					FileOutputStream outOne = new FileOutputStream(saveFile);
					ObjectOutputStream outTwo = new ObjectOutputStream(outOne);
					outTwo.writeObject(record.获取棋谱());
					outOne.close();
					outTwo.close();
				} catch (IOException event) {
				}
			}
		}
		if (e.getSource() == 演示棋谱) {
			con.removeAll();
			con.repaint();
			con.validate();
			validate();
			保存棋谱.setEnabled(false);

			int state = fileChooser.showOpenDialog(null);
			File openFile = fileChooser.getSelectedFile();
			if (openFile != null && state == JFileChooser.APPROVE_OPTION) {
				try {
					FileInputStream inOne = new FileInputStream(openFile);
					ObjectInputStream inTwo = new ObjectInputStream(inOne);
					棋谱 = (LinkedList) inTwo.readObject();
					inOne.close();
					inTwo.close();
					ChessBoard board = new ChessBoard(45, 45, 9, 10);
					demon = new Demon(board);
					demon.set棋谱(棋谱);
					con.add(demon, BorderLayout.CENTER);
					con.validate();
					validate();
					this.setTitle(演示棋谱.getText() + ":" + openFile);
				} catch (Exception event) {
					JLabel label = new JLabel("不是棋谱文件");
					label.setFont(new Font("隶书", Font.BOLD, 60));
					label.setForeground(Color.red);
					label.setHorizontalAlignment(SwingConstants.CENTER);
					con.add(label, BorderLayout.CENTER);
					con.validate();
					this.setTitle("没有打开棋谱");
					validate();
				}
			} else {
				JLabel label = new JLabel("没有打开棋谱文件呢");
				label.setFont(new Font("隶书", Font.BOLD, 50));
				label.setForeground(Color.pink);
				label.setHorizontalAlignment(SwingConstants.CENTER);
				con.add(label, BorderLayout.CENTER);
				con.validate();
				this.setTitle("没有打开棋谱文件呢");
				validate();
			}
		}
	}

	public static void main(String args[]) {
		new ChineseChess();
	}
}

2.象棋棋盘类文件ChessBoard.java

package cn.edu.ouc.chineseChess;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

/**
 * 棋盘类
 * 
 * @author cnlht
 */
public class ChessBoard extends JPanel implements MouseListener,
		MouseMotionListener {
	public ChessPoint point[][];
	public int unitWidth, unitHeight;
	private int x轴长, y轴长;
	private int x, y;
	private Image img;
	protected Image pieceImg;
	private boolean move = false;
	public String 红方颜色 = "红方", 黑方颜色 = "黑方";
	ChessPiece 红车1, 红车2, 红马1, 红马2, 红相1, 红相2, 红帅, 红士1, 红士2, 红兵1, 红兵2, 红兵3, 红兵4,
			红兵5, 红炮1, 红炮2;
	ChessPiece 黑车1, 黑车2, 黑马1, 黑马2, 黑将, 黑士1, 黑士2, 黑卒1, 黑卒2, 黑卒3, 黑卒4, 黑卒5, 黑象1,
			黑象2, 黑炮1, 黑炮2;

	int startX, startY;
	int startI, startJ;
	public boolean 红方走棋 = true, 黑方走棋 = false;
	Rule rule = null;
	public MakeChessManual record = null;

	public ChessBoard(int w, int h, int r, int c) {
		setLayout(null);
		addMouseListener(this);
		addMouseMotionListener(this);
		Color bc = getBackground();
		unitWidth = w;
		unitHeight = h;
		x轴长 = r;
		y轴长 = c;

		point = new ChessPoint[r + 1][c + 1];

		for (int i = 1; i <= r; i++) {
			for (int j = 1; j <= c; j++) {
				point[i][j] = new ChessPoint(i * unitWidth, j * unitHeight,
						false);
			}
		}

		rule = new Rule(this, point);
		record = new MakeChessManual(this, point);

		img = Toolkit.getDefaultToolkit().getImage("board.jpg");
		pieceImg = Toolkit.getDefaultToolkit().getImage("piece.gif");
		
		红车1 = new ChessPiece("車", Color.red, bc, w - 4, h - 4, this);
		红车1.set棋子类别(红方颜色);
		红车2 = new ChessPiece("車", Color.red, bc, 
以下是一个简单的JAVA实现中国象棋的代码示例: ```java //定义子类 class ChessPiece { String name; // 子名称 int x, y; // 子坐标 public ChessPiece(String name, int x, int y) { this.name = name; this.x = x; this.y = y; } public String toString() { return name + " (" + x + "," + y + ")"; } } //定义盘类 class ChessBoard { ChessPiece[][] board; public ChessBoard() { board = new ChessPiece[10][9]; } // 初始化盘 public void init() { // 初始化黑方 board[0][0] = new ChessPiece("車", 0, 0); board[0][1] = new ChessPiece("馬", 0, 1); board[0][2] = new ChessPiece("象", 0, 2); board[0][3] = new ChessPiece("士", 0, 3); board[0][4] = new ChessPiece("將", 0, 4); board[0][5] = new ChessPiece("士", 0, 5); board[0][6] = new ChessPiece("象", 0, 6); board[0][7] = new ChessPiece("馬", 0, 7); board[0][8] = new ChessPiece("車", 0, 8); board[2][1] = new ChessPiece("炮", 2, 1); board[2][7] = new ChessPiece("炮", 2, 7); board[3][0] = new ChessPiece("卒", 3, 0); board[3][2] = new ChessPiece("卒", 3, 2); board[3][4] = new ChessPiece("卒", 3, 4); board[3][6] = new ChessPiece("卒", 3, 6); board[3][8] = new ChessPiece("卒", 3, 8); // 初始化红方 board[9][0] = new ChessPiece("車", 9, 0); board[9][1] = new ChessPiece("馬", 9, 1); board[9][2] = new ChessPiece("象", 9, 2); board[9][3] = new ChessPiece("士", 9, 3); board[9][4] = new ChessPiece("帥", 9, 4); board[9][5] = new ChessPiece("士", 9, 5); board[9][6] = new ChessPiece("象", 9, 6); board[9][7] = new ChessPiece("馬", 9, 7); board[9][8] = new ChessPiece("車", 9, 8); board[7][1] = new ChessPiece("炮", 7, 1); board[7][7] = new ChessPiece("炮", 7, 7); board[6][0] = new ChessPiece("兵", 6, 0); board[6][2] = new ChessPiece("兵", 6, 2); board[6][4] = new ChessPiece("兵", 6, 4); board[6][6] = new ChessPiece("兵", 6, 6); board[6][8] = new ChessPiece("兵", 6, 8); } // 打印盘 public void print() { for (int i = 0; i < board.length; i++) { for (int j = 0; j < board[i].length; j++) { if (board[i][j] != null) { System.out.print(board[i][j] + "\t"); } else { System.out.print("\t"); } } System.out.println(""); } } // 移动子 public void move(int startX, int startY, int endX, int endY) { ChessPiece piece = board[startX][startY]; board[startX][startY] = null; board[endX][endY] = piece; piece.x = endX; piece.y = endY; } } //测试类 public class ChessTest { public static void main(String[] args) { ChessBoard board = new ChessBoard(); board.init(); board.print(); board.move(3, 0, 4, 0); board.print(); } } ``` 以上代码是一个简单的实现,具体规则和完整实现可以根据需求进行扩展。
评论 33
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值