import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Container;
import java.awt.Graphics;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Vector;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
public class FiveInARow extends JFrame implements MouseListener {
Vector v = new Vector(); //所有的每步走棋資訊
Vector white = new Vector(); //白方走棋資訊
Vector black = new Vector(); //黑方走棋資訊
boolean b; //用來判斷白旗還是黑棋
int whiteCount, blackCount; //計算悔棋步數
int w = 50; //間距大小
int px = 100, py = 100; //棋盤的大小
int pxw = px + w, pyw = py + w;
int width = w * 16, height = w * 16;
int vline = width + px; //垂直線的長度
int hline = height + py; //水準線的長度
/**
* 構造方法
*/
public FiveInARow() {
super("單機版五子棋");
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//關閉按鈕
Container con = this.getContentPane();
con.setLayout(new BorderLayout());
this.addMouseListener(this);//添加監聽
this.setSize(1200, 1200);//設定窗體大小
this.setBackground(Color.yellow);
this.setVisible(true);
}
* 畫棋盤和棋子
* @param
public void paint(Graphics g) {
g.clearRect(0, 0, this.getWidth(), this.getHeight());//清除畫闆
g.setColor(Color.BLACK);//繪制網格顔色
g.drawRect(px, py, width, height);//網格大小
g.drawString("單機版五子棋小遊戲,右擊可以悔棋,歡迎使用", 180, 70);
for (int i=0; i<15; i++) {
g.drawLine(pxw+i*w, py, pxw+i*w, hline);//每條橫線和豎線
g.drawLine(px, pyw+i*w, vline, pyw+i*w);
for (int x=0; x<v.size(); x++) {
String str = (String)v.get(x);
String tmp[] = str.split("-");
int a = Integer.parseInt(tmp[0]);
int b = Integer.parseInt(tmp[1]);
a = a * w + px;
b = b * w + py;
if (x%2 == 0) {
g.setColor(Color.WHITE);
} else {
g.setColor(Color.BLACK);
g.fillArc(a-w/2, b-w/2, w, w, 0, 360);
@Override
public void mouseClicked(MouseEvent e) {
if (e.getButton() == e.BUTTON1) {
int x = e.getX();
int y = e.getY();
x = (x - x % w) + (x % w > w / 2 ? w : 0);
y = (y - y % w) + (y % w > w / 2 ? w : 0);
x = (x - px) / w;
y = (y - py) / w;
if (x >= 0 && y >= 0 && x <= 16 && y <= 16) {
if (v.contains(x+"-"+y)) {
System.out.println("已經有棋了!");
v.add(x+"-"+y);
this.repaint();
if (v.size() % 2 == 0) {
black.add(x+"-"+y);
this.victory(x, y, black);
// System.out.println("黑棋");
white.add(x+"-"+y);
this.victory(x, y, white);
// System.out.println("白棋");
// System.out.println(e.getX()+"-"+e.getY());
// System.out.println(e.getX()+"-"+e.getY()+"|"+x+"-"+y+"\t超出邊界了");
if (e.getButton() == e.BUTTON3) { //右擊悔棋的方法
// System.out.println("滑鼠右擊--悔棋");
if (v.isEmpty()) {
JOptionPane.showMessageDialog(this, "沒有棋可悔");
if (v.size() % 2 == 0) { //判斷是白棋悔棋,還是黑棋悔棋
blackCount++;
if (blackCount > 3) {
JOptionPane.showMessageDialog(this, "黑棋已經悔了3步");
v.remove(v.lastElement());
whiteCount++;
if (whiteCount > 3) {
JOptionPane.showMessageDialog(this, "白棋已經悔了3步");
* 判斷勝利的方法
private void victory(int x, int y, Vector contain) {
int cv = 0; //垂直方向棋子數量
int ch = 0; //水準方向棋子數量
int ci1 = 0; //斜面方向棋子數量1
int ci2 = 0; //斜面方向棋子數量2
//計算水準方向棋子數量
for (int i=1; i<5; i++) {
if (contain.contains((x+i)+"-"+y)) {
ch++;
break;
if (contain.contains((x-i)+"-"+y)) {
//計算垂直方向棋子數量
if (contain.contains(x+"-"+(y+i))) {
cv++;
if (contain.contains(x+"-"+(y-i))) {
//計算45°斜面方向棋子數量
if (contain.contains((x+i)+"-"+(y+i))) {
ci1++;
if (contain.contains((x-i)+"-"+(y-i))) {
//計算135°斜面方向棋子數量
if (contain.contains((x+i)+"-"+(y-i))) {
ci2++;
for (int i=1; i<5; i++) {(http://www.amjmh.com/v/)
if (contain.contains((x-i)+"-"+(y+i))) {
if (ch>=4 || cv>=4 ||ci1>=4 ||ci2>=4) {
System.out.println(v.size()+"步棋");
//判斷是黑棋赢,還是白棋赢
JOptionPane.showMessageDialog(null, "黑棋赢了");
JOptionPane.showMessageDialog(null, "白棋赢了");
this.v.clear();
this.black.clear();
this.white.clear();
public void mouseEntered(MouseEvent e) {
// TODO Auto-generated method stub
public void mouseExited(MouseEvent e) {
public void mousePressed(MouseEvent e) {
public void mouseReleased(MouseEvent e) {
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
public class Start {
public static void main(String[] args) {
FiveInARow five = new FiveInARow();