@@ -6,28 +6,42 @@ class Five_chess
66 static int cols =16 ;
77 static int row_num ;
88 static int col_num ;
9+ static int machine_row ;
10+ static int machine_col ;
911 static String piece_x ;
1012 static String piece_y ;
11- static char player ;
13+ static char player = '★' ;
1214 static boolean flag ;
15+ static String answer ;
16+ static boolean machine ;
1317 static Scanner input =new Scanner (System .in );
1418
1519 public static void main (String [] args ) {
1620 drawBoard ();
1721 printBoard ();
22+ System .out .println ("是否选择" +player +"作为人机和您对战:yes/no" );
23+ String answer =input .next ();
24+ setMachine (answer );
1825 while (true ) {
1926 judgePlayer (player );
2027 System .out .println (flag ?"白子:" :"黑子:" );
2128 player =flag ?'★' :'☆' ;
2229 while (true ) {
23- getCoordinate ();
24- if (judgePiece (piece_x ,piece_y )) {
25- continue ;
26- }else {
30+ if (machine &&flag ) {
31+ machinePiece ();
2732 setPiece (player );
2833 printBoard ();
2934 break ;
30- }
35+ }else {
36+ getCoordinate ();
37+ if (judgePiece (piece_x ,piece_y )) {
38+ continue ;
39+ }else {
40+ setPiece (player );
41+ printBoard ();
42+ break ;
43+ }
44+ }
3145 }
3246 if (judgeWin (player )){
3347 System .out .println (flag ?"白子赢了" :"黑子赢了" );
@@ -177,15 +191,33 @@ public static int getDirection(int piece_changeX,int piece_changeY,char piece){
177191 }
178192
179193 public static boolean judgeWin (char piece ){
180- if ((getDirection (0 ,1 ,piece )+getDirection (0 ,-1 ,piece ))>=4 )
181- return true ;
182- else if (getDirection (1 ,0 ,piece )+getDirection (-1 ,0 ,piece )>=4 )
194+ if ((getDirection (0 ,1 ,piece )+getDirection (0 ,-1 ,piece ))>=4 ){
195+ return true ;
196+ }
197+ else if (getDirection (1 ,0 ,piece )+getDirection (-1 ,0 ,piece )>=4 ){
183198 return true ;
184- else if (getDirection (1 ,1 ,piece )+getDirection (-1 ,-1 ,piece )>=4 )
199+ }
200+ else if (getDirection (1 ,1 ,piece )+getDirection (-1 ,-1 ,piece )>=4 ){
185201 return true ;
186- else if (getDirection (1 ,-1 ,piece )+getDirection (-1 ,1 ,piece )>=4 )
202+ }
203+ else if (getDirection (1 ,-1 ,piece )+getDirection (-1 ,1 ,piece )>=4 ){
187204 return true ;
205+ }
188206 else
189207 return false ;
190208 }
209+ //机器人对战
210+ public static void setMachine (String answer ){
211+
212+ if (answer .equals ("yes" )) {
213+ machine =true ;
214+ }else {
215+ machine =false ;
216+ }
217+ }
218+ //机器人落子判定
219+ public static void machinePiece (){
220+ row_num =(int )(Math .random ()*14 +1 );
221+ col_num =(int )(Math .random ()*14 +1 );
222+ }
191223}
0 commit comments