扫雷

//lei.h
#ifndef   __LEI_H__  
#define   __LEI_H__  

#define  ROW  10  
#define  COL  10  

//设置雷  
void  set_mine(char mine[ROW + 2][COL + 2], int row, int col);

//显示界面  
void  display(char mine[ROW + 2][COL + 2], int row, int col);

//统计雷的个数  
int get_mine_count(char board[ROW + 2][COL + 2], int x, int y);

//玩游戏  
void play_game(char mine[ROW + 2][COL + 2], char board[ROW + 2][COL + 2]);


#endif  __LEI_H__


//mian.c
#define _CRT_SECURE_NO_WARNINGS 1  
#include <stdio.h>  
#include <stdlib.h>  
#include <time.h>  
#include "lei.h"  

void menu()
{
    printf("*************************\n");
    printf("********* 1.play ********\n");
    printf("********* 0.exit ********\n");
    printf("*************************\n");
}

void game()
{
    //定义雷数组,显示数组  
    char mine[ROW + 2][COL + 2];
    char board[ROW + 2][COL + 2];
    //初始化数组  
    int i;
    int j;
    for (i = 0; i < ROW + 2; i++)
    {
        for (j = 0; j < COL + 2; j++)
        {
            mine[i][j] = '0';
            board[i][j] = '*';
        }
    }
    //设置雷  
    set_mine(mine, ROW + 2, COL + 2);
    //显示雷和界面数组  
    //display(mine, ROW + 2, COL + 2);  
    display(board, ROW + 2, COL + 2);
    //玩游戏  
    play_game(mine, board);
}
int main()
{
    int choose = 0;
    srand((unsigned int)time(NULL));
    do
    {
        menu();
        printf("请选择>");
        scanf("%d", &choose);
        switch (choose)
        {
        case 1:
            game();
            break;
        case 0:
            printf("退出程序\n");
            break;
        default:
            printf("输入错误,请重新输入!");
        }
    } while (choose);
    system("pause");
    return 0;
}
//game.c
#define _CRT_SECURE_NO_WARNINGS 1  
#include <stdio.h>  
#include <stdlib.h>  

#include  "lei.h"  
#define MINE_COUNT  10  


//设置雷  
void  set_mine(char mine[ROW + 2][COL + 2], int row, int col)
{
    int count = MINE_COUNT;
    int x;
    int y;
    while (count)
    {
        x = rand() % 10 + 1; //产生1~10的数  
        y = rand() % 10 + 1;
        if (mine[x][y] == '0')
        {
            mine[x][y] = '1';
            count--;
        }
    }
}

//显示界面  
void  display(char board[ROW + 2][COL + 2], int row, int col)
{
    int i;
    int j;
    printf("   ");
    for (i = 1; i < row - 1; i++)
        printf(" %d ", i);
    printf("\n----------------------------------\n");
    for (i = 1; i < row - 1; i++)
    {
        printf("%2d|", i);
        for (j = 1; j < col - 1; j++)
        {
            printf(" %c ", board[i][j]);
        }
        printf("\n");

    }
}

//统计雷的个数  
int get_mine_count(char mine[ROW + 2][COL + 2], int x, int y)
{
    return (mine[x - 1][y - 1] - '0' + mine[x - 1][y] - '0' + mine[x - 1][y + 1] - '0'
        + mine[x][y - 1] - '0' + mine[x][y + 1] - '0' + mine[x + 1][y - 1] - '0' +
        mine[x + 1][y] - '0' + mine[x + 1][y + 1] - '0');
}

//玩游戏  
void play_game(char mine[ROW + 2][COL + 2], char board[ROW + 2][COL + 2])
{
    while (1)
    {
        //输入坐标  
        printf("输入坐标(x,y):");
        int x;
        int y;
        int count = 0;
        scanf("%d%d", &x, &y);
        //坐标合法性  
        if ((1 <= x && x <= ROW) && (1 <= y && y <= COL))
        {
            //判断该位置又没有雷  
            if (mine[x][y] == '1')
            {
                printf("你被炸死了\n");
                display(mine, ROW + 2, COL + 2);
                break;
            }
            else
            {
                int ret = get_mine_count(mine, x, y);
                board[x][y] = ret + '0'; //1+'0'='1'  
                                         //坐标周围没雷,可以实现展开  
                if (ret == 0)
                {
                    board[x][y] = board[x - 1][y - 1] = board[x - 1][y] =
                        board[x - 1][y + 1] = board[x][y - 1] = board[x][y + 1] =
                        board[x + 1][y - 1] = board[x + 1][y] = board[x + 1][y + 1] = ' ';
                }
                count++;
                display(board, ROW + 2, COL + 2);
                if (count == MINE_COUNT)
                {
                    printf("恭喜你,赢了\n");
                    display(mine, ROW + 2, COL + 2);
                    break;
                }
            }
        }
        else
            printf("越界,输入错误\n");
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值