?? 猜數游戲.c
字號:
/*猜數游戲*/
/*程序產生一個隨機數,游戲者輸入數據進行猜測。管理員可輸入密碼,
其中普通管理員只能獲得答案,超級管理員獲得答案并能修改普通管理員密碼*/
#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) ;/*循環輸入確認密碼,直到用戶輸入正確或超過三次,自動退出*/
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -