连连看第一更

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;
    }


    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
好的,以下是一个简单的C语言连连看代码: ``` #include <stdio.h> #include <stdlib.h> #include <stdbool.h> #include <time.h> #define N 6 #define M 8 int board[N+2][M+2]; int dir[4][2] = {{0,1},{0,-1},{1,0},{-1,0}}; void init_board() { for (int i = 1; i <= N; i++) { for (int j = 1; j <= M; j++) { board[i][j] = rand() % 6 + 1; } } } bool dfs(int x1, int y1, int x2, int y2) { if (board[x1][y1] != board[x2][y2]) return false; if (x1 == x2 && y1 == y2) return true; for (int i = 0; i < 4; i++) { int nx1 = x1 + dir[i][0], ny1 = y1 + dir[i][1]; int nx2 = x2 + dir[i][0], ny2 = y2 + dir[i][1]; if (nx1 == nx2 && ny1 == ny2) continue; if (board[nx1][ny1] == 0 && board[nx2][ny2] == 0) continue; if (board[nx1][ny1] == board[nx2][ny2] && dfs(nx1, ny1, nx2, ny2)) return true; } return false; } bool check(int x1, int y1, int x2, int y2) { if (board[x1][y1] != board[x2][y2]) return false; if (x1 == x2 || y1 == y2) return true; if (dfs(x1, y1, x2, y2)) return true; return false; } void remove_block(int x1, int y1, int x2, int y2) { board[x1][y1] = 0; board[x2][y2] = 0; } void drop_block() { for (int i = N; i >= 1; i--) { bool full = true; for (int j = 1; j <= M; j++) { if (board[i][j] == 0) { full = false; break; } } if (full) { for (int j = 1; j <= M; j++) { for (int k = i; k > 1; k--) { board[k][j] = board[k-1][j]; } board[1][j] = rand() % 6 + 1; } i++; } } } void print_board() { for (int i = 1; i <= N; i++) { for (int j = 1; j <= M; j++) { printf("%d ", board[i][j]); } printf("\n"); } } int main() { srand(time(NULL)); init_board(); print_board(); while (true) { int x1, y1, x2, y2; printf("请输入第一个坐标(x1,y1):"); scanf("%d%d", &x1, &y1); printf("请输入第二个坐标(x2,y2):"); scanf("%d%d", &x2, &y2); if (check(x1, y1, x2, y2)) { remove_block(x1, y1, x2, y2); drop_block(); print_board(); } else { printf("无法消除,请重新输入坐标!\n"); } } return 0; } ``` 该代码会生成一个N行M列的棋盘,棋盘上的每个格子随机填充1~6之间的数字。玩家需要输入要消除的两个方块的坐标,如果它们满足以下任意一个条件,就可以消除它们: 1. 它们的数字相同; 2. 它们在同一行或同一列上,它们之间没有其他方块阻挡; 3. 它们之间有一条不超过两个直角拐弯的路径可以连接起来,路径上没有其他方块阻挡。 如果成功消除了这两个方块,它们就会消失,上面的方块会往下掉落填补空缺。每次成功消除一组方块后,程序会重新生成一个新的棋盘并打印出来,玩家需要在新的棋盘上继续消除方块,直到棋盘上没有可消除的方块为止。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值