這個小游戲是我和我姐們兒的java課程設(shè)計,也是我做的第一個java項目,適合初學(xué)者,希望能幫到那些被java課設(shè)所困擾的孩紙們~~~
一、該游戲需要實現(xiàn)
1、設(shè)計主框架,界面。
2、利用actionlistener接口實現(xiàn)按鈕事件的監(jiān)聽。
3、重新開始功能的實現(xiàn)。
4、悔棋功能的實現(xiàn)。
5、退出功能的實現(xiàn)。
6、棋盤中棋子點類的定義。
7、利用mouselistener接口實現(xiàn)事件監(jiān)聽,并實現(xiàn)接口里的所有方法。
8、當鼠標移動到棋盤上的交點上,且該點上無棋子時能夠變成小手形狀。
9、點擊棋盤時,利用if語句判斷該點是否點在交點上,并利用foreach語句和棋子類中的getx(),gety()方法遍歷每一個棋子的位置判斷該點是否有棋子。
10、當判斷到可以在所點擊的點上下子時,畫棋子時利用for循環(huán)遍歷已有的每一個點并利用graphics類的setcolor設(shè)置顏色,利用graphics類的filloval方法設(shè)置形狀大小。
11、當畫完棋子時要及時判斷輸贏,用棋子所在索引和for循環(huán)遍歷最后一個棋子的各個方向,如果有在同一條直線上的棋子個數(shù)大于等于五的即當前棋子所代表的那方贏。
12、勝負已定的時候,能夠彈出相應(yīng)的信息。
二、功能代碼實現(xiàn)
2.1進入游戲
1
2
3
4
|
public static void main(string[] args) { startchessjframe f= new startchessjframe(); //創(chuàng)建主框架 f.setvisible( true ); //顯示主框架 } |
2.2初始化,定義一些要用到的量。
1
2
3
4
5
|
private chessboard chessboard; //對戰(zhàn)面板 private panel toolbar; //工具條面板 private button startbutton; //設(shè)置開始按鈕 private button backbutton; //設(shè)置悔棋按鈕 private button exitbutton; //設(shè)置退出按鈕 |
2.3界面的構(gòu)造方法(游戲的框架)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public startchessjframe(){ chessboard= new chessboard(); //初始化面板對象,創(chuàng)建和添加菜單 myitemlistener lis= new myitemlistener(); //初始化按鈕事件監(jiān)聽器內(nèi)部類 toolbar= new panel(); //工具面板欄實例化 startbutton= new button( "重新開始" ); backbutton= new button( "悔棋" ); exitbutton= new button( "退出" ); //三個按鈕初始化 toolbar.setlayout( new flowlayout(flowlayout.left)); //將工具面板按鈕用flowlayout布局 toolbar.add(backbutton); toolbar.add(startbutton); toolbar.add(exitbutton); //將三個按鈕添加到工具面板上 startbutton.addactionlistener(lis); backbutton.addactionlistener(lis); exitbutton.addactionlistener(lis); //將三個按鈕事件注冊監(jiān)聽事件 add(toolbar,borderlayout.south); //將工具面板布局到界面南方也就是下面 add(chessboard); //將面板對象添加到窗體上 setdefaultcloseoperation(jframe.exit_on_close); //設(shè)置界面關(guān)閉事件 pack(); //自適應(yīng)大小 } |
2.4按鈕的實現(xiàn)與監(jiān)聽(構(gòu)造方法內(nèi)部)
1
2
3
4
5
6
7
8
9
10
11
12
|
myitemlistener lis= new myitemlistener(); //初始化按鈕事件監(jiān)聽器內(nèi)部類 toolbar= new panel(); //工具面板欄實例化 startbutton= new button( "重新開始" ); backbutton= new button( "悔棋" ); exitbutton= new button( "退出" ); //三個按鈕初始化 toolbar.setlayout( new flowlayout(flowlayout.left)); //將工具面板按鈕用flowlayout布局 toolbar.add(backbutton); toolbar.add(startbutton); toolbar.add(exitbutton); //將三個按鈕添加到工具面板上 startbutton.addactionlistener(lis); backbutton.addactionlistener(lis); exitbutton.addactionlistener(lis); //將三個按鈕事件注冊監(jiān)聽事件 |
2.5按鈕事件的監(jiān)聽
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
private class myitemlistener implements actionlistener{ public void actionperformed(actionevent e) { object obj=e.getsource(); //獲取事件源 if (obj==startbutton){ system.out.println( "重新開始..." ); //重新開始 //jfiveframe.this內(nèi)部類引用外部類 chessboard.restartgame(); } else if (obj==exitbutton){ system.exit( 0 ); //結(jié)束應(yīng)用程序 } else if (obj==backbutton){ system.out.println( "悔棋..." ); //悔棋 chessboard.goback(); } } } |
2.6重新開始按鈕的功能實現(xiàn)
1
2
3
4
5
6
7
8
9
|
public void restartgame(){ //清除棋子 for ( int i= 0 ;i<chesslist.length;i++) chesslist[i]= null ; /*恢復(fù)游戲相關(guān)的變量值*/ isback= true ; gameover= false ; //游戲是否結(jié)束 chesscount= 0 ; //當前棋盤的棋子個數(shù) repaint(); } |
2.7悔棋按鈕的功能實現(xiàn)
1
2
3
4
5
6
7
8
9
10
11
12
|
public void goback(){ if (chesscount== 0 ) return ; chesslist[chesscount- 1 ]= null ; chesscount--; if (chesscount> 0 ){ xindex=chesslist[chesscount- 1 ].getx(); yindex=chesslist[chesscount- 1 ].gety(); } isback=!isback; repaint(); } |
2.8當棋盤根據(jù)需要變大或變小時窗口應(yīng)隨之發(fā)生改變
1
2
3
4
5
|
//dimension:矩形chessboard類內(nèi)部 public dimension getpreferredsize(){ return new dimension(margin* 2 +grid_span*cols,margin* 2 +grid_span*rows); } pack(); //自適應(yīng)大小startchessboard類內(nèi)部 |
2.9定義棋子類
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
import java.awt.*; public class point { private int x; //棋子在棋盤中的x索引值 private int y; //棋子在棋盤中的y索引值 private color color; //顏色 public static int diameter= 30 ; //直徑 public point( int x, int y,color color){ this .x=x; this .y=y; this .color=color; } //得到棋子在棋盤中的x索引值 public int getx(){ return x; } //得到棋子在棋盤中的y索引值 public int gety(){ return y; } //得到棋子顏色 public color getcolor(){ return color; } } |
三、功能部分代碼實現(xiàn)
3.1初始化,定義一些要用到的量。
1
2
3
4
5
6
7
8
9
|
public static int margin= 30 ; //邊距 public static int grid_span= 35 ; //網(wǎng)格間距 public static int rows= 18 ; //棋盤行數(shù) public static int cols= 18 ; //棋盤列數(shù) point[] chesslist= new point[(rows+ 1 )*(cols+ 1 )]; //初始化每個數(shù)組元素為null boolean isback= true ; //默認開始是黑棋先下 boolean gameover= false ; //游戲是否結(jié)束 int chesscount; //當前棋盤的棋子個數(shù) int xindex,yindex; //當前剛下棋子的索引 |
3.2棋盤對象的構(gòu)造方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public chessboard(){ setbackground(color.light_gray); //設(shè)置背景顏色為灰色 addmouselistener( this ); //添加事件監(jiān)聽器 addmousemotionlistener( new mousemotionlistener() { //匿名內(nèi)部類 @override public void mousemoved(mouseevent e) { int x1=(e.getx()-margin+grid_span/ 2 )/grid_span; int y1=(e.gety()-margin+grid_span/ 2 )/grid_span; //將鼠標單擊的坐標位置轉(zhuǎn)化為網(wǎng)格索引 if (x1< 0 ||x1>rows||y1< 0 ||y1>cols||gameover||findchess(x1,y1)){ //游戲已經(jīng)結(jié)束,不能下;落在棋盤外,不能下;x,y位置已經(jīng)有棋子存在,不能下 setcursor( new cursor(cursor.default_cursor)); //設(shè)置成默認形狀 } else { setcursor( new cursor(cursor.hand_cursor)); //設(shè)置成手型 } } @override public void mousedragged(mouseevent e) { } }); } |
3.3設(shè)置鼠標監(jiān)聽器,變小手(在構(gòu)造方法內(nèi)部)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
|
addmousemotionlistener( new mousemotionlistener() { //匿名內(nèi)部類 @override public void mousemoved(mouseevent e) { int x1=(e.getx()-margin+grid_span/ 2 )/grid_span; int y1=(e.gety()-margin+grid_span/ 2 )/grid_span; //將鼠標單擊的坐標位置轉(zhuǎn)化為網(wǎng)格索引 if (x1< 0 ||x1>rows||y1< 0 ||y1>cols||gameover||findchess(x1,y1)){ //游戲已經(jīng)結(jié)束,不能下;落在棋盤外,不能下;x,y位置已經(jīng)有棋子存在,不能下 setcursor( new cursor(cursor.default_cursor)); //設(shè)置成默認形狀 } else { setcursor( new cursor(cursor.hand_cursor)); //設(shè)置成手型 } } @override public void mousedragged(mouseevent e) { } }); |
3.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
|
public void mousepressed(mouseevent e) { //鼠標按鍵在組件上按下時調(diào)用 if (gameover) //游戲已經(jīng)結(jié)束,不能下 return ; string colorname=isback ? "黑棋" : "白棋" ; xindex=(e.getx()-margin+grid_span/ 2 )/grid_span; yindex=(e.gety()-margin+grid_span/ 2 )/grid_span; //將鼠標單擊的坐標位置轉(zhuǎn)化為網(wǎng)格索引 if (xindex< 0 ||xindex>rows||yindex< 0 ||yindex>cols) //棋子落在棋盤外,不能下 return ; if (findchess(xindex,yindex)) //x,y位置已經(jīng)有棋子存在,不能下 return ; point ch= new point(xindex,yindex,isback ? color.black : color.white); chesslist[chesscount++]=ch; repaint(); //通知系統(tǒng)重新繪制 if (iswin()){ string msg=string.format( "恭喜,%s贏啦~" , colorname); joptionpane.showmessagedialog( this , msg); gameover= true ; } else if (chesscount==(cols+ 1 )*(rows+ 1 )) { string msg=string.format( "棋鼓相當,棒棒噠~" ); joptionpane.showmessagedialog( this ,msg); gameover= true ; } isback=!isback; } |
3.5繪制棋盤,棋子還有紅框框
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
public void paintcomponent(graphics g){ super .paintcomponent(g); //畫棋盤 for ( int i= 0 ;i<=rows;i++){ //畫橫線 g.drawline(margin, margin+i*grid_span, margin+cols*grid_span, margin+i*grid_span); } for ( int i= 0 ;i<=cols;i++){ //畫直線 g.drawline(margin+i*grid_span, margin, margin+i*grid_span,margin+rows*grid_span); } /*畫棋子*/ for ( int i= 0 ;i<chesscount;i++){ int xpos=chesslist[i].getx()*grid_span+margin; //網(wǎng)格交叉的x坐標 int ypos=chesslist[i].gety()*grid_span+margin; //網(wǎng)格交叉的y坐標 g.setcolor(chesslist[i].getcolor()); //設(shè)置顏色 g.filloval(xpos-point.diameter/ 2 , ypos-point.diameter/ 2 , point.diameter, point.diameter); if (i==chesscount- 1 ){ g.setcolor(color.red); //標記最后一個棋子為紅色 g.drawrect(xpos-point.diameter/ 2 , ypos-point.diameter/ 2 , point.diameter, point.diameter); } } } |
3.6判斷輸贏
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
|
/*判斷哪方贏*/ private boolean iswin(){ int continuecount= 1 ; //連續(xù)棋子的個數(shù) for ( int x=xindex- 1 ;x>= 0 ;x--){ //橫向向左尋找 color c=isback ? color.black : color.white; if (getchess(x,yindex,c)!= null ){ continuecount++; } else break ; } for ( int x=xindex+ 1 ;x<=rows;x++){ //橫向向右尋找 color c=isback ? color.black : color.white; if (getchess(x,yindex,c)!= null ){ continuecount++; } else break ; } if (continuecount>= 5 ){ //判斷記錄數(shù)大于等于五,即表示此方獲勝 return true ; } else continuecount= 1 ; // for ( int y=yindex- 1 ;y>= 0 ;y--){ //縱向向上尋找 color c=isback ? color.black : color.white; if (getchess(xindex,y,c)!= null ){ continuecount++; } else break ; } for ( int y=yindex+ 1 ;y<=rows;y++){ //縱向向下尋找 color c=isback ? color.black : color.white; if (getchess(xindex,y,c)!= null ){ continuecount++; } else break ; } if (continuecount>= 5 ){ //判斷記錄數(shù)大于等于五,即表示此方獲勝 return true ; } else continuecount= 1 ; // for ( int x=xindex+ 1 ,y=yindex- 1 ;y>= 0 &&x<=cols;x++,y--){ //右下尋找 color c=isback ? color.black : color.white; if (getchess(x,y,c)!= null ){ continuecount++; } else break ; } for ( int x=xindex- 1 ,y=yindex+ 1 ;y<=rows&&x>= 0 ;x--,y++){ //左上尋找 color c=isback ? color.black : color.white; if (getchess(x,y,c)!= null ){ continuecount++; } else break ; } if (continuecount>= 5 ){ //判斷記錄數(shù)大于等于五,即表示此方獲勝 return true ; } else continuecount= 1 ; // for ( int x=xindex- 1 ,y=yindex- 1 ;y>= 0 &&x>= 0 ;x--,y--){ //左下尋找 color c=isback ? color.black : color.white; if (getchess(x,y,c)!= null ){ continuecount++; } else break ; } for ( int x=xindex+ 1 ,y=yindex+ 1 ;y<=rows&&x<=cols;x++,y++){ //右上尋找 color c=isback ? color.black : color.white; if (getchess(x,y,c)!= null ){ continuecount++; } else break ; } if (continuecount>= 5 ){ //判斷記錄數(shù)大于等于五,即表示此方獲勝 return true ; } else continuecount= 1 ; return false ; } |
3.7彈出相應(yīng)消息框(在鼠標按壓函數(shù)內(nèi)部)
1
2
3
4
5
6
7
8
9
10
11
|
if (iswin()){ string msg=string.format( "恭喜,%s贏啦~" , colorname); joptionpane.showmessagedialog( this , msg); gameover= true ; } else if (chesscount==(cols+ 1 )*(rows+ 1 )) //平局 { string msg=string.format( "棋鼓相當,棒棒噠~" ); joptionpane.showmessagedialog( this ,msg); gameover= true ; } |
3.8上面用到的一個判斷某點是否有棋子的函數(shù)
1
2
3
4
5
6
7
|
private boolean findchess( int x, int y){ for (point c:chesslist){ if (c!= null &&c.getx()==x&&c.gety()==y) return true ; } return false ; } |
3.9因為該棋盤類實現(xiàn)了鼠標監(jiān)聽接口monselistener,所以要重寫該接口內(nèi)的所有方法,其它方法如下
1
2
3
4
5
6
7
8
9
10
11
12
|
@override public void mouseclicked(mouseevent e) { //鼠標按鍵在組件上單擊(按下并釋放)時調(diào)用 } @override public void mousereleased(mouseevent e) { ////鼠標按鍵在組件上釋放時調(diào)用 } @override public void mouseentered(mouseevent e) { //鼠標進入組件時調(diào)用 } @override public void mouseexited(mouseevent e){ //鼠標離開組件時調(diào)用 } |
四、運行結(jié)果
五、代碼匯總
該游戲總共建了三個類,一個是界面startchessjframe,一個是棋盤類chessboard,一個是棋子類point
5.1startchessjframe類
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
|
package chess.lcc.com; import javax.swing.*; import java.awt.event.*; import java.awt.*; /* * 五子棋的主框架,程序啟動類 */ public class startchessjframe extends jframe { private chessboard chessboard; //對戰(zhàn)面板 private panel toolbar; //工具條面板 private button startbutton; //設(shè)置開始按鈕 private button backbutton; //設(shè)置悔棋按鈕 private button exitbutton; //設(shè)置退出按鈕 public startchessjframe(){ settitle( "單機版五子棋" ); //設(shè)置標題 chessboard= new chessboard(); //初始化面板對象,創(chuàng)建和添加菜單 myitemlistener lis= new myitemlistener(); //初始化按鈕事件監(jiān)聽器內(nèi)部類 toolbar= new panel(); //工具面板欄實例化 startbutton= new button( "重新開始" ); backbutton= new button( "悔棋" ); exitbutton= new button( "退出" ); //三個按鈕初始化 toolbar.setlayout( new flowlayout(flowlayout.left)); //將工具面板按鈕用flowlayout布局 toolbar.add(backbutton); toolbar.add(startbutton); toolbar.add(exitbutton); //將三個按鈕添加到工具面板上 startbutton.addactionlistener(lis); backbutton.addactionlistener(lis); exitbutton.addactionlistener(lis); //將三個按鈕事件注冊監(jiān)聽事件 add(toolbar,borderlayout.south); //將工具面板布局到界面南方也就是下面 add(chessboard); //將面板對象添加到窗體上 setdefaultcloseoperation(jframe.exit_on_close); //設(shè)置界面關(guān)閉事件 pack(); //自適應(yīng)大小 } private class myitemlistener implements actionlistener{ public void actionperformed(actionevent e) { object obj=e.getsource(); //獲取事件源 if (obj==startbutton){ system.out.println( "重新開始..." ); //重新開始 //jfiveframe.this內(nèi)部類引用外部類 chessboard.restartgame(); } else if (obj==exitbutton){ system.exit( 0 ); //結(jié)束應(yīng)用程序 } else if (obj==backbutton){ system.out.println( "悔棋..." ); //悔棋 chessboard.goback(); } } } public static void main(string[] args) { startchessjframe f= new startchessjframe(); //創(chuàng)建主框架 f.setvisible( true ); //顯示主框架 } } |
5.2chessboard類
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
|
package chess.lcc.com; import javax.swing.*; import java.awt.*; import java.awt.event.mouselistener; import java.awt.event.mousemotionlistener; import java.awt.event.mouseevent; /*五子棋-棋盤類*/ public class chessboard extends jpanel implements mouselistener{ public static int margin=30;//邊距 public static int grid_span=35;//網(wǎng)格間距 public static int rows=15;//棋盤行數(shù) public static int cols=15;//棋盤列數(shù) point[] chesslist=new point[(rows+1)*(cols+1)];//初始化每個數(shù)組元素為null boolean isback=true;//默認開始是黑棋先下 boolean gameover=false;//游戲是否結(jié)束 int chesscount;//當前棋盤的棋子個數(shù) int xindex,yindex;//當前剛下棋子的索引 public chessboard(){ setbackground(color.light_gray);//設(shè)置背景顏色為黃色 addmouselistener(this);//添加事件監(jiān)聽器 addmousemotionlistener(new mousemotionlistener() {//匿名內(nèi)部類 @override public void mousemoved(mouseevent e) { int x1=(e.getx()-margin+grid_span/2)/grid_span; int y1=(e.gety()-margin+grid_span/2)/grid_span;//將鼠標單擊的坐標位置轉(zhuǎn)化為網(wǎng)格索引 if(x1<0||x1>rows||y1<0||y1>cols||gameover||findchess(x1,y1)){//游戲已經(jīng)結(jié)束,不能下;落在棋盤外,不能下;x,y位置已經(jīng)有棋子存在,不能下 setcursor(new cursor(cursor.default_cursor));//設(shè)置成默認形狀 }else{ setcursor(new cursor(cursor.hand_cursor));//設(shè)置成手型 } } @override public void mousedragged(mouseevent e) { } }); } /*繪制*/ public void paintcomponent(graphics g){ super.paintcomponent(g);//畫棋盤 for(int i=0;i<=rows;i++){//畫橫線 g.drawline(margin, margin+i*grid_span, margin+cols*grid_span, margin+i*grid_span); } for(int i=0;i<=cols;i++){//畫直線 g.drawline(margin+i*grid_span, margin, margin+i*grid_span,margin+rows*grid_span); } /*畫棋子*/ for(int i=0;i<chesscount;i++){ int xpos=chesslist[i].getx()*grid_span+margin;//網(wǎng)格交叉的x坐標 int ypos=chesslist[i].gety()*grid_span+margin;//網(wǎng)格交叉的y坐標 g.setcolor(chesslist[i].getcolor());//設(shè)置顏色 g.filloval(xpos-point.diameter/2, ypos-point.diameter/2, point.diameter, point.diameter); if(i==chesscount-1){ g.setcolor(color.red);//標記最后一個棋子為紅色 g.drawrect(xpos-point.diameter/2, ypos-point.diameter/2, point.diameter, point.diameter); } } } @override public void mousepressed(mouseevent e) {//鼠標按鍵在組件上按下時調(diào)用 if(gameover)//游戲已經(jīng)結(jié)束,不能下 return ; string colorname=isback ? "黑棋" : "白棋"; xindex=(e.getx()-margin+grid_span/2)/grid_span; yindex=(e.gety()-margin+grid_span/2)/grid_span;//將鼠標單擊的坐標位置轉(zhuǎn)化為網(wǎng)格索引 if(xindex<0||xindex>rows||yindex<0||yindex>cols)//棋子落在棋盤外,不能下 return ; if(findchess(xindex,yindex))//x,y位置已經(jīng)有棋子存在,不能下 return ; point ch=new point(xindex,yindex,isback ? color.black : color.white); chesslist[chesscount++]=ch; repaint();//通知系統(tǒng)重新繪制 if(iswin()){ string msg=string.format("恭喜,%s贏啦~", colorname); joptionpane.showmessagedialog(this, msg); gameover=true; } else if(chesscount==(cols+1)*(rows+1)) { string msg=string.format("棋鼓相當,棒棒噠~"); joptionpane.showmessagedialog(this,msg); gameover=true; } isback=!isback; } @override public void mouseclicked(mouseevent e) {//鼠標按鍵在組件上單擊(按下并釋放)時調(diào)用 } @override public void mousereleased(mouseevent e) {////鼠標按鍵在組件上釋放時調(diào)用 } @override public void mouseentered(mouseevent e) {//鼠標進入組件時調(diào)用 } @override public void mouseexited(mouseevent e){//鼠標離開組件時調(diào)用 } private boolean findchess(int x,int y){ for(point c:chesslist){ if(c!=null&&c.getx()==x&&c.gety()==y) return true; } return false; } /*判斷那方贏*/ private boolean iswin(){ int continuecount=1;//連續(xù)棋子的個數(shù) for(int x=xindex-1;x>=0;x--){//橫向向左尋找 color c=isback ? color.black : color.white; if(getchess(x,yindex,c)!=null){ continuecount++; }else break; } for(int x=xindex+1;x<=rows;x++){//橫向向右尋找 color c=isback ? color.black : color.white; if(getchess(x,yindex,c)!=null){ continuecount++; }else break; } if(continuecount>=5){//判斷記錄數(shù)大于等于五,即表示此方獲勝 return true; }else continuecount=1; // for(int y=yindex-1;y>=0;y--){//縱向向上尋找 color c=isback ? color.black : color.white; if(getchess(xindex,y,c)!=null){ continuecount++; }else break; } for(int y=yindex+1;y<=rows;y++){//縱向向下尋找 color c=isback ? color.black : color.white; if(getchess(xindex,y,c)!=null){ continuecount++; }else break; } if(continuecount>=5){//判斷記錄數(shù)大于等于五,即表示此方獲勝 return true; }else continuecount=1; // for(int x=xindex+1,y=yindex-1;y>=0&&x<=cols;x++,y--){//右下尋找 color c=isback ? color.black : color.white; if(getchess(x,y,c)!=null){ continuecount++; }else break; } for(int x=xindex-1,y=yindex+1;y<=rows&&x>=0;x--,y++){//左上尋找 color c=isback ? color.black : color.white; if(getchess(x,y,c)!=null){ continuecount++; }else break; } if(continuecount>=5){//判斷記錄數(shù)大于等于五,即表示此方獲勝 return true; }else continuecount=1; // for(int x=xindex-1,y=yindex-1;y>=0&&x>=0;x--,y--){//左下尋找 color c=isback ? color.black : color.white; if(getchess(x,y,c)!=null){ continuecount++; }else break; } for(int x=xindex+1,y=yindex+1;y<=rows&&x<=cols;x++,y++){//右上尋找 color c=isback ? color.black : color.white; if(getchess(x,y,c)!=null){ continuecount++; }else break; } if(continuecount>=5){//判斷記錄數(shù)大于等于五,即表示此方獲勝 return true; }else continuecount=1; return false; } private point getchess(int xindex,int yindex,color color){ for(point c:chesslist){ if(c!=null&&c.getx()==xindex&&c.gety()==yindex&&c.getcolor()==color) return c; } return null; } public void restartgame(){//清除棋子 for(int i=0;i<chesslist.length;i++) chesslist[i]=null; /*恢復(fù)游戲相關(guān)的變量值*/ isback= true ; gameover= false ; //游戲是否結(jié)束 chesscount= 0 ; //當前棋盤的棋子個數(shù) repaint(); } public void goback(){ if (chesscount== 0 ) return ; chesslist[chesscount- 1 ]= null ; chesscount--; if (chesscount> 0 ){ xindex=chesslist[chesscount- 1 ].getx(); yindex=chesslist[chesscount- 1 ].gety(); } isback=!isback; repaint(); } //dimension:矩形 public dimension getpreferredsize(){ return new dimension(margin* 2 +grid_span*cols,margin* 2 +grid_span*rows); } } |
5.3point類
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
|
package chess.lcc.com; import java.awt.*; public class point { private int x; //棋子在棋盤中的x索引值 private int y; //棋子在棋盤中的y索引值 private color color; //顏色 public static int diameter= 30 ; //直徑 public point( int x, int y,color color){ this .x=x; this .y=y; this .color=color; } //得到棋子在棋盤中的x索引值 public int getx(){ return x; } //得到棋子在棋盤中的y索引值 public int gety(){ return y; } //得到棋子顏色 public color getcolor(){ return color; } } |
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持服務(wù)器之家。
原文鏈接:https://blog.csdn.net/lin14543/article/details/51867892