求几C语言个小游戏代码,简单的,要注释、、谢谢了、
答案:2 悬赏:20 手机版
解决时间 2021-02-19 23:24
- 提问者网友:書生途
- 2021-02-18 23:07
求几C语言个小游戏代码,简单的,要注释、、谢谢了、
最佳答案
- 五星知识达人网友:鸽屿
- 2021-02-19 00:10
//这是一个显示方格的小程序,小方格可一左右移动的,可以按A键、D键、方向键,按n
//时则退出程序。这个程序整体很简单你看一会就能明白了,上下移动还没弄好。
#include<stdio.h>
void main(){
int i,keyCount=0;
int n=196,e=179,wu=218,eu=191,wd=192,ed=217; //定义方格边框
char move='';
while(1){
move=getch();
if(move==0)
move=getch();
if(move=='n')
break;
if((move=='a'||move=='A'||move==75)&&keyCount>0)
keyCount--;
if((move=='d'||move=='D'||move==77)&&keyCount<76)
keyCount++;
printf("--------------------------------------------\n"); //线条打印
printf("Press \'a\' the square move left ,\n and press \'d\' the square move right!\nPress \'n\' to exit!\n"); //打印说明
printf("--------------------------------------------\n");
for(i=1;i<=16;i++)
printf("\n");
for(i=1;i<=keyCount;i++)
printf(" ");
printf("%c%c%c\n",wu,n,eu); //打印上边框
for(i=1;i<=keyCount;i++)
printf(" ");
printf("%c %c\n",e,e) ; //打印中间边框
for(i=1;i<=keyCount;i++)
printf(" ");
printf("%c%c%c\n",wd,n,ed); //打印底边框
}
}
//时则退出程序。这个程序整体很简单你看一会就能明白了,上下移动还没弄好。
#include<stdio.h>
void main(){
int i,keyCount=0;
int n=196,e=179,wu=218,eu=191,wd=192,ed=217; //定义方格边框
char move='';
while(1){
move=getch();
if(move==0)
move=getch();
if(move=='n')
break;
if((move=='a'||move=='A'||move==75)&&keyCount>0)
keyCount--;
if((move=='d'||move=='D'||move==77)&&keyCount<76)
keyCount++;
printf("--------------------------------------------\n"); //线条打印
printf("Press \'a\' the square move left ,\n and press \'d\' the square move right!\nPress \'n\' to exit!\n"); //打印说明
printf("--------------------------------------------\n");
for(i=1;i<=16;i++)
printf("\n");
for(i=1;i<=keyCount;i++)
printf(" ");
printf("%c%c%c\n",wu,n,eu); //打印上边框
for(i=1;i<=keyCount;i++)
printf(" ");
printf("%c %c\n",e,e) ; //打印中间边框
for(i=1;i<=keyCount;i++)
printf(" ");
printf("%c%c%c\n",wd,n,ed); //打印底边框
}
}
全部回答
- 1楼网友:撞了怀
- 2021-02-19 01:39
// Calcu24.cpp : Defines the entry point for the console application.
//
#include "conio.h"
#include "stdlib.h"
#include "time.h"
#include "math.h"
#include "string.h"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;
}
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;
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯