猜数字游戏c语言编程代码(猜数字游戏c语言编程代码,猜的次数超过

http://www.itjxue.com  2023-03-04 19:03  来源:未知  点击次数: 

c语言:猜数字游戏代码

加了得分榜,没写排序,没时间了。自己加一下就OK

#include stdio.h

#include time.h

#include stdlib.h

#include string.h //以上的是要用到的头文件

int score=0;

void SELECT(int);

void Guess(int,int);

void Select();

void letsgo();

void NB(int); //以上的是函数原型

struct PLAYER

{

char name[10];

int plscore;

};

void save()

{ printf("您的得分是%i",score);

PLAYER player;

printf("请输入姓名:");

scanf("%s",player.name);

player.plscore=score;

FILE *fp;

if((fp=fopen("record.txt","ab"))==NULL)

{

printf("不能打开文件!");

exit(0);

}

fwrite(player,sizeof(PLAYER),1,fp);

printf("记录保存完毕!");

fclose(fp);

}

void print()

{

printf("分数榜:");

FILE *fp;

if((fp=fopen("record.txt","rb"))==NULL)

{

printf("不能打开文件!");

exit(0);

}

PLAYER player;

while(fread(player,sizeof(PLAYER),1,fp)==1)

{

printf("\n姓名:%s\n",player.name);

printf("分数:%i\n",player.plscore);

}

fclose(fp);

}

void main()

{

char end;

printf(" \t\t欢迎光临!!\n\t 在这里我将来测试你的运气!\n");

printf(" 请你猜下数字,但机会就只有三次!!\n");

printf("\n 1、开始游戏^__^\n 2、我是倒霉鬼,我不玩\n 3、查看分数榜");

printf(" 请选择1或2\n");

Select();

printf("\n按任意键退出");

fflush(stdin);

end=getchar();

if(score0)

save();

printf("Thanks For Playing \2 886\n");

}

void Select()

{

int s;

static int lihai=0;

fflush(stdin);

scanf("%d",s);

switch(s)

{

case 1:

letsgo();break;

case 2:

printf("唉,你不想玩,我就不逼你咯\n");exit(0);

case 3:print();

break;

default:

lihai++;

if(lihai==3||lihai==5)

{

NB(lihai);

break;

}

printf("你输入的是什么啊!重新输入 1 或 2 \n ");

Select();

}

}

void letsgo()

{

int select;

printf("请你选择猜数字的难度\n ");

printf("1、简单-猜测 0 到 9 的数字\n ");

printf("2、一般-猜测 0 到 99 的数字\n ");

printf("3、困难-猜测 0 到 999 的数字\n ");

printf("请不要乱选!!\n");

scanf("%d",select);

switch(select)

{

case 1:SELECT(10);break;

case 2:SELECT(100);break;

case 3:SELECT(1000);break;

case 4:print();

default:

{

printf("请不要乱选!!\n");

letsgo();

}

}

}

void NB(int l)

{

char a[20];

static int jihui=0;

printf("\n老大!我服了你,%d 次都输入错误。\n",l);

printf("\n可能你在捣乱~~\n");

printf("(如果不想结束,请输入[sorry]中括号内的否则结束游戏)\n");

fflush(stdin);

gets(a);

if (strcmp(a,"sorry")==0||strcmp(a,"SORRY")==0)

{

jihui++;

if(jihui==2)

{

printf("\n死性不改。道歉也没有用了!\n");

goto end;

}

printf("好吧。给你次机会请输入 1 或 2 \n ");

Select();

}

end:

printf("");

}

void SELECT(int n)

{

int num;

srand((unsigned)time(NULL));

num=rand()%n;

if(n==10)

{

n=3;

printf("\n你有 %d 次猜测的机会\n",n);

}

if(n==100)

{

n=5;

printf("\n你有 %d 次猜测的机会\n",n);

}

if(n==1000)

{

n=7;

printf("\n你有 %d 次猜测的机会\n",n);

}

Guess(num,n);

}

void Guess(int num,int n)

{

int search,k,o;

static int i=1;

char ans;

if(n==3)

k=10;

if(n==5)

k=100;

if(n==7)

k=1000;

o=(num-k/1)%k;

if(o0)

o=0;

printf("\n请猜这个数是多少(在%d到%d):",o,(k/5+num)%k);

fflush(stdin);

scanf("%d",search);

if(numsearch)

printf("\n小了\n");

if(numsearch)

printf("\n大了\n");

if(num==search)

{

printf("\n恭喜您了!你在第 %d 次猜对了\n",i);

score+=10;

printf("是否继续y / n:\n");

fflush(stdin);

ans=getchar();

if(ans=='y'||ans=='Y')

{

i=1;

letsgo();

}

}

else

{

i++;

if(i=n)

Guess(num,n);

else

{

printf("\n你的 %d 次机会已经没有了!!,答案是 %d \n",i-1,num);

printf("是否继续y / n:\n");

fflush(stdin);

ans=getchar();

if(ans=='y'||ans=='Y')

{

i=1;

letsgo();

}

}

}

}

满意请采纳。

猜数游戏c语言

#includestdio.h

#includestdlib.h //随机数生成函数srand()与rand() 所需的头文件

#includetime.h //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语言简单程序代码

#include

#include

#include

#include

int

i,j=1;

int

scores[6];

void

main()

{

char

control='\0';

int

rand1,guess,score;

printf("开始游戏吗

?(y?n)");

control=getchar();

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

{

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

?(y?n)");

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;i20;i++)

{

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

scanf("%d",guess);

if(guessrand1)printf("大啦!\n");

else

if(guess

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

break;

}

i++;

}

i+=1;

if(i==1)

{

score=100;

scores[0]++;

}

else

if(i=2i=3)

{

score=90;

scores[1]++;

}

else

if(i=4i=6)

{

score=80;

scores[2]++;

}

else

if(i=7i=10)

{

score=70;

scores[3]++;

}

else

if(i=11i=15)

{

score=60;

scores[4]++;

}

else

{

score=0;

scores[5]++;

}

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

scores[7]+=score;

j++;

printf("是否继续(y?n)\n");

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

control=getchar();

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

{

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

fflush(stdin);

control=getchar();

printf("%c",control);

}

}

system("cls");//清屏

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

for(i=0;i5;i++)

{

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

}

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

0,scores[5]);

}

如何猜数游戏C语言程序设计?

编程如下:

#include

#include

#include

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

{

srand((unsigned)time(NULL));

int a = rand()%100;

a+=1;

int b,c;

c = 0;

while (c {

printf("Please enter a number: ");

if (scanf("%d", b) == 0)

{

fprintf(stderr, "Invalid entry.\n");

return EXIT_FAILURE;

}

else

{

if (b == a)

{

printf("You are correct, the number is %d!\n", a);

printf("You used %d times to get the answer.\n", c);

break;

}

else if (b a)

printf("The number is bigger than it suppose to.\n");

else

printf("The number is smaller than it suppose to.\n");

c++;

}

if (c == 10)

{

printf("You used 10 times, please try again!\n");

break;

}

}

return EXIT_SUCCESS;

}

编程的注意事项:

1、程序不仅需要给计算机读 , 也要给程序员读。 程序设计风格的原则 , 代码应该清楚的和简单的 , 具有直截了当的逻辑 , 自然的表达式 , 通行的语言使用方式 , 有意义的名字和帮助作用和注释。

2、自定义类型名以大写字母开头,各单词之间以大写字母分隔,如 CallType (即骆驼式命 名法) 。 变量名以小写字母开头, 各单词之间以大写字母 分隔 (变量活动范围前缀以下划线 分隔) ,如 m_pReleaseIn。函数名以大写字母开头,各单词之间以大写字母分隔(进程、进 程页及子函数前缀以下划 线分隔) ,如 Sub_ErrorDealing。

3、命名宏定义时,表示最大个数时定义为 XXX_MAX_NUM(如最大子节点个数可用 SNODE_MAX_NUM表示) ,表示最大取值时定义为 XXX_MAX(如 PT 板 E1的最大取值 可用 PT_E1_MAX表示) 。定义最小个数时定义为 XXX_MIN_NUM,定义最小取值时定义 为 XXX_MIN。 (以防止下标使用时难以分辨是否需要减 1) 。

c语言猜数字游戏源代码

小游戏2048:

#includestdio.h

#includestdlib.h

#includeconio.h

#includetime.h

#includewindows.h

int ?jsk( ); ??//计算空格数

void rsgm( ); ?//重置游戏

void inkey( );? //按键输入

void left( ); ??//向左移动

void right( ); ?//向右移动

void up( );? //向上移动

void down( );? //向下移动

void show( ); ??//输出界面

void adnum( ); //添加随机数

void yes( );? ?//游戏是否结束(1是0否)

void gtxy(int x, int y); //控制光标位置的函数

int a[4][4];? ?//存储16个格子中的数字

int score = 0;? //每局得分

int best = 0;? //最高得分

int ifnum;? //是否需要添加数字(1是0否)

int over; ??//游戏结束标志(1是0否)

int i,j,k;

int main( )

{ rsgm( ); ?//重置游戏

? inkey( ); ?//按键输入

? return 0;

}

void Color(int a) ? //设定字符颜色的函数(a应为1-15)

{? SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE),a);? }

void rsgm( ) ? //重置游戏

{ score = 0; ifnum = 1; over = 0; srand((unsigned)time(0));? //启动随机数发生器

int n = rand( ) % 16; ? //随机函数产生0-15的数字

for (i = 0; i 4; i++)

{for (j = 0; j 4; j++)

?? { if (n == 0) { int k = rand( ) % 3; if (k == 0 || k == 1) { a[i][j] = 2; }

? ? ? ? ? ? ? ? ? ? ? ? else { a[i][j] = 4; } n--; }

? ?? else { a[i][j] = 0; n--; }

?? }

}

adnum( );

system("cls");

CONSOLE_CURSOR_INFO gb={1,0};? //以下两行是隐藏光标的设置,gb代指光标

SetConsoleCursorInfo( GetStdHandle(STD_OUTPUT_HANDLE), gb );

Color(14); ? //设置字体淡黄色

printf("\n\n\t\t? 2048小游戏"); Color(7); ? //恢复白字黑底

printf("\n\t┌──────┬──────┬──────┬──────┐");

printf("\n\t│????? │????? │????? │????? │");

printf("\n\t├──────┼──────┼──────┼──────┤");

printf("\n\t│????? │????? │????? │????? │");

printf("\n\t├──────┼──────┼──────┼──────┤");

printf("\n\t│????? │????? │????? │????? │");

printf("\n\t├──────┼──────┼──────┼──────┤");

printf("\n\t│????? │????? │????? │????? │");

printf("\n\t└──────┴──────┴──────┴──────┘");

show( );

}

void show( ) ? //输出界面

{ for(i=0;i4;i++)

for(j=0;j4;j++)

?? { gtxy(7*j+9,2*i+4); ? //gtxy(7*j+9, 2*i+4)是光标到指定位置输出数字

? ?? if(a[i][j]==0){printf("????? "); Color(7); printf("│");}

? ?? else if(a[i][j]10){ if (a[i][j] == 2) { Color(14); }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? else if (a[i][j] == 4) { Color(13); }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? else if (a[i][j] == 8) { Color(12); }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? printf(" ??%d? ", a[i][j]); Color(7 ); printf("│");

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? }

? ? else if (a[i][j] 100){if (a[i][j] == 16) { Color(12); }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? else if (a[i][j] == 32) { Color(10); }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? else if (a[i][j] == 64) { Color(2 ); }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? printf("? %d? ", a[i][j]); Color(7); printf("│");

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? }

?? else if (a[i][j] 1000) {if (a[i][j] == 128) { Color(9); }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? else if (a[i][j] == 256) { Color(1); }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? else if (a[i][j] == 512) { Color(13); }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? printf("? %d ", a[i][j]); Color(7); printf("│");

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? }

? ? else if (a[i][j] 10000) {if (a[i][j] == 1024) { Color(5); }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? else { Color(15); }

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? printf(" %d ", a[i][j]); Color(7); printf("│");

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? }

}

if (jsk( ) == 0)

? {? yes( );? if (over) { gtxy(9,12); Color(10);

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? printf("\n\n? 游戏结束!是否继续? [ Y/N ]:"); }

? }

}

void inkey( ) ? //按键输入

{ int key;

while (1)

? ? { key = getch( );

? ? ? if (over) { if (key == 89|| key == 121) { rsgm( ); continue; }

? ? ? ? ? ? ? ? ? ? ? else ?if (key == 78|| key == 110) { return; }

? ? ? ? ? ? ? ? ? ? ? else ?continue; }

? ? ? ifnum = 0;

? ?? if(key==224)key=getch( );

? ? ? ? switch (key)

? ? ? ? ? ? { case 75: left( ); break;

? ? ? ? ? ? ? case 77: right( ); break;

? ? ? ? ? ? ? case 72: up( ); break;

? ? ? ? ? ? ? case 80: down( );break;

}

? if (score best) { best = score; }

? if (ifnum) { adnum( ); show( ); }

}

}

int jsk( ) ? //计算空格数

{ int n = 0;

for (i = 0; i 4; i++)

?? { for (j = 0; j 4; j++) { if ( a[i][j] == 0) {n++;}? } ? }

return n;

}

void left( ) ? //向左移动

{ for (i = 0; i 4; i++)

{for (j = 1, k = 0; j 4; j++)

? ? { if (a[i][j] 0)

? ? ?? { if ( a[i][k] == a[i][j])

? ? ? ? ? ?? { a[i][k] *= 2;? k++;

? ? ? ? ? ? ? ? score = score + 2 * a[i][j];

? ? ? ? ? ? ? ? a[i][j] = 0; ?ifnum = 1; }

? ? ? ? ? else if ( a[i][k] == 0) { a[i][k] = a[i][j]; a[i][j] = 0; ifnum = 1; }

? ? ? ? ? else { a[i][k + 1] = a[i][j]; if ((k + 1) != j) { a[i][j] = 0; ifnum = 1; }

? ? ? ? ? k++; }

? ? ?? }

? ?? }

}

}

void right( ) ? //向右移动

{for (i = 0; i 4; i++)

? {for (j = 2, k = 3; j = 0; j--)

? ?? {if (a[i][j] 0)

? ? ? ? { if (a[i][k] == a[i][j])

? ? ? ? ? ? ?? {a[i][k] *= 2; k--; score = score + 2 * a[i][j]; a[i][j] = 0; ifnum = 1; }

??? else if ( a[i][k] == 0) {a[i][k] = a[i][j]; a[i][j] = 0; ifnum = 1; }

??? else { a[i][k - 1] = a[i][j]; if ((k - 1) != j) { a[i][j] = 0; ifnum = 1; } k--; }

?? }

? ?? }

}

}

void up( ) ? //向上移动

{for (i = 0; i 4; i++)

{for (j = 1, k = 0; j 4; j++)

? ?? {if (a[j][i] 0)

? ? ? ?? {if ( a[k][i] == a[j][i]) { a[k][i] *= 2; k++;score = score + 2 * a[j][i];

? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?? a[j][i] = 0; ifnum = 1; }

? ? ? ? ? else if ( a[k][i] == 0) { a[k][i] = a[j][i]; a[j][i] = 0; ifnum = 1; }

? ? ? ? ? else { a[k + 1][i] = a[j][i]; if ((k + 1) != j) { a[j][i] = 0; ifnum = 1; }

? ? ? ? ? k++; }

?? }

? ? }

}

}

void down( ) ? //向下移动

{ for (i = 0; i 4; i++)

{for (j = 2, k = 3; j = 0; j--)

? {if (a[j][i] 0)

? ?? {if (a[k][i] == a[j][i])

? ? ? ? ? {a[k][i] *= 2; k--;score = score + 2 * a[j][i]; a[j][i] = 0; ifnum = 1; }

? ? ?? else if (a[k][i] == 0) {a[k][i] = a[j][i]; a[j][i] = 0; ifnum = 1; }

? ? ?? else {a[k - 1][i] = a[j][i];

? ? ? ? ? ? ? ? if ((k - 1) != j) {a[j][i] = 0; ifnum = 1; } k--; }

? ? ? }

?? }

}

}

void adnum( ) ? //添加随机数

{ srand(time(0)); int n = rand( ) % jsk( );

for (int i = 0; i 4; i++)

?? {for (int j = 0; j 4; j++)

? ? ? { if (a[i][j] == 0) {if (n != 0) { n--; }

? ? ? ? else {int k = rand( ) % 3;

? ? ? ? ? ? ? ? if (k == 0 || k == 1) {a[i][j] = 2; return; }

? ? ? ? ? ? ? ? else {a[i][j] = 4; return; } }

? ? ? }

?? }

}

}

void yes( ) ? //游戏是否结束

{ for (int i = 0; i 4; i++)

{for (int j = 0; j 3; j++)

? ?? {if (a[i][j] == a[i][j + 1] || a[j][i] == a[j + 1][i]) {over = 0; return; }}

}

over = 1;

}

void gtxy(int x, int y) ? //控制光标位置的函数

{ COORD zb; ? //zb代指坐标

zb.X = x;

zb.Y = y;

SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), zb);

}

(责任编辑:IT教学网)

更多

推荐HTML/Xhtml文章