c语言猜数字游戏源代码
发布网友
发布时间:2022-04-21 01:43
我来回答
共4个回答
热心网友
时间:2022-04-24 03:21
小游戏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("\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;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("\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);
}
热心网友
时间:2022-04-24 04:39
# include<stdio.h>
# include<stdlib.h>
# include<time.h>
main()
{
int a,b;
char c;
srand(time(NULL));
a=1+(rand()%1000);
printf("I have a number between 1 and 1000.\nCan you guess my number?\nPlease type your first guess.\n");
scanf("%d",&b);
while(b!=-1)
{
if(b==a)
{
printf("Excellent! You guessed the number!\nWould you like to play again(y or n)?");
scanf("%c",&c);
scanf("%c",&c);
switch(c){
case 'y':
printf("I have a number between 1 and 1000.\nCan you guess my number?\nPlease type your first guess.\n");
scanf("%d",&b);
break;
case 'n':
break;
}
}
while(b<a)
{
printf("Too low.Try again.");
scanf("%d",&b);
}
while(b>a)
{
printf("Too high.Try again.");
scanf("%d",&b);
}
}
}
热心网友
时间:2022-04-24 06:14
/*幸运52游戏 */
#include<stdio.h>
void main()
{
int a,b,c;
char name[20];
printf("请输入您的姓名:\n");
scanf("%s",name);
printf("请设置玩家的游戏猜谜次数\n");
scanf("%d",&b);
printf("请设置猜谜的正确答案\n");
scanf("%d",&c);
printf("\n好了,游戏开始\n*****************************************\n请输入数字:\n");
scanf("%d",&a);
while(a!=c&&b>0)
{
if(a>c)
printf("大了\n");
else
printf("小了\n");
b--;
scanf("%d",&a);
}
if(a==c)
{
printf("\n*****************************************\n恭喜您过关了!\n玩家:%s\t",name);
}
else
printf("\n*****************************************\n游戏结束,大虾请重新来过\n");
getch();
}
热心网友
时间:2022-04-24 08:05
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int EachDiff(int a[],int n)
{
int i,j;
for(i=0;i<n;i++)
for(j=i+1;j<n;j++)
if(a[i]==a[j])
return 0;
else
return 1;
}
int JudgeA(int a[],int b[],int n)
{
int i,result=0;
for(i=0;i<n;i++)
{
if(a[i]==b[i]) result++;
}
return result;
}
int JudgeB(int a[],int b[],int n)
{
int i,j,result=0;
for(i=0;i<n;i++)
for(j=0;j<n;j++)
if(a[i]==b[j]&&i!=j)
return result;
}
void CreateNum(int a[],int n)
{
int i;
srand(time(0));
do
{
for(i=0;i<n;i++)a[i]=rand()%10;
}while(!EachDiff(a,n));
}
int JudgeInput(int a[],int n)
{
int i;
if(!EachDiff(a,n))
return 0;
else
for(i=0;i<n;i++)
if(a[i]>9||a[i]<0)
return 0;
else
return 1;
}
int main()
{
//system();
int a[4],b[4],i;
int fth=1;
printf("作者 Dante -- 谢煜东");
printf("\t\t 欢迎来到猜数字游戏\n\n");
printf(" 计算机想好了一个4位无重复数字,首位可以为零。\n 例如:0913是可以的,3502也是可以的,6787不可以。\n");
printf(" 现在您想办法把这个数字猜出来。\n\n");
printf(" 每猜一个数字,计算机会根据这个数字给出几A几B:\n 其中A前面的数字表示位置正确的数的个数,\n 而B前的数字表示数字正确而位置不对的数的个数。\n\n");
//printf(" 如正确答案为5234,而猜的人猜5346,则是1A2B。\n 其中有一个5的位置对了,记为1A;\n 而3和4这两个数字对了,而位置没对,因此记为2B;\n 合起来就是1A2B。\n\n");
printf(" 记住!1:你共有8次机会,2:输入的数字不能重复。\n\n");
printf(" 当出现\"第几次:\"字样并等待的时候,您就可以输入你要猜测的四位数字,\n 输入完之后回车确认。祝您开心\n\n 明白之后任意键游戏正式开始!");
getchar();
CreateNum(a,4);
system("cls");
printf("\t\t现在可以输入你要猜的四位数字了,输入之后回车确认。\n");
while(fth<=8)
{
int t=1000;
printf("第%d次: ",fth);
scanf("%d",amp;b[0]);
for(i=0;i<3;i++)
{
b[i+1]=b[i]%t;
b[i]=b[i]/t;
t/=10;
}
for(i=0;i<4;i++)
if(JudgeInput(b,4)==0)
{
printf("您的输入有问题,请重新输入\n");
continue;
}
if(JudgeA(a,b,4)==4)
{
printf("\t结果: 答对了!好聪明!");
system("pause");
//return;
}
printf("\t结果:您有%d个数字和位置都对了,有%d个数字对了而位置没对\n",JudgeA(a,b,4),JudgeB(a,b,4));
fth++;
}
printf("\n很遗憾。正确答案是:");
for(i=0;i<4;i++)
printf("%d",a[i]);
printf("下次再玩吧!88\n");
system("pause");
return 0;
}
用C语言编写的小游戏代码是什么?
void csh( ) //初始化 { printf("\n\n 猜 数 字 小 游 戏\n\n");printf(“ 猜四个数字,如数字与顺序都正确记为A,数字正确位置不对记为B.\n”);} void start( ) //开始游戏 {int m,n; //m是完全猜对的个数,n是顺序不对的个数 while(1){srand((unsigne...
c语言小游戏:猜数字 随机一个1-100之间的数,根据数据输入进行提示_百度...
int main(void){ int value=0;int num=0;srand((unsigned int) time(NULL));num=rand()%100+1; //1-100 while(1){ scanf("%d",&value);if(num>value){ printf("您猜小了\n");} else if(num<value){ printf("您猜大了\n");} else if(num=value){ printf("恭喜您猜对了\...
猜数字游戏 C语言,最好附带说明
void main(){ int Password=0,Number=0,price=58,i=0;clrscr();printf("\n===This is a Number Guess Game!===\n");while( Password != 1234 ){ if( i >= 3 )return;i++;puts("Please input Password: ");scanf("%d",&Password);} i=0;while( Number!=price ){ do{ puts...
c语言设计一个猜数游戏,电脑会自动提示大了或小了,直到猜对为止
include "stdlib.h"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("大了,请重新输入:");} els...
c语言编程 编一个猜数字游戏
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(){ /* 以时间作为时间种子保证...
【c语言】编写一个猜数字游戏
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("the ...
用C语言编写一段猜数字游戏,跟一般的不一样,规则如下
int main(void){ int a,low,k,he,d;k=50; //k为答案数 low=1;he=100;scanf("%d",&a);while(a!=k){ if(a<k){ low=a;printf("猜数的范围是:%d到%d\n",low,he);} if(a>k){ he=a;printf("猜数的范围是:%d到%d\n",low,he);} scanf("%d",&a);} printf("答案...
猜数字游戏用C语言怎么编啊?谢谢拉
{ int iRand;int iInput;while(1){ printf("Let's Go:\n");iRand = rand();while(1){ scanf("%d",&iInput);if(iInput == iRand){ break;} if(iInput > iRand){ printf("Your answer is HIGH,try again.\n");} else { printf("Your answer is LOW,try again.\n");} ...
猜数字游戏C语言编程
include 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语言猜数字游戏程序编写
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...