扫雷游戏

2022-06-01  本文已影响0人  y_7539

主程序

#include "game.h"



void menu()
{
    printf("***************扫雷游戏************\n");
    printf("************** 1.start ************\n");
    printf("************** 0.end   ************\n");
    printf("***********************************\n");
}

void game()
{
    //创建两个棋盘
    char mine[ROWS][COLS] = { 0 };
    char show[ROWS][COLS] = { 0 };


    //初始化两个棋盘
    InitBoard(mine, ROWS, COLS, '0');
    InitBoard(show, ROWS, COLS, '*');

    //展示棋盘
    //DisplayBoard(mine, ROW, COL);
    DisplayBoard(show, ROW, COL);

    //布置雷
    SetMine(mine, ROW, COL);
    //DisplayBoard(mine, ROW, COL);

    //排查雷
    FindMine(mine, show, ROW, COL);
}

int main()
{
    srand((unsigned int)time(NULL));
    int input = 0;
    do
    {
        menu();
        printf("请选择:\n");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            printf("游戏开始\n");
            game();
            break;
        case 0:
            printf("游戏结束\n");
            break;
        default:
            printf("无效输入,请重新选择\n");
            break;
        }
    } while (input);

}

函数

#include "game.h"

//初始化棋盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char shape)
{
    int i = 0;
    int j = 0;
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < cols; j++)
        {
            board[i][j] = shape;
        }
    }
}

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

//布置雷
void SetMine(char board[ROWS][COLS], int row, int col)
{
    int count = set_mine;
    int x = 0;
    int y = 0;
    //随机生成十个雷,范围在0-9
    while (count)
    {
        x = rand() % row + 1;
        y = rand() % col + 1;
        //雷是‘1’,没有雷就布置
        if (board[x][y] == '0')
        {
            board[x][y] = '1';
            count--;
        }
    }
}

//统计雷的个数 假设坐标位x,y,附近的雷为 (x-1,y-1),(x-1, y), (x-1, y+1), (x, y-1), (x, y+1), (x+1, y-1), (x+1,y),(x+1,y+1)
int CountMine(char mine[ROWS][COLS], int x, int y)
{

    return mine[x - 1][y - 1] +
        mine[x - 1][y] +
        mine[x - 1][y + 1] +
        mine[x][y - 1] +
        mine[x][y + 1] +
        mine[x + 1][y - 1] +
        mine[x + 1][y] +
        mine[x + 1][y + 1] - 8 * '0';
}

//排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
    //输入坐标,坐标合法
    //  1.排到雷炸死,2.统计坐标周围雷的个数
    int x = 0;
    int y = 0;
    int all = ROW * COL - set_mine;
    int flags = 0;
    while (flags<all)
    {
        printf("请输入坐标:\n");
        scanf("%d %d", &x, &y);
        if (x >= 1 && x <= row && y >= 1 && y <= col)
        {
            if (mine[x][y] == '0')
            {
                int count = CountMine(mine, x, y);
                show[x][y] = count + '0';
                DisplayBoard(show, row, col);
                flags++;
            }
            else
            {
                printf("你被炸死了\n");
                break;
            }
        }
        else
        {
            printf("坐标非法,请重新输入\n");
        }
    }
    if (all == flags)
    {
        printf("扫雷成功\n");
    }
}

头文件

#pragma once
#define _CRT_SECURE_NO_WARNINGS 1
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#define ROW 9
#define COL 9
#define set_mine 10

#define ROWS ROW+2
#define COLS COL+2

//初始化棋盘
void InitBoard(char board[ROWS][COLS], int rows, int cols, char shape);

//展示棋盘
void DisplayBoard(char board[ROWS][COLS], int row, int col);

//布置雷
void SetMine(char board[ROWS][COLS], int row, int col);

//排查雷
void FindMine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
上一篇下一篇

猜你喜欢

热点阅读