当前位置:首页 » 编程语言 » 猜字游戏c语言

猜字游戏c语言

发布时间: 2022-09-01 18:16:51

A. 设计一个猜字游戏的c语言程序

以下程序的功能是随机产生数字,要求用户猜测程序中产生的随机数字,并输入,根据猜测的结果程序给出不同的响应,如果15次没猜对则退出。

源程序如下:
#include <stdio.h>
#include<stdlib.h>
#include<ctype.h>
main()
{
int count;/*猜数字的次数*/
int number;/*系统产生的随机数字*/
int guess;/*程序员输入数字*/
char yes='Y';
clrscr();
printf("\nNow let us play the game.\n Guess the number:");
while (toupper(yes)=='Y')
{
count=0;
randomize();
number=random(100)+1;
do
{
do
{
printf("\nInput an integer number(1~100):");
scanf("%d",&guess);
}while(!(guess>=1&&guess<=100));/*结束第二层DO~WHILE循环*/
if (guess<number)
printf("\n Your answer is low,try again!");/*如果用户输入的数字小于系统随机数,则输出数字太小的提示信息*/

if (guess>number)
printf("\n Your answer is high,try again!");/*如果用户输入的数字大于系统随机数,则输出数字太小的提示信息*/

count++;/*猜测次数加一*/
if (count==15)
{
printf("\n This is the %d times! Think it hard next!",count);
exit(0);/*如猜测15次还没猜对,则退出游戏*/
}
}while (!(guess==number));
if (count<=7)/*猜测的次数小于7次*/
{
printf("\n You have got it in %d times.\n",count);
printf("\n you guess right,Congretulations!");/*游戏成功则提示祝贺信息*/
}
else
{
printf("\n You got it in %d times.\n",count);
printf("\n I bet you can do it better!");/*游戏失败则提示鼓励信息*/
}
printf("\n NEXT?(Y/N):");/*选择是否重新游戏*/
scanf("%c",&yes);
}
}
运行程序时请用户猜数字,该数字由系统随机产生,用户最多有七次猜测的机会,如果在七次内猜对数字,则程序显示祝贺信息,如果用户大于七次猜对数字,则程序显示鼓励信息,如果用户连续15次都没有猜对数字,则游戏自动退出。结束一次游戏后,系统询问用户进行下一次猜数字游戏,用户输入“Y”则开始下一次猜数字游戏,用户如果输入“N”则退出游戏。

B. C语言用循环猜字游戏:使用随机数生成一个三位数作为谜底进行猜字游戏,每次猜的时候给出提示,

#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int main()
{
int a,b,c=10;
srand(time(NULL));
a=rand()%1000;
printf("系统已经给出了一个0-999的数,请输入一个数。\n");
while(1)
{ c--;
scanf("%d",&b);
if(b>a)
printf("猜大了,还剩%d次机会,请继续\n",c);
if(b<a)
printf("猜小了,还剩%d次机会,请继续\n",c);
if(b==a)
{ printf("恭喜你答对了\n");
printf("系统给的数是:%d",a);
break;}
if(c==0)
{ printf("你已经没有机会了,请重新开始吧!\n");break;}
}

}

C. c语言猜数字游戏源代码

小游戏2048:

#include<stdio.h>

#include<stdlib.h>

#include<conio.h>

#include<time.h>

#include<windows.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(" 2048小游戏"); Color(7); //恢复白字黑底

printf(" ┌──────┬──────┬──────┬──────┐");

printf(" │ │ │ │ │");

printf(" ├──────┼──────┼──────┼──────┤");

printf(" │ │ │ │ │");

printf(" ├──────┼──────┼──────┼──────┤");

printf(" │ │ │ │ │");

printf(" ├──────┼──────┼──────┼──────┤");

printf(" │ │ │ │ │");

printf(" └──────┴──────┴──────┴──────┘");

show( );

}

void show( ) //输出界面

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

for(j=0;j<4;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(" 游戏结束!是否继续? [ 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);

}

D. c语言编程:猜数字游戏

#include <time.h>
#include <stdio.h>
#include <stdlib.h>
#define UI unsigned short int
void game()
{
UI answer;
UI input;
UI lower=1;
UI upper=100;
UI count=0;
srand(time(NULL));
do{answer=rand()%101;}
while(answer==0);
puts("Welcome to the number guessing game!");
do
{
puts("Please enter an integer from 1 to 100 (again):");
scanf("%lu",&input);
getchar();
count=count+1;
if(input==answer){puts("You succeeded!");printf("The number of time(s) you entered is %lu.\n",count);}
else
{
puts("You failed!");
if(input<answer){if(input>lower){lower=input;}puts("The answer is greater than your input.");}
else {if(input<upper){upper=input;}puts("The answer is less than your input.");}
printf("The answer is from %lu to %lu.\n",lower,upper);
}
}
while(input!=answer);
}
#undef UI
int main()
{
game();
system("Pause");
return 0;
}

E. C语言编写猜数字游戏

#include<stdio.h>
#include<time.h>
#include<math.h>
int main()
{
int i,a,n;
srand((int)time(NULL));
n=rand()%100+1;
for(i=0; i<5; i++)
{
printf("请输入一个介于1到100的整数:");
scanf("%d",&a);
if(a<n)
printf("猜小了!\n");
if(a>n)
printf("猜大了!\n");
if(a==n)
{
printf("猜对了!\n");
break;
}
}
return 0;
}

F. 猜数字游戏c语言编程一到五

#include<stdio.h>

#include<stdlib.h>

#include<time.h>

intmain(){

srand((unsigned)time(NULL));

intguessNum=0;

while(1)

{

intrandNum=rand()%6;

printf("请输入1-5的数字:");

scanf("%d",&guessNum);

if(guessNum<1||guessNum>5){

printf("输入不合法! ");

continue;

}

if(randNum==guessNum){

printf("恭喜你,猜对了!");

break;

}else

{

printf("很遗憾,差一点,继续努力! ");

}

}

}

G. 怎样用C语言实现猜字游戏

//电脑随机生成一个50以内的整数,然后从键盘输入一个整数猜系统生成的数,直到猜对为止,并输出所猜次数。
#include<stdio.h>
#include
<stdlib.h>
#include<time.h>
void
main()
{
int
count=0;
int
flag=1;
do
{
srand(time(NULL));
int
num1,num=rand()%51;
printf("请输入您要猜的数(0-50):");
scanf("%d",&num1);
if(num1>50)
{
printf("输入有误,请重新输入\n");
continue;
}
printf("随机数为:%d\n",num);
count++;
if(num1==num)
{
flag=0;
}
printf("猜错了!3请继续\n\n\n");
}while(flag);
printf("%d",count);
}

H. c语言猜数字游戏

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<time.h>

char*getHint(char*secret,char*guess)
{
inti,countA=0,countB=0;
intcount[10]={0};
char*result=(char*)malloc(strlen(secret));
if(strlen(secret)!=strlen(guess))
{
printf("UnequalLength!");
returnNULL;
}
for(i=0;i<4;i++)
{
if(secret[i]==guess[i])
countA++;
else
{
count[secret[i]-'0']++;
if(count[secret[i]-'0']<=0)
{
countB++;
}
count[guess[i]-'0']--;
if(count[guess[i]-'0']>=0)
{
countB++;
}
}
}
sprintf(result,"%d%c%d%c",countA,'A',countB,'B');
returnresult;
}

intmain()
{
inti=0,j=0;
charnum[4]={0},input[5]={0};

srand((unsigned)time(NULL));//初始化随机数
for(i=0;i<4;i++)//生成4个随机数
{
num[i]=(rand()%10)+48;
}
for(j=0;j<10;j++)
{
printf("pleaseinput4-digtalsnumber[%d]times:",j+1);
fgets(input,sizeof(input),stdin);
fflush(stdin);
if(strlen(input)!=4)
{
continue;
}
printf("input[%s] ",input);

strcpy(input,getHint(num,input));
printf("ret=[%s] ",input);
if(strcmp(input,"4A0B")==0)
{
printf("YouareWin! ");
break;
}
}

return0;
}

I. c语言:猜数字游戏代码

#include<stdio.h>
#include<stdlib.h>
#include<time.h>

intmain()
{
intnum,n,i,cnt=0,finish=0;
srand((unsignedint)time(NULL));
num=rand()%100;
printf("请猜数字,0~100之间 ");
do{
scanf("%d",&i);
cnt++;
if(i<0&&i>=100)
{
printf("GameOver ");
finish=1;
}
elseif(i>num)printf("Toobig ");
elseif(i<num)printf("Toosmall ");
else
{
printf("你用了%d次机会猜中!",cnt);
finish=1;
}

}while(!finish);
return0;
}

热点内容
中国银行查询密码是什么 发布:2025-01-16 02:33:20 浏览:791
坚果pro录音文件夹 发布:2025-01-16 02:31:46 浏览:938
支付宝的登录密码忘记了如何改 发布:2025-01-16 02:30:30 浏览:221
解压作业泥 发布:2025-01-16 02:28:02 浏览:806
我的世界rpg服务器空岛 发布:2025-01-16 02:26:49 浏览:90
ps脚本函数 发布:2025-01-16 02:15:28 浏览:480
android显示行数据 发布:2025-01-16 02:07:40 浏览:963
压缩皇冠 发布:2025-01-16 01:51:27 浏览:274
全键盘编程键盘 发布:2025-01-16 01:38:59 浏览:422
尾货棉服直播间脚本 发布:2025-01-16 01:21:45 浏览:228