当前位置:首页 » 编程语言 » c语言猜数字游戏
扩展阅读
webinf下怎么引入js 2023-08-31 21:54:13
堡垒机怎么打开web 2023-08-31 21:54:11

c语言猜数字游戏

发布时间: 2022-02-06 16:53:04

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

同学你好!
下面是我自己做了一个,运行是成功的。希望对你有所帮助!
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<time.h>
void
main()
{
int
i,j,cnt,guard=1;
while(guard)/*利用guard来判断是否继续进行游戏*/
{
srand((unsigned)time(NULL));
i=rand()%100;/*随机生成整数*/
cnt=0;/*比较次数置零*/
for(;;)
{
printf("Please
input
the
data:");
scanf("%d",&j);
cnt++;/*次数累加*/
if(j>i)
printf("Too
high!\n");
else
if(j<i)
printf("Too
low!\n");
else
{
printf("Right!\n");
printf("The
cnt
is:%d\n",cnt);
printf("Wanna
continue?
1.continue
0.exit\n");
scanf("%d",&guard);/*选择是否继续*/
break;
}
}
}
}
希望我的回答对你有帮助,祝你的C成绩进步

❷ 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;
}

❸ C语言编程,猜数游戏

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
void main()
{
int a;
srand(time(NULL));
a=rand()%101;
int n,count=0;
printf("input the number you want guess:");
while(n!=a)
{
count++;
if(count>8)
break;
scanf("%d",&n);
if(n>a)
printf("too big,try again:");
if(n<a)
printf("tow low,try again:");
}
if(n==a)
printf("you are right\n");
else
printf("you are lose\n");
}
这个游戏肯定赢啊

❹ 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);

}

❺ 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;
}

❻ 用C语言编写一个猜数字游戏的程序(最好附带说明)

有偿,先程序后cash ,有意加我

❼ c语言 简单猜数游戏 急

#include
<stdio.h>
#include<stdlib.h>
#include
<time.h>
main()
{
int
a,b,i=1;
srand((unsigned)time(NULL));//产生随机数种子
a=rand()%100+1;//产生随机数
printf("\n\t-------------------
欢迎进入猜数游戏
-------------------\n\n");
printf("
@游戏开始@
请猜一个数(1~100):");
scanf("%d",&b);
while(i<8)//判断的循环体
{
if(a==b)
{printf("
o(≥v≤)o
恭喜你答对了!\n");break;}
else
if(a>b)
printf("
@对不起@
你猜小了...\n");
else
if(a<b)
printf("
@对不起@
你猜大了...\n");
printf("
@没关系@
再猜一次吧:");
scanf("%d",&b);
i++;
}
if(i==8)printf("-_-!!!对不起,你猜错了8次,你输了\n");
}
这个简单点,基本功能实现

❽ c语言编程 编一个猜数字游戏

源码如下:

/*File:guess.c*/

#include<stdio.h>/*standardinput&outputsupport*/

#include<stdlib.h>/*srand()rand()*/

#include<time.h>/*time()*/

/*宏定义*/

#defineNUMBER_LENGTH5/*随机数长度*/

#defineNUMBER_LIMIT10/*随机数限制,每一位0-9*/

#defineINPUT_LENTH128/*输入缓冲区大小*/

chargoal[NUMBER_LENGTH]={0};/*保存随机数*/

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

charinput[INPUT_LENTH]={0};/*保存输入*/

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

voidinitData()

{

inti=0;

while(i<NUMBER_LENGTH)

goal[i++]=0;

i=0;

while(i<NUMBER_LIMIT)

{

flag[i++]=0;

}

}

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

voidinitBuffer()

{

inti=0;

while(i<INPUT_LENTH)

input[i++]=0;

}

/*显示猜测结果*/

voiddisplay()

{

intcount=0;

inti=0;

while(i<NUMBER_LENGTH)

{

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

{

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

count++;

}

else

{

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

}

i++;

}

printf(" RIGHT:%dbit(s) ",count);

if(count==NUMBER_LENGTH)

{

printf("Youwin!Thenumberis%s. ",goal);

exit(0);

}

}

/*生成随机数*/

voidgeneral()

{

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

srand((unsignedint)time(NULL));

inti=0;

while(i<NUMBER_LENGTH)

{

chartmp;

do

{

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

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

flag[tmp]=1;

goal[i++]=tmp;

}

}

/*输入方法,用于猜测*/

voidguess()

{

printf(": ");

scanf("%s",input);

display();

initBuffer();

}

/*主函数,程序主框架*/

intmain(intargc,constchar*argv[])

{

initData();

initBuffer();

general();

while(1)guess();

return0;

}

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

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