/*猜数游戏*/ /*程序产生一个随机数,游戏者输入数据进行猜测。治理员可输入密码, 其中普通治理员只能获得答案,超级治理员获得答案并能修改普通治理员密码*/ #include<stdio.h> #include<stdlib.h> #include<time.h> #define NO 0 #define YES 1 #define QUIT -1 #define EASY 1 #define COMMON 2 #define HARD 3 #define PASSWORD2 -111111 /*超级治理员密码,不可修改*/ char *PasswordName= "LiangJianbin";/*超级治理员确认密码,不可修改*/ int Password1 =111111 ; /*普通治理员密码,可修改*/ int number; /*计算机产生的随机数*/ int guess_value=0; /*游戏者输入的猜测数*/ int nbr_of_guesses=0;/*累加游戏者猜测的次数*/ int done=0; /*标记游戏者是否猜对,若猜对则 done=1*/ void the_back_door(int password,int answer); /*开个后门好作弊,判定普通或超级治理员*/ void super_Adm_menu(int answer); /*超级治理员选择菜单*/ void game_introduction(void);/*游戏介绍,选择游戏级别或退出*/ void quit_or_not(void); /*确认是否退出*/ void change_password(int answer);/*修改普通治理员密码*/ void input_data(void);/*输入数据或退出,治理员可输入密码*/ void congratulate(void) ; /*祝贺猜对并问是否继续*/ int judge(void);/*判定数据是否正确*/ void enter_password_name(char *true_password);/*确认超级治理员*/ int main(void) { game_introduction();/*游戏介绍,选择游戏级别或退出*/ system("pause"); return 0; } void game_introduction(void)/*游戏介绍,选择游戏级别或退出*/ { int selection; int flag=0;/*循周游戏介绍标志,选择游戏级别或退出后flag=1,循环结束*/ do { nbr_of_guesses=0; puts("This is a guess number game.You can input a number to guess."); do { puts("\n1--Get a easy degree."); puts("\n2--Get a common degree."); puts("\n3--Get a hard degree."); puts("\n-1--Quit the game."); puts("\nEnter a selection."); scanf("%d",&selection); }while(selection!=-1&&selection<1||selection>3); if(selection==EASY) { printf("\n\nGetting a Random number\n"); /*产生随机数*/ srand( (unsigned) time(NULL)); number=rand(); flag=1; } else if (selection==COMMON) { printf("\n\nGetting a Random number\n"); /*产生随机数*/ srand( (unsigned) time(NULL)); number=rand(); flag=1; } else if (selection==HARD) { printf("\n\nGetting a Random number\n"); /*产生随机数*/ number=rand(); flag=1 ; } else quit_or_not(); /*确认是否退出*/ } while(flag==0);/*循周游戏介绍,选择游戏级别或退出*/ input_data();/*输入数据或退出,治理员可输入密码*/ } void quit_or_not(void) /*确认是否退出*/ { int flag=0; /*放弃后可再来一次*/ printf("\nOh,You just give it up ,do you want to try again?"); printf("\nplease input 1 to start,or other number to end\n"); scanf("%d",&flag); if (flag==YES) { guess_value=0; game_introduction();/*游戏介绍,选择游戏级别或退出*/ } else { system("pause"); exit(0); } } void input_data(void)/*输入数据或退出,治理员可输入密码*/ { int flag=0; do { do { printf("\nPick a number between 0 and %d :",RAND_MAX); printf("\nOr input -1 to give up :"); scanf("%d",&guess_value); /*Get a number*/ nbr_of_guesses++; if(guess_value==Password1||guess_value==PASSWORD2) the_back_door(guess_value,number); /*开个后门好作弊,判定普通或超级治理员*/ } while(guess_value!=QUIT && guess_value<0 || guess_value>RAND_MAX); if(guess_value==QUIT) quit_or_not(); /*确认是否退出*/ else flag=judge(); /*判定数据是否正确*/ } while(flag==0);/*循环输入数据,直到用户输入正确或选择退出*/ congratulate() ;/*祝贺猜对并问是否继续*/ } int judge(void)/*判定数据是否正确*/ { done=NO; if(number==guess_value) done=YES; else if(number<guess_value) printf("\nYou guessed %d is high!\n",guess_value); else printf("\nYou guessed %d is low!\n",guess_value); the_back_door(guess_value,number);/*开个后门好作弊,判定普通或超级治理员*/ return done;/*返回done值,判定数据是否正确*/ } void the_back_door(int password,int answer) /*开个后门好作弊,判定普通或超级治理员*/ { if(password==Password1) { printf("\nOh You are the Administrator!the answer is %d",answer); input_data();/*输入数据或退出,治理员可输入密码*/ } if(password==PASSWORD2) super_Adm_menu(number);/*超级治理员选择菜单*/ } void super_Adm_menu ( int answer) /*超级治理员选择菜单*/ { int selection; enter_password_name( PasswordName);/*确认超级治理员*/ do { puts("\n1--Get the answer."); puts("\n2--Change the password."); puts("\n3--Quit the game."); puts("\nEnter a selection."); scanf("%d",&selection); }while(selection<1||selection>3); if(selection==1) { printf("\nthe answer is %d",answer); input_data();/*输入数据或退出,治理员可输入密码*/ } if(selection==2) change_password(number);/*修改普通治理员密码*/ else { system("pause"); exit(0); } } void change_password(int answer)/*修改普通治理员密码*/ { int new_password_1,new_password_2; int sum=0; int selection; puts("Please enter the new password:"); scanf("%d",&new_password_1); do { puts("Please make sure the new password:"); scanf("%d",&new_password_2); sum++; if(sum>3) { puts("you are tired,leave the computer and have a rest!goodbye!"); system("pause"); exit(0) ; /*退出游戏*/ } }while (new_password_1!=new_password_2 ) ;/*循环输入确认密码,直到用户输入正确或超过三次,自动退出*/ Password1 =new_password_1; puts("\n the new password is built!"); printf("It is %d\n",new_password_1); do { puts("\n1--Get the answer."); puts("\n2--Change the password."); puts("\n3--Quit the game."); puts("\nEnter a selection."); scanf("%d",&selection);
}while(selection<1||selection>3); if(selection==1) { printf("\nthe answer is %d",answer); input_data();/*输入数据或退出,治理员可输入密码*/ } if(selection==2) change_password(number);/*修改普通治理员密码*/ else { system("pause"); exit(0); } } void congratulate(void) /*祝贺猜对并问是否继续*/ { int selection; printf("\n\nCongratulations! You guessed right in %d Guesses!",nbr_of_guesses); printf("\n\nThe number was %d\n\n",number); do { puts("\nDo you want to try again?"); puts("\n1--Yes,I want to try again."); puts("\n2--No,I must leave now."); puts("\nEnter a selection:"); scanf("%d",&selection); }while(selection<1||selection>2); if(selection==YES) game_introduction();/*游戏介绍,选择游戏级别或退出*/ else { system("pause"); exit(0); } } void enter_password_name(char *true_password)/*确认超级治理员*/ { char *password; char password_name[31]; int sum=0,flag=0; puts("Oh You are the super Administrator!"); do { true_password= PasswordName; puts("Please enter the password_name:"); password= gets(password_name); while(*true_password!='\0'||*password!='\0') { if (*password==*true_password) { flag=1; password++; true_password++; } else { flag=0; break; } } sum++; if(sum>4) { puts("you lose your password_name!goodbye!"); system("pause"); exit(0) ; /*退出游戏*/ } }while (flag==0) ;/*循环输入确认密码,直到用户输入正确或超过三次,自动退出*/
视频教程列表
文章教程搜索
C语言程序设计推荐教程
C语言程序设计热门教程
|