如何让系统生成随机数字及案例------->扫雷游戏的制作(内置关机程序)

随机数字的生成要用到x=rand()%n但是仅仅使用这个我们会发现每一次的数字生成的都是一样的,所以肯定漏了什么,这里我们要用到srand(),关于srand()具体是什么,我就不讲了,直接说怎么生成。

头文件包含

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

建议在主函数开头设置起点

   int main()
{

                srand((unsigned int)time(NULL));

}

设置完起点,后面就可以直接使用rand了

rand()怎么用?

比如1~3之间生成一个数我们用x接

x=rand()%3+1;//rand()随机生成一个蛮大范围的数字,我们需要对它进行一系列操作实现取一定范围数字

要取m~n范围(m,n>0;m<n)

x=rand()%(n-m+1)+m会了没

下面我们来实践一下------>扫雷游戏的制作--------->建议vs

 

这里就直接给代码吧

1.建立一个头文件game.h

代码为:

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

#define EASY 10//          修改雷的个数

#define ROW 9//        修改长宽       
#define LIST 9//        修改长宽   
#define ROWS ROW+2
#define LISTS LIST+2

void initboard(char board[ROWS][LISTS], int row, int list, char set);//初始化布局

void drawboard(char board[ROWS][LISTS], int row, int list);//绘制布局

void setmine(char board[ROWS][LISTS], int row, int list);//设立雷

int detectmine(char board[ROWS][LIST], char show[ROWS][LISTS], int row, int list);//侦查雷

int is_win(char board[ROWS][LISTS], int row, int list);//判断赢

2.建立game.c源文件

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"

void initboard(char board[ROWS][LISTS], int row, int list, char set)
{
    for (int i = 0; i < row; i++)
    {
        for (int j = 0; j < list; j++)
        {
            board[i][j] = set;
        }
    }

}

void drawboard(char board[ROWS][LISTS], int row, int list)
{
    printf("--------------------\n");
    for (int n = 0; n <= row; n++)
    {

        printf(" %d |", n);
        
    }
    printf("\n---|---|---|---|---|---|---|---|---|---|\n");
    for (int i = 1; i <= row; i++)
    {
        printf(" %d |", i);
        for (int j = 1; j <= list; j++)
        {
            printf(" %c |", board[i][j]);
        }
        printf("\n---|---|---|---|---|---|---|---|---|---|\n");

    }
    printf("--------------------\n");

}


void setmine(char board[ROWS][LISTS], int row, int list)
{
    int x = 0, y = 0;
    int count = EASY;
    for (;count;)
    {
        x = rand() % row + 1;
        y = rand() % list + 1;
        if (board[x][y] == '0')
        {
            board[x][y] = '1';
            count--;
        }
    }
}

void detect(char board[ROWS][LISTS],char show[ROWS][LISTS], int x, int y)
{
    if(show[x][y]!=' '&& x >= 1 && x <= ROW && y >= 1 && y <= LIST)
    {
        char ch = '0';
        for (int i = -1; i <= 1; i++)
        {
            for (int j = -1; j <= 1; j++)
            {
                ch = board[x + i][y + j] + ch - '0';
            }
        }
        if (ch == '0')
        {
            show[x][y] = ' ';
            for (int i = -1; i <= 1; i=i+2)
            {
                detect(board, show, x+i, y);
                detect(board, show, x, y+i);

            }
        }
        else
        {
            show[x][y] = ch;
        }
    }
}

int detectmine(char board[ROWS][LISTS], char show[ROWS][LISTS], int row, int list)
{
    
    char ch='0';
    int x=0, y=0;
    
    printf("请输入要排查的位置--->");
    scanf("%d%d", &x, &y);
    printf("\n");
    //如果(x,y)是雷
    if(x>=1&&x<=row&&y>=1&&y<=list)
    {
        if (board[x][y] == '1')
        {
            printf("你被炸死了\n");
            return 1;
        }
        //不是雷
        else
        {
            for (int i = -1; i <= 1; i++)
                for (int j = -1; j <= 1; j++)
                {
                    ch = board[x + i][y + j] + ch - '0';
                }
        }
        if (ch = '0')
        {
            detect(board, show, x, y);
        }
        else
        {
            show[x][y] = ch;
        }
        return 0;
    }
    else if (x == 0 && y == 0)
    {
        printf("请输入要标记的位置");
        scanf("%d%d", &x, &y);
        printf("\n");
        show[x][y] = '&';
    }
    else 
    {
        printf("位置输入错误\n");
    }
}

int is_win(char board[ROWS][LISTS], int row, int list)
{
    int count = 0;
    for (int i = 1; i <= row; i++)
    {
        for (int j = 1; j <= list; j++)
        {
            if (board[i][j] == '*'||board[i][j]=='&')
                count++;
        }
    }
    if (count == EASY)
    {
        printf("***********WIN***********\n");
        return 1;
    }
    else return 0;
}

3.建立text.c文件--------->这个是框架

#define _CRT_SECURE_NO_WARNINGS 1
#include"game.h"
void menu()
{
    printf("*********************\n");
    printf("******* 1.play ******\n");
    printf("******* 0.exit ******\n");
    printf("*********************\n");

}


void rule()
{
    printf("\n");
    printf("*****************   按照(行,列)的方式输入位置     ************\n");
    printf("*****************   输入位置(0,0)选择作标记       *************\n");
    printf("*****************   输了就关机,你只有一次机会       *************\n");
    printf("*****************   认真一点,五分钟绰绰有余       *************\n");
    


    printf("\n");
    printf("按任意键开始游戏");
    getchar();
    system("cls");
}

void game()
{
    //设置棋盘
    char show[ROWS][LISTS] = {0};
    char board[ROWS][LISTS] = {0};
    initboard(show, ROWS, LISTS, '*');
    initboard(board, ROWS, LISTS, '0');
    
    //设置雷的位置
    setmine(board, ROW, LIST);


    //画棋盘
    drawboard(show, ROW, LIST);

    while(1)
    {
        //排查雷
        //1.(x,y)是不是雷
        //(1)是雷----->结束游戏
        //(2) 不是雷---->分析周围有几个雷,将其char数字打到(x,y)中
        int a=detectmine(board, show, ROW, LIST);
        if (a == 1)
        {
            
            printf("*****************   这都能输,那么菜????       *************\n");
            printf("*****************  当然,输了可以调出命令提示符       *************\n");
            printf("*****************   输入shutdown -a取消关机       *************\n");
            drawboard(board, ROW, LIST);
            break;
        }
        a = is_win(show, ROW, LIST);
        if (a == 1)
        {
            system("shutdown -a");
            drawboard(board, ROW, LIST);
            break;
        }
        system("cls");
        drawboard(show, ROW, LIST);
        //判断是否结束
    }

}


int main()
{
    menu();
    srand((unsigned int)time(NULL));
    int input;
again:
    printf("请选择1/0--->");
    scanf("%d", &input);
    getchar();
    system("cls");
    switch (input)
    {
    case 1:
        printf("开始游戏\n");
        system("shutdown -s -t 300");
        rule();
        game();
            break;
    case 0:
        printf("退出游戏\n");
        break;
    default:
        printf("选择错误,请重新选择\n");
        goto again;
        break;
    }


    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值