简单的扫雷!!!

/*
简单的扫雷,用了数组。//可以把棋子设为一个类,添加棋子的属性。。。
*/

import java.util.Random;

public class Mymine {

private int[][] arr=new int[10][10];        //显示棋盘的数组
private int[][] arr2=new int[10][10];       //接收周围点击后的数值的变化,根据此变化判断输出的内容
private int[][] arr3=new int[10][10];
//private int[][] arr3=new int [100][2];

private int[][] arr1=new int[10][2];        //定义存放横纵坐标的数组
int k=0;
boolean flag=true;

//设置表示状态的量
//显示棋盘
public void show(){
        System.out.print("  ");
    for(int i=0;i<10;i++)
        System.out.print(i+"-");
    System.out.println();
    for(int i=0;i<arr.length;i++){
        System.out.print(i+" ");
        //如果是雷的时候显示

        for(int j=0;j<arr[i].length;j++){


            if(flag==true){
                if(arr[i][j]==-1){
                    System.out.print("E-");
                }
                else if(arr[i][j]>0){   
                    System.out.print(arr[i][j]+"-");
                }else{
                    System.out.print("+-");
                }
            }   
            if(flag==false){
                if(arr2[i][j]==1){  
                    System.out.print(arr[i][j]+"-");//-1表示是雷
                }else{
                    System.out.print("+-");
                }
            }                                                   //显示其他不是雷的东西


        }
        System.out.println();


    }

}
//显示的重载
public void show1(){
    System.out.print("  ");
    for(int i=0;i<10;i++)
        System.out.print(i+"-");
    System.out.println();
    for(int i=0;i<arr.length;i++){
        System.out.print(i+" ");

    //如果是雷的时候显示

        for(int j=0;j<arr[i].length;j++){

            if(arr2[i][j]==-1){

                System.out.print(" -");
            }
            else if(arr2[i][j]==1){ 
                System.out.print(arr[i][j]+"-");
            }else
                System.out.print("+-");
        }

        System.out.println();
    }

}

//当点击是空格的时候判断它周围的东西,有一点缺陷,在棋盘的外围一周不能判断
public void judge1(int x,int y){

    if(x-1>=0&&x+1<=9&&y-1>=0&&y+1<=9){


        if(arr3[x-1][y]==0 ){
            if(arr[x-1][y]>0 ){
                arr2[x-1][y]=1;

                arr3[x-1][y]=1;
            }
            else if(arr[x-1][y]==0 ) {
                arr2[x-1][y]=-1;
                arr3[x-1][y]=1;

                judge1(x-1,y);
            }
            else{
                arr2[x-1][y]=0;
                arr3[x-1][y]=1;
            }
        }
        
        if(arr3[x+1][y]==0 && x<9){
            if(arr[x+1][y]>0  ){
                arr2[x+1][y]=1;
                arr3[x+1][y]=1;
            }
            else if(arr[x+1][y]==0 ) {
                arr2[x+1][y]=-1;
                arr3[x+1][y]=1;
                judge1(x+1,y);
            }
            else{
                arr2[x+1][y]=0;
                arr3[x+1][y]=1;
            }
        }
        
        if(arr3[x+1][y-1]==0){
            if(arr[x+1][y-1]>0 ){
                arr2[x+1][y-1]=1;
                arr3[x+1][y-1]=1;
            }
            else if(arr[x+1][y-1]==0 ) {
                arr2[x+1][y-1]=-1;
                arr3[x+1][y-1]=1;
                judge1(x+1,y-1);
            }
            else{
                arr2[x+1][y-1]=0;
                arr3[x+1][y-1]=1;
            }
        }
        /
        if( arr3[x+1][y+1]==0){
            if(arr[x+1][y+1]>0 ){
                arr2[x+1][y+1]=1;
                arr3[x+1][y+1]=1;
            }
            else if(arr[x+1][y+1]==0) {
                arr2[x+1][y+1]=-1;
                arr3[x+1][y+1]=1;
                judge1(x+1,y+1);
            }
            else{
                arr2[x+1][y+1]=0;
                arr3[x+1][y+1]=1;
            }
        }
        /
        if(arr3[x-1][y-1]==0){
            if(arr[x-1][y-1]>0  ){
                arr2[x-1][y-1]=1;
                arr3[x-1][y-1]=1;
            }
            else if(arr[x-1][y-1]==0 ) {
                arr2[x-1][y-1]=-1;
                arr3[x-1][y-1]=1;
                judge1(x-1,y-1);
            }
            else{
                arr2[x-1][y-1]=0;
                arr3[x-1][y-1]=1;
            }
        }
        /
        if(arr3[x][y-1]==0){
            if(arr[x][y-1]>0  ){
                arr2[x][y-1]=1;
                arr3[x][y-1]=1;
            }
            else if(arr[x][y-1]==0 ) {
                arr2[x][y-1]=-1;
                arr3[x][y-1]=1;
                judge1(x,y-1);
            }
            else{
                arr2[x][y-1]=0;
                arr3[x][y-1]=1;
            }
        }
        /
        if(arr3[x][y+1]==0 ){
            if(arr[x][y+1]>0 ){
                arr2[x][y+1]=1;
                arr3[x][y+1]=1;
            }
            else if(arr[x][y+1]==0 ) {
                arr2[x][y+1]=-1;
                arr3[x][y+1]=1;
                judge1(x,y+1);
            }
            else{
                arr2[x][y+1]=0;
                arr3[x][y+1]=1;
            }
        }
        /
        if(arr3[x-1][y+1]==0 ){
            if(arr[x-1][y+1]>0){
                arr2[x-1][y+1]=1;
                arr3[x-1][y+1]=1;
            }
            else if(arr[x-1][y+1]==0) {
                arr2[x-1][y+1]=-1;
                arr3[x-1][y+1]=1;
                judge1(x-1,y+1);
            }
            else{
                arr2[x-1][y+1]=0;
                arr3[x-1][y+1]=1;
            }
        }
        

    }
}

//生成10个随机的雷横纵坐标,返回在一个数组里面
public void num(){
    Random r=new Random();

    for(int i=0;i<arr1.length;i++){
        arr1[i][0]=r.nextInt(10);
        arr1[i][1]=r.nextInt(10);
        for(int j=0;j<i;j++){
            if(arr1[i][0]==arr1[j][0]&&arr1[i][1]==arr1[i][1]){
                i--;
                break;
            }
        }
    }

}
//把每个雷周围的几个空格的数字改变
public void findMine(){
    int x=0,y=0;
    for(int i=0;i<10;i++){
        x=arr1[i][0];
        y=arr1[i][1];


        if(x-1>=0&&y-1>=0)
            arr[x-1][y-1]++;
        if(y-1>=0)
            arr[x][y-1]++;
        if(x-1>=0)
            arr[x-1][y]++;
        if(x+1<=9&&y-1>=0)
            arr[x+1][y-1]++;
        if(x-1>=0&&y+1<=9)
            arr[x-1][y+1]++;
        if(y+1<=9)
            arr[x][y+1]++;
        if(x+1<=9&&y+1<=9)
            arr[x+1][y+1]++;
        if(x+1<=9)
            arr[x+1][y]++;

    }   
    for(int i=0;i<10;i++){
        arr[arr1[i][0]][arr1[i][1]]=-1;
    }
} 
//判断输入的值以及要显示的状态
public int judge(int x,int y){


    if(arr[x][y]==-1){
        k=1;
        show();
    }
    else if(arr[x][y]>0){
        arr2[x][y]=1;
        k=2;
        flag=false;
        show();
        flag=true;
    }else{ 
        k=3;
        judge1(x,y);

        show1();
    }

    return k;
}

}

//测试类的实现
import java.util.Scanner;

public class Test {

public static void main(String[] args) {
    Scanner input=new Scanner(System.in);
    Mymine mymine=new Mymine();
    mymine.show();  
    mymine.num();
    mymine.findMine();
    mymine.show();


    int x=0;
    int y=0;
    int k=-1;

    do{
        System.out.println("请输入横纵坐标:");
        x=input.nextInt();
        y=input.nextInt();
        if(x>=0&&x<=9&&y>=0&&y<=9){
            k=mymine.judge(x,y);
            if(k==1){
                System.out.println("你输了!!!");
                break;  
            }               
        }
        else
            System.out.println("您输入的有误,请输入横纵坐标:");  
    }while(true);

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的Python实现扫雷的示例代码: ```python import random # 游戏参数 ROWS = 10 COLUMNS = 10 MINES = 10 # 方格状态 UNREVEALED = -1 MINE = -2 # 创建游戏面板 board = [[UNREVEALED for _ in range(COLUMNS)] for _ in range(ROWS)] # 随机布雷 mines = random.sample(range(ROWS * COLUMNS), MINES) for mine in mines: row, col = divmod(mine, COLUMNS) board[row][col] = MINE # 计算周围地雷数量 for row in range(ROWS): for col in range(COLUMNS): if board[row][col] == MINE: continue count = 0 for r in range(max(0, row - 1), min(row + 2, ROWS)): for c in range(max(0, col - 1), min(col + 2, COLUMNS)): if board[r][c] == MINE: count += 1 board[row][col] = count # 显示游戏面板 def print_board(): print(" ", end="") for col in range(COLUMNS): print(f"{col:2d}", end="") print() for row in range(ROWS): print(f"{row:2d} ", end="") for col in range(COLUMNS): if board[row][col] == UNREVEALED: print(" .", end="") elif board[row][col] == MINE: print(" *", end="") else: print(f" {board[row][col]}", end="") print() # 点击方格 def click(row, col): if board[row][col] == MINE: print("你踩到了地雷,游戏结束!") return False elif board[row][col] == UNREVEALED: board[row][col] = 0 for r in range(max(0, row - 1), min(row + 2, ROWS)): for c in range(max(0, col - 1), min(col + 2, COLUMNS)): if board[r][c] == UNREVEALED: click(r, c) return True else: return True # 开始游戏 print_board() while True: row = int(input("请输入行号:")) col = int(input("请输入列号:")) if not click(row, col): break print_board() ``` 这个简易版扫雷游戏使用的是命令行界面,先随机生成地雷,然后根据地雷数量计算每个方格周围的地雷数量。玩家输入行号和列号来点击方格,如果踩到地雷,则游戏结束,否则会递归地展开周围未点击过的方格,直到点击到有地雷的方格或者所有非地雷方格都被点击为止。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值