本文实例为大家分享了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 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 266 267 268 269 270 271 |
package com.lr.bean; import java.util.scanner; import java.util.random; import com.lr.bean.point;
public class link{ public static void main(string[] args){ scanner sc= new scanner(system.in); system.out.println( "==========================" ); system.out.println( "\t\t连连看小游戏" ); system.out.println( "\t\t版权:lr" ); system.out.println( "==========================" );
system.out.println( "请先输入图片数量(难度系数 1~9):" ); int picnum=sc.nextint();
system.out.println( "请输入棋盘的行数:" ); int rows=sc.nextint();
int cols= 0 ; //列数 int count= 0 ; //计数器 do { if (count> 0 ){ system.out.println( "输入有误,列数必须大于0!" ); } system.out.println( "请输入棋盘列数:" ); cols=sc.nextint(); count++; } while ( cols< 1 || cols% 2 != 0 ); //创建二维数组,生成棋盘,列数+2的原因:做出边框的效果 数组刚刚生成时,每个元素都是0 int [][] chessboard= new int [ rows+ 2 ][ cols+ 2 ];
//随机生成的图片数值存入这个二维数组中,注意:边框不存值,任为0 initboard( chessboard ,picnum); system.out.println( "初始化后的棋盘为:" ); showboard2( chessboard); //打乱棋盘 shuffle( chessboard ); //输出 system.out.println( "打乱后的棋盘为:" ); showboard2( chessboard);
//实现消除业务 // 1.定义两个point对象 point p1= new point(); point p2= new point(); // 2.游戏状态 isgameover boolean isgameover= false ; do { // 3.循环输入两个点 do...while system.out.println( "请输入两个点的坐标" ); p1.x=sc.nextint(); p1.y=sc.nextint(); p2.x=sc.nextint(); p2.y=sc.nextint(); // 4.判断这两个数是否可以消除 if ( iserazeok( chessboard,p1,p2)){ //如果可以消除,将这两个点在chessboard 中的值都设为0 chessboard[p1.x][p1.y]= 0 ; chessboard[p2.x][p2.y]= 0 ; if ( checkgameover( chessboard )){ isgameover= true ; } } //显示消除后的棋盘 showboard2( chessboard ); } while ( !isgameover ); system.out.println( "游戏结束!" ); }
//判断是否能消除的业务 public static boolean iserazeok( int [][] chessboard ,point p1,point p2){ // 1.两个点不是同一个 if ( p1.equals( p2) ){ system.out.println( "输入的两个点位置不能相同!" ); } // 2。两个点的值是否相等 if (chessboard[p1.x][p1.y] !=chessboard[p2.x][p2.y]){ system.out.println( "输入的两个点值不相同!请重新输入" ); return false ; } // 3.判断两个点的连线情况 if ( dooneline(chessboard,p1,p2) || dotwoline(chessboard,p1,p2) || dothreeline(chessboard,p1,p2)){ return true ; } return false ; }
// 1连线 public static boolean dooneline( int [][] chessboard,point p1,point p2){ //定义最大值和最小值 int max= 0 ; int min= 0 ; //判断是循环行还是循环列 if ( p1.x==p2.x){ //找y的最大值、找y的最小值、循环从 min+1 至 max-1、判断是否为0、如果中间有一个不为0,则返回 false max=p1.y>p2.y?p1.y:p2.y; min=p1.y<p2.y?p1.y:p2.y; for ( int i=min+ 1 ;i<max;i++){ if (chessboard[p1.x][i]!= 0 ){ return false ; } } return true ; } else if ( p1.y==p2.y){ //找x的最大值、找x的最小值、循环从 min+1 至 max-1、判断是否为0、如果中间有一个不为0,则返回 false max=p1.x>p2.x?p1.x:p2.x; min=p1.x<p2.x?p1.x:p2.x; for ( int i=min+ 1 ;i<max;i++){ if (chessboard[i][p1.y]!= 0 ){ return false ; } } return true ; } return false ; }
// 2连线 public static boolean dotwoline( int [][] chessboard,point p1,point p2){ //定义两个临时点 point t1= new point(); t1.x=p1.x; t1.y=p2.y;
point t2= new point(); t2.x=p2.x; t2.y=p1.y;
if ( chessboard[t1.x][t1.y]== 0 && dooneline(chessboard, p1,t1 ) && dooneline(chessboard, t1,p2) ){ return true ; } if ( chessboard[t2.x][t2.y]== 0 && dooneline(chessboard, p1,t2 ) && dooneline(chessboard, t2,p2) ){ return true ; } return false ; }
// 3连线 public static boolean dothreeline( int [][] chessboard,point p1,point p2){ //先循环行 :x for ( int i= 0 ;i<chessboard.length;i++){ point t= new point(); t.x=i; t.y=p1.y; if ( chessboard[t.x][t.y]== 0 && dooneline(chessboard,t, p1) && dotwoline(chessboard, t,p2) ){ return true ; } } //再循环列 :y for ( int i= 0 ;i<chessboard.length;i++){ point t= new point(); t.x=p1.x; t.y=i; if ( chessboard[t.x][t.y]== 0 && dooneline(chessboard,t, p1) && dotwoline(chessboard, t,p2) ){ return true ; } } return false ; }
//判断游戏是否结束:循环这个数组,判断所有的位置都为0 public static boolean checkgameover( int [][] chessboard){ for ( int i= 1 ;i<chessboard.length- 1 ;i++){ for ( int j= 1 ;i<chessboard[i].length- 1 ;j++){ if ( chessboard[i][j]!= 0 ){ return false ; } } } return true ; }
public static void initboard( int [][] chessboard, int picnum){ random r= new random(); for ( int i= 1 ;i<chessboard.length- 1 ;i++){ for ( int j= 1 ;j<chessboard[i].length- 1 ;j=j+ 2 ){ int pic=r.nextint( picnum )+ 1 ; chessboard[i][j]=pic; chessboard[i][j+ 1 ]=pic; } } }
//打乱棋盘,只能对中间的值打乱,而不能打扰边框 //交换数组的两个值 ,随机生成的四个下标,每个下标表示一个数 x1,y1 x2,y2 =》 chessboard【x2】【y1】 将上面两个数交换 //概率:棋盘越大,交换越多 public static void shuffle( int [][] chessboard ){ random r= new random(); int x1= 0 ; int y1= 0 ; int x2= 0 ; int y2= 0 ; int temp= 0 ; for ( int i= 0 ;i<chessboard.length*chessboard[ 0 ].length* 10 ;i++){ //生成的四个下标,不能为0,也不能为 length-1 x1=r.nextint( chessboard.length- 2 )+ 1 ; y1=r.nextint( chessboard[ 0 ].length- 2 )+ 1 ; x2=r.nextint( chessboard.length- 2 )+ 1 ; y2=r.nextint( chessboard[ 0 ].length- 2 )+ 1 ; //完成交换 temp=chessboard[x1][y1]; chessboard[x1][y1]=chessboard[x2][y2]; chessboard[x2][y2]=temp; } } //简单的输出 public static void showboard( int [][] chessboard){ for ( int i= 0 ;i<chessboard.length;i++){ for ( int j= 0 ;j<chessboard[i].length;j++){ system.out.print(chessboard[i][j]+ "\t" ); } system.out.println(); } }
//私有方法:专门用来输出棋盘最上面和最下面要出现的列号 private static void showcolsnum( int [][] chessboard ){ for ( int i= 0 ;i<chessboard[ 0 ].length;i++){ if ( i== 0 || i==chessboard[i].length- 1 ){ system.out.print( "\t" ); } else { system.out.print( "*" +i+ "\t" ); } } system.out.println();
}
//带行列提示的输出 public static void showboard2( int [][] chessboard){ showcolsnum( chessboard ); //输出error:列号 //中间完成棋盘 for ( int i= 0 ;i<chessboard.length;i++ ){ //加入前面行号的输出 if ( i== 0 || i==chessboard[i].length- 1 ){ system.out.print( " " ); } else { system.out.print( "" +i); } for ( int j= 0 ;j<chessboard[i].length;j++){ //边框要用 * 来修饰 if ( i== 0 || j== 0 || i==chessboard.length- 1 || j==chessboard[i].length- 1 ){ if ( j==chessboard[i].length- 1 ){ system.out.print( " # " ); } else { system.out.print( " #\t" ); } } else { //不是边框,就输出数组中对应的数字 if ( chessboard[i][j]== 0 ){ system.out.print( " \t" ); } else { system.out.print( " " +chessboard[i][j]+ "\t" ); } } } //加入后面的行号 if ( i== 0 || i==chessboard.length- 1 ){ system.out.print( " " ); } else { system.out.print( "" +i); } system.out.println(); } showcolsnum( chessboard ); //输出列号
}
} |
point类没有写出来了,比较简单,里面就存了两个数据,表示数字的行和列,就不上图了。
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
原文链接:https://blog.csdn.net/emilyRR/article/details/40596647