- ubuntu12.04环境下使用kvm ioctl接口实现最简单的虚拟机
- Ubuntu 通过无线网络安装Ubuntu Server启动系统后连接无线网络的方法
- 在Ubuntu上搭建网桥的方法
- ubuntu 虚拟机上网方式及相关配置详解
CFSDN坚持开源创造价值,我们致力于搭建一个资源共享平台,让每一个IT人在这里找到属于你的精彩世界.
这篇CFSDN的博客文章Java棋类游戏实践之中国象棋由作者收集整理,如果你对这篇文章有兴趣,记得点赞哟.
本文实例讲述了java实现的中国象棋游戏代码,分享给大家供大家参考,具体代码如下 。
1、实践目的:
1.鼠标点击、拖动等事件的应用与区别 。
2.棋谱文件的保存与读取 。
3.完善象棋的规则.
2、实践内容:
中国象棋历史悠久,吸引了无数的人研究,现对中国象棋的对战和实现棋谱的制作做如下的设计和说明,供大家参考学习.
1、机机对弈,红方先手。在符合规则的情况下拖动棋子到目的地,松鼠标落子.
人人对弈图 。
2、制作棋谱,选择制作棋谱菜单后,对弈开始,并记录了下棋过程.
选择“制作棋谱”菜单 。
棋谱制作完毕红方胜出 。
一方胜出后弹出胜利消息对话框。点击确定后,选择“保存棋谱”菜单,弹出保存文件对话框.
保存棋谱对话框 。
3.演示棋谱,选择演示棋谱菜单后,弹出打开对话框,选择保存好的棋谱,开始演示.
演示棋谱对话框 。
演示棋谱过程(自动和手动两种) 。
3、参考代码:
1.象棋主类 文件ChineseChess.java 。
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
|
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 。
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
369
|
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, 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.java 。
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
|
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) {
// 构造棋子
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.棋子点坐标类文件 。
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
|
package
cn.edu.ouc.chineseChess;
/**
* 棋点类
*
* @author cnlht
*/
public
class
ChessPoint {
/** 棋子坐标 */
int
x, y;
/** 该坐标 是否有子*/
boolean
有棋子;
/** 改坐标的棋子 */
ChessPiece piece =
null
;
/** 坐标所属棋盘 */
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;
}
// 设置改点棋子
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.玩法规则类文件Rule.java 。
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
|
package
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
可否走棋;
}
}
|
6.走步类文件MoveStep.java 。
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
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 。
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
|
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 。
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
|
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 自动演示 =
null
;
int
index = -
1
;
ChessBoard board =
null
;
JTextArea text;
JTextField 时间间隔 =
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(
"自动演示"
);
stop =
new
JButton(
"暂停演示"
);
自动演示 =
new
Thread(
this
);
replay.addActionListener(
this
);
next.addActionListener(
this
);
auto.addActionListener(
this
);
stop.addActionListener(
this
);
text =
new
JTextArea();
时间间隔 =
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(
"时间间隔(秒)"
, SwingConstants.CENTER));
p.add(时间间隔);
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
{
演示结束(
"棋谱演示完毕"
);
}
}
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(时间间隔.getText().trim());
}
catch
(NumberFormatException ee) {
time =
1000
;
}
if
(!(自动演示.isAlive())) {
自动演示 =
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
);
自动演示.start();
}
}
if
(e.getSource() == stop) {
if
(e.getActionCommand().equals(
"暂停演示"
)) {
演示过程 =
"暂停演示"
;
stop.setText(
"继续演示"
);
stop.repaint();
}
if
(e.getActionCommand().equals(
"继续演示"
)) {
演示过程 =
"继续演示"
;
自动演示.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()) {
演示结束(
"棋谱演示完毕"
);
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
演示结束(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();
}
}
|
4、总结与要求 1.理解8个文件,没有太复杂的代码。 2.理解鼠标的MouseListener,MouseMotionListener两个接口的区别,五子棋的实现不需要MouseMotionListener。 3.使用LinkedList记录棋谱的方法.
希望大家喜欢这篇文章,制作一款属于自己的中国象棋游戏.
最后此篇关于Java棋类游戏实践之中国象棋的文章就讲到这里了,如果你想了解更多关于Java棋类游戏实践之中国象棋的内容请搜索CFSDN的文章或继续浏览相关文章,希望大家以后支持我的博客! 。
本文分享自华为云社区《大模型LLM之分布式训练》,作者: 码上开花_Lancer。 随着语言模型参数量和所需训练数据量的急速增长,单个机器上有限的资源已无法满足大语言模型训练的要求。需要设计分布式训
本文分享自华为云社区《五大基础算法--动态规划法》,作者: 大金(内蒙的)。 一、基本概念 动态规划法,和分治法极其相似。区别就是,在求解子问题时,会保存该子问题的解,后面的子问题求解时,可以直接拿来
pip install scp pip install pexpect 测试代码: import os import stat import paramiko # 用于调用scp命令 def s
我目前正在实现“ token ”REST 服务。 token 只是一个字符串,由一些参数构建而成,然后经过哈希处理并在一定时间后过期。 我想在我的 REST 服务中有一个可以验证 token 的端点,
打开软删除后,我在客户端上添加一条记录,推送,删除添加的记录推送,然后尝试使用与初始记录相同的主键添加新记录(然后推送),我得到一个异常(exception)。 EntityDomainManager
打开软删除后,我在客户端上添加一条记录,推送,删除添加的记录推送,然后尝试使用与初始记录相同的主键添加新记录(然后推送),我得到一个异常(exception)。 EntityDomainManager
我有一个应用程序,每 x 秒接收一次天气信息。我想将此数据保存到 XML 文件中。 我应该为每个天气通知创建一个新的 XML 文件,还是将每个通知附加到同一个 XML 文件中?我不确定 XML 标准的
我猜我们大多数人都必须在某个时候处理这个问题,所以我想我会问这个问题。 当您的 BLL 中有很多集合并且您发现自己一遍又一遍地编写相同的旧内联(匿名)谓词时,显然有必要进行封装,但实现封装的最佳方
我有一些 c# 代码已经运行了一段时间了..我不得不说,虽然我了解 OO 原则的基础知识,但显然有不止一种方法可以给猫剥皮(尽管我讨厌那个短语!)。 因此,我有一个基本抽象类作为基本数据服务类,如下所
我设计了一个 SQL 数据库系统(使用 Postgre),我有一个问题,即创建一个关系/引用的常见做法是什么,这种关系/引用即使在引用的对象被删除时也能持续存在。 比如有一个UserORM,还有Act
我们的目标是搜索用户输入的字符串并计算在其中找到多少元音。不幸的是我被困在这里,有什么帮助吗? def numVowels(s): vowels= "AEIOUaeiou" if s
我有一个适用于我的“items”int 数组的旋转函数。下面的代码完成了它,除了我不必要地传输值。我正在努力实现“就地”轮换。我的意思是 ptrs 会递增或递减,而不是从数组中获取值。我需要通过这种方
我有一个 json 存储在我的应用程序文档文件夹中,我需要在我的所有 View 中使用它。我正在加载 json 并将其添加到每个 View 中的 NSMutableArray。但现在我了解到,我可以将
我用 C++ 开始了一个项目。这种语言的内存管理对我来说是新的。 我过去常常使用 new () 创建对象,然后传递指针,虽然它可以工作,但调试起来很痛苦,人们看到代码时会用有趣的眼神看着我。我为它没有
已结束。 这个问题是 off-topic .它目前不接受答案。 想要改进这个问题? Update the question所以它是on-topic堆栈溢出。 关闭 10 年前。 Improve thi
保持类松散耦合是编写易于理解、修改和调试的代码的一个重要方面——我明白这一点。然而,作为一个新手,几乎任何时候我都会超越我所苦苦挣扎的最简单的例子。 我或多或少地了解如何将字符串、整数和简单数据类型封
我发现我需要编写大量重复代码,因为我无法从其他 Controller 调用函数。例如,这里新闻提要内容在我的代码中重复,我对一个 Controller 做一些特定的事情,然后需要像这样加载我的新闻提要
假设需要一种数字数据类型,其允许值在指定范围内。更具体地说,假设要定义一个整数类型,其最小值为0,最大值为5000。这种情况在很多情况下都会出现,例如在对数据库数据类型,XSD数据类型进行建模时。 在
假设我想循环整个数组来访问每个元素。使用 for 循环、for...in 循环或 for...of 循环是 JavaScript 开发人员的标准做法吗? 例如: var myArray = ["app
我有一个旧的 SL4/ria 应用程序,我希望用 Breeze 取代它。我有一个关于内存使用和缓存的问题。我的应用程序加载工作列表(一个典型的用户可以访问大约 1,000 个这些工作)。此外,还有很多
我是一名优秀的程序员,十分优秀!