C语言程序Power of Logic

Power of Logic 0.9.0Beta1

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

void print(){
    printf("Power of Logic for Macintosh,Powered by Mimi 1984,Copyright(C)Mimi 1984\n");
    printf("Version 0.9.0Beta2 CHEAT 0000 DEBUG 1111 INFO 2222\n");
    printf("Thanks for using the Beta version!\n\n");
}
int game(){
    int i,j,n,ref[8],a[4],b[4],times=0;
    srand((unsigned int)time(0));
    for(i=0;i<8;i++)
        ref[i]=i;
    i=0;
    while(1){
        n=rand()%8;
        if(ref[n]!=-1){
            a[i]=n;
            ref[n]=-1;
            if((++i)==4)break;
        }
    }//随机数生成,存入a[4]作为答案
input:printf("Input your answer\n");//返回点input
    int x=0,y=0,error=0;
    rewind(stdin);//清除键盘缓冲区
    for(i=0;i<4;i++){
        int check=scanf("%d",&b[i]);//输入数据b[4]
        if(check!=1)error+=1;
    }
    if(error!=0){
        printf("ERROR:Invalid input.\n");
        goto input;
    }//判断b[4]是否合法(整数)
    if(b[0]==0&&b[1]==0&&b[2]==0&&b[3]==0){
        printf("The correct answer is:");
        for(i=0;i<4;i++){
            printf(" %d",a[i]);
            if(i==3)printf("\n");
        }
        goto output;
    }//输入作弊码0000,输出答案a[4],结束程序
    if(b[0]==1&&b[1]==1&&b[2]==1&&b[3]==1){
        printf("DEBUG MODE.\nThe correct answer is:");
        for(i=0;i<4;i++){
            printf(" %d",a[i]);
            if(i==3)printf("\n");
        }
        goto input;
    }//输入调试码1111,输出答案a[4],进入调试模式
    if(b[0]==2&&b[1]==2&&b[2]==2&&b[3]==2){
        printf("Sorry.No English Version Provided.\n");
        goto input;
    }//输入帮助码2222,查看帮助
    for(i=0;i<3;i++){
        for(j=i+1;j<4;j++){
            if(b[i]==b[j]){
                printf("ERROR:No duplicate numbers allowed.\n");
                goto input;
            }
        }
    }//判断b[4]是否合法(重复数)
    for(i=0;i<3;i++){
        if(b[i]<=-1||b[i]>=8){
            printf("ERROR:Only number 0~7 are allowed.\n");
            goto input;
        }
    }//判断b[4]是否合法(范围0~7)
    for(i=0;i<4;i++){
        if(a[i]==b[i])x++;
    }
    printf("%d ",x);//数字正确,位置正确的个数x
    for(i=0;i<4;i++){
        for(j=0;j<4;j++){
            if(a[i]==b[j])y++;
        }
    }
    y-=x;
    printf("%d\n",y);//数字正确,位置不正确的个数y
    if(times==10)printf("Come On!You have guessed for too many times!\n");
    if(times==15){
        printf("Oh Fuck!You have guessed for TOO many times!\nThe correct answer is:");
        for(i=0;i<4;i++){
            printf(" %d",a[i]);
            if(i==3)printf("\n");
        }
        goto output;
    }//猜测次数检查
    if(x==4){
        printf("Bingo!\n");
        times+=1;
    }
    else{
        times+=1;
        goto input;
    }//全部猜对,结束程序,否则重猜
output:printf("Have Fun!\n");//返回点output
    return times;//猜测次数作为返回值
}
int main(){
    time_t start,end;
    int cost,times,score;//花费时间,猜测次数,得分
    print();
    while(1){
        printf("Press Enter To Start The Game.\n");
        getchar();
        time(&start);//计时开始
        times=game();
        time(&end);//计时结束
        cost=difftime(end,start);
        score=100-cost/10-times*2;
        if(score<=0)score=0;//负分归零系统
        printf("Run Time:%d Seconds\n",cost);
        printf("Times You Guessed:%d\n",times);
        printf("Your Score:%d\n\n",score);
    }
    return 0;
}

发表评论

您的电子邮箱地址不会被公开。 必填项已用 * 标注

Captcha Code