C语言--扫雷

test.c

#include"game.h"  

int main()
{
    int input = 0;
    char mine[rows][cols];
    char show[rows][cols];
    int i = 0;
    int j = 0;
    for (i = 0; i < rows - 1; i++)
    {
        for (j = 0; j < cols - 1; j++)
        {
            mine[i][j] = '0';
            show[i][j] = '*';
        }
    }
    menu();
    while (1)
    {
        printf("请选择:");
        scanf("%d", &input);
        if (input == 1)
        {
            printf("进入游戏\n");
            Game(mine, show);
            break;
        }
        else if (input == 0)
        {
            printf("退出游戏!\n");
            exit(0);
            break;
        }
        else
        {
            printf("输入有误!\n");
        }
    }
    system("pause");
    return 0;
}

game.c

#include"game.h"  

int menu()
{
    printf("********************************************\n");
    printf("********************************************\n");
    printf("*************welcome  to saolei*************\n");
    printf("*************   1.      play   *************\n");
    printf("*************   0.      exit   *************\n");
    printf("********************************************\n");
    printf("********************************************\n");
    return 0;
}



void set_mine(char mine[rows][cols])
{
    int count = Count;
    int x = 0;
    int y = 0;
    srand((unsigned)time(NULL));
    while (count)
    {
        x = rand() % 9 + 2;
        y = rand() % 9 + 2;
        if (mine[x][y] == '0')
        {
            mine[x][y] = '1';
            count--;
        }
    }
}//埋雷


void display(char show[rows][cols])
{
    int i = 0;
    int j = 0;
    printf(" ");
    for (i = 1; i < cols - 1; i++)
    {
        printf(" %d ", i);
    }
    printf("\n");
    for (i = 1; i < rows - 1; i++)
    {
        printf("%2d", i);
        for (j = 1; j < cols - 1; j++)
        {
            printf(" %c ", show[i][j]);
        }
        printf("\n");
    }
}
//打印棋盘


int get_num(char mine[rows][cols], int x, int y)
{
    int count = 0;
    if (mine[x - 1][y - 1] == '1')
    {
        count++;
    }
    if (mine[x - 1][y] == '1') 
    {
        count++;
    }
    if (mine[x - 1][y + 1] == '1')  
    {
        count++;
    }
    if (mine[x][y + 1] == '1')  
    {
        count++;
    }
    if (mine[x + 1][y + 1] == '1') 
    {
        count++;
    }
    if (mine[x + 1][y] == '1')  
    {
        count++;
    }
    if (mine[x + 1][y -1] == '1')
    {
        count++;
    }
    if (mine[x][y - 1] == '1') 
    {
        count++;
    }
    return  count;
}//计算雷的个数

int Sweep(char mine[rows][cols], char show[rows][cols])
{
    int count = 0;
    int x = 0;
    int y = 0;
    while (count != ((rows - 2)*(cols - 2) - Count))
    {
        printf("请输入坐标:\n");
        scanf("%d%d", &x, &y);
        if (mine[x][y] == '1')
        {
            printf("踩到雷!\n");
            return 0;
        }
        else
        {
            int ret = get_num(mine, x, y);
            show[x][y] = ret + '0';//加字符0,使个数显示正确 
            display(show);
            count++;
        }
    }
    printf("你赢了!\n");
    display(mine);
    return 0;
}



int Game(char mine[rows][cols], char show[rows][cols])
{
    set_mine(mine);
    display(show);
    //display(mine); 
    Sweep(mine, show);
    return 0;
}

game.h

#define _CRT_SECURE_NO_WARNINGS 1  
#include<stdio.h>  
#include<stdlib.h>  
#include<time.h>  
#include<string.h>  
#define rows 12  
#define cols 12
#define Count 20  

int menu();
void display(char show[rows][cols]);
int Game(char mine[rows][cols], char show[rows][cols]);  
void set_mine(char mine[rows][cols]); 
int Sweep(char mine[rows][cols], char show[rows][cols]); 
int get_num(char mine[rows][cols], int x, int y);  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值