簡(jiǎn)單的五子棋操作用兩種方法實(shí)現(xiàn)
最近有五子棋的大作業(yè),周折半天才弄明白其中的原理,查閱了許多資料,然后網(wǎng)上的代碼只有幾篇原創(chuàng)并且注釋很少,感覺(jué)不好理解。所以感覺(jué)有必要分享一下自己的心得

本人使用兩種方法:
1:(傳統(tǒng)方法)鼠標(biāo)點(diǎn)擊事件。大致流程為 定義窗口——從寫(xiě)JPanel中的paint函數(shù)(畫(huà)圖由paint實(shí)現(xiàn))——畫(huà)棋盤(pán)——設(shè)置數(shù)組儲(chǔ)存坐標(biāo)以及是否有棋子——添加鼠標(biāo)點(diǎn)擊事件畫(huà)棋子(判斷鼠標(biāo)點(diǎn)擊的位置離此點(diǎn)最近的那個(gè)店的坐標(biāo)并畫(huà)棋子)——判斷是否有勝利
1)這里說(shuō)一下paint函數(shù),paint函數(shù)定義在那個(gè)界面里他會(huì)自動(dòng)執(zhí)行畫(huà)圖不需要調(diào)用,所以你只需要寫(xiě)好約束的事件讓他畫(huà)完棋盤(pán)后該什么時(shí)候在哪里畫(huà)棋子。
2)repaint 函數(shù)是起到重畫(huà)作用,你點(diǎn)擊過(guò)后再最近的那個(gè)店需要畫(huà)棋子,repaint就起到從畫(huà)的作用
3)本人判斷成行的方法是分別定義四個(gè)變量代碼五子棋個(gè)數(shù),當(dāng)》=5時(shí)停止。彈出新的窗口。具體是如果此點(diǎn)左右都有相同顏色的棋子,就吧這個(gè)點(diǎn)向左找到一直顏色不同為止,然后從這點(diǎn)向下(右)直接計(jì)數(shù)。
4)本人的棋子是畫(huà)出來(lái)的,如果追求美觀可以用image方法使用下載下來(lái)的圖片棋子(但是思想一致)。
附上代碼和注釋?zhuān)?/p>
import java.awt.Color; import java.awt.Component; import java.awt.Container; import java.awt.FlowLayout; import java.awt.Font; import java.awt.Graphics; import java.awt.GridLayout; import java.awt.Point; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import java.awt.event.MouseAdapter; import java.awt.event.MouseEvent; import javax.swing.*; public class wuziqi extends JFrame { void judgle(int a[][],int i,int j) { //需要定義兩個(gè)變量(作用相同)一個(gè)判斷黑棋子,一個(gè)判斷白棋 int k=1; int kk=1;//判斷橫方向 int l=1;int kl=1;//判斷豎方向 int k1=1;int kk1=1;//判斷左上和右下方向 int k2=1; int kk2=1;//判斷左下和右上方向 //橫方向的判斷 int m=i;int n=j;int m1=i;int n1=j; int m2=i;int n2=j; while(m-1>=0&&a[m-1][j]==1) {m--;}//上下判斷 while(m<18&&a[m 1][j]==1) {k ;m ;} //左右判斷 // while(n-1>=0&&a[i][n-1]==1) {n--;} while(n<18&&a[i][n 1]==1) {l ;n ;} //左上右下 while(m1-1>=0&&n1-1>=0&&a[m1-1][n1-1]==1) {m1--;n1--;} while(m1<18&&n1<18&&a[m1 1][n1 1]==1) {k1 ;m1 ;n1 ;} //左下右上方向 while(m2-1>=0&&n2 1<19&&a[m2-1][n2 1]==1) {m2--;n2 ;} while(m2<18&&n2-1>=0&&a[m2 1][n2-1]==1) {k2 ;m2 ;n2--;} m=i; n=j; m1=i; n1=j; m2=i; n2=j; while(m-1>=0&&a[m-1][j]==2) {m--;}//上下判斷 while(m<18&&a[m 1][j]==2) {kk ;m ;} //左右判斷 // while(n-1>=0&&a[i][n-1]==2) {n--;} while(n<18&&a[i][n 1]==2) {kl ;n ;} //左上右下 while(m1-1>=0&&n1-1>=0&&a[m1-1][n1-1]==2) {m1--;n1--;} while(m1<18&&n1<18&&a[m1 1][n1 1]==2) {kk1 ;m1 ;n1 ;} //左下右上方向 while(m2-1>=0&&n2 1<19&&a[m2-1][n2 1]==2) {m2--;n2 ;} while(m2<18&&n2-1>=0&&a[m2 1][n2-1]==2) {kk2 ;m2 ;n2--;} if(k>=5||l>=5||k1>=5||k2>=5||kk>=5||kl>=5||kk1>=5||kk2>=5) { setTitle("游戲已結(jié)束");JFrame frame=new JFrame ("恭喜獲勝"); Container c=frame.getContentPane(); c.setLayout(new GridLayout(4,1,0,0) ); if(a[i][j]==1) { JLabel label=new JLabel("恭喜黑色贏了",JLabel.CENTER); label.setFont(new Font("宋體",0,35));c.add(label);} if(a[i][j]==2) { JLabel label=new JLabel("恭喜白色贏了",JLabel.CENTER); label.setFont(new Font("宋體",0,35));c.add(label);} JButton b1=new JButton("再來(lái)一把"); JButton b2=new JButton("結(jié) 束"); b1.setFocusPainted(false); b2.setFocusPainted(false); c.add(new JLabel("")); c.add(b1); b1.setBackground(Color.YELLOW); c.add(b2); c.setBackground(Color.red); frame.setSize(300, 300); frame.setLocationRelativeTo(null); frame.setVisible(true);//順序問(wèn)題 frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); b1.addActionListener(new ActionListener() {//建立監(jiān)聽(tīng)事件 public void actionPerformed(ActionEvent e) { if(b1==e.getSource()) for(int i=0;i<19;i ) for(int j=0;j<19;j ) {a[i][j]=0;} frame.dispose();repaint(); } });//關(guān)閉當(dāng)前窗口 b2.addActionListener(new ActionListener() {//建立監(jiān)聽(tīng)事件 public void actionPerformed(ActionEvent e) {if(b2==e.getSource()) System.exit(0);//關(guān)閉所有 } }); setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE); } } public wuziqi() { setTitle("這是一個(gè)五子棋游戲"); setVisible(true); Container c = getContentPane(); DrawPanel1 jp1 = new DrawPanel1(); c.add(jp1); setSize(1000, 1010); jp1.setBackground(new Color(60,150,200));//隨便賦值一個(gè)背景顏色 this.setResizable(false); this.setLocationRelativeTo(null);//劇中放置,要在setsize后面放置 setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); c.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) { { int x=0,y=0; for (int i = 0; i < 19; i ) { if (jp1.b4[i][2] >e.getX()) {if(i>0) { if(e.getX()-jp1.b4[i-1][2] e.getY())//找到點(diǎn)擊的點(diǎn)右側(cè)的最近那個(gè)坐標(biāo)值 { if(i>0) { if(e.getY()-jp1.b3[2][i-1]=0&&i<=18) {//橫方向的判斷 int m=i;int n=j;int m1=i;int n1=j; int m2=i;int n2=j; while(m-1>=0&&a[m-1][j]==true) {m--;}//上下判斷 while(m<18&&a[m 1][j]==true) {k ;m ;} //左右判斷 // while(n-1>=0&&a[i][n-1]==true) {n--;} while(n<18&&a[i][n 1]==true) {l ;n ;} //左上右下 while(m1-1>=0&&n1-1>=0&&a[m1-1][n1-1]==true) {m1--;n1--;} while(m1<18&&n1<18&&a[m1 1][n1 1]==true) {k1 ;m1 ;n1 ;} //左下右上方向 while(m2-1>=0&&n2 1<19&&a[m2-1][n2 1]==true) {m2--;n2 ;} while(m2<18&&n2-1>=0&&a[m2 1][n2-1]==true) {k2 ;m2 ;n2--;} if(k>=5||l>=5||k1>=5||k2>5) { setTitle("游戲已結(jié)束");JFrame frame=new JFrame ("恭喜獲勝"); Container c=frame.getContentPane(); c.setLayout(new GridLayout(4,1,0,0) ); JLabel label=new JLabel("恭喜你贏了",JLabel.CENTER); label.setFont(new Font("宋體",0,35)); JButton b1=new JButton("再來(lái)一把"); JButton b2=new JButton("結(jié) 束"); b1.setFocusPainted(false); b2.setFocusPainted(false); c.add(new JLabel("")); c.add(label); c.add(b1); b1.setBackground(Color.YELLOW); c.add(b2); c.setBackground(Color.red); //c.add(b1); //c.add(b2); frame.setSize(300, 300); frame.setLocationRelativeTo(null); frame.setVisible(true);//順序問(wèn)題 frame.setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); b1.addActionListener(new ActionListener() {//建立監(jiān)聽(tīng)事件 public void actionPerformed(ActionEvent e) { if(b1==e.getSource()) for(int i=0;i<19;i ) for(int j=0;j<19;j ) {judgle[i][j]=false; black[i][j]=false; white[i][j]=false;b[i][j].setIcon(null); frame.dispose();repaint(); } } }); b2.addActionListener(new ActionListener() {//建立監(jiān)聽(tīng)事件 public void actionPerformed(ActionEvent e) {if(b2==e.getSource())System.exit(0); } }); //System.exit(0);//結(jié)束 } } public wuziqi2() { setTitle("這是一個(gè)五子棋游戲"); //Container c=getContentPane(); //setLayout(new FlowLayout(2,10,10));//流布局管理器 DrawPanel1 d1=new DrawPanel1(); d1.setSize(950,950); d1.setLayout(new GridLayout(19,19,0,0));//網(wǎng)格布局19行19列 d1.setBackground(new Color(0,200,200)); setResizable(false); add(d1); for(int i=0;i<19;i ) { for(int j=0;j<19;j ) {b[i][j]=new JButton("");d1.add(b[i][j]); b[i][j].setContentAreaFilled(false);//透明 b[i][j].setBorderPainted(false);//取消邊框 b[i][j].setFocusPainted(false); //取消焦點(diǎn) }} for( int i=0;i<19;i ) { for(int j=0;j<19;j ) b[i][j].addActionListener(new ActionListener() {//建立監(jiān)聽(tīng)事件 public void actionPerformed(ActionEvent e) { for( int i=0;i<19;i ){ for(int j=0;j<19;j ) if(!judgle[i][j]) if(e.getSource()==b[i][j]) { if(n%2==0) { drawlconw icon = new drawlconw(40, 40);white[i][j]=true;//有bai棋子 b[i][j].setIcon(icon);judgle1(white,i,j);} if(n%2!=0) { drawlconb icon = new drawlconb(40, 40);black[i][j]=true;//有hei棋子 b[i][j].setIcon(icon);judgle1(black,i,j);} n ;//奇數(shù)偶數(shù)的計(jì)量方式,沒(méi)有特別意義 judgle[i][j]=true;//有了棋子 }} } }); } setSize(950,950); setLocationRelativeTo(null);//要放在sitsize后面,不然整個(gè)圖形就在 setVisible(true); setDefaultCloseOperation(WindowConstants.DISPOSE_ON_CLOSE); } public static void main(String[] args) { wuziqi2 f= new wuziqi2(); } static class drawlconw implements Icon{ // 實(shí)現(xiàn)Icon接口白色棋子 private int width; // 聲明圖標(biāo)的寬 private int height; // 聲明圖標(biāo)的長(zhǎng) public int getIconHeight() { // 實(shí)現(xiàn)getIconHeight()方法 return this.height; } public int getIconWidth() { // 實(shí)現(xiàn)getIconWidth()方法 return this.width; } public drawlconw(int width, int height) { // 定義構(gòu)造方法 this.width = width; this.height = height; } // 實(shí)現(xiàn)paintIcon()方法 public void paintIcon(Component arg0, Graphics arg1, int x, int y) { arg1.setColor(Color.white); arg1.fillOval(x, y, width, height); // 繪制一個(gè)圓形 } } static class drawlconb implements Icon{ // 實(shí)現(xiàn)Icon接口白色棋子 private int width; // 聲明圖標(biāo)的寬 private int height; // 聲明圖標(biāo)的長(zhǎng) public int getIconHeight() { // 實(shí)現(xiàn)getIconHeight()方法 return this.height; } public int getIconWidth() { // 實(shí)現(xiàn)getIconWidth()方法 return this.width; } public drawlconb(int width, int height) { // 定義構(gòu)造方法 this.width = width; this.height = height; } // 實(shí)現(xiàn)paintIcon()方法 public void paintIcon(Component arg0, Graphics arg1, int x, int y) { arg1.setColor(Color.black); arg1.fillOval(x, y, width, height); // 繪制一個(gè)圓形 } } class DrawPanel1 extends JPanel { // Graphics g=getGraphics(); public void paint(Graphics g) { super.paint(g); for ( int i = 25; i < 910; i = i 48) {//畫(huà)豎線(xiàn) g.drawLine(30, i, 912, i); g.setColor(Color.black); } for (int i = 30; i <= 925; i = i 49) {//畫(huà)橫線(xiàn) g.drawLine(i, 25, i, 890); } } } }
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
本人菜雞剛學(xué)java,有很多理解不好的地方,忘大佬指出!
5G游戲
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶(hù)投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。
版權(quán)聲明:本文內(nèi)容由網(wǎng)絡(luò)用戶(hù)投稿,版權(quán)歸原作者所有,本站不擁有其著作權(quán),亦不承擔(dān)相應(yīng)法律責(zé)任。如果您發(fā)現(xiàn)本站中有涉嫌抄襲或描述失實(shí)的內(nèi)容,請(qǐng)聯(lián)系我們jiasou666@gmail.com 處理,核實(shí)后本網(wǎng)站將在24小時(shí)內(nèi)刪除侵權(quán)內(nèi)容。