C语言猜数小游戏的实现

刚入门小萌新,代码简单请不要介意

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

void menu();//游戏运行菜单 
void game();//游戏主体 
void choice();//选择是否进行游戏 

void menu(){//游戏菜单 
    int a;
    printf("欢迎参加猜数游戏\n");
    printf("按1开始游戏  按0退出游戏\n");
    scanf("%d",&a);
    switch(a){                                //选择是否进行游戏 
        case 1:game();break;
        case 2:printf("欢迎下次再来\n");break;
        default:break;
        }
    } 
void game(){                                         //游戏主体 
    int guess;                                        //猜的数字 
    int random = 1;                                        //随机数 
    int degreee = 1;                                 //所猜次数
    srand((int)time(NULL));                                
    random = rand()%100+1;                             //产生随机数字 
    while(1) 
    {
        printf("please enter a number\n",guess);
        scanf("%d",&guess);
        if(degreee >= 10)
        {
            printf("I'm sorry you didn't guess\n");
            choice();
        }
        else
        {
            if(guess > random)
            {
                printf("you guess a big\n");
            }
            else if(guess < random)
            {
                printf("you guess a small\n");
            }
            else
            {
                printf("you guess a right\n");
                break;
            }
            
        }
        degreee ++;
    }
    printf("你猜了%d次就猜到了\n",degreee);
    choice();  
}
void choice()//选择是否进行游戏界面 
{
    char m;
    printf("是否继续参加游戏");
    printf("继续请按(Y) 退出请按(N)\n");
    scanf("%s",&m);
    if(m == 'Y' || m == 'y')
    {
        game();
    }
    else if(m == 'N'||m == 'n')
    {
        printf("你一共猜了%d次\n",time);
    }
    else
    {
        choice();//玩家选择其他按键讲返回选择程序 
    }

int main(int argc, char *argv[]) {        //游戏主函数 
    menu();
    game();
    choice(); 
    return 0;
}

猜你喜欢

转载自blog.csdn.net/m0_61105833/article/details/120216375