c语言简单游戏代码?c语言入门代码大全
大家好,今天给各位分享c语言简单游戏代码的一些知识,其中也会对c语言入门代码大全进行解释,文章篇幅可能偏长,如果能碰巧解决你现在面临的问题,别忘了关注本站,现在就马上开始吧!
求几C语言个小游戏代码,简单的,要注释、、谢谢了、
// Calcu24.cpp: Defines the entry point for the console application.
//
/*
6-6
24点游戏
*/
#include"conio.h"
#include"stdlib.h"
#include"time.h"
#include"math.h"
#include"string.h"/*
从一副扑克牌中,任取4张。
2-10按其点数计算(为了表示方便10用T表示),J,Q,K,A统一按 1计算
要求通过加减乘除四则运算得到数字 24。
本程序可以随机抽取纸牌,并用试探法求解。
*/void GivePuzzle(char* buf)
{
char card[]={'A','2','3','4','5','6','7','8','9','T','J','Q','K'}; for(int i=0; i<4; i++){
buf[i]= card[rand()% 13];
}
}
void shuffle(char* buf)
{
for(int i=0; i<5; i++){
int k= rand()% 4;
char t= buf[k];
buf[k]= buf[0];
buf[0]= t;
}
}
int GetCardValue(int c)
{
if(c=='T') return 10;
if(c>='0'&& c<='9') return c-'0';
return 1;
}
char GetOper(int n)
{
switch(n)
{
case 0:
return'+';
case 1:
return'-';
case 2:
return'*';
case 3:
return'/';
} return'';
}double MyCalcu(double op1, double op2, int oper)
{
switch(oper)
{
case 0:
return op1+ op2;
case 1:
return op1- op2;
case 2:
return op1* op2;
case 3:
if(fabs(op2)>0.0001)
return op1/ op2;
else
return 100000;
} return 0;
}
void MakeAnswer(char* answer, int type, char* question, int* oper)
{
char p[4][3];
for(int i=0; i<4; i++)
{
if( question[i]=='T')
strcpy(p[i],"10");
else
sprintf(p[i],"%c", question[i]);
}
switch(type)
{
case 0:
sprintf(answer,"%s%c(%s%c(%s%c%s))",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 1:
sprintf(answer,"%s%c((%s%c%s)%c%s)",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 2:
sprintf(answer,"(%s%c%s)%c(%s%c%s)",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 3:
sprintf(answer,"((%s%c%s)%c%s)%c%s",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
case 4:
sprintf(answer,"(%s%c(%s%c%s))%c%s",
p[0], GetOper(oper[0]), p[1], GetOper(oper[1]), p[2], GetOper(oper[2]), p[3]);
break;
}
}
bool TestResolve(char* question, int* oper, char* answer)
{
//等待考生完成
int type[5]={0,1,2,3,4};//计算类型
double p[4];
double sum=0;
//
for(int i=0; i<4; i++)//循环取得点数
{
p[i]=GetCardValue(int(question[i]));
} for(i=0;i<5;i++)
{
MakeAnswer(answer,type[i],question,oper);//获取可能的答案
switch(type[i])
{
case 0:
sum=MyCalcu(p[0],MyCalcu( p[1],MyCalcu(p[2], p[3], oper[2]),oper[1]),oper[0]);//A*(B*(c*D))
break;
case 1:
sum=MyCalcu(p[0],MyCalcu(MyCalcu(p[1], p[2], oper[1]),p[3],oper[2]),oper[0]);//A*((B*C)*D)
break;
case 2:
sum=MyCalcu(MyCalcu(p[0], p[1], oper[0]),MyCalcu(p[2], p[3], oper[2]),oper[1]);//(A*B)*(C*D)
break;
case 3:
sum=MyCalcu(MyCalcu(MyCalcu(p[0], p[1], oper[0]),p[2],oper[1]),p[3],oper[2]);//((A*B)*C)*D
break;
case 4:
sum=MyCalcu(MyCalcu(p[0],MyCalcu(p[1], p[2], oper[1]),oper[0]),p[3],oper[2]);//(A*(B*C))*D
break;
}
if(sum==24) return true;
}
return false;
}
/*
采用随机试探法:就是通过随机数字产生加减乘除的组合,通过大量的测试来命中的解法
提示:
1.需要考虑用括号控制计算次序的问题比如:( 10- 4)*( 3+ A),实际上计算次序的数目是有限的:
A*(B*(c*D))
A*((B*C)*D)
(A*B)*(C*D)
((A*B)*C)*D
(A*(B*C))*D
2.需要考虑计算结果为分数的情况:( 3+(3/ 7))* 7
3.题目中牌的位置可以任意交换
*/
bool TryResolve(char* question, char* answer)
{
int oper[3];//存储运算符,0:加法 1:减法 2:乘法 3:除法
for(int i=0; i<1000* 1000; i++)
{
//打乱纸牌顺序
shuffle(question);
//随机产生运算符
for(int j=0; j<3; j++)
oper[j]= rand()% 4; if( TestResolve(question, oper, answer)) return true;
} return false;
}
int main(int argc, char* argv[])
{
//初始化随机种子
srand((unsigned)time( NULL)); char buf1[4];//题目
char buf2[30];//解答
printf("***************************\n");
printf("计算24\n");
printf("A J Q K均按1计算,其它按牌点计算\n");
printf("目标是:通过四则运算组合出结果:24\n");
printf("***************************\n\n");
for(;;)
{
GivePuzzle(buf1);//出题
printf("题目:");
for(int j=0; j<4; j++){
if( buf1[j]=='T')
printf("10");
else
printf("%c", buf1[j]);
} printf("\n按任意键参考答案...\n");
getch(); if( TryResolve(buf1, buf2))//解题
printf("参考:%s\n", buf2);
else
printf("可能是无解...\n"); printf("按任意键出下一题目,x键退出...\n");
if( getch()=='x') break;
} return 0;
}
用C语言编写的小游戏代码是什么
“猜数字小游戏”,每个数字后按空格,最后按回车确认
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
int a[4],b[4];
int count=0;//计算猜测次数
void csh();//初始化
void start();//开始游戏
int main()
{ csh();
start();
}
void csh()//初始化
{ printf("
猜数字小游戏
");
printf(“猜四个数字,如数字与顺序都正确记为A,数字正确位置不对记为B.
”);
}
void start()//开始游戏
{int m,n;//m是完全猜对的个数,n是顺序不对的个数
while(1)
{srand((unsigned)time(NULL));//初始化随机数发生器srand()
while(1){ for(int i=0;i<4;i++) a[i]=rand()%10;//rand()函数每次随机产生一个0-9的数
if((a[3]!=a[2]&&a[3]!=a[1]&&a[3]!=a[0])&&
(a[2]!=a[1]&&a[2]!=a[0])&&a[1]!=a[0]) break;}//4个随机数各自不相等
printf("请依次输入4个一位整数:
");
while(1)
{for(int i=0;i<4;i++) scanf(“%d”,&b[i]);
printf("你输入的是:%d%d%d%d",b[0],b[1],b[2],b[3]);
m=0;n=0;
for(int i=0;i<4;i++)
{for(int j=0;j<4;j++)
{ if(b[i]==a[j]&&i==j)m=m+1; if(b[i]==a[j]&&i!=j)n=n+1;}
}
count=count+1;
printf("%dA%dB你试了%d次
",m,n,count);
if(m==4)break;
if(count==8){ count=0; break;}
}
printf("
");
if(m==4)printf("你猜对了(^-^)!就是:%d%d%d%d
",a[0],a[1],a[2],a[3]);
else printf("你输了(T-T)!哈哈!应该是:%d%d%d%d
",a[0],a[1],a[2],a[3]);
int z;
printf("(要继续吗?1或0)
");
scanf(“%d”,&z);
if(z==0) break;
}
}
如何使用C语言编写简单小游戏
C语言是计算机专业都要学习的一门基础学科。一般来说,是比较枯燥的.那么,我们能不能通过编一些小游戏来提高它的趣味性呢?这样学习程序设计,就不会是一件艰苦,枯燥的事,它变得象电脑游戏一样充满好奇,富有乐趣。
例如2048这款游戏:
方法/步骤:
#include<stdio.h>
#include<stdlib.h>
#include<time.h>
#include<conio.h>
#include<windows.h>
#define SIZE 4
static int score=0;
void putn(int n[][SIZE]);
void getn(int n[][SIZE]);
int isempty(int n[][SIZE]);
int isfull(int n[][SIZE]);
void math(int n[][SIZE],char c);
void tow(int n[][SIZE]);
void toa(int n[][SIZE]);
void tos(int n[][SIZE]);
void tod(int n[][SIZE]);
//主函数
int main()
{
int i,j;
int n[SIZE][SIZE];
char c='';
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
{
n[i][j]=0;
}
}
printf("***********************
"
" 2048(%dX%d)
"
" control:W/A/S/D
"
"press any key to begin
"
"***********************
",SIZE,SIZE);
getch();
system("cls");
//n[0][1]=2048;
//n[0][3]=2048;
while(1)
{
if(isempty(n))
getn(n);
putn(n);
if(!isempty(n)&&isfull(n))
break;
sleep(200);
c=getch();
while(c!='w'&&c!='a'&&c!='s'&&c!='d')
c=getch();
math(n,c);
system("cls");
}
printf(" Game Over!
",score);
return 0;
}
//函数
void putn(int n[][SIZE])
{
int i,j;
for(i=0;i<SIZE;i++)
{
for(j=0;j<SIZE;j++)
printf("|");
printf("|
");
for(j=0;j<SIZE;j++)
{
if(n[i][j]==0)
printf("|");
else
printf("|%4d",n[i][j]);
}
printf("|
");
for(j=0;j<SIZE;j++)
printf("|_____");
printf("|
");
}
printf("score:%d",score);
}
void getn(int n[][SIZE])
{
int a,b;
a=rand()%SIZE;
b=rand()%SIZE;
while(n[a][b]!=0)
{
a=rand()%SIZE;
b=rand()%SIZE;
}
n[a][b]=2;
}
int isempty(int n[][SIZE])
{
int i,j,count=0;
for(i=0;i<SIZE;i++)
for(j=0;j<SIZE;j++)
if(n[i][j]==0)
count++;
return count;
}
int isfull(int n[][SIZE])
{
int i,j,count=0;
for(i=0;i<SIZE;i++)
{
for(j=1;j<SIZE-1;j++)
{
if(n[i][j]==n[i][j+1]||n[i][j]==n[i][j-1])
count++;
}
}
for(j=0;j<SIZE;j++)
{
for(i=1;i<SIZE-1;i++)
{
if(n[i][j]==n[i+1][j]||n[i][j]==n[i-1][j])
count++;
}
}
return count>0?0:1;
}
void math(int n[][SIZE],char c)
{
switch(c)
{
case'w':tow(n);break;
case'a':toa(n);break;
case's':tos(n);break;
case'd':tod(n);break;
default:;
}
}
void tow(int n[][SIZE])
{
int i,j,a;
int m[SIZE];
for(a=0;a<SIZE;a++)
m[a]=0;
for(j=0;j<SIZE;j++)
{
for(a=0;a<SIZE;a++)
{
for(i=0;i<SIZE-1;i++)
{
if(n[i][j]==0)
{
n[i][j]=n[i+1][j];
n[i+1][j]=0;
}
}
}
}
for(j=0;j<SIZE;j++)
{
for(a=0,i=0;i<SIZE;i++)
{
if(n[i][j]!=n[i+1][j]&&n[i][j]!=0||n[i][j]==2048)
{
m[a++]=n[i][j];
n[i][j]=0;
}
else if(n[i][j]==n[i+1][j])
{
m[a++]=n[i][j]+n[i+1][j];
score+=m[a-1];
n[i][j]=0,n[i+1][j]=0;
}
}
for(i=0;i<SIZE;i++)
{
n[i][j]=m[i];
m[i]=0;
}
}
}
void toa(int n[][SIZE])
{
int i,j,a;
int m[SIZE];
for(a=0;a<SIZE;a++)
m[a]=0;
for(i=0;i<SIZE;i++)
{
for(a=0;a<SIZE;a++)
{
for(j=0;j<SIZE-1;j++)
{
if(n[i][j]==0)
{
n[i][j]=n[i][j+1];
n[i][j+1]=0;
}
}
}
}
for(i=0;i<SIZE;i++)
{
for(a=0,j=0;j<SIZE;j++)
{
if(n[i][j]!=n[i][j+1]&&n[i][j]!=0||n[i][j]==2048)
{
m[a++]=n[i][j];
n[i][j]=0;
}
else if(n[i][j]==n[i][j+1])
{
m[a++]=n[i][j]+n[i][j+1];
score+=m[a-1];
n[i][j]=0,n[i][j+1]=0;
}
}
for(j=0;j<SIZE;j++)
{
n[i][j]=m[j];
m[j]=0;
}
}
}
void tos(int n[][SIZE])
{
int i,j,a;
int m[SIZE];
for(a=0;a<SIZE;a++)
m[a]=0;
for(j=SIZE-1;j>=0;j--)
{
for(a=SIZE-1;a>=0;a--)
{
for(i=SIZE-1;i>0;i--)
{
if(n[i][j]==0)
{
n[i][j]=n[i-1][j];
n[i-1][j]=0;
}
}
}
}
for(j=SIZE-1;j>=0;j--)
{
for(a=SIZE-1,i=SIZE-1;i>=0;i--)
{
if(n[i][j]!=n[i-1][j]&&n[i][j]!=0||n[i][j]==2048)
{
m[a--]=n[i][j];
n[i][j]=0;
}
else if(n[i][j]==n[i-1][j])
{
m[a--]=n[i][j]+n[i-1][j];
score+=m[a+1];
n[i][j]=0,n[i-1][j]=0;
}
}
for(i=SIZE-1;i>=0;i--)
{
n[i][j]=m[i];
m[i]=0;
}
}
}
void tod(int n[][SIZE])
{
int i,j,a;
int m[SIZE];
for(a=0;a<SIZE;a++)
m[a]=0;
for(i=SIZE-1;i>=0;i--)
{
for(a=SIZE-1;a>=0;a--)
{
for(j=SIZE-1;j>0;j--)
{
if(n[i][j]==0)
{
n[i][j]=n[i][j-1];
n[i][j-1]=0;
}
}
}
}
for(i=SIZE-1;i>=0;i--)
{
for(a=SIZE-1,j=SIZE-1;j>=0;j--)
{
if(n[i][j]!=n[i][j-1]&&n[i][j]!=0||n[i][j]==2048)
{
m[a--]=n[i][j];
n[i][j]=0;
}
else if(n[i][j]==n[i][j-1])
{
m[a--]=n[i][j]+n[i][j-1];
score+=m[a+1];
n[i][j]=0,n[i][j-1]=0;
}
}
for(j=SIZE-1;j>=0;j--)
{
n[i][j]=m[j];
m[j]=0;
}
}
}
OK,本文到此结束,希望对大家有所帮助。