简单的扫雷程序

游戏分析: 
1.先提示的应该是一个简单的menu,其中包括“play”和“exit”; 
2.在main函数中应该是一个test的函数,主要测试整个游戏; 
3.在test中应该是可以让游戏进行循环玩的,所以需要包括一个while语句,while语句的主要部分是每次提示玩游戏; 
4.关于扫雷的整个实现是用play_game实现的,在这个部分我们需要考虑怎样去实现这个功能; 
5.根据上面分析,我们只需要在play_ game中实现初始化扫雷(init_game)和扫雷(sweep)即可; 
6.在初始化扫雷(init_game)中,我们需要对界面中雷的位置进行处理,需要一个set _mine; 

7.在扫雷中,按照整个游戏的逻辑,需要对坐标进行一个判断,有无雷,没有雷的话,周围有多少颗雷,

头文件

#ifndef __MINE_H__
#define __MINE_H__


enum op
{
    EXIT,
    PLAY
};


#define ROWS 11
#define COLS 11
#define COUNT 30


void init_game(char mine[ROWS][COLS],char show[ROWS][COLS]);   //初始化扫雷界面
void set_mine(char mine[ROWS][COLS]);    //设置雷区
void get_num(int x, int y);    //随机产生雷的位置
void display(char show[ROWS][COLS]);   //打印出界面
void sweep(char mine[ROWS][COLS], char show[ROWS][COLS]);  //开始扫雷
int get_mine(char mine[ROWS][COLS], int x, int y);   //附近雷的个数


#endif

源文件

#define _CRT_SECURE_NO_WARNINGS 1
#include"015.h"
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>


void init_game(char mine[ROWS][COLS], char show[ROWS][COLS]) //初始化扫雷界面
{
    memset(mine, '0', ROWS*COLS*sizeof(char));
    memset(show, '*', ROWS*COLS*sizeof(char));
    set_mine(mine);
}


int get_mine_num(int x, int y)   //随机产生雷的位置
{
    return rand() % (y - x + 1) + x;
}


void set_mine(char mine[ROWS][COLS])   //设置雷区
{
    int count = COUNT;
    int x = 0;
    int y = 0;
    srand((unsigned)time(NULL));
    while (count)
    {
        x = get_mine_num(1, 9);
        y = get_mine_num(1, 9);
        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("%d", i);
        for (j = 1; j < COLS - 1; j++)
        {
            printf(" %c ", show[i][j]);
        }
        printf("\n");
    }
}


void 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("请输入坐标>:");
        scanf("%d%d", &x, &y);
        if (mine[x][y] == '1')
        {
            printf("踩雷了!\n");
            return;
        }
        else
        {
            int ret = get_mine(mine, x, y);
            show[x][y] = ret + '0';
            display(show);
            count++;
        }
    }
    printf("恭喜你赢了!\n");
    display(mine);
}


int get_mine(char mine[ROWS][COLS], int x, int y)   //附近雷的个数
{
    int count = 0;
    if (mine[x - 1][y - 1] == '1') count++; //坐标位置的上3个
    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][y + 1] == '1') count++;


    if (mine[x + 1][y - 1] == '1') count++; //坐标位置的下3个
    if (mine[x + 1][y] == '1') count++;
    if (mine[x + 1][y + 1] == '1') count++;


    return count;
}


#define _CRT_SECURE_NO_WARNINGS 1
#include"015.h"
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
void menu()
{
    printf("********************\n");
    printf("****  1.play  ******\n");
    printf("****  0.exit  ******\n");
    printf("********************\n");
}


void play_game()
{
    char mine[ROWS][COLS];
    char show[ROWS][COLS];
    init_game(mine, show);
    display(show);
    sweep(mine, show);
}


void test()
{
    int input = 0;
    do
    {
        menu();
        printf("请输入指令>:");
        scanf("%d", &input);
        switch (input)
        {
        case PLAY:
            play_game();
            break;
        case EXIT:
            break;
        }
    } while(input);
}


int main()
{
    test();
    system("pause");

}


猜你喜欢

转载自blog.csdn.net/luligod/article/details/80168446