c语言,编写一个猜数字游戏

c语言,编写一个猜数字游戏,第1张

源码如下:

/ File: guessc /

#include <stdioh>  / standard input & output support /

#include <stdlibh> / srand() rand() /

#include <timeh>   / time() /

/ 宏定义 /

#define NUMBER_LENGTH   5   / 随机数长度 /

#define NUMBER_LIMIT    10  / 随机数限制, 每一位0-9 /

#define INPUT_LENTH     128 / 输入缓冲区大小 /

char goal[NUMBER_LENGTH]    = {0};  / 保存随机数 /

char flag[NUMBER_LIMIT]     = {0};  / 保存随机数标志, 保证不重复 /

char input[INPUT_LENTH]     = {0};  / 保存输入 /

/ 初始化用于保存数据的数组 /

void initData()

{

int i = 0;

while (i < NUMBER_LENGTH)

goal[i++] = 0;

i = 0;

while (i < NUMBER_LIMIT)

{

flag[i++] = 0;

}

}

/ 初始化用于保存缓冲区的数组 /

void initBuffer()

{

int i = 0;

while (i < INPUT_LENTH)

input[i++] = 0;

}

/ 显示猜测结果 /

void display()

{

int count = 0;

int i = 0;

while (i < NUMBER_LENGTH)

{

if (input[i] == goal[i])

{

printf("%c", 'o');

count++;

}

else

{

printf("%c", 'x');

}

i++;

}

printf("\nRIGHT: %d bit(s)\n", count);

if (count == NUMBER_LENGTH)

{

printf("You win! The number is %s\n", goal);

exit(0);

}

}

/ 生成随机数 /

void general()

{

/ 以时间作为时间种子保证生成的随机数真正具有随机性质 /

srand((unsigned int)time(NULL));

int i = 0;

while (i < NUMBER_LENGTH)

{

char tmp;

do

{

tmp = '0' + ((i != 0)  (rand() % 10) : (1 + rand() % 9));

} while (flag[tmp] != 0);

flag[tmp] = 1;

goal[i++] = tmp;

}

}

/ 输入方法,用于猜测 /

void guess()

{

printf("Please input the number you guessed:\n");

scanf("%s", input);

display();

initBuffer();

}

/ 主函数,程序主框架 /

int main (int argc, const char  argv[])

{

initData();

initBuffer();

general();

while (1) guess();

return 0;

}

==============================================

运行结果见附图,希望我的回答能够对你有所帮助。

#include

#include

#include

#include

int

i,j=1;

int

scores[6];

void

main()

{

char

control='\0';

int

rand1,guess,score;

printf("开始游戏吗

(yn)");

control=getchar();

while(control!='y'&&control!='y'&&control!='n'&&control!='n')//屏蔽其他按键

{

printf("无效字符!开始游戏吗

(yn)");

fflush(stdin);

control=getchar();

printf("%c",control);

}

while((control=='y')||(control=='y'))

{

system("cls");

srand((unsigned)time(null));

rand1=rand()%10+1;

//printf("%d",rand1);

for(i=0;i<20;i++)

{

printf("请输入你猜的数:");

scanf("%d",&guess);

if(guess>rand1)printf("大啦!\n");

else

if(guess

:猜对了\a\n");//响铃\a

break;

}

i++;

}

i+=1;

if(i==1)

{

score=100;

scores[0]++;

}

else

if(i>=2&&i<=3)

{

score=90;

scores[1]++;

}

else

if(i>=4&&i<=6)

{

score=80;

scores[2]++;

}

else

if(i>=7&&i<=10)

{

score=70;

scores[3]++;

}

else

if(i>=11&&i<=15)

{

score=60;

scores[4]++;

}

else

{

score=0;

scores[5]++;

}

printf("第%d次得分是:%d\n",j,score);

scores[7]+=score;

j++;

printf("是否继续(yn)\n");

fflush(stdin);//请输入缓冲区

control=getchar();

while(control!='y'&&control!='y'&&control!='n'&&control!='n')

{

printf("无效字符!只能按y或y,n或n是否继续(yn)\n");

fflush(stdin);

control=getchar();

printf("%c",control);

}

}

system("cls");//清屏

printf("+++++++++++++++-----------------以下是得分情况:---------------\n");

for(i=0;i<5;i++)

{

printf("+++++++++++++++------------------得%d分%d次:------------------\n",10(10-i),scores[i]);

}

printf("+++++++++++++++------------------得%d分%d次:------------------\n",

0,scores[5]);

}

刚写的

import javautilScanner;

public class Main{

public static void main(String[] args) {

Scanner sc = new Scanner(Systemin);

double dNum = 100  Mathrandom();

int iNum = (int) dNum, inNum, count = 0;

while(true) {

inNum = scnextInt();

if(inNum == iNum) {

Systemoutprint("猜对了\n");

break;

} else if(++count == 10) {

Systemoutprint("猜测次数已达10次,猜数失败\n");

break;

} else if(inNum < iNum) {

Systemoutprint("输入的值小,请输入更大的数\n");

} else if(inNum > iNum) {

Systemoutprint("输入的值大,请输入更小的数");

}

}

scclose();

}

}

#include <stdioh>

#include <stdlibh>

#include <timeh>

#define N 4/随机抽N个数/

#define NUM 8/NUM次猜数的机会/

void detect(char s[])

{

int i,j,num;

int a,b;

char ch[N];

for(num=0;num<NUM;num++)

{

a=b=0;

printf("第%d次机会:",num+1);

for(i=0;i<N;i++)

{

ch[i]=getch();

if(ch[i]>='0'&&ch[i]<='9')

{

for(j=0;j<i;j++)

if(ch[i]==ch[j]) break;

if(j<i) i--;

else

{

putchar(ch[i]);

for(j=0;j<N;j++)

{

if(ch[i]==s[j])

if(i==j) a++;

else b++;

}

}

}

else

i--;

}

printf(" %dA%dB\n",a,b);

if(a==N)

{

printf("恭喜你答对了!\n");

break;

}

}

if(num==NUM)

printf("很遗憾,正确答案为:%s\n",s);

}

main()

{

int i,j;

char s[N+1];

srand(time(0));

for(i=0;i<N;i++)

{

s[i]=rand()%10;

for(j=0;j<i;j++)

if(s[i]==s[j]) break;

if(j<i) i--;

else

{

s[i]+='0';

putchar('');

}

}

s[i]='\0';

printf("\n总共%d次机会\n",NUM);

detect(s);

}

_______________________

运行结果:

-----------------------

总共8次机会

第1次机会:1234 0A2B

第2次机会:2345 0A2B

第3次机会:3456 1A0B

第4次机会:4567 0A1B

第5次机会:5678 0A1B

第6次机会:6789 1A1B

第7次机会:3792 0A2B

第8次机会:9482 1A3B

很遗憾,正确答案为:8429

请按任意键继续

终端编码问题,我用了英文,但功能是完整的,图一乐:

#include <stdioh>

#include <stdlibh>

#include <timeh>

int main() {

int key, input;

srand(time(NULL));

key = rand() % 5 + 1;

printf("Guess who am I (from 1 to 5, 0 for exit)\n");

printf("Your answer: ");

while (1) {

scanf("%d%c", &input);

if (input == 0) {

break;

} else if (input == key) {

printf("Yes! Congratulations!");

break;

} else {

printf("No Try again!\nYour answer: ");

}

}

return 0;

}

#include<stdioh>

#include<stdlibh>

#include<timeh>

#include<stringh>

main()

{

int n[4],a,b,c,d,yes=0,meble=0;

int times=10,y=0;

srand(time(NULL));

int x[10];

memset(x, 0, sizeof(int)10);

int i = 0;

while (1)

{

if(i == 4)

break;

int k = rand() % 10;

if(x[k] == 0)

{

x[k] = 1;

n[i] = k;

i++;

}

}

// printf("%d %d %d %d\n",n[0],n[1],n[2],n[3]);

printf("猜数游戏:四个数0-9,按一定顺序排列,你需要在\n");

printf("10次机会内,猜中数字而且数的顺序要正确\n");

while(times!=0)

{

printf("请输入4个数(0-9)\n");

printf("数与数之间一个空格分开\n");

scanf("%d %d %d %d",&a,&b,&c,&d);

yes=0;meble=0;

if(n[0]==a)yes++;

if(n[1]==b)yes++;

if(n[2]==c)yes++;

if(n[3]==d)yes++;

if(a==n[1]||a==n[2]||a==n[3])meble++;

if(b==n[0]||b==n[2]||b==n[3])meble++;

if(c==n[0]||c==n[1]||c==n[3])meble++;

if(d==n[0]||d==n[1]||d==n[2])meble++;

printf("%d 个数完全正确, %d 个数值对但位置错了 \n",yes,meble);

y=y+1;

printf("第 %d 次机会",y);

printf("\n");printf("\n");printf("\n");

times--;

if(a==n[0]&&b==n[1]&&c==n[2]&&d==n[3])

{printf("恭喜 - -。 猪竟然都能赢\n");break;}

if(times==0)

{printf("game over 了\n");break;}

}

system("pause");

}

思路:随机产生4个不同的数(0-9),然后你输入4个数做比较,输出结果。

#include<stdioh>

#include<stdlibh> //随机数生成函数srand()与rand() 所需的头文件

#include<timeh> //time()所需的头文件

int main()

{

int sysdata;//系统生成的数据

int n; //所猜的数据

int sum = 0;//记录猜的次数

srand((unsigned)time(NULL)); //随机数发生器初始化函数,以时间为种子

sysdata = rand()%100+1; //随机生成1到100的随机数

printf("退出程序请按:ctrl + c!\n");

while(1)

{

printf("请输入你猜的数据(1-100): ");

if(scanf("%d",&n) != 1) //用于判断用户是否结束游戏

{

break; //跳出while循环,结束游戏

}

sum++; //每输入一次数据,猜的次数加1

if(sum == 10) // 当猜的次数大于10次的时候重新生成新的随机数

{

if(n == sysdata) //最后一次猜数正确,输出结果,结束游戏

{

printf("猜数正确,总共猜了%d次!\n",sum);

break; //跳出while循环,结束游戏

}

else //最后一次猜数不正确则重新开始游戏

{

printf("猜数次数超过%d次,重新开始游戏!\n",sum);

sysdata = rand()%100+1; //随机生成1到100的随机数

sum = 0;

}

}

else

{

if(n == sysdata)

{

printf("猜数正确,总共猜了%d次!\n",sum);//猜数正确,结束游戏

break; //跳出while循环,结束游戏

}

else if(n > sysdata)

{

printf("你猜的数据太大!\n");

}

else

{

printf("你猜的数据太小!\n");

}

}

}

printf("猜数游戏结束!\n");

return 0;

}

测试结果:

退出程序请按:ctrl + c!

请输入你猜的数据(1-100): 50

你猜的数据太小!

请输入你猜的数据(1-100): 75

你猜的数据太小!

请输入你猜的数据(1-100): 85

你猜的数据太大!

请输入你猜的数据(1-100): 80

你猜的数据太小!

请输入你猜的数据(1-100): 82

你猜的数据太小!

请输入你猜的数据(1-100): 84

猜数正确,总共猜了6次!

猜数游戏结束!

Press any key to continue

以上就是关于c语言,编写一个猜数字游戏全部的内容,包括:c语言,编写一个猜数字游戏、猜数字游戏 C语言简单程序代码、用JAVA编写一个简单的猜数游戏程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zz/10207138.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-05-06
下一篇 2023-05-06

发表评论

登录后才能评论

评论列表(0条)

保存