zoukankan      html  css  js  c++  java
  • 一个笨拙的猜数游戏代码参考

    直接上代码!!!

    #include <stdio.h>
    #include <stdlib.h>
    
    #define TOP 1000
    #define BOTTOM 0
    
    /*
        由Mr.Blue and Mr.Green制作于2016.7.31 21:17
        本程序采用块状分段,使程序更加简单,但可读性降低,望见谅
    */
    
    int main(int argc, char * argv[])
    {
        int toobig, toosmall, temp;
        char input;
        printf("这是一个笨拙的猜数游戏.请在心里记下一个0~1000之间的数,然后按任意键开始.\n");
        getch();
        //--------------------猜测块---------------------------------------------------------
        passing:{
            toobig = TOP;
            toosmall = BOTTOM;
            temp = (TOP - BOTTOM) / 2;
            while(1){
                printf("%d太大,太小或刚好(b,s or r)\n",temp);
                switch( (input = getchar() ) ){
                    case 'b':{
                        toobig = temp; //将toobig置为temp,因为temp太大
                        temp = temp - (temp - toosmall) / 2; //如果太大,则temp向toosmall折半
                        break;
                    }
                    case 's':{
                        toosmall = temp; //将toosmall置为temp,因为temp太小
                        temp = temp + (toobig - temp) / 2; //如果太小,则temp向toobig折半
                        break;
                    }
                    case 'r':
                        goto loop; //如果正确,跳至结果块
                    default :{ //如果输入有误,重新输入
                        printf("请输入正确选项!\n");
                        fflush(stdin); //清空缓冲区防止getchar()误读,后面做法一致
                        continue;
                        }
                }
                if(temp == toobig || temp == toosmall){ //让程序更聪明
                    printf("敢玩我?嘿嘿,被我看出来了!\n");
                    goto restart;
                }
                fflush(stdin);
            }
        }
        //---------------------结果块-----------------------------------------------
        loop:{ 
            printf("我就知道我会猜出来\n");
        }
        //-------------------------重置块----------------------------------------------
        restart:{ 
            fflush(stdin);
            printf("重新开始吗?(y or n)\n");
            input = getchar();
            if(input == 'y'){ //如果要重玩,就重玩
                fflush(stdin);
                goto passing;
            }
            else if(input == 'n') //不想就关了吧
                exit(0);
            else{ //如果用户不小心输入错误,重新询问
                printf("请输入正确的选项!\n");
                goto restart;
            }
        }
        /////////////////////////////////////////////////////////////////////
        return 0;
    }

    运行截图

      我猜89

      

      我猜873

      

      我玩它

      

  • 相关阅读:
    燕之屋燕窝被指97%为糖水和增稠剂 营养价值不如鸡蛋
    烧烤需要准备些什么东西?烧烤步骤
    烧烤材料清单都有什么?
    西雅图华人码农生存实录
    【读书】李建:《读书的“有用”与“无用”》
    孩子,我为什么要求你读书
    读书和不读书差别在哪里?
    招人不是HR第一职责,留住人才是
    select 可输入的下拉框
    toLocaleString 日期
  • 原文地址:https://www.cnblogs.com/mrblug/p/5723978.html
Copyright © 2011-2022 走看看