之前的两篇文章:java实现两人五子棋游戏(二) 画出棋盘;java实现两人五子棋游戏(三) 画出棋子;java实现两人五子棋游戏(四) 落子动作的实现;Java实现两人五子棋游戏(五) 判断是否有一方胜出,可以点击查看。
前面我们已经画好棋盘和棋子,并可以自由落子,且可以判断棋盘中是否有五连珠。接下来的工作是,每次落子后交换行棋方(即将下一棋子颜色由白->黑或黑->白),并将胜出判断增加颜色判断条件(棋盘上存在五连珠->棋盘上存在同色的五连珠)。
主模块main.java和棋子类chessman.java不变,我们要对drawchessboard.java中的落子函数mousepressed和五连珠判断函数iswin做一下调整。
main.java
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
package xchen.test.simplegobang;
import java.awt.container;
import javax.swing.jframe;
import xchen.test.simplegobang.drawchessboard;
public class main extends jframe{
private drawchessboard drawchessboard;
public main() {
drawchessboard = new drawchessboard();
//frame标题
settitle( "单机五子棋" );
container containerpane =getcontentpane();
containerpane.add(drawchessboard);
}
public static void main(string[] args) {
main m = new main();
m.setsize( 800 , 800 );
m.setvisible( true );
}
}
|
chessman.java
?
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
|
package xchen.test.simplegobang;
public class chessman {
private int color; //1-white,0-black
private boolean placed = false ;
int matchcount = 1 ;
public chessman( int color, boolean placed){
this .color=color;
this .placed=placed;
}
public boolean getplaced() {
return placed;
}
public void setplaced( boolean placed) {
this .placed = placed;
}
public int getcolor() {
return color;
}
public void setcolor( int color) {
this .color = color;
}
}
|
第一步,修改mousepressed函数,使得每次落子后,都将行棋方让给对家(即每次落子都改变棋子颜色)
drawchessboard.java中的mousepressed函数部分,只要每次都修改chesscolor的值即可。
?
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
|
@override
//当用户按下鼠标按钮时发生
public void mousepressed(mouseevent e) {
int point_x=e.getx();
int point_y=e.gety();
int imgwidth = boardimg.getheight( this );
int imgheight = boardimg.getwidth( this );
int fwidth = getwidth();
int fheight= getheight();
int x=(fwidth-imgwidth)/ 2 ;
int y=(fheight-imgheight)/ 2 ;
int span_x=imgwidth/rows;
int span_y=imgheight/rows;
//system.out.println("press");
int status_x = 0 ;
int status_y = 0 ;
if (point_x>=x && point_x<=x+imgwidth && point_y>=y && point_y <= y+imgheight)
{
//system.out.println("合法");
for ( int i= 0 ;i<rows+ 1 ;i++)
{
if (point_x>=x-chessman_width/ 2 + 1 +i*span_x)
{
if (point_x<=x+chessman_width/ 2 - 1 +i*span_x) //如果是width/2会在中间点出现两个匹配值
{
//system.out.println("point x "+i+" "+point_x+" "+(x-chessman_width/2+i*span_x)+" "+(x+chessman_width/2+i*span_x));
status_x = i;
}
}
}
for ( int i= 0 ;i<rows+ 1 ;i++)
{
if (point_y>=y-chessman_width/ 2 + 1 +i*span_y)
{
if (point_y <= y+chessman_width/ 2 - 1 +i*span_y)
{
//system.out.println("point y "+i+" "+point_y+" "+(y-chessman_width/2+1+i*span_y)+" "+(y+chessman_width/2-1+i*span_y));
status_y = i;
}
}
}
chessman chessman = new chessman(chesscolor, true );
chessstatus[status_x][status_y]=chessman;
system.out.println( "chess color:" +chesscolor);
if (chesscolor==black)
{
chesscolor = white;
} else {
chesscolor = black;
}
repaint();
if (iswin(status_x, status_y, chessstatus))
{
system.out.println( "win!!!!!" );
}
}
}
|
运行一下
第二步,将胜出判断增加颜色判断条件(棋盘上存在五连珠->棋盘上存在同色的五连珠)
在iswin函数的判断条件上,加上对chessman.getcolor的颜色判断
iswin函数代码
?
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
|
boolean iswin( int point_x, int point_y,chessman[][] cm)
{
for ( int i= 0 ;i<rows+ 1 ;i++)
{
for ( int j= 0 ;j<rows+ 1 ;j++)
{
//横向查找
if (chessstatus[i][j]!= null &&chessstatus[i][j].getplaced()== true )
{
int matchcolor = chessstatus[i][j].getcolor();
//向右侧查找
for ( int n= 1 ;n<= 4 ;n++)
{
if ((i+n>= 0 )&&(i+n)<=rows)
{
if (chessstatus[i+n][j]!= null &&chessstatus[i+n][j].getplaced()== true &&chessstatus[i+n][j].getcolor()==matchcolor)
{
chessstatus[i][j].matchcount++;
system.out.println( "pos:" +i+ " " +j+ " right count++:" +(i+n)+ " " +j+ " count:" +chessstatus[i][j].matchcount);
if (chessstatus[i][j].matchcount== 5 )
{
return true ;
}
} else
{
break ;
}
}
}
//向左侧查找
for ( int n= 1 ;n<= 4 ;n++)
{
if ((i-n>= 0 )&&(i-n)<=rows)
{
if (chessstatus[i-n][j]!= null &&chessstatus[i-n][j].getplaced()== true &&chessstatus[i-n][j].getcolor()==matchcolor)
{
chessstatus[i][j].matchcount++;
system.out.println( "pos:" +i+ " " +j+ " " + "left count++:" +(i-n)+ " " +j+ " count:" +chessstatus[i][j].matchcount);
if (chessstatus[i][j].matchcount== 5 )
{
return true ;
}
} else
{
if (chessstatus[i-n][j]!= null )
{
chessstatus[i][j].matchcount = 1 ;
}
break ;
}
}
}
chessstatus[i][j].matchcount= 1 ; //refresh count
}
}
}
for ( int i= 0 ;i<rows+ 1 ;i++)
{
for ( int j= 0 ;j<rows+ 1 ;j++)
{
//纵向
if (chessstatus[i][j]!= null &&chessstatus[i][j].getplaced()== true )
{
int matchcolor = chessstatus[i][j].getcolor();
//向下查找,左上角为坐标原点,y轴正方向向下
for ( int n= 1 ;n<= 4 ;n++)
{
if ((j+n>= 0 )&&(j+n)<=rows)
{
if (chessstatus[i][j+n]!= null &&chessstatus[i][j+n].getplaced()== true &&chessstatus[i][j+n].getcolor()==matchcolor)
{
chessstatus[i][j].matchcount++;
system.out.println( "pos:" +i+ " " +j+ " up count++:" +(i)+ " " +(j+n)+ " count:" +chessstatus[i][j].matchcount);
if (chessstatus[i][j].matchcount== 5 )
{
return true ;
}
} else
{
break ;
}
}
}
//向上查找
for ( int n= 1 ;n<= 4 ;n++)
{
if ((j-n>= 0 )&&(j-n)<=rows)
{
if (chessstatus[i][j-n]!= null &&chessstatus[i][j-n].getplaced()== true &&chessstatus[i][j-n].getcolor()==matchcolor)
{
chessstatus[i][j].matchcount++;
system.out.println( "pos:" +i+ " " +j+ " " + "left count++:" +(i)+ " " +(j-n)+ " count:" +chessstatus[i][j].matchcount);
if (chessstatus[i][j].matchcount== 5 )
{
return true ;
}
} else
{
if (chessstatus[i][j-n]!= null )
{
chessstatus[i][j].matchcount = 1 ;
}
break ;
}
}
}
chessstatus[i][j].matchcount= 1 ; //refresh count
}
}
}
//方向:左上右下
for ( int i= 0 ;i<rows+ 1 ;i++)
{
for ( int j= 0 ;j<rows+ 1 ;j++)
{
//左上
if (chessstatus[i][j]!= null &&chessstatus[i][j].getplaced()== true )
{
int matchcolor = chessstatus[i][j].getcolor();
//向下查找,左上角为坐标原点,y轴正方向向下
for ( int n= 1 ;n<= 4 ;n++)
{
if ((j-n>= 0 )&&(j-n)<=rows&&(i-n)>= 0 &&(i-n)<=rows)
{
if (chessstatus[i-n][j-n]!= null &&chessstatus[i-n][j-n].getplaced()== true &&chessstatus[i-n][j-n].getcolor()==matchcolor)
{
chessstatus[i][j].matchcount++;
system.out.println( "pos:" +i+ " " +j+ " up count++:" +(i-n)+ " " +(j-n)+ " count:" +chessstatus[i][j].matchcount);
if (chessstatus[i][j].matchcount== 5 )
{
return true ;
}
} else
{
break ;
}
}
}
//右下
for ( int n= 1 ;n<= 4 ;n++)
{
if ((j+n>= 0 )&&(j+n)<=rows&&(i+n)>= 0 &&(i+n)<=rows)
{
if (chessstatus[i+n][j+n]!= null &&chessstatus[i+n][j+n].getplaced()== true &&chessstatus[i+n][j+n].getcolor()==matchcolor)
{
chessstatus[i][j].matchcount++;
system.out.println( "pos:" +i+ " " +j+ " " + "left count++:" +(i+n)+ " " +(j+n)+ " count:" +chessstatus[i][j].matchcount);
if (chessstatus[i][j].matchcount== 5 )
{
return true ;
}
} else
{
if (chessstatus[i+n][j+n]!= null )
{
chessstatus[i][j].matchcount = 1 ;
}
break ;
}
}
}
chessstatus[i][j].matchcount= 1 ; //refresh count
}
}
}
//方向:左下右上
for ( int i= 0 ;i<rows+ 1 ;i++)
{
for ( int j= 0 ;j<rows+ 1 ;j++)
{
//左下
if (chessstatus[i][j]!= null &&chessstatus[i][j].getplaced()== true )
{
int matchcolor = chessstatus[i][j].getcolor();
//向下查找,左上角为坐标原点,y轴正方向向下
for ( int n= 1 ;n<= 4 ;n++)
{
if ((j+n>= 0 )&&(j+n)<=rows&&(i-n)>= 0 &&(i-n)<=rows)
{
if (chessstatus[i-n][j+n]!= null &&chessstatus[i-n][j+n].getplaced()== true &&chessstatus[i-n][j+n].getcolor()==matchcolor)
{
chessstatus[i][j].matchcount++;
system.out.println( "pos:" +i+ " " +j+ " up count++:" +(i-n)+ " " +(j+n)+ " count:" +chessstatus[i][j].matchcount);
if (chessstatus[i][j].matchcount== 5 )
{
return true ;
}
} else
{
break ;
}
}
}
//右上
for ( int n= 1 ;n<= 4 ;n++)
{
if ((j-n>= 0 )&&(j-n)<=rows&&(i+n)>= 0 &&(i+n)<=rows)
{
if (chessstatus[i+n][j-n]!= null &&chessstatus[i+n][j-n].getplaced()== true &&chessstatus[i+n][j-n].getcolor()==matchcolor)
{
chessstatus[i][j].matchcount++;
system.out.println( "pos:" +i+ " " +j+ " " + "left count++:" +(i+n)+ " " +(j-n)+ " count:" +chessstatus[i][j].matchcount);
if (chessstatus[i][j].matchcount== 5 )
{
return true ;
}
} else
{
if (chessstatus[i+n][j-n]!= null )
{
chessstatus[i][j].matchcount = 1 ;
}
break ;
}
}
}
chessstatus[i][j].matchcount= 1 ; //refresh count
}
}
}
|