import java.util.Scanner;
import java.util.Random;
public class Test3{
public static void main(String []args){
//输入行和列
int rows=0;//行
int cols=0;//列
Scanner sc =new Scanner(System.in);
do{ System.out.println(“请输入行:”);
rows=sc.nextInt();
sc.nextLine();
}while(rows<2);
do{ System.out.println(“请输入列:”);
cols=sc.nextInt();
sc.nextLine();
}while(cols<2||cols%2!=0);
int level;
do{ System.out.println(“请输入游戏等级(5–20):”);
level=sc.nextInt();
sc.nextLine();
}while(level<5||level>20);
//创建二维数组
int [][] board=new int[rows+2][cols+2];
System.out.println(“显示原始棋盘:”);
showBoard( board);
//随机生成数据
genBoard( board,level);
System.out.println(“显示随机生成后的棋盘:”);
showBoard( board);
//打乱
shuffle( board);
System.out.println("显示打乱后的棋盘:");
showBoard2( board);
//玩起来
for(int i=0;i
System.out.println("请输入想要消除的第1个数字的行与列:");
int r1=sc.nextInt();
int c1=sc.nextInt();
sc.nextLine();
System.out.println("请输入想要消除的第2个数字的行与列:");
int r2=sc.nextInt();
int c2=sc.nextInt();
sc.nextLine();
playGeam(board,r1, c1, r2,c2);
System.out.println("你玩过之后的棋盘:");
showBoard2( board);
WaitInput();
}
}
//玩游戏,
public static void playGeam(int [][]board,int r1,int c1,int r2,int c2){
if((r1==r2 && board[r1][c1]==board[r2][c2])||(c1==c2 && board[r1][c1]==board[r2][c2])){
board[r1][c1]=0;
board[r2][c2]=0;
}
}
//等待
public static void WaitInput(){
Scanner sc=new Scanner(System.in);
System.out.println("请按任意键继续吧。。。。。");
sc.nextLine();
}
//显示棋盘
public static void showBoard(int[][] board){
for(int i=0;i
for(int j=0;j
System.out.print(board[i][j]+"\t");
}
System.out.println();
}
}
public static void showBoard2(int[][] board){
showCols( board);
for(int i=0;i
if(i>0&&i
System.out.print(i);
}
for(int j=0;j
if((i==0&&j==board[i].length-1)||(i==board.length-1&&j==board[i].length-1)){
System.out.print("*");
}
else if(j==board.length-1){
System.out.print("*"+i);
}
else if(i==0||i==board.length-1||j==0||j==board[i].length-1){
System.out.print("********");
}
else if(board[i][j]==0){
System.out.print("\t");
}else{
System.out.print(board[i][j]+"\t");
}
}
System.out.println();
}
showCols( board);
}
//输出列号
public static void showCols( int [][] board){
for(int i=0;i
if(i==0){
System.out.print("\t");
}else{
System.out.print(i+"\t");
}
}
System.out.println();
}
//随机生成棋盘的数据 注意 第一行,列 不生成 ,最后一行 列不生成
//一次生成两个相同的数据 ,相邻的两列
public static void genBoard( int[][] board,int level){
if(board==null){
return;
}
Random r=new Random();
for(int i=1;i
for(int j=1;j
board[i][j]=r.nextInt(level)+1;
board[i][j+1]=board[i][j];
}
}
}
//打乱这个棋盘 每次随机生成两个棋子的下标,再将这两个棋子交换 循环交换(注意循环的终止。。。。。)
public static void shuffle( int[][] board){
//循环的次数与行 列有关
int x1;
int y1;
int x2;
int y2;
int temp;
Random r=new Random();
for(int i=0;i
x1= r.nextInt( board.length-2 )+1;//保证第一行不会被交换
y1= r.nextInt( board[0].length-2 )+1;
x2= r.nextInt( board.length-2 )+1;
y2= r.nextInt( board[0].length-2 )+1;
temp=board[x1][y1];
board[x1][y1]=board[x2][y2];
board[x2][y2]=temp;
}
}
}