码迷,mamicode.com
首页 > 其他好文 > 详细

排雷小游戏

时间:2020-11-01 10:57:12      阅读:9      评论:0      收藏:0      [点我收藏+]

标签:判断   初始   美化   错误   图片   amp   布局   ||   展示   

这次写了一个排雷小游戏,源代码及流程如下。
1,分别运用头文件(game.h)以及源代码(main.c)(game.c)的形式让代码更加清晰;
2,难点:
初始界面的布局以及微调让其更加美化。
扫描一个格子周围8个格子雷的个数以及棋盘周围判断时的溢出问题。
3,优化
为了界面更加清新简单使用系统cls清屏小技巧。

game.h

#include  "game.h"

void menu()
{
    printf("###############################\n");
    printf("### 1 PLAY ######### 2 EXIT ###\n");
    printf("###############################\n");
    printf("请输入你的选项:");
}
void show()
{
    for (int i = 1; i <= 39; i++) {
        printf("-");
    }
}
void show_board(char board[][12], int row, int col)
{
    printf("    ");
    for (int i = 1; i <= 10; i++) {
        printf(" %d  ",i);
    }
    printf("\n");
    for (int i = 1; i < row - 1; i++)
    {
        printf("   ");
        show();
        printf("\n");
        printf("%2d ", i);
        for (int j = 1; j < col - 1; j++)
        {
            printf("| %c ", board[i][j]);
        }
        printf("\n");
    }
}

void set_mine(char mine_board[][12], int row, int col)
{
    int i = 0;
    while (i < 99)
    {
        int x = rand() % 10 + 1;
        int y = rand() % 10 + 1;
        if (mine_board[x][y] == ‘0‘)
        {
            mine_board[x][y] = ‘1‘;
            i++;
        }
    }
}

int get_mines(char mine_board[][12], int row, int col, int x, int y)
{
    return mine_board[x - 1][y - 1] + mine_board[x - 1][y] + mine_board[x - 1][y + 1] +         mine_board[x][y - 1] + mine_board[x][y + 1] +         mine_board[x + 1][y - 1] + mine_board[x + 1][y] + mine_board[x + 1][y + 1] - 8 * ‘0‘;
}
void move(char my_board[][12], char mine_board[][12], int row, int col)
{
    int count = 80;
    int x = 0;
    int y = 0;
    do {
        show_board(my_board, ROW, COL);
        printf("请输入坐标:");
        scanf_s("%d%d", &x, &y);
        if (x < 1 || x > 10 || y < 1 || y > 10)
        {
            printf("输入坐标错误");
            continue;
        }
        if (my_board[x][y] != ‘*‘)
        {
            printf("输入坐标重复");
            continue;
        }
        system("cls");
        if (mine_board[x][y] == ‘1‘)
        {
            break;
        }

        int num = get_mines(mine_board, ROW, COL, x, y);
        my_board[x][y] = num + ‘0‘;
        count--;
    } while (count > 0);
    if (count > 0)
    {
        printf("你被炸死了!\n");
    }
    else
    {
        printf("你赢了!\n");
    }
    printf("下面是雷区的排布!\n");
    show_board(mine_board, ROW, COL);
}

void GAME()
{
    srand((unsigned long)time(NULL));

    char my_board[ROW][COL];
    char mine_board[ROW][COL];

    memset(my_board, ‘*‘, sizeof(my_board));
    memset(mine_board, ‘0‘, sizeof(mine_board));
    set_mine(mine_board, ROW, COL);
    move(my_board, mine_board, ROW, COL);
}

game.c

#include  "game.h"

void menu()
{
    printf("###############################\n");
    printf("### 1 PLAY ######### 2 EXIT ###\n");
    printf("###############################\n");
    printf("请输入你的选项:");
}
void show()
{
    for (int i = 1; i <= 39; i++) {
        printf("-");
    }
}
void show_board(char board[][12], int row, int col)
{
    printf("    ");
    for (int i = 1; i <= 10; i++) {
        printf(" %d  ",i);
    }
    printf("\n");
    for (int i = 1; i < row - 1; i++)
    {
        printf("   ");
        show();
        printf("\n");
        printf("%2d ", i);
        for (int j = 1; j < col - 1; j++)
        {
            printf("| %c ", board[i][j]);
        }
        printf("\n");
    }
}

void set_mine(char mine_board[][12], int row, int col)
{
    int i = 0;
    while (i < 99)
    {
        int x = rand() % 10 + 1;
        int y = rand() % 10 + 1;
        if (mine_board[x][y] == ‘0‘)
        {
            mine_board[x][y] = ‘1‘;
            i++;
        }
    }
}

int get_mines(char mine_board[][12], int row, int col, int x, int y)
{
    return mine_board[x - 1][y - 1] + mine_board[x - 1][y] + mine_board[x - 1][y + 1] +         mine_board[x][y - 1] + mine_board[x][y + 1] +         mine_board[x + 1][y - 1] + mine_board[x + 1][y] + mine_board[x + 1][y + 1] - 8 * ‘0‘;
}
void move(char my_board[][12], char mine_board[][12], int row, int col)
{
    int count = 80;
    int x = 0;
    int y = 0;
    do {
        show_board(my_board, ROW, COL);
        printf("请输入坐标:");
        scanf_s("%d%d", &x, &y);
        if (x < 1 || x > 10 || y < 1 || y > 10)
        {
            printf("输入坐标错误");
            continue;
        }
        if (my_board[x][y] != ‘*‘)
        {
            printf("输入坐标重复");
            continue;
        }
        system("cls");
        if (mine_board[x][y] == ‘1‘)
        {
            break;
        }

        int num = get_mines(mine_board, ROW, COL, x, y);
        my_board[x][y] = num + ‘0‘;
        count--;
    } while (count > 0);
    if (count > 0)
    {
        printf("你被炸死了!\n");
    }
    else
    {
        printf("你赢了!\n");
    }
    printf("下面是雷区的排布!\n");
    show_board(mine_board, ROW, COL);
}

void GAME()
{
    srand((unsigned long)time(NULL));

    char my_board[ROW][COL];
    char mine_board[ROW][COL];

    memset(my_board, ‘*‘, sizeof(my_board));
    memset(mine_board, ‘0‘, sizeof(mine_board));
    set_mine(mine_board, ROW, COL);
    move(my_board, mine_board, ROW, COL);
}

main.c

#include  "game.h"

int main()
{
    int select = 0;
    int quit = 1;
    while (quit)
    {
        menu();
        scanf_s("%d", &select);
        switch (select)
        {
        case 1:
            GAME();
            break;
        case 2:
            quit = 0;
            break;
        default:
            printf("输入有误\n");
            break;
        }
    }
    printf("拜拜\n");
    return 0;

}

1,初始话游戏进入界面如下
技术图片
2,初始化棋盘设置如下
技术图片
3,游戏开始及过程展示如下
技术图片

排雷小游戏

标签:判断   初始   美化   错误   图片   amp   布局   ||   展示   

原文地址:https://blog.51cto.com/14946388/2545740

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!