Java棋類游戲?qū)嵺`之中國(guó)象棋
本文實(shí)例講述了java實(shí)現(xiàn)的中國(guó)象棋游戲代碼,分享給大家供大家參考,具體代碼如下
一、實(shí)踐目的:
1.鼠標(biāo)點(diǎn)擊、拖動(dòng)等事件的應(yīng)用與區(qū)別
2.棋譜文件的保存與讀取
3.完善象棋的規(guī)則。
二、實(shí)踐內(nèi)容:
中國(guó)象棋歷史悠久,吸引了無數(shù)的人研究,現(xiàn)對(duì)中國(guó)象棋的對(duì)戰(zhàn)和實(shí)現(xiàn)棋譜的制作做如下的設(shè)計(jì)和說明,供大家參考學(xué)習(xí)。
1、機(jī)機(jī)對(duì)弈,紅方先手。在符合規(guī)則的情況下拖動(dòng)棋子到目的地,松鼠標(biāo)落子。

人人對(duì)弈圖
2、制作棋譜,選擇制作棋譜菜單后,對(duì)弈開始,并記錄了下棋過程。

選擇“制作棋譜”菜單


棋譜制作完畢紅方勝出
一方勝出后彈出勝利消息對(duì)話框。點(diǎn)擊確定后,選擇“保存棋譜”菜單,彈出保存文件對(duì)話框。

保存棋譜對(duì)話框
3.演示棋譜,選擇演示棋譜菜單后,彈出打開對(duì)話框,選擇保存好的棋譜,開始演示。

演示棋譜對(duì)話框

演示棋譜過程(自動(dòng)和手動(dòng)兩種)
三、參考代碼:
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("中國(guó)象棋");
制作棋譜 = 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軸長(zhǎng), y軸長(zhǎng);
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軸長(zhǎng) = r;
y軸長(zhǎng) = 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, w - 4, h - 4, this);
紅車2.set棋子類別(紅方顏色);
紅馬1 = new ChessPiece("馬", Color.red, bc, w - 4, h - 4, this);
紅馬1.set棋子類別(紅方顏色);
紅馬2 = new ChessPiece("馬", Color.red, bc, w - 4, h - 4, this);
紅馬2.set棋子類別(紅方顏色);
紅炮1 = new ChessPiece("炮", Color.red, bc, w - 4, h - 4, this);
紅炮1.set棋子類別(紅方顏色);
紅炮2 = new ChessPiece("炮", Color.red, bc, w - 4, h - 4, this);
紅炮2.set棋子類別(紅方顏色);
紅相1 = new ChessPiece("相", Color.red, bc, w - 4, h - 4, this);
紅相1.set棋子類別(紅方顏色);
紅相2 = new ChessPiece("相", Color.red, bc, w - 4, h - 4, this);
紅相2.set棋子類別(紅方顏色);
紅士1 = new ChessPiece("仕", Color.red, bc, w - 4, h - 4, this);
紅士1.set棋子類別(紅方顏色);
紅士2 = new ChessPiece("仕", Color.red, bc, w - 4, h - 4, this);
紅士2.set棋子類別(紅方顏色);
紅帥 = new ChessPiece("帥", Color.red, bc, w - 4, h - 4, this);
紅帥.set棋子類別(紅方顏色);
紅兵1 = new ChessPiece("兵", Color.red, bc, w - 4, h - 4, this);
紅兵1.set棋子類別(紅方顏色);
紅兵2 = new ChessPiece("兵", Color.red, bc, w - 4, h - 4, this);
紅兵2.set棋子類別(紅方顏色);
紅兵3 = new ChessPiece("兵", Color.red, bc, w - 4, h - 4, this);
紅兵3.set棋子類別(紅方顏色);
紅兵4 = new ChessPiece("兵", Color.red, bc, w - 4, h - 4, this);
紅兵4.set棋子類別(紅方顏色);
紅兵5 = new ChessPiece("兵", Color.red, bc, w - 4, h - 4, this);
紅兵5.set棋子類別(紅方顏色);
黑將 = new ChessPiece("將", Color.black, bc, w - 4, h - 4, this);
黑將.set棋子類別(黑方顏色);
黑士1 = new ChessPiece("士", Color.black, bc, w - 4, h - 4, this);
黑士1.set棋子類別(黑方顏色);
黑士2 = new ChessPiece("士", Color.black, bc, w - 4, h - 4, this);
黑士2.set棋子類別(黑方顏色);
黑車1 = new ChessPiece("車", Color.black, bc, w - 4, h - 4, this);
黑車1.set棋子類別(黑方顏色);
黑車2 = new ChessPiece("車", Color.black, bc, w - 4, h - 4, this);
黑車2.set棋子類別(黑方顏色);
黑炮1 = new ChessPiece("炮", Color.black, bc, w - 4, h - 4, this);
黑炮1.set棋子類別(黑方顏色);
黑炮2 = new ChessPiece("炮", Color.black, bc, w - 4, h - 4, this);
黑炮2.set棋子類別(黑方顏色);
黑象1 = new ChessPiece("象", Color.black, bc, w - 4, h - 4, this);
黑象1.set棋子類別(黑方顏色);
黑象2 = new ChessPiece("象", Color.black, bc, w - 4, h - 4, this);
黑象2.set棋子類別(黑方顏色);
黑馬1 = new ChessPiece("馬", Color.black, bc, w - 4, h - 4, this);
黑馬1.set棋子類別(黑方顏色);
黑馬2 = new ChessPiece("馬", Color.black, bc, w - 4, h - 4, this);
黑馬2.set棋子類別(黑方顏色);
黑卒1 = new ChessPiece("卒", Color.black, bc, w - 4, h - 4, this);
黑卒1.set棋子類別(黑方顏色);
黑卒2 = new ChessPiece("卒", Color.black, bc, w - 4, h - 4, this);
黑卒2.set棋子類別(黑方顏色);
黑卒3 = new ChessPiece("卒", Color.black, bc, w - 4, h - 4, this);
黑卒3.set棋子類別(黑方顏色);
黑卒4 = new ChessPiece("卒", Color.black, bc, w - 4, h - 4, this);
黑卒4.set棋子類別(黑方顏色);
黑卒5 = new ChessPiece("卒", Color.black, bc, w - 4, h - 4, this);
黑卒5.set棋子類別(黑方顏色);
point[1][10].setPiece(紅車1, this);
point[2][10].setPiece(紅馬1, this);
point[3][10].setPiece(紅相1, this);
point[4][10].setPiece(紅士1, this);
point[5][10].setPiece(紅帥, this);
point[6][10].setPiece(紅士2, this);
point[7][10].setPiece(紅相2, this);
point[8][10].setPiece(紅馬2, this);
point[9][10].setPiece(紅車2, this);
point[2][8].setPiece(紅炮1, this);
point[8][8].setPiece(紅炮2, this);
point[1][7].setPiece(紅兵1, this);
point[3][7].setPiece(紅兵2, this);
point[5][7].setPiece(紅兵3, this);
point[7][7].setPiece(紅兵4, this);
point[9][7].setPiece(紅兵5, this);
point[1][1].setPiece(黑車1, this);
point[2][1].setPiece(黑馬1, this);
point[3][1].setPiece(黑象1, this);
point[4][1].setPiece(黑士1, this);
point[5][1].setPiece(黑將, this);
point[6][1].setPiece(黑士2, this);
point[7][1].setPiece(黑象2, this);
point[8][1].setPiece(黑馬2, this);
point[9][1].setPiece(黑車2, this);
point[2][3].setPiece(黑炮1, this);
point[8][3].setPiece(黑炮2, this);
point[1][4].setPiece(黑卒1, this);
point[3][4].setPiece(黑卒2, this);
point[5][4].setPiece(黑卒3, this);
point[7][4].setPiece(黑卒4, this);
point[9][4].setPiece(黑卒5, this);
}
public void paintComponent(Graphics g) {
super.paintComponent(g);
int imgWidth = img.getWidth(this);
int imgHeight = img.getHeight(this);// 獲得圖片的寬度與高度
int FWidth = getWidth();
int FHeight = getHeight();// 獲得窗口的寬度與高度
int x = (FWidth - imgWidth) / 2;
int y = (FHeight - imgHeight) / 2;
g.drawImage(img, x, y, null);
for (int j = 1; j <= y軸長(zhǎng); j++) {
g.drawLine(point[1][j].x, point[1][j].y, point[x軸長(zhǎng)][j].x,
point[x軸長(zhǎng)][j].y);
}
for (int i = 1; i <= x軸長(zhǎng); i++) {
if (i != 1 && i != x軸長(zhǎng)) {
g.drawLine(point[i][1].x, point[i][1].y, point[i][y軸長(zhǎng) - 5].x,
point[i][y軸長(zhǎng) - 5].y);
g.drawLine(point[i][y軸長(zhǎng) - 4].x, point[i][y軸長(zhǎng) - 4].y,
point[i][y軸長(zhǎng)].x, point[i][y軸長(zhǎng)].y);
} else {
g.drawLine(point[i][1].x, point[i][1].y, point[i][y軸長(zhǎng)].x,
point[i][y軸長(zhǎng)].y);
}
}
g.drawLine(point[4][1].x, point[4][1].y, point[6][3].x, point[6][3].y);
g.drawLine(point[6][1].x, point[6][1].y, point[4][3].x, point[4][3].y);
g.drawLine(point[4][8].x, point[4][8].y, point[6][y軸長(zhǎng)].x,
point[6][y軸長(zhǎng)].y);
g.drawLine(point[4][y軸長(zhǎng)].x, point[4][y軸長(zhǎng)].y, point[6][8].x,
point[6][8].y);
for (int i = 1; i <= x軸長(zhǎng); i++) {
g.drawString("" + i, i * unitWidth, unitHeight / 2);
}
int j = 1;
for (char c = 'A'; c <= 'J'; c++) {
g.drawString("" + c, unitWidth / 4, j * unitHeight);
j++;
}
}
/**鼠標(biāo)按下事件*/
public void mousePressed(MouseEvent e) {
ChessPiece piece = null;
Rectangle rect = null;
if (e.getSource() == this)
move = false;
if (move == false)
if (e.getSource() instanceof ChessPiece) {
piece = (ChessPiece) e.getSource();
startX = piece.getBounds().x;
startY = piece.getBounds().y;
rect = piece.getBounds();
for (int i = 1; i <= x軸長(zhǎng); i++) {
for (int j = 1; j <= y軸長(zhǎng); j++) {
int x = point[i][j].getX();
int y = point[i][j].getY();
if (rect.contains(x, y)) {
startI = i;
startJ = j;
break;
}
}
}
}
}
public void mouseMoved(MouseEvent e) {
}
/**鼠標(biāo)拖動(dòng)事件*/
public void mouseDragged(MouseEvent e) {
ChessPiece piece = null;
if (e.getSource() instanceof ChessPiece) {
piece = (ChessPiece) e.getSource();
move = true;
e = SwingUtilities.convertMouseEvent(piece, e, this);
}
if (e.getSource() == this) {
if (move && piece != null) {
x = e.getX();
y = e.getY();
if (紅方走棋 && ((piece.棋子類別()).equals(紅方顏色))) {
piece.setLocation(x - piece.getWidth() / 2,
y - piece.getHeight() / 2);
}
if (黑方走棋 && (piece.棋子類別().equals(黑方顏色))) {
piece.setLocation(x - piece.getWidth() / 2,
y - piece.getHeight() / 2);
}
}
}
}
/**松開鼠標(biāo)事件*/
public void mouseReleased(MouseEvent e) {
ChessPiece piece = null;
move = false;
Rectangle rect = null;
if (e.getSource() instanceof ChessPiece) {
piece = (ChessPiece) e.getSource();
rect = piece.getBounds();
e = SwingUtilities.convertMouseEvent(piece, e, this);
}
if (e.getSource() == this) {
boolean containChessPoint = false;
int x = 0, y = 0;
int m = 0, n = 0;
if (piece != null) {
for (int i = 1; i <= x軸長(zhǎng); i++) {
for (int j = 1; j <= y軸長(zhǎng); j++) {
x = point[i][j].getX();
y = point[i][j].getY();
if (rect.contains(x, y)) {
containChessPoint = true;
m = i;
n = j;
break;
}
}
}
}
if (piece != null && containChessPoint) {
Color pieceColor = piece.獲取棋子顏色();
if (point[m][n].isPiece()) {
Color c = (point[m][n].getPiece()).獲取棋子顏色();
if (pieceColor.getRGB() == c.getRGB()) {
piece.setLocation(startX, startY);
(point[startI][startJ]).set有棋子(true);
} else {
boolean ok = rule.movePieceRule(piece, startI, startJ,
m, n);
if (ok) {
ChessPiece pieceRemoved = point[m][n].getPiece();
point[m][n].reMovePiece(pieceRemoved, this);
point[m][n].setPiece(piece, this);
(point[startI][startJ]).set有棋子(false);
record.記錄棋譜(piece, startI, startJ, m, n);
record.記錄吃掉的棋子(pieceRemoved);
rule.isWine(pieceRemoved);
if (piece.棋子類別().equals(紅方顏色)) {
紅方走棋 = false;
黑方走棋 = true;
}
if (piece.棋子類別().equals(黑方顏色)) {
黑方走棋 = false;
紅方走棋 = true;
}
validate();
repaint();
} else {
piece.setLocation(startX, startY);
(point[startI][startJ]).set有棋子(true);
}
}
} else {
boolean ok = rule
.movePieceRule(piece, startI, startJ, m, n);
if (ok) {
point[m][n].setPiece(piece, this);
(point[startI][startJ]).set有棋子(false);
record.記錄棋譜(piece, startI, startJ, m, n);
record.記錄吃掉的棋子("沒吃棋子");
if (piece.棋子類別().equals(紅方顏色)) {
紅方走棋 = false;
黑方走棋 = true;
}
if (piece.棋子類別().equals(黑方顏色)) {
黑方走棋 = false;
紅方走棋 = true;
}
} else {
piece.setLocation(startX, startY);
(point[startI][startJ]).set有棋子(true);
}
}
}
if (piece != null && !containChessPoint) {
piece.setLocation(startX, startY);
(point[startI][startJ]).set有棋子(true);
}
}
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
}
3.棋子類文件ChessPiece.java
package cn.edu.ouc.chineseChess;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* 棋子類
*
* @author cnlht
*/
public class ChessPiece extends JLabel {
String name; // 棋子名字
Color backColor = null, foreColor;// 背景色和前景色
String 顏色類別 = null;
ChessBoard board = null;
int width, height;// 大小
public ChessPiece(String name, Color fc, Color bc, int width, int height,
ChessBoard board) {// 構(gòu)造棋子
this.name = name;
this.board = board;
this.width = width;
this.height = height;
foreColor = fc;
backColor = bc;
setSize(width, height);
setBackground(bc);
addMouseMotionListener(board);
addMouseListener(board);
}
// 繪制棋子
public void paint(Graphics g) {
g.drawImage(board.pieceImg, 2, 2, width-2, height-2, null);
g.setColor(foreColor);
g.setFont(new Font("楷體", Font.BOLD, 26));
g.drawString(name, 7, height - 8);// 在棋子上繪制 “棋子名”
g.setColor(Color.black);
//g.drawOval(1, 1, width - 1, height - 1);
float lineWidth = 2.3f;
((Graphics2D)g).setStroke(new BasicStroke(lineWidth));
((Graphics2D)g).drawOval(2, 2, width-2, height-2);
}
public int getWidth() {
return width;
}
public int getHeight() {
return height;
}
public String getName() {
return name;
}
public Color 獲取棋子顏色() {
return foreColor;
}
public void set棋子類別(String 類別) {
顏色類別 = 類別;
}
public String 棋子類別() {
return 顏色類別;
}
}
4.棋子點(diǎn)坐標(biāo)類文件
package cn.edu.ouc.chineseChess;
/**
* 棋點(diǎn)類
*
* @author cnlht
*/
public class ChessPoint {
/** 棋子坐標(biāo) */
int x, y;
/** 該坐標(biāo) 是否有子*/
boolean 有棋子;
/** 改坐標(biāo)的棋子 */
ChessPiece piece = null;
/** 坐標(biāo)所屬棋盤 */
ChessBoard board = null;
public ChessPoint(int x, int y, boolean boo) {
this.x = x;
this.y = y;
有棋子 = boo;
}
public boolean isPiece() {
return 有棋子;
}
public void set有棋子(boolean boo) {
有棋子 = boo;
}
public int getX() {
return x;
}
public int getY() {
return y;
}
// 設(shè)置改點(diǎn)棋子
public void setPiece(ChessPiece piece, ChessBoard board) {
this.board = board;
this.piece = piece;
board.add(piece);
int w = (board.unitWidth);
int h = (board.unitHeight);
piece.setBounds(x - w / 2, y - h / 2, w, h);// 棋子位置,寬度,高度
有棋子 = true;
board.validate();
}
public ChessPiece getPiece() {
return piece;
}
public void reMovePiece(ChessPiece piece, ChessBoard board) {
this.board = board;
this.piece = piece;
board.remove(piece);
board.validate();
有棋子 = false;
}
}
5.玩法規(guī)則類文件Rule.java
package cn.edu.ouc.chineseChess;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* 走棋規(guī)則類
*
* @author cnlht
*/
public class Rule {
ChessBoard board = null;
ChessPiece piece = null;
ChessPoint point[][];
int startI, startJ, endI, endJ;
public Rule(ChessBoard board, ChessPoint point[][]) {
this.board = board;
this.point = point;
}
public void isWine(ChessPiece piece) {
this.piece = piece;
if (piece.getName() == "將" || piece.getName() == "帥") {
if (piece.顏色類別 == "紅方") {
JOptionPane.showMessageDialog(null, "黑方 勝利!");
} else {
JOptionPane.showMessageDialog(null, "紅方 勝利!");
}
}
}
public boolean movePieceRule(ChessPiece piece, int startI, int startJ,
int endI, int endJ) {
this.piece = piece;
this.startI = startI;
this.startJ = startJ;
this.endI = endI;
this.endJ = endJ;
int minI = Math.min(startI, endI);
int maxI = Math.max(startI, endI);
int minJ = Math.min(startJ, endJ);
int maxJ = Math.max(startJ, endJ);
boolean 可否走棋 = false;
if (piece.getName().equals("車")) {
if (startI == endI) {
int j = 0;
for (j = minJ + 1; j <= maxJ - 1; j++) {
if (point[startI][j].isPiece()) {
可否走棋 = false;
break;
}
}
if (j == maxJ) {
可否走棋 = true;
}
} else if (startJ == endJ) {
int i = 0;
for (i = minI + 1; i <= maxI - 1; i++) {
if (point[i][startJ].isPiece()) {
可否走棋 = false;
break;
}
}
if (i == maxI) {
可否走棋 = true;
}
} else {
可否走棋 = false;
}
} else if (piece.getName().equals("車")) {
if (startI == endI) {
int j = 0;
for (j = minJ + 1; j <= maxJ - 1; j++) {
if (point[startI][j].isPiece()) {
可否走棋 = false;
break;
}
}
if (j == maxJ) {
可否走棋 = true;
}
} else if (startJ == endJ) {
int i = 0;
for (i = minI + 1; i <= maxI - 1; i++) {
if (point[i][startJ].isPiece()) {
可否走棋 = false;
break;
}
}
if (i == maxI) {
可否走棋 = true;
}
} else {
可否走棋 = false;
}
}else if (piece.getName().equals("馬")) {
int xAxle = Math.abs(startI - endI);
int yAxle = Math.abs(startJ - endJ);
if (xAxle == 2 && yAxle == 1) {
if (endI > startI) {
if (point[startI + 1][startJ].isPiece()) {
可否走棋 = false;
} else {
可否走棋 = true;
}
}
if (endI < startI) {
if (point[startI - 1][startJ].isPiece()) {
可否走棋 = false;
} else {
可否走棋 = true;
}
}
}else if (xAxle == 1 && yAxle == 2) {
if (endJ > startJ) {
if (point[startI][startJ + 1].isPiece()) {
可否走棋 = false;
} else {
可否走棋 = true;
}
}
if (endJ < startJ) {
if (point[startI][startJ - 1].isPiece()) {
可否走棋 = false;
} else {
可否走棋 = true;
}
}
} else {
可否走棋 = false;
}
} else if (piece.getName().equals("馬")) {
int xAxle = Math.abs(startI - endI);
int yAxle = Math.abs(startJ - endJ);
if (xAxle == 2 && yAxle == 1) {
if (endI > startI) {
if (point[startI + 1][startJ].isPiece()) {
可否走棋 = false;
} else {
可否走棋 = true;
}
}
if (endI < startI) {
if (point[startI - 1][startJ].isPiece()) {
可否走棋 = false;
} else {
可否走棋 = true;
}
}
}else if (xAxle == 1 && yAxle == 2) {
if (endJ > startJ) {
if (point[startI][startJ + 1].isPiece()) {
可否走棋 = false;
} else {
可否走棋 = true;
}
}
if (endJ < startJ) {
if (point[startI][startJ - 1].isPiece()) {
可否走棋 = false;
} else {
可否走棋 = true;
}
}
} else {
可否走棋 = false;
}
} else if (piece.getName().equals("象")) {
int centerI = (startI + endI) / 2;
int centerJ = (startJ + endJ) / 2;
int xAxle = Math.abs(startI - endI);
int yAxle = Math.abs(startJ - endJ);
if (xAxle == 2 && yAxle == 2 && endJ <= 5) {
if (point[centerI][centerJ].isPiece()) {
可否走棋 = false;
} else {
可否走棋 = true;
}
} else {
可否走棋 = false;
}
} else if (piece.getName().equals("相")) {
int centerI = (startI + endI) / 2;
int centerJ = (startJ + endJ) / 2;
int xAxle = Math.abs(startI - endI);
int yAxle = Math.abs(startJ - endJ);
if (xAxle == 2 && yAxle == 2 && endJ >= 6) {
if (point[centerI][centerJ].isPiece()) {
可否走棋 = false;
} else {
可否走棋 = true;
}
} else {
可否走棋 = false;
}
} else if (piece.getName().equals("炮")) {
int number = 0;
if (startI == endI) {
int j = 0;
for (j = minJ + 1; j <= maxJ - 1; j++) {
if (point[startI][j].isPiece()) {
number++;
}
}
if (number > 1) {
可否走棋 = false;
} else if (number == 1) {
if (point[endI][endJ].isPiece()) {
可否走棋 = true;
}
} else if (number == 0 && !point[endI][endJ].isPiece()) {
可否走棋 = true;
}
} else if (startJ == endJ) {
int i = 0;
for (i = minI + 1; i <= maxI - 1; i++) {
if (point[i][startJ].isPiece()) {
number++;
}
}
if (number > 1) {
可否走棋 = false;
} else if (number == 1) {
if (point[endI][endJ].isPiece()) {
可否走棋 = true;
}
} else if (number == 0 && !point[endI][endJ].isPiece()) {
可否走棋 = true;
}
} else {
可否走棋 = false;
}
} else if (piece.getName().equals("兵")) {
int xAxle = Math.abs(startI - endI);
int yAxle = Math.abs(startJ - endJ);
if (endJ >= 6) {
if (startJ - endJ == 1 && xAxle == 0) {
可否走棋 = true;
}
else {
可否走棋 = false;
}
} else if (endJ <= 5) {
if ((startJ - endJ == 1) && (xAxle == 0)) {
可否走棋 = true;
} else if ((endJ - startJ == 0) && (xAxle == 1)) {
可否走棋 = true;
} else {
可否走棋 = false;
}
}
} else if (piece.getName().equals("卒")) {
int xAxle = Math.abs(startI - endI);
int yAxle = Math.abs(startJ - endJ);
if (endJ <= 5) {
if (endJ - startJ == 1 && xAxle == 0) {
可否走棋 = true;
} else {
可否走棋 = false;
}
} else if (endJ >= 6) {
if ((endJ - startJ == 1) && (xAxle == 0)) {
可否走棋 = true;
} else if ((endJ - startJ == 0) && (xAxle == 1)) {
可否走棋 = true;
} else {
可否走棋 = false;
}
}
}
else if (piece.getName().equals("士")) {
int xAxle = Math.abs(startI - endI);
int yAxle = Math.abs(startJ - endJ);
if (endI <= 6 && endI >= 4 && xAxle == 1 && yAxle == 1) {
可否走棋 = true;
} else {
可否走棋 = false;
}
} else if (piece.getName().equals("仕")) {
int xAxle = Math.abs(startI - endI);
int yAxle = Math.abs(startJ - endJ);
if (endI <= 6 && endI >= 4 && xAxle == 1 && yAxle == 1) {
可否走棋 = true;
} else {
可否走棋 = false;
}
} else if ((piece.getName().equals("帥"))
|| (piece.getName().equals("將"))) {
int xAxle = Math.abs(startI - endI);
int yAxle = Math.abs(startJ - endJ);
if (endI <= 6 && endI >= 4) {
if ((xAxle == 1 && yAxle == 0) || (xAxle == 0 && yAxle == 1)) {
可否走棋 = true;
} else {
可否走棋 = false;
}
} else {
可否走棋 = false;
}
}
return 可否走棋;
}
}
6.走步類文件MoveStep.java
package cn.edu.ouc.chineseChess;
import java.awt.Point;
/**
* 走步類
*
* @author cnlht
*
*/
public class MoveStep implements java.io.Serializable {
public Point pStart, pEnd;
public MoveStep(Point p1, Point p2) {
pStart = p1;
pEnd = p2;
}
}
7.制作棋譜類MakeChessManual.java
package cn.edu.ouc.chineseChess;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.LinkedList;
/**
* 制作棋譜類
*
* @author cnlht
*/
public class MakeChessManual extends JPanel implements ActionListener {
JTextArea text = null;
JScrollPane scroll = null;
ChessBoard board = null;
ChessPoint[][] point;
LinkedList 棋譜 = null;
LinkedList 吃掉的棋子 = null;
JButton buttonUndo;
int i = 0;
public MakeChessManual(ChessBoard board, ChessPoint[][] point) {
this.board = board;
this.point = point;
text = new JTextArea();
scroll = new JScrollPane(text);
棋譜 = new LinkedList();
吃掉的棋子 = new LinkedList();
buttonUndo = new JButton("悔棋");
buttonUndo.setFont(new Font("隸書", Font.PLAIN, 18));
setLayout(new BorderLayout());
add(scroll, BorderLayout.CENTER);
add(buttonUndo, BorderLayout.SOUTH);
buttonUndo.addActionListener(this);
}
public char numberToLetter(int n) {
char c = '\0';
switch (n) {
case 1:
c = 'A';
break;
case 2:
c = 'B';
break;
case 3:
c = 'C';
break;
case 4:
c = 'D';
break;
case 5:
c = 'E';
break;
case 6:
c = 'F';
break;
case 7:
c = 'G';
break;
case 8:
c = 'H';
break;
case 9:
c = 'I';
break;
case 10:
c = 'J';
break;
}
return c;
}
public void 記錄棋譜(ChessPiece piece, int startI, int startJ, int endI,
int endJ) {
Point pStart = new Point(startI, startJ);
Point pEnd = new Point(endI, endJ);
MoveStep step = new MoveStep(pStart, pEnd);
棋譜.add(step);
String 棋子類別 = piece.棋子類別();
String name = piece.getName();
String m = "#" + 棋子類別 + name + ": " + startI + numberToLetter(startJ)
+ " 到 " + endI + numberToLetter(endJ);
text.append(m);
if (piece.棋子類別().equals(board.黑方顏色))
text.append("\n");
}
public void 記錄吃掉的棋子(Object object) {
吃掉的棋子.add(object);
}
public LinkedList 獲取棋譜() {
return 棋譜;
}
public void actionPerformed(ActionEvent e) {
int position = text.getText().lastIndexOf("#");
if (position != -1)
text.replaceRange("", position, text.getText().length());
if (棋譜.size() > 0) {
MoveStep lastStep = (MoveStep) 棋譜.getLast();
棋譜.removeLast();
Object qizi = 吃掉的棋子.getLast();
吃掉的棋子.removeLast();
String temp = qizi.toString();
if (temp.equals("沒吃棋子")) {
int startI = lastStep.pStart.x;
int startJ = lastStep.pStart.y;
int endI = lastStep.pEnd.x;
int endJ = lastStep.pEnd.y;
ChessPiece piece = point[endI][endJ].getPiece();
point[startI][startJ].setPiece(piece, board);
(point[endI][endJ]).set有棋子(false);
if (piece.棋子類別().equals(board.紅方顏色)) {
board.紅方走棋 = true;
board.黑方走棋 = false;
}
if (piece.棋子類別().equals(board.黑方顏色)) {
board.黑方走棋 = true;
board.紅方走棋 = false;
}
} else {
ChessPiece removedPiece = (ChessPiece) qizi;
int startI = lastStep.pStart.x;
int startJ = lastStep.pStart.y;
int endI = lastStep.pEnd.x;
int endJ = lastStep.pEnd.y;
ChessPiece piece = point[endI][endJ].getPiece();
point[startI][startJ].setPiece(piece, board);
point[endI][endJ].setPiece(removedPiece, board);
(point[endI][endJ]).set有棋子(true);
if (piece.棋子類別().equals(board.紅方顏色)) {
board.紅方走棋 = true;
board.黑方走棋 = false;
}
if (piece.棋子類別().equals(board.黑方顏色)) {
board.黑方走棋 = true;
board.紅方走棋 = false;
}
}
}
}
}
8.演示棋譜類文件Demon.java
package cn.edu.ouc.chineseChess;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
/**
* 演示棋譜類
*
* @author cnlht
*/
public class Demon extends JPanel implements ActionListener, Runnable {
public JButton replay = null, next = null, auto = null, stop = null;
LinkedList 棋譜 = null;
Thread 自動(dòng)演示 = null;
int index = -1;
ChessBoard board = null;
JTextArea text;
JTextField 時(shí)間間隔 = null;
int time = 1000;
String 演示過程 = "";
JSplitPane splitH = null, splitV = null;
public Demon(ChessBoard board) {
this.board = board;
replay = new JButton("重新演示");
next = new JButton("下一步");
auto = new JButton("自動(dòng)演示");
stop = new JButton("暫停演示");
自動(dòng)演示 = new Thread(this);
replay.addActionListener(this);
next.addActionListener(this);
auto.addActionListener(this);
stop.addActionListener(this);
text = new JTextArea();
時(shí)間間隔 = new JTextField("1");
setLayout(new BorderLayout());
JScrollPane pane = new JScrollPane(text);
JPanel p = new JPanel(new GridLayout(3, 2));
p.add(next);
p.add(replay);
p.add(auto);
p.add(stop);
p.add(new JLabel("時(shí)間間隔(秒)", SwingConstants.CENTER));
p.add(時(shí)間間隔);
splitV = new JSplitPane(JSplitPane.VERTICAL_SPLIT, pane, p);
splitH = new JSplitPane(JSplitPane.HORIZONTAL_SPLIT, board, splitV);
splitV.setDividerSize(5);
splitV.setDividerLocation(400);
splitH.setDividerSize(5);
splitH.setDividerLocation(460);
add(splitH, BorderLayout.CENTER);
validate();
}
public void set棋譜(LinkedList 棋譜) {
this.棋譜 = 棋譜;
}
public char numberToLetter(int n) {
char c = '\0';
switch (n) {
case 1:
c = 'A';
break;
case 2:
c = 'B';
break;
case 3:
c = 'C';
break;
case 4:
c = 'D';
break;
case 5:
c = 'E';
break;
case 6:
c = 'F';
break;
case 7:
c = 'G';
break;
case 8:
c = 'H';
break;
case 9:
c = 'I';
break;
case 10:
c = 'J';
break;
}
return c;
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() == next) {
index++;
if (index < 棋譜.size()) {
演示一步(index);
} else {
演示結(jié)束("棋譜演示完畢");
}
}
if (e.getSource() == replay) {
board = new ChessBoard(45, 45, 9, 10);
splitH.remove(board);
splitH.setDividerSize(5);
splitH.setDividerLocation(460);
splitH.setLeftComponent(board);
splitH.validate();
index = -1;
text.setText(null);
}
if (e.getSource() == auto) {
next.setEnabled(false);
replay.setEnabled(false);
try {
time = 1000 * Integer.parseInt(時(shí)間間隔.getText().trim());
} catch (NumberFormatException ee) {
time = 1000;
}
if (!(自動(dòng)演示.isAlive())) {
自動(dòng)演示 = new Thread(this);
board = new ChessBoard(45, 45, 9, 10);
splitH.remove(board);
splitH.setDividerSize(5);
splitH.setDividerLocation(460);
splitH.setLeftComponent(board);
splitH.validate();
text.setText(null);
自動(dòng)演示.start();
}
}
if (e.getSource() == stop) {
if (e.getActionCommand().equals("暫停演示")) {
演示過程 = "暫停演示";
stop.setText("繼續(xù)演示");
stop.repaint();
}
if (e.getActionCommand().equals("繼續(xù)演示")) {
演示過程 = "繼續(xù)演示";
自動(dòng)演示.interrupt();
stop.setText("暫停演示");
stop.repaint();
}
}
}
public synchronized void run() {
for (index = 0; index < 棋譜.size(); index++) {
try {
Thread.sleep(time);
} catch (InterruptedException e) {
}
while (演示過程.equals("暫停演示")) {
try {
wait();
} catch (InterruptedException e) {
notifyAll();
}
}
演示一步(index);
}
if (index >= 棋譜.size()) {
演示結(jié)束("棋譜演示完畢");
next.setEnabled(true);
replay.setEnabled(true);
}
}
public void 演示一步(int index) {
MoveStep step = (MoveStep) 棋譜.get(index);
Point pStart = step.pStart;
Point pEnd = step.pEnd;
int startI = pStart.x;
int startJ = pStart.y;
int endI = pEnd.x;
int endJ = pEnd.y;
ChessPiece piece = (board.point)[startI][startJ].getPiece();
if ((board.point)[endI][endJ].isPiece() == true) {
ChessPiece pieceRemoved = (board.point)[endI][endJ].getPiece();
(board.point)[endI][endJ].reMovePiece(pieceRemoved, board);
board.repaint();
(board.point)[endI][endJ].setPiece(piece, board);
(board.point)[startI][startJ].set有棋子(false);
board.repaint();
} else {
(board.point)[endI][endJ].setPiece(piece, board);
(board.point)[startI][startJ].set有棋子(false);
}
String 棋子類別 = piece.棋子類別();
String name = piece.getName();
String m = "#" + 棋子類別 + name + ": " + startI + numberToLetter(startJ)
+ " 到 " + endI + numberToLetter(endJ);
text.append(m);
if (piece.棋子類別().equals(board.黑方顏色))
text.append("\n");
}
public void 演示結(jié)束(String message) {
splitH.remove(board);
splitH.setDividerSize(5);
splitH.setDividerLocation(460);
JLabel label = new JLabel(message);
label.setFont(new Font("隸書", Font.BOLD, 40));
label.setForeground(Color.blue);
label.setHorizontalAlignment(SwingConstants.CENTER);
splitH.setLeftComponent(label);
splitH.validate();
}
}
四、總結(jié)與要求
1.理解8個(gè)文件,沒有太復(fù)雜的代碼。
2.理解鼠標(biāo)的MouseListener,MouseMotionListener兩個(gè)接口的區(qū)別,五子棋的實(shí)現(xiàn)不需要MouseMotionListener。
3.使用LinkedList記錄棋譜的方法。
希望大家喜歡這篇文章,制作一款屬于自己的中國(guó)象棋游戲。
相關(guān)文章
詳解在SpringBoot應(yīng)用中獲取應(yīng)用上下文方法
本篇文章主要介紹了詳解在SpringBoot應(yīng)用中獲取應(yīng)用上下文方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-04-04
通過代碼實(shí)例了解SpringBoot啟動(dòng)原理
這篇文章主要介紹了通過代碼實(shí)例了解SpringBoot啟動(dòng)原理,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2019-12-12
SpringBoot2 整合Ehcache組件,輕量級(jí)緩存管理的原理解析
這篇文章主要介紹了SpringBoot2 整合Ehcache組件,輕量級(jí)緩存管理,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-08-08
Java 中Object的wait() notify() notifyAll()方法使用
這篇文章主要介紹了Java 中Object的wait() notify() notifyAll()方法使用的相關(guān)資料,需要的朋友可以參考下2017-05-05
springboot?實(shí)現(xiàn)動(dòng)態(tài)刷新配置的詳細(xì)過程
這篇文章主要介紹了springboot實(shí)現(xiàn)動(dòng)態(tài)刷新配置,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2023-05-05
SpringBoot中的maven插件spring-boot-maven-plugin使用
這篇文章主要介紹了SpringBoot中的maven插件spring-boot-maven-plugin使用方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-12-12
Spring開發(fā)核心之AOP的實(shí)現(xiàn)與切入點(diǎn)持久化
面向?qū)ο缶幊淌且环N編程方式,此編程方式的落地需要使用“類”和 “對(duì)象”來實(shí)現(xiàn),所以,面向?qū)ο缶幊唐鋵?shí)就是對(duì) “類”和“對(duì)象” 的使用,面向切面編程,簡(jiǎn)單的說,就是動(dòng)態(tài)地將代碼切入到類的指定方法、指定位置上的編程思想就是面向切面的編程2022-10-10
5種必會(huì)的Java異步調(diào)用轉(zhuǎn)同步的方法你會(huì)幾種
這篇文章主要介紹了5種必會(huì)的Java異步調(diào)用轉(zhuǎn)同步的方法你會(huì)幾種,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-12-12
spring boot創(chuàng)建項(xiàng)目包依賴問題的解決
本篇文章主要介紹了spring boot創(chuàng)建項(xiàng)目包依賴問題的解決,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-11-11

