1.象棋主类 文件ChineseChess.Java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138package 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.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369package 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, 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轴长; j++) {
g.drawLine(point[1][j].x, point[1][j].y, point[x轴长][j].x,
point[x轴长][j].y);
}
for (int i = 1; i <= x轴长; i++) {
if (i != 1 && i != x轴长) {
g.drawLine(point[i][1].x, point[i][1].y, point[i][y轴长 - 5].x,
point[i][y轴长 - 5].y);
g.drawLine(point[i][y轴长 - 4].x, point[i][y轴长 - 4].y,
point[i][y轴长].x, point[i][y轴长].y);
} else {
g.drawLine(point[i][1].x, point[i][1].y, point[i][y轴长].x,
point[i][y轴长].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轴长].x,
point[6][y轴长].y);
g.drawLine(point[4][y轴长].x, point[4][y轴长].y, point[6][8].x,
point[6][8].y);
for (int i = 1; i <= x轴长; 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++;
}
}
/**鼠标按下事件*/
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轴长; i++) {
for (int j = 1; j <= y轴长; 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) {
}
/**鼠标拖动事件*/
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);
}
}
}
}
/**松开鼠标事件*/
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轴长; i++) {
for (int j = 1; j <= y轴长; 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.java1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69package 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) {// 构造棋子
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 颜色类别;
}
}
04 ChessPoint类1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369package 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, 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轴长; j++) {
g.drawLine(point[1][j].x, point[1][j].y, point[x轴长][j].x,
point[x轴长][j].y);
}
for (int i = 1; i <= x轴长; i++) {
if (i != 1 && i != x轴长) {
g.drawLine(point[i][1].x, point[i][1].y, point[i][y轴长 - 5].x,
point[i][y轴长 - 5].y);
g.drawLine(point[i][y轴长 - 4].x, point[i][y轴长 - 4].y,
point[i][y轴长].x, point[i][y轴长].y);
} else {
g.drawLine(point[i][1].x, point[i][1].y, point[i][y轴长].x,
point[i][y轴长].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轴长].x,
point[6][y轴长].y);
g.drawLine(point[4][y轴长].x, point[4][y轴长].y, point[6][8].x,
point[6][8].y);
for (int i = 1; i <= x轴长; 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++;
}
}
/**鼠标按下事件*/
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轴长; i++) {
for (int j = 1; j <= y轴长; 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) {
}
/**鼠标拖动事件*/
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);
}
}
}
}
/**松开鼠标事件*/
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轴长; i++) {
for (int j = 1; j <= y轴长; 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) {
}
}
05 Demon类1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369package 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, 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轴长; j++) {
g.drawLine(point[1][j].x, point[1][j].y, point[x轴长][j].x,
point[x轴长][j].y);
}
for (int i = 1; i <= x轴长; i++) {
if (i != 1 && i != x轴长) {
g.drawLine(point[i][1].x, point[i][1].y, point[i][y轴长 - 5].x,
point[i][y轴长 - 5].y);
g.drawLine(point[i][y轴长 - 4].x, point[i][y轴长 - 4].y,
point[i][y轴长].x, point[i][y轴长].y);
} else {
g.drawLine(point[i][1].x, point[i][1].y, point[i][y轴长].x,
point[i][y轴长].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轴长].x,
point[6][y轴长].y);
g.drawLine(point[4][y轴长].x, point[4][y轴长].y, point[6][8].x,
point[6][8].y);
for (int i = 1; i <= x轴长; 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++;
}
}
/**鼠标按下事件*/
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轴长; i++) {
for (int j = 1; j <= y轴长; 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) {
}
/**鼠标拖动事件*/
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);
}
}
}
}
/**松开鼠标事件*/
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轴长; i++) {
for (int j = 1; j <= y轴长; 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) {
}
}
06 MakeChessManual类1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149package 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;
}
}
}
}
}
07 MoveStep类1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18package 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;
}
}
08 Rule类1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320package cn.edu.ouc.chineseChess;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
/**
* 走棋规则类
*
* @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 可否走棋;
}
}