C语言小游戏——扫雷

game.h

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


#define easy 10
#define ROW 9
#define COL 9
#define ROWS ROW+2
#define COLS COL+2
//初始化棋盘
void initboard(char board[ROWS][COLS], int rows, int cols,char set);
//打印棋盘
void displayboard(char board[ROWS][COLS], int row, int col);
//    布置雷
void setmine(char mine[ROWS][COLS], int row, int col);
//排查雷
void findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col);
//优化
void fast(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int* count);


game.c

#include"game.h"
void initboard(char board[ROWS][COLS], int rows, int cols, char set)
{
    int i = 0;
    int j = 0;
    for (i = 0; i < rows; i++)
    {
        for (j = 0; j < cols; j++)
        {
            board[i][j] = set;
        }
    }
}
void displayboard(char board[ROWS][COLS], int row, int col)
{
    int i = 0;
    int j = 0;
    //打印列号
    printf("--------扫雷--------\n");
    for (i = 0; i <= col; 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 mine[ROWS][COLS], int row, int col)
{
    //布置10个雷
    int count = easy;
    while (count)
    {
        //生产随机的下标
        int x = rand() % row + 1;
        int y = rand() % col + 1;
        //判断雷是否重复
        if (mine[x][y] == '0')
        {
            mine[x][y] = '1';
            count--;
        }
    }
}
static int get_mine_count(char mine[ROWS][COLS], int x, int y)
{
    int i = 0;
    int j = 0;
    int count = 0;
    for (i = x - 1; i <= x + 1; i++)
    {
        for (j = y - 1; j <= y + 1; j++)
        {
            if (mine[i][j] == '1')
            {
                count++;
            }
        }
    }

    return count;
}

void fast(char mine[ROWS][COLS], char show[ROWS][COLS], int x, int y, int* count)//优化
{
    int i = 0;
    int j = 0;
    if (x >= 1 && x <= ROW && y >= 1 && y <= COL && mine[x][y] == '0' && show[x][y] == '*')
    {
        if (get_mine_count(mine, x, y))
        {
            show[x][y] = get_mine_count(mine, x, y) + '0';
            (*count)--;

        }
        else
        {
            show[x][y] = ' ';
            (*count)--;
            for (i = x - 1; i <= x + 1; i++)
            {
                for (j = y - 1; j <= y + 1; j++)
                {
                    fast(mine, show, i, j, count);
                }
            }
        }
    }
}
void findmine(char mine[ROWS][COLS], char show[ROWS][COLS], int row, int col)
{
    //1.输入排查坐标
    //2.检查是否是雷
       //(1)是雷    =炸死=游戏结束
       //(2)不是雷 =统计坐标周围有几个雷=存储排查雷的信息到show,游戏继续
    int x = 0;
    int y = 0;
    int count = row*col-easy;
    while (1)
    {
    printf("请输入坐标:");
    scanf("%d %d", &x, &y);//x-(1,9) y-(1,9)
    //判断坐标合法性
    if (x >= 1 && x <= row && y >= 1 && y <= col)
    {
        if (mine[x][y] == '1')
        {
            printf("很遗憾,你被炸死了\n");
            displayboard(mine, row, col);
            break;
        }
        else
        {
            //不是雷,统计周围雷的个数
            int count = get_mine_count(mine, x, y);
            //show[x][y] = count+'0';
            //优化
            fast(mine,show,x,y,&count);
           //显示排查出的信息
            displayboard(show, row, col);
            
        }
    }
    else
    {
        printf("请重新输入\n");
    }
    if (count==0)
    {
        printf("恭喜你,排雷成功\n");
        displayboard(mine, row, col);
    }
   }

}

test.c

#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};//存放排查出的雷的信息
    //初始化棋盘
    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()
{
    int input = 0;
    srand((unsigned int)time(NULL));
    do
    {
        menu();
        printf("请选择:");
        scanf("%d", &input);
        switch (input)
        {
        case 1:
            game();//扫雷游戏
            break;
        case 0:
            printf("退出游戏\n");
            break;
        default:
            printf("选择错误,重新选择\n");
            break;
        }
    } while (input);
    
    return 0;
}

  • 12
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然,我可以给你介绍一下C语言扫雷游戏代码。下面是一个简单的C语言扫雷游戏代码示例: ```c #include <stdio.h> #include <stdlib.h> #include <time.h> #define SIZE 10 #define MINES 10 int board[SIZE][SIZE]; int revealed[SIZE][SIZE]; void initializeBoard() { int i, j; for (i = 0; i < SIZE; i++) { for (j = 0; j < SIZE; j++) { board[i][j] = 0; revealed[i][j] = 0; } } } void placeMines() { int count = 0; while (count < MINES) { int x = rand() % SIZE; int y = rand() % SIZE; if (board[x][y] != -1) { board[x][y] = -1; count++; } } } void printBoard() { int i, j; for (i = 0; i < SIZE; i++) { for (j = 0; j < SIZE; j++) { if (revealed[i][j]) { if (board[i][j] == -1) { printf("* "); } else { printf("%d ", board[i][j]); } } else { printf(". "); } } printf("\n"); } } int countAdjacentMines(int x, int y) { int count = 0; int i, j; for (i = -1; i <= 1; i++) { for (j = -1; j <= 1; j++) { if (x + i >= 0 && x + i < SIZE && y + j >= 0 && y + j < SIZE) { if (board[x + i][y + j] == -1) { count++; } } } } return count; } void revealCell(int x, int y) { if (x < 0 || x >= SIZE || y < 0 || y >= SIZE) { return; } if (revealed[x][y]) { return; } revealed[x][y] = 1; if (board[x][y] == 0) { int i, j; for (i = -1; i <= 1; i++) { for (j = -1; j <= 1; j++) { revealCell(x + i, y + j); } } } } int main() { srand(time(NULL)); initializeBoard(); placeMines(); int gameOver = 0; while (!gameOver) { printBoard(); int x, y; printf("Enter the coordinates of the cell to reveal (x y): "); scanf("%d %d", &x, &y); if (board[x][y] == -1) { printf("Game Over! You hit a mine.\n"); gameOver = 1; } else { revealCell(x, y); int revealedCount = 0; int i, j; for (i = 0; i < SIZE; i++) { for (j = 0; j < SIZE; j++) { if (revealed[i][j]) { revealedCount++; } } } if (revealedCount == SIZE * SIZE - MINES) { printf("Congratulations! You won the game.\n"); gameOver = 1; } } } return 0; } ``` 这是一个简单的命令行扫雷游戏,游戏板大小为10x10,总共有10个地雷。你可以根据需要进行修改和扩展。希望对你有帮助!

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值