猜数字小游戏简单源代码
猜数字小游戏简单源代码,闲着无聊写的菜代码,很垃圾也是纯粹练习一下手工高亮代码而已
// header file
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
// define var-type
typedef const char* PCSTR;
typedef PCSTR const CPCSTR;
// define read-only variables
#define ERROR_INPUT 0x80000000
CPCSTR INFO_ERROR = "对不起,您输入的数字不对,请重新输入:";
CPCSTR INFO_SPLIT = "******************************************************";
CPCSTR INFO_INPUT = "请您输入一个数字:";
CPCSTR INFO_GAME = "您好,现在请您猜一个1000以内的整数。\n您有%d次机会,每次您猜错了,系统会提\n示您猜得大了或者小了。祝您玩得愉快!\n";
CPCSTR INFO_FAIL = "您好,您的次数已到!";
CPCSTR INFO_QUIT = "请问您还想继续玩吗?是请按1:";
CPCSTR INFO_TIMES= "这是您第%d次猜数,结果是";
PCSTR INFO_GUESS[3] = {
"您输入的数字太小,请继续猜。",
"您猜对了!恭喜!",
"您输入的数字过大,请继续猜。",
};
// get the sign of the integer
int sgn(int n)
{
if(n==0) return 0;
else return n>0?1:-1;
}
// input a integer
int InfoGetInt(PCSTR str)
{
int n;
puts(str);
if (scanf("%d", &n)==1) return n;
else
{
while(getchar()!='\n');
return ERROR_INPUT;
}
}
// game start function,
// int _nChance : how many chances to guess
int GameStart(int _nChance)
{
int nNum = rand() % 1000 + 1;
int n = 1, nChance = _nChance;
for ( ; n && nChance>0; --nChance) // loop while nChance>0
{
n = InfoGetInt(INFO_INPUT);
while (n<1 || n>1000) // check input
{
n = InfoGetInt(INFO_ERROR);
}
n = sgn(n-nNum);
printf(INFO_TIMES, _nChance - nChance + 1);
puts(INFO_GUESS[n+1]);
}
return nChance;
}
// main function
int main(void)
{
int nChance = 20;
// output infomation about the game
printf(INFO_GAME, nChance);
puts(INFO_SPLIT);
srand(time(NULL)); // randomize
do // loop input test
{
if (GameStart(nChance) == 0) puts(INFO_FAIL);
puts(INFO_SPLIT);
// is exit program
}while(InfoGetInt(INFO_QUIT)==1);
return 0; // must be null
}
[color=#FFFFFF]#include<stdio.h>
#include<stdlib.h>
#include<time.h>
// define var-type
typedef const char* PCSTR;
typedef PCSTR const CPCSTR;
// define read-only variables
#define ERROR_INPUT 0x80000000
CPCSTR INFO_ERROR = "对不起,您输入的数字不对,请重新输入:";
CPCSTR INFO_SPLIT = "******************************************************";
CPCSTR INFO_INPUT = "请您输入一个数字:";
CPCSTR INFO_GAME = "您好,现在请您猜一个1000以内的整数。\n您有%d次机会,每次您猜错了,系统会提\n示您猜得大了或者小了。祝您玩得愉快!\n";
CPCSTR INFO_FAIL = "您好,您的次数已到!";
CPCSTR INFO_QUIT = "请问您还想继续玩吗?是请按1:";
CPCSTR INFO_TIMES= "这是您第%d次猜数,结果是";
PCSTR INFO_GUESS[3] = {
"您输入的数字太小,请继续猜。",
"您猜对了!恭喜!",
"您输入的数字过大,请继续猜。",
};
// get the sign of the integer
int sgn(int n)
{
if(n==0) return 0;
else return n>0?1:-1;
}
// input a integer
int InfoGetInt(PCSTR str)
{
int n;
puts(str);
if (scanf("%d", &n)==1) return n;
else
{
while(getchar()!='\n');
return ERROR_INPUT;
}
}
// game start function,
// int _nChance : how many chances to guess
int GameStart(int _nChance)
{
int nNum = rand() % 1000 + 1;
int n = 1, nChance = _nChance;
for ( ; n && nChance>0; --nChance) // loop while nChance>0
{
n = InfoGetInt(INFO_INPUT);
while (n<1 || n>1000) // check input
{
n = InfoGetInt(INFO_ERROR);
}
n = sgn(n-nNum);
printf(INFO_TIMES, _nChance - nChance + 1);
puts(INFO_GUESS[n+1]);
}
return nChance;
}
// main function
int main(void)
{
int nChance = 20;
// output infomation about the game
printf(INFO_GAME, nChance);
puts(INFO_SPLIT);
srand(time(NULL)); // randomize
do // loop input test
{
if (GameStart(nChance) == 0) puts(INFO_FAIL);
puts(INFO_SPLIT);
// is exit program
}while(InfoGetInt(INFO_QUIT)==1);
return 0; // must be null
}
[[it] 本帖最后由 雨中飞燕 于 2008-2-4 03:30 编辑 [/it]]