悬赏,C语言猜数字游戏程序编写
#include
#include
//#include
unsigned char Num[4] = {0},getNum[4] = {0x20,0x20,0x20,0x20};
void help(void)
{
char i =0,j = 0;
for(i =0;i<4;i++)
{
for(j = 0;j<4;j++)
if(Num[j] == getNum[i])
{
if(i==j)
printf("%dth is correct!
",i);
else
{
printf("%d is a correct data
",getNum[i]);
}
}
else
{
if(i == 3&&j==3)
{printf("/********************small game*****************/\ input 4 data gess the random data,input h can get help\ ");}
}
}
}
void main( void )
{
int orginal = 0,getdata = 0,i = 0,j = 0;
// { int a;a = 878*101;cout<<a<<endl}
//first = 0,second = 0,third = 0,fourth = 0;
/* Seed the random-number generator with GetTickCount so that
the numbers will be different every time we run.
*/
srand( (unsigned)time( NULL ) );
orginal = rand();
Num[3] = orginal%10000/1000;
Num[2] = orginal%1000/100;
Num[1] = orginal%1000%100/10;
Num[0] = orginal%10;
// printf(" %d",orginal);
for(i = 0;i<4;)
{
for(j = i+1;j<4;j++)
{
if(Num[i]==Num[j])
{ Num[i]++;if(Num[i]>9) Num[i] = 0;i=0;break;}
}
if(j == 4)i++;
}
get: for(i = 0;i<4;)
{
printf("input Num %d ",i);
scanf("%s",&getNum[i]);
if(getNum[i]-0x30>9||getNum[i]-0x30<0)
{
if (getNum[i] == 'H'||getNum[i] =='h')
{
help();
}
else
printf("input one num
");
//if(i>0)i--;
}
else
{
getNum[i] -=0x30;
for(j = 0;j<i;j++)
{
if(getNum[i] == getNum[j])
{printf("have a same data,please input another
"); break;}
}
if(j==i)
i++;
}
}
printf("you input num %d%d%d%d Y or N?
",getNum[0],getNum[1],getNum[2],getNum[3]);
do{
scanf("%c",&j);
printf("%c",j);
if((j == 'N') || (j == 'n'))goto get;
}while( !((j == 'Y') || (j == 'y')));
printf("over");
}
#include
#include
#include
void main()
{
int magic,guess,k;
char over,c;
printf("退出游戏请输入z
");
do
{
srand (time(NULL));
magic=rand()%100+1;
printf("请输入猜测的数字1~100
");
for(k=0;guess!=magic;k++)
{
scanf("%d",&guess);
if (guess==magic)printf("恭喜,你猜对了!
");
scanf("%c",&over);
if (over=='z')
{
printf("感谢你的参与!!
");
exit(0);
}
if(guess<magic)printf("太小了!
");
if(guess>magic)printf("太大了!
");
}
printf("你猜了%d次
",k);
printf("是否继续游戏y/n
");
scanf("%c",&c);
}while(c=='y');
if(c=='n'||c=='z')
{
printf("感谢你的参与!!
");
exit(0);
}
getch();
}
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <ctype.h>
#define MAX_N (100) //随机整数的上限
#define MIN_N (1) //随机整数的下限
int game(void) //猜数字的游戏函数,返回值是猜数字的次数
{
int rnd,inp,cnt=0;
rnd=rand()%(MAX_N-MIN_N)+MIN_N;
printf("Let's play a guessing game.\nI have a number between %d and %d\n",MIN_N,MAX_N);
do
{
printf("Your guess ? ");
scanf("%d",&inp);
++cnt;
if (inp<rnd) puts("It's lower.");
else if (inp>rnd) puts("It's higher.");
else if (inp==rnd) {
printf("You got it right in %d guesses !\n",cnt);
}
}while (inp!=rnd);
fflush(stdin);
return cnt;
}
int main(void)
{
int total_guesses=0,best=0,now=0,num_of_game=0;
srand(time(NULL));
do
{
++num_of_game; //统计玩游戏的盘数
total_guesses+=now=game(); //统计总共猜数字的次数
if (!best) best=now; //统计最佳成绩
else if (now<best) best=now;
printf("Play again (y or n)?");
}while (toupper(getchar())=='Y');
printf("Total score :\nnumber of games = %d\n",num_of_game);
printf("total guesses = %d\n",total_guesses);
printf("guesses/game = %g\n",(double)total_guesses/num_of_game);
printf("best game = %d\n",best);
return 0;
}
//---------------------------------------------------------------------------
我用C++写了个。
#include<iostream>
using namespace std;
int main()
{
int input;
int time=0;
int total_time=0;
int num=20;
int best_shot=30;
char set='y';
cout<<"Let's play a guessing game"<<endl;
cout<<"I have a number between 1 and 100"<<endl;
while(1)
{
time++;
total_time++;
cout<<"your guess:";
cin>>input;
if(input<num)
cout<<"it's higher"<<endl;
else if(input>num)
cout<<"it's lower"<<endl;
else
{
cout<<"you got it right in "<<time<<" guesses"<<endl;
cout<<"play again?(quit by n or N)";
cin>>set;
if(best_shot>time)
best_shot=time;
time=0;
if(set=='n'||set=='N')
break;
}
}
cout<<"total scores:"<<endl;
cout<<"total guesses:"<<total_time<<endl;
cout<<"best game:"<<best_shot<<endl;
return 0;
}
太简单了,给你个思路自己写吧。
while(flag)
{
if(guess > num )
{
printf("big")
}
else if (guess < num)
{
printf("lower");
else
{
printf("equal")
flag = 0;
}
}
}
用C语言编写的小游戏代码是什么?
void start();int main() { csh();start();return 0;} void csh() \/\/ 初始化 { printf("\\n\\n 猜 数 字 小 游 戏\\n\\n");printf(" 猜四个数字,如数字与顺序都正确记为A,数字正确位置不对记为B.\\n");} void start() \/\/ 开始游戏 { int m, n; \/\/ m是完全猜对的...
用C语言编写一个“猜数字游戏”的程序
printf("你现在想退出吗? 输入a结束\\n");scanf("%c",&ch);if(ch == 'a')break;continue;} if(i > 10) \/\/机会用完了 { printf("你的机会已经用完,没机会再猜了\\n");break;} } if(i < 3)printf("太棒了\\n"); \/\/ 对游戏者的评价 else if(i < 5)printf("非常好\\n...
悬赏,C语言猜数字游戏程序编写
include <stdlib.h> include include <ctype.h> define MAX_N (100) \/\/随机整数的上限 define MIN_N (1) \/\/随机整数的下限 int game(void) \/\/猜数字的游戏函数,返回值是猜数字的次数 { int rnd,inp,cnt=0;rnd=rand()%(MAX_N-MIN_N)+MIN_N;printf("Let's play a guessing...
c语言编程 编一个猜数字游戏
\/* File: guess.c *\/ include <stdio.h> \/* standard input & output support *\/ include <stdlib.h> \/* srand() rand() *\/ include \/* time() *\/ \/* 宏定义 *\/ define NUMBER_LENGTH 5 \/* 随机数长度 *\/ define NUMBER_LIMIT 10 \/* 随机数限制, 每一位0-9 ...
求C语言编写的猜数字游戏程序
int main(){ char s[88],r[88];printf(" 本游戏只能猜8次!!\\n请输入游戏四位不重复的数字\\n");scanf("%s",s);if(strlen(s)!=4)exit(1);else { int t=0;while(1){ t++;int a=0,b=0;printf("输入猜的4位数\\n");scanf("%s",r);for(int i=0;i<4;i++)for(int...
用C语言编写猜数游戏
n==g)break;else if(g<n)printf("Lower Than That\\n");else printf("Higher Than That\\n");} if(i==0)printf("Game Over!\\n");\/\/\/10次 else printf("%d Times\\n",10-i);\/\/\/猜中 return 0;}
c语言设计一个猜数游戏,电脑会自动提示大了或小了,直到猜对为止
int main(int argc, char* argv[]){ int r;int g;srand(time(0));r = rand()%1000;\/\/产生一个小于1000的随机数,让大家猜 printf("请输入你猜的数:");scanf("%d",&g);while(g != r)\/\/没猜中就循环 { if(g > r){ printf("大了,请重新输入:");} else if(g < r){ ...
c语言猜数字
if(n < Win) \/\/猜的数字小了 { i++;printf("you guess number small\\n");scanf("%d",&n);} if(n == Win) \/\/ 正确 { i++;printf("you guees is right\\n");printf("你现在想退出吗? 输入a结束\\n");scanf("%c",&ch);if(ch == 'a')break;continue;} if(i > ...
猜数字游戏C语言编程
void main(){ int magic,guess,k;char over,c;printf("退出游戏请输入z\\n");do { srand (time(NULL));magic=rand()%100+1;printf("请输入猜测的数字1~100\\n");for(k=0;guess!=magic;k++){ scanf("%d",&guess);if (guess==magic)printf("恭喜,你猜对了!\\n");scanf("%c",&...
【c语言】编写一个猜数字游戏
include void circle(int i){ int n ;printf("请输入您所猜的数字(0-100) \\n");scanf("%d",&n);if(i<n){ printf("the number is larger,input agine.\\n");circle(i);} if(i>n){ printf("the number is smaller.input agine \\n");circle(i);} if(i==n)\/\/?不是 printf...