-
Notifications
You must be signed in to change notification settings - Fork 0
/
Game
445 lines (328 loc) · 7.95 KB
/
Game
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
Game.java
import javax.swing.*;
public class Game {
public static void main(String[] args) {
GameFrame GFrame=new GameFrame();
GFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GFrame.setVisible(true);
}
}
GameFrame.java
import java.awt.*;
import javax.swing.*;
class GameFrame extends JFrame {
private static final int Width=570;
private static final int Height=470;
GameFrame(){
setTitle("五子棋游戏");
CenteredFrame();
setSize(Width,Height);
GamePanel Gpanel=new GamePanel();
add(Gpanel);
}
void CenteredFrame(){
Toolkit kit=Toolkit.getDefaultToolkit();
Dimension screenSize=kit.getScreenSize();
int screenHeight=screenSize.height;
int screenWidth=screenSize.width;
int Xposition=(screenWidth-Width)/2;
int Yposition=(screenHeight-Height)/2;
setLocation(Xposition,Yposition);
}
}
GamePanel.java
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
class GamePanel extends JPanel {
private Point cursor=new Point(40,60); //棋盘坐标
private int[][] ChessState=new int[18][18];//棋盘状态
private int i=0;//横坐标
private int j=0;//纵坐标
private final static int testnum=5;//五子棋的规定棋子数
private Player Black=new Player(1,Color.BLACK,"黑方");//黑方棋子
private Player White=new Player(2,Color.WHITE,"白方");//白方棋子
private Player Cplayer=null;//当前用户的引用
private JTextField textBlack=new JTextField("",5);//黑方文本提示框对象和文本长度设置
private JTextField textWhite=new JTextField("",5);//白方文本提示框对象和文本长度设置
private String Nothing="";
private String textblack="请黑方下子";//黑方提示文本
private String textwhite="请白方下子";//白方提示文本
GamePanel(){
setLayout(null);
Initialization();
setFocusable(true);
JButton Rutton=new JButton("重新开局");
Rutton.setBounds(20,14,100,26);
RestartListener restart=new RestartListener();
Rutton.addActionListener(restart);
add(Rutton);
textBlack.setHorizontalAlignment(JTextField.CENTER);
textBlack.setBounds(150,14,110,26);
textBlack.setEditable(false);
add(textBlack);
textWhite.setHorizontalAlignment(JTextField.CENTER);+ textWhite.setBounds(290,14,110,26);
textWhite.setEditable(false);
add(textWhite);
JTextArea gInstruction=new JTextArea();
gInstruction.setSelectedTextColor(new Color(238,238,238));
String gSInstruction=
"Play1(黑方) Key \nup--------W \ndown----S \nleft--------A \nright------F \n\n"+
"Play2(白方) Key \nup--------up \ndown----down \nleft--------left \nright------right \n\n"+
"Exit game: \nPress Esc";
gInstruction.setText(gSInstruction);
gInstruction.setEditable(false);
gInstruction.setBounds(440,60,100,340);
add(gInstruction);
ChessManPerformListener Perform=new ChessManPerformListener();
addKeyListener(Perform);
}
// 设置第一个JTextField输出"请黑方下棋",坐标为(40,60)
void Initialization(){
for(int i=0,j;i<18;i++)
for(j=0;j<18;j++){
ChessState[i][j]=0;
}
cursor.setLocation(40,60);
Cplayer=Black;
textBlack.setText(textblack);
textWhite.setText(Nothing);
}
//画棋盘和初始化棋局状态
protected void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2=(Graphics2D) g;
for(int i=60;i<=400;i+=20){
g2.drawLine(40,i,380,i);
}
for(int j=40;j<=380;j+=20){
g2.drawLine(j,60,j,400);
}
g2.drawString("_",cursor.x-6,cursor.y);
g2.drawString("_",cursor.x,cursor.y);
for(i=0;i<18;i++)
for(j=0;j<18;j++){
if(ChessState[i][j]!=0){
if(ChessState[i][j]==1){
g2.setPaint(Black.getplayerChessManColor());
}
if(ChessState[i][j]==2){
g2.setPaint(White.getplayerChessManColor());
}
g2.fillOval(j*20+40-10, i*20+60-10, ChessMan.getChessManSize(), ChessMan.getChessManSize());
}
}
}
//判断棋盘的当前位置是否已有棋子
boolean isChessState(){
this.j=(cursor.x-40)/20;
this.i=(cursor.y-60)/20;
if(ChessState[this.i][this.j]!=0)
return true;
else
return false;
}
//记录落子后棋盘的当前位置的状态
void RecordChessState(){
this.j=(cursor.x-40)/20;
this.i=(cursor.y-60)/20;
ChessState[this.i][this.j]=Cplayer.getCurrentIdentify();
}
//判断当前玩家落子后是否赢了
void JudgeWin(){
for(int i=0;i<4;i++)
if(JudgeWinLine(i)){
//提示当前玩家已经获得胜利
try{
String Ginformation="GameInformation";
String Message="恭喜玩家"+Cplayer.getsIdentify()+"获胜!"+"\n"+"继续游戏还是退出游戏?";
String[] options = {"继续","退出"};
int selection=JOptionPane.showOptionDialog(null,Message,Ginformation,
JOptionPane.YES_NO_OPTION,JOptionPane.INFORMATION_MESSAGE ,null,options,options[0]);//throws HeadlessException
if(selection==JOptionPane.OK_OPTION){
Initialization();
repaint();
return;
}
if(selection==JOptionPane.NO_OPTION){
System.exit(0);//退出程序
}
}catch(HeadlessException e){
e.printStackTrace();
}
}
//如果当前方没有赢棋则双方轮换
ChangeCurrentPlayer();
}
//在当前方向上是否有连续的五只棋子
boolean JudgeWinLine(int direction){
int i,j,di,dj,count;
i=j=di=dj=count=0;
switch(direction){
case 0:
j=this.j-(testnum-1);
i=this.i;
dj=1;
di=0;
break;
case 1:
j=this.j;
i=this.i-(testnum-1);
dj=0;
di=1;
break;
case 2:
j=this.j-(testnum-1);
i=this.i+(testnum-1);
dj=1;
di=-1;
break;
case 3:
j=this.j-(testnum-1);
i=this.i-(testnum-1);
dj=1;
di=1;
break;
}
for(int k=0;k<testnum*2+1;k++){
if(j>=0&&j<18&&i>=0&&i<18){
if(ChessState[i][j]==Cplayer.getCurrentIdentify()){
count++;
if(count>=testnum)
return true;
}
else
count=0;
}
j+=dj;
i+=di;
}
return false;
}
//更换当前玩家
void ChangeCurrentPlayer(){
if(Cplayer==Black){
Cplayer=White;
textBlack.setText(Nothing);
textWhite.setText(textwhite);
}
else{
Cplayer=Black;
textBlack.setText(textblack);
textWhite.setText(Nothing);
}
}
//重新开局监听器
private class RestartListener implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
Initialization();
repaint();
requestFocus();
}
}
//棋盘、棋局状态监听器
private class ChessManPerformListener implements KeyListener{
//玩家2的按键
public void keyPressed(KeyEvent event) {
int keyCode=event.getKeyCode();
if(keyCode==KeyEvent.VK_ESCAPE)
System.exit(0);
if(Cplayer.getCurrentIdentify()==2){//判别当前玩家
if(keyCode==KeyEvent.VK_LEFT){
if(cursor.x>40)
cursor.x-=20;
}
else if(keyCode==KeyEvent.VK_RIGHT){
if(cursor.x<380)
cursor.x+=20;
}
else if(keyCode==KeyEvent.VK_UP){
if(cursor.y>60)
cursor.y-=20;
}
else if(keyCode==KeyEvent.VK_DOWN){
if(cursor.y<400)
cursor.y+=20;
}
else if(keyCode==KeyEvent.VK_ENTER){
if(!isChessState()){
Cplayer.PerformChessMan();
RecordChessState();
repaint();
JudgeWin();//判定当前落子后是否赢棋
}
}
repaint();
}
}
public void keyReleased(KeyEvent event) {}
//玩家1的按键
public void keyTyped(KeyEvent event) {
char keyChar=event.getKeyChar();
if(Cplayer.getCurrentIdentify()==1){//判别当前玩家
if(keyChar=='a'){
if(cursor.x>40)//对移动光标超界现象做判别
cursor.x-=20;
}
else if(keyChar=='d'){
if(cursor.x<380)
cursor.x+=20;
}
else if(keyChar=='w'){
if(cursor.y>60)
cursor.y-=20;
}
else if(keyChar=='s'){
if(cursor.y<400)
cursor.y+=20;
}
else if(keyChar==' '){
if(!isChessState()){ //落子前先判断当前位置上是否已有棋子
Cplayer.PerformChessMan();//落子
RecordChessState();//记录当前落子后棋盘状态
repaint();
JudgeWin();//判定当前落子后是否赢棋
}
}
}
repaint();
}
}
}
ChessMan.java
import java.awt.Color;
class ChessMan {
private static final int ChessManSize=20;
private Color ChessManColor;
ChessMan(Color c){
ChessManColor=c;
}
static int getChessManSize(){
return ChessManSize;
}
Color getChessManColor(){
return ChessManColor;
}
}
Player.java
import java.awt.Color;
class Player {
private int identify;
private ChessMan pChessMan;
private String PlayerStringIdentify;
Player(int identify,Color c,String sIdentify){
this.identify=identify;
pChessMan=new ChessMan(c);
this.PlayerStringIdentify=sIdentify;
}
int getCurrentIdentify(){
return identify;
}
String getsIdentify(){
return PlayerStringIdentify;
}
void PerformChessMan(){}
Color getplayerChessManColor(){
return pChessMan.getChessManColor();
}
}