前言
之前学完java基础课程,试着简单做了一下java的一个五子棋小游戏,记录下来。
界面
由于直接用的java库中的一些基本控件写的一个GUI,并没有做过多优化,感觉比较丑
下面是界面展示:
黑子先行,但是我这边简化规则,并没有考虑黑子先行的一些禁手。
下面直接贴代码
接口类
我把五子棋界面的一些常量都定义在了这个接口类中,包括棋盘的起始坐标,棋盘线的间距和棋子半径
1
2
3
4
5
6
7
8
|
public interface constant { int [][] chessLocation = new int [ 15 ][ 15 ]; static final int x = 50 ; //左上角位置 static final int y = 50 ; static final int LN = 15 ; //棋盘一些常量 static final int R = 45 ; } |
实现类
接口
这个类中继承了 constant、MouseListener、ActionListener三个接口
其中:
- constant为自己定义
- MouseListener为鼠标监听
- ActionListener为事件监听
函数
show()绘制窗口基本框架
paint()绘制棋盘网格线和棋子
IsWin()判断输赢的基本逻辑
mouseClicked()获取鼠标位置,判断棋子落点等
actionPerformed()判断鼠标点击哪个按钮(开始游戏or认输or悔棋)执行相应操作
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
|
import java.awt.Color; import java.awt.Graphics; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import javax.swing.JButton; import javax.swing.JFrame; import javax.swing.JOptionPane; import javax.swing.JPanel; public class game_logic extends JPanel implements constant, MouseListener, ActionListener { int chess_x = 0 , chess_y = 0 ; int X = 0 , Y = 0 ; boolean IsBlack = true ; //判断黑白 boolean flag = false ; //是否已经开始游戏 //生成三个响应按钮 JFrame frame = new JFrame(); JButton start = new JButton( "开始游戏" ); JButton regret = new JButton( "悔棋" ); JButton Lost = new JButton( "认输" ); public void ShowUI() { frame.setSize( 740 , 800 ); frame.setTitle( "五子棋" ); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //点击关闭结束程序 frame.setLocationRelativeTo( null ); //窗口居中 frame.setVisible( true ); //窗体可视化 frame.setResizable( false ); //窗体大小不可调整 frame.add( this ); this .setBackground(Color.LIGHT_GRAY); //设置背景颜色 this .addMouseListener( this ); //窗体中添加鼠标监听器 start.setSize( 50 , 80 ); //设置按钮大小 start.addActionListener( this ); //按钮添加事件监听器 Lost.setSize( 50 , 80 ); Lost.addActionListener( this ); regret.setSize( 50 , 80 ); regret.addActionListener( this ); this .add(start); //添加按钮到棋盘上 this .add(Lost); this .add(regret); } /** * 绘制方法 * 绘制五子棋棋盘 * @param g */ @Override public void paint(Graphics g) { super .paint(g); for ( int i = 0 ; i < LN; i++) { //画棋盘 g.drawLine(x, y + i * R, x + (LN - 1 ) * R, y + i * R); //行*15 g.drawLine(x + i * R, y, x + i * R, y + (LN - 1 ) * R); //列*15 } for ( int i = 0 ; i < LN; i++) { //画棋子 for ( int j = 0 ; j < LN; j++) { if (chessLocation[i][j] == 1 ) { g.setColor(Color.BLACK); //黑棋先行 g.fillOval( 50 + i * R - 23 , 50 + j * R - 23 , R, R); } if (chessLocation[i][j] == 2 ) { g.setColor(Color.WHITE); g.fillOval( 50 + i * R - 23 , 50 + j * R - 23 , R, R); } repaint(); } } } /** *判断输赢 * */ public int IsWin() { int k = 0 ; for ( int f = 2 ; f < 12 ; f++) { for ( int g = 2 ; g < 12 ; g++) { if (chessLocation[f][g] == 1 ) { if (chessLocation[f][g] == chessLocation[f - 1 ][g] && chessLocation[f - 1 ][g] == chessLocation[f - 2 ][g] && chessLocation[f - 2 ][g] == chessLocation[f + 1 ][g] && chessLocation[f + 1 ][g] == chessLocation[f + 2 ][g]) { k = 1 ; break ; } if (chessLocation[f][g] == chessLocation[f][g - 1 ] && chessLocation[f][g - 1 ] == chessLocation[f][g - 2 ] && chessLocation[f][g - 2 ] == chessLocation[f][g + 1 ] && chessLocation[f][g + 1 ] == chessLocation[f][g + 2 ]) { k = 1 ; break ; } if (chessLocation[f][g] == chessLocation[f - 1 ][g - 1 ] && chessLocation[f - 1 ][g - 1 ] == chessLocation[f - 2 ][g - 2 ] && chessLocation[f - 2 ][g - 2 ] == chessLocation[f + 1 ][g + 1 ] && chessLocation[f + 1 ][g + 1 ] == chessLocation[f + 2 ][g + 2 ]) { k = 1 ; break ; } if (chessLocation[f][g] == chessLocation[f - 1 ][g + 1 ] && chessLocation[f - 1 ][g + 1 ] == chessLocation[f - 2 ][g + 2 ] && chessLocation[f - 2 ][g + 2 ] == chessLocation[f + 1 ][g - 1 ] && chessLocation[f + 1 ][g - 1 ] == chessLocation[f + 2 ][g - 2 ]) { k = 1 ; break ; } } if (chessLocation[f][g] == 2 ) { if (chessLocation[f][g] == chessLocation[f - 1 ][g] && chessLocation[f - 1 ][g] == chessLocation[f - 2 ][g] && chessLocation[f - 2 ][g] == chessLocation[f + 1 ][g] && chessLocation[f + 1 ][g] == chessLocation[f + 2 ][g]) { k = 2 ; break ; } if (chessLocation[f][g] == chessLocation[f][g - 1 ] && chessLocation[f][g - 1 ] == chessLocation[f][g - 2 ] && chessLocation[f][g - 2 ] == chessLocation[f][g + 1 ] && chessLocation[f][g + 1 ] == chessLocation[f][g + 2 ]) { k = 2 ; break ; } if (chessLocation[f][g] == chessLocation[f - 1 ][g - 1 ] && chessLocation[f - 1 ][g - 1 ] == chessLocation[f - 2 ][g - 2 ] && chessLocation[f - 2 ][g - 2 ] == chessLocation[f + 1 ][g + 1 ] && chessLocation[f + 1 ][g + 1 ] == chessLocation[f + 2 ][g + 2 ]) { k = 2 ; break ; } if (chessLocation[f][g] == chessLocation[f - 1 ][g + 1 ] && chessLocation[f - 1 ][g + 1 ] == chessLocation[f - 2 ][g + 2 ] && chessLocation[f - 2 ][g + 2 ] == chessLocation[f + 1 ][g - 1 ] && chessLocation[f + 1 ][g - 1 ] == chessLocation[f + 2 ][g - 2 ]) { k = 2 ; break ; } } } } return k; } @Override public void mouseClicked(MouseEvent e) { X = e.getX(); Y = e.getY(); //获取鼠标位置 if (flag == true ) { if (X >= 25 && X <= 705 && Y >= 25 && Y <= 705 ) { //比棋盘稍微大一点的落子判定范围,即棋盘边缘位置 //应该安放的棋子的位置 chess_x = (X - 20 ) / R; chess_y = (Y - 20 ) / R; if (chessLocation[chess_x][chess_y] == 0 ) { //存储棋子状态,转换棋子颜色 if (IsBlack == true ) { chessLocation[chess_x][chess_y] = 1 ; IsBlack = false ; } else { chessLocation[chess_x][chess_y] = 2 ; IsBlack = true ; } if (IsWin() == 1 ) { JOptionPane.showMessageDialog( this , "黑棋获胜" ); flag = false ; } if (IsWin() == 2 ) { JOptionPane.showMessageDialog( this , "白棋获胜" ); flag = false ; } repaint(); } } } } @Override public void mousePressed(MouseEvent e) { } @Override public void mouseReleased(MouseEvent e) { } @Override public void mouseEntered(MouseEvent e) { } @Override public void mouseExited(MouseEvent e) { } @Override public void actionPerformed(ActionEvent e) { String buttonName = e.getActionCommand(); if (buttonName.equals( "开始游戏" ) && flag == false ) { //开始游戏,棋盘清空 flag = true ; for ( int i = 0 ; i < LN; i++) { for ( int j = 0 ; j < LN; j++) { chessLocation[i][j] = 0 ; } } IsBlack = true ; repaint(); } if (buttonName.equals( "认输" ) && flag == true ) { flag = false ; if (IsBlack) { JOptionPane.showMessageDialog( this , ",白棋认输,黑棋获胜" ); } else { JOptionPane.showMessageDialog( this , ",黑棋认输,白棋获胜" ); } } if (buttonName.equals( "悔棋" ) && flag == true ) { if (chessLocation[chess_x][chess_y] == 1 ) { JOptionPane.showMessageDialog( this , "黑方悔棋" ); } if (chessLocation[chess_x][chess_y] == 2 ) { JOptionPane.showMessageDialog( this , "白方悔棋" ); } chessLocation[chess_x][chess_y] = 0 ; IsBlack = !IsBlack; repaint(); } } } |
其中比较有趣的是五子棋判赢方式,假设棋盘大小15*15,则我只需要判断正中间的13*13d的格子,向两边扩展,判断是否五子连珠。
具体说明代码里都有注释,不多赘述。
主函数类
1
2
3
4
5
6
|
public class Main_game { public static void main(String[] args) { game_logic start= new game_logic(); start.ShowUI(); } } |
总结
实现了五子棋小游戏的基本功能,但是略感粗糙,细节不足。对于基本控件调用一学就会,做一个小的游戏demo这是对流程控制和操作逻辑的训练很有效的一种方式。之前看了别人的代码觉得简单,但是自己写的时候往往逻辑流程难以连续,思维混乱,有些过程只有自己写了才知道其中的坑。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/weixin_45436831/article/details/119036771