超简易c语言扫雷小游戏

今天写一个简单的扫雷游戏。
首先,做出游戏选择菜单:


void menu()
{
    printf("************************\n");
    printf("************************\n");
    printf("*****1.play   0.exit****\n");
    printf("************************\n");
}
int main()
{
    int choice;
    srand((unsigned int)time(NULL));
    do
    {
        menu();
        printf("请选择:");
        scanf_s("%d", &choice);
        switch (choice)
        {
        case 1:
            game();
            break;
        case 0:
            break;
        default:
            printf(" 输入错误,请输入:1或0。\n");
            break;
        }
    } while (choice);
    return 0;
}

大概是这样的:

这里写图片描述

第二步,做出游戏界面 ,扫雷的话要两层界面,一面是雷,一面是显示界面,都是简单的二维数组,程序如下:

void init_mines(char mine[ROWS][COLS], char show[ROWS][COLS], int rows, int cols, int row, int col)
{
    int i, x, y;
    memset(mine, '0', rows*cols*sizeof(char));
    memset(show, '$', rows*cols*sizeof(char));
    for (i = 0; i < MINES; i++)
    {
        while (1)
        {
            x = rand() % row + 1;
            y = rand() % col + 1;
            if (mine[x][y] == '0')
            {
                mine[x][y] = '1';
                break;
            }
        }
    }
}

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

界面如图:

这里写图片描述

第三步,设计你选择位置坐标并判断是否踩到雷:

void game()
{
    char mines[ROWS][COLS], show_area[ROWS][COLS], ret;
    int x, y;
    init_mines(mines, show_area, ROWS, COLS, ROW, COL);
    display(mines, ROW, COL);
    display(show_area, ROW, COL);
    while (1)
    {
        printf("请输入x and y\n");
        scanf_s("%d %d", &x, &y);
        if (x >= 1 && x <= ROW && y >= 1 && y <= COL)
        {
            ret = checkwin(mines, show_area, ROW, COL, x, y);
            if (ret == 'w')
            {
                display(mines, ROW, COL);
                printf("厉害啊!\n");
                break;
            }
            else if (ret == '*')
            {
                display(mines, ROW, COL);
                printf("bye bye you are boom 。\n");
                break;
            }
            else
            {
                display(show_area, ROW, COL);
                printf("\n");
            }
        }
        else printf("输错了吧\n");
    }
}
static char checkmine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
    int m, n;
    char mine_count = '0';
    for (m = x - 1; m <= x + 1; m++)
    {
        for (n = y - 1; n <= y + 1; n++)
        {
            if (mine[m][n] == '1')
                mine_count++;
        }
    }
    return mine_count;
}

haha踩到雷了:
这里写图片描述
最后检测你是否胜利:

int checkwin(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y)
{
    int count = 0;
    if (mine[x][y] == '0')
        show[x][y] = checkmine(mine, show, x, y);
    else return '*';
    for (x = 1; x <= row; x++)
    {
        for (y = 1; y <= col; y++)
        {
            if (show[x][y] == '.')
                count++;
        }
    }
    if (count == MINES)
        return 'w';

这里写图片描述

总体上就是这样,一下是源代码:
头文件:


#ifndef  __LEI_H__
#define  __LEI_H__

#include<stdio.h>
#include<time.h>
#include<stdlib.h>
#include<string.h>

#define COLS 12
#define ROWS 12
#define COL 10
#define ROW 10
#define MINES 10

void init_mines(char mine[ROWS][COLS], char show[ROWS][COLS], int rows, int cols, int row, int col);
void display(char mine[ROWS][COLS], int row, int col);
int checkwin(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y);
#endif

主函数:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>
#include"lei.h"

void menu()
{
    printf("************************\n");
    printf("************************\n");
    printf("*****1.play   0.exit****\n");
    printf("************************\n");
}
void game()
{
    char mines[ROWS][COLS], show_area[ROWS][COLS], ret;
    int x, y;
    init_mines(mines, show_area, ROWS, COLS, ROW, COL);
    display(mines, ROW, COL);
    display(show_area, ROW, COL);
    while (1)
    {
        printf("请输入x and y\n");
        scanf_s("%d %d", &x, &y);
        if (x >= 1 && x <= ROW && y >= 1 && y <= COL)
        {
            ret = checkwin(mines, show_area, ROW, COL, x, y);
            if (ret == 'w')
            {
                display(mines, ROW, COL);
                printf("厉害啊!\n");
                break;
            }
            else if (ret == '*')
            {
                display(mines, ROW, COL);
                printf("bye bye you are boom 。\n");
                break;
            }
            else
            {
                display(show_area, ROW, COL);
                printf("\n");
            }
        }
        else printf("输错了吧\n");
    }
}
int main()
{
    int choice;
    srand((unsigned int)time(NULL));
    do
    {
        menu();
        printf("请选择:");
        scanf_s("%d", &choice);
        switch (choice)
        {
        case 1:
            game();
            break;
        case 0:
            break;
        default:
            printf(" 输入错误,请输入:1或0。\n");
            break;
        }
    } while (choice);
    return 0;
}

雷界面:


#define ROW 10
#define COL 10
///设置雷
#include"lei.h"
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<string.h>

void init_mines(char mine[ROWS][COLS], char show[ROWS][COLS], int rows, int cols, int row, int col)
{
    int i, x, y;
    memset(mine, '0', rows*cols*sizeof(char));
    memset(show, '$', rows*cols*sizeof(char));
    for (i = 0; i < MINES; i++)
    {
        while (1)
        {
            x = rand() % row + 1;
            y = rand() % col + 1;
            if (mine[x][y] == '0')
            {
                mine[x][y] = '1';
                break;
            }
        }
    }
}

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

static char checkmine(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y)
{
    int m, n;
    char mine_count = '0';
    for (m = x - 1; m <= x + 1; m++)
    {
        for (n = y - 1; n <= y + 1; n++)
        {
            if (mine[m][n] == '1')
                mine_count++;
        }
    }
    return mine_count;
}

int checkwin(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col, int x, int y)
{
    int count = 0;
    if (mine[x][y] == '0')
        show[x][y] = checkmine(mine, show, x, y);
    else return '*';
    for (x = 1; x <= row; x++)
    {
        for (y = 1; y <= col; y++)
        {
            if (show[x][y] == '.')
                count++;
        }
    }
    if (count == MINES)
        return 'w';
    return 0;
}

这样就完成了扫雷小游戏,如果有好的建议或想法请加qq294636185,谢谢!

猜你喜欢

转载自blog.csdn.net/xy294636185/article/details/79860406