编写程序—实现扫雷游戏

版权声明:版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/kevin980123 https://blog.csdn.net/kevin980123/article/details/79846524

编写程序,实现扫雷游戏


程序代码如下:


game.h


#ifndef __GAME_H__
#define __GAME_H__
#define ROWS 11
#define COLS 11
#define ROW (ROWS-2)
#define COL (COLS-2)
#define COUNT 10
void InitBoard(char mine[ROWS][COLS], char show[ROWS][COLS], int rows, int cols);
void DisplayBoard(char show[ROWS][COLS], int row, int col);
void SetMine(char mine[ROWS][COLS], int count, int row, int col, int x, int y);
void GetMineCount(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y);
int UnknownNum(char show[ROWS][COLS], int row, int col);
#endif//__GAME_H__

game.c

#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "game.h"

void InitBoard(char mine[ROWS][COLS], char show[ROWS][COLS],int rows, int cols)
{
    memset(mine, '0', rows*cols*sizeof(mine[0][0]));
    memset(show, '*', rows*cols*sizeof(show[0][0]));
}

void SetMine(char mine[ROWS][COLS], int count, int row, int col,int x,int y)
{
    int i = 0;
    while (count)
    {
        int X = rand() % row + 1 ;
        int Y = rand() % col + 1;
        if ((X != x) && (Y != y) && (mine[X][Y] == '0'))
        {
            mine[X][Y] = '1';
            count--;
        }
    }
}

void DisplayBoard(char show[ROWS][COLS], int row, int col)
{
    int i = 0;
    int j = 0;
    printf("%d  ", i);
    for (i = 1; i <= col; i++)
    {
        printf("%d ", i);
    }
    printf("\n\n");
    for (i = 1; i <= row; i++)
    {
        printf("%d  ", i);
        for (j = 1; j <= col; j++)
        {
            printf("%c ", show[i][j]);
        }
        printf("\n");
    }
}

void GetMineCount(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
    if ((mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1]
        + mine[x][y - 1] + mine[x][y] + mine[x][y + 1]
        + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1]
        - 8 * '0') == '0')
    {
        show[x][y] = ' ';
        if ((x - 1 >= 1) && (x - 1 <= ROW) && (y - 1 >= 1) && (y - 1 <= COL) && (show[x - 1][y - 1] == '*'))
            GetMineCount(mine, show, x - 1, y - 1);

        if (((x - 1 >= 1) && (x - 1 <= ROW)) && ((y >= 1) && (y <= COL)) && (show[x - 1][y] == '*'))
            GetMineCount(mine, show, x - 1, y);

        if (((x - 1 >= 1) && (x - 1 <= ROW)) && ((y + 1 >= 1) && (y + 1 <= COL)) && (show[x - 1][y + 1] == '*'))
            GetMineCount(mine, show, x - 1, y + 1);

        if (((x >= 1) && (x <= ROW)) && ((y - 1 >= 1) && (y - 1 <= COL)) && (show[x][y - 1] == '*'))
            GetMineCount(mine, show, x, y - 1);

        if (((x >= 1) && (x <= ROW)) && ((y + 1 >= 1) && (y + 1 <= COL)) && (show[x][y + 1] == '*'))
            GetMineCount(mine, show, x, y + 1);

        if (((x + 1 >= 1) && (x + 1 <= ROW)) && ((y - 1 >= 1) && (y - 1 <= COL)) && (show[x + 1][y - 1] == '*'))
            GetMineCount(mine, show, x + 1, y - 1);

        if (((x + 1 >= 1) && (x + 1 <= ROW)) && ((y >= 1) && (y <= COL)) && (show[x + 1][y] == '*'))
            GetMineCount(mine, show, x + 1, y);

        if (((x + 1 >= 1) && (x + 1 <= ROW)) && ((y + 1 >= 1) && (y + 1 <= COL)) && (show[x + 1][y + 1] == '*'))
            GetMineCount(mine, show, x + 1, y + 1);
    }
    else
    {
        show[x][y] = mine[x - 1][y - 1] + mine[x - 1][y] + mine[x - 1][y + 1]
                           + mine[x][y - 1] + mine[x][y] + mine[x][y + 1]
                           + mine[x + 1][y - 1] + mine[x + 1][y] + mine[x + 1][y + 1]
                           - 8 * '0';
    }
}

int UnknownNum(char show[ROWS][COLS], int row, int col)
{
    int i = 0;
    int j = 0;
    int count = 0;
    for (i = 1; i <= row; i++)
    {
        for (j = 1; j <= col; j++)
        {
            if (show[i][j] == '*')
            {
                count++;
            }
        }
    }
    return count;
}

test.c


#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include "game.h"
void menu()
{
    printf("********************\n");
    printf("*****  1.Play  *****\n");
    printf("*****  0.Exit  *****\n");
    printf("********************\n");
}
void game()
{
    char mine[ROWS][COLS] = {0};
    char show[ROWS][COLS] = {0};
    int x = 0;
    int y = 0;
    int safe = ROW * COL - COUNT;
    InitBoard(mine, show, ROWS, COLS);//初始化棋盘
    DisplayBoard(show, ROW, COL);//打印棋盘
    while (safe)
    {
        printf("请输入一个坐标:");
        scanf("%d%d", &x, &y);
        if (safe == ROW * COL - COUNT)
        {
            SetMine(mine, COUNT, ROW, COL, x, y);//布置地雷
        }
        if ((x >= 1) && (x <= ROW) && (y >= 1) && (y <= COL))
        {
            if (mine[x][y] == '1')
            {
                DisplayBoard(mine, ROW, COL);
                printf("很遗憾,你被炸死了,祝下次好运!\n");
                break;
            }
            else
            {
                GetMineCount(mine, show, x, y);
            }
            safe = UnknownNum(show, ROW, COL) - 10;
            DisplayBoard(show, ROW, COL);
        }
        else
        {
            printf("坐标有误\n");
        }
    }
    if (safe == 0)
    {
        printf("恭喜你获得“扫雷新兵”称号\n");
    }
}

void test()
{
    int input = 0;
    do
    {
        menu();
        printf("请选择:");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            game();
            break;
        case 0:
            printf("退出程序\n");
            break;
        default:
            printf("选择错误,请重新选择\n");
            break;
        }
    } while (input);
}

int main()
{
    srand((unsigned int)time(NULL));
    test();
    system("pause");
    return 0;
}

程序运行结果如下:


这里写图片描述

这里写图片描述

猜你喜欢

转载自blog.csdn.net/kevin980123/article/details/79846524