
// Calcu24cpp : Defines the entry point for the console application
//
/
6-6
24点游戏
/
#include "conioh"
#include "stdlibh"
#include "timeh"
#include "mathh"
#include "stringh"/
从一副扑克牌中,任取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)>00001)
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(cD))
break;
case 1:
sum=MyCalcu(p[0],MyCalcu(MyCalcu(p[1], p[2], oper[1]),p[3],oper[2]),oper[0]); //A((BC)D)
break;
case 2:
sum=MyCalcu(MyCalcu(p[0], p[1], oper[0]),MyCalcu(p[2], p[3], oper[2]),oper[1]); // (AB)(CD)
break;
case 3:
sum=MyCalcu(MyCalcu(MyCalcu(p[0], p[1], oper[0]),p[2],oper[1]),p[3],oper[2]); //((AB)C)D
break;
case 4:
sum=MyCalcu(MyCalcu(p[0],MyCalcu(p[1], p[2], oper[1]),oper[0]),p[3],oper[2]); //(A(BC))D
break;
}
if(sum==24) return true;
}
return false;
}
/
采用随机试探法:就是通过随机数字产生 加减乘除的 组合,通过大量的测试来命中的解法
提示:
1 需要考虑用括号控制计算次序的问题 比如:( 10 - 4 ) ( 3 + A ), 实际上计算次序的数目是有限的:
A(B(cD))
A((BC)D)
(AB)(CD)
((AB)C)D
(A(BC))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;
}
贪吃蛇
using System;
using SystemCollectionsGeneric;
using SystemComponentModel;
using SystemData;
using SystemDrawing;
using SystemText;
using SystemWindowsForms;
using SystemCollections;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private bool i;//开关
snake a_snake = new snake(5);//实例化个长度为5的蛇
food afood = new food();//实例化一个食物
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = eGraphics;
if (i)//点击了开始button
{
a_snakedrawsnake(g);//画出蛇
afooddrawfood(g);//画出来食物
}
if (a_snakedeadsnake())//如果蛇死亡事件为真
{
timer1Enabled = false;//timer控件停止
if (DialogResultYes ==
MessageBoxShow("GAME OVER", "是否重新开始", MessageBoxButtonsYesNo))
//messagebox消息
{
//点击确定后重新开始游戏
button1Enabled = true;
a_snake = new snake(5);//初始化蛇
afood = new food();//初始化食物
i = false;//开关为假
gClear(pictureBox1BackColor);//清理picturebox
}
else
ApplicationExit();//关闭程序
}
}
private void button1_Click(object sender, EventArgs e)
{
i = true; //开关为真
afoodF_point = afoodgetpoint();//产生一个食物的随机坐标
pictureBox1Refresh();//刷新picturebox
timer1Enabled = true;//开启timer控件
timer1Interval = 100; //时间间隔为01秒
button1Enabled = false;
}
private void timer1_Tick(object sender, EventArgs e)
{
pictureBox1Refresh();//刷新picturebox
a_snakeextendsnake();//蛇伸长一节
if (a_snakeheadpoint == afoodF_point)
afoodF_point = afoodgetpoint();//蛇头坐标与食物相同
//就只伸长
else
a_snakecontractsnake();//没吃到食物就缩短一节
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
if (eKeyCode == KeysW && a_snakeWay != 2)
{
a_snakeWay = 0;
}
if (eKeyCode == KeysD && a_snakeWay != 3)
{
a_snakeWay = 1;
}
if (eKeyCode == KeysS && a_snakeWay != 0)
{
a_snakeWay = 2;
}
if (eKeyCode == KeysA && a_snakeWay != 1)
{
a_snakeWay = 3;
}
//设置KeyDown事件,用按件控制方向
}
}
public class segment//[一节蛇]类
{
private int number;//私有成员
public int Number//[一节蛇]的编号
{
get
{
return number;
}
set
{
number = value;
}
}
private Point orign;
public Point Orign//[一节蛇]的坐标
{
get
{
return orign;
}
set
{
orign = value;
}
}
public void drawpart(Graphics g)//画[一节蛇]
{
Pen p = new Pen(ColorRed);
gDrawEllipse(p, orignX, orignY, 10, 10);
//画红色圆圈代表一节蛇
}
}
public class snake//蛇类
{
ArrayList alist;//定义一个先进先出的数据表
public Point headpoint;//蛇头坐标
private int way = 1;//初始方向为右
public int Way
{
get
{
return way;
}
set
{
way = value;
}
}
public snake(int count)//蛇的构造函数
{
segment apart;
Point apoint = new Point(30, 30);//初始位置
alist = new ArrayList(count);//初始化数据表
for (int i = 1; i <= count; i++)
{
apointX = apointX + 10;
apart = new segment();
apartNumber = i;
apartOrign = apoint;
alistAdd(apart);//将没节蛇存在表里面
if (i == count)//将最后的一节蛇定为蛇头
headpoint = apoint;
}
}
public void drawsnake(Graphics g)//画蛇
{
for (int i = 0; i < alistCount; i++)
{
segment seg = (segment)alist[i];
segdrawpart(g);
}
}
public void extendsnake()//伸长一节蛇
{
segment seg = new segment();
segNumber = alistCount + 1;
Point p;
if (way == 0)
p = new Point(headpointX, headpointY - 10);
else if (way == 2)
p = new Point(headpointX, headpointY + 10);
else if (way == 3)
p = new Point(headpointX - 10, headpointY);
else
p = new Point(headpointX + 10, headpointY);
segOrign = p;
alistAdd(seg);//将新的一节蛇添加到表尾
headpoint = segOrign;//重新设蛇头
}
public void contractsnake()//蛇缩短一节
{
alistRemove(alist[0]);//删除表的第一个元素
}
public bool deadsnake()//射死亡事件
{
if (headpointX < 0 || headpointY < 0 || headpointX > 350 || headpointY > 270)
//判断是否撞墙了
return true;
for (int i = 0; i < alistCount - 1; i++)
{
segment seg = (segment)alist[i];
if (segOrign == headpoint)//判断是否咬到自己
return true;
}
return false;
}
}
public class food//食物类
{
private Point f_point;
public Point F_point//食物的坐标
{
get
{
return f_point;
}
set
{
f_point = value;
}
}
public void drawfood(Graphics g)//画食物
{
SolidBrush b = new SolidBrush(ColorBlue);
Rectangle rtg = new Rectangle(f_pointX, f_pointY, 10, 10);
gFillRectangle(b, rtg);
//实心的蓝色方块
}
public Point getpoint()//获得食物坐标[随机数point]
{
int i = 10;
Random rdm = new Random(SystemDateTimeNowMillisecond + i);
i = rdmNext(0, 27);
int j = rdmNext(0, 27);
Point newp = new Point(i 10, j 10);
return newp;
}
}
}
下一百层
using System;
using SystemCollectionsGeneric;
using SystemComponentModel;
using SystemData;
using SystemDrawing;
using SystemText;
using SystemWindowsForms;
using SystemCollections;
namespace WindowsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
lift alift = new lift();//梯子实例化
people man = new people();//人物实例化
bool i;//开关
int j =1;//人物移动方向
private void pictureBox1_Paint(object sender, PaintEventArgs e)
{
Graphics g = eGraphics;
if (i)
{
aliftdrawlift(g);//画梯子
mandrawpeople(g);//画人物
}
if (manmandead())//人物死亡为真
{
timer1Enabled = false;
timer2Enabled = false;
timer3Enabled = false;
timer4Enabled = false;
if (DialogResultYes ==
MessageBoxShow("Game Over", "重新开始游戏?", MessageBoxButtonsYesNo))
{ //重新开始游戏
button1Enabled = true;
manFootpoint = new Point(aliftdownmostX + 50, aliftdownmostY - 20);
}
else
ApplicationExit();//退出游戏
}
}
private void button1_Click(object sender, EventArgs e)
{
i = true;//打开开关
pictureBox1Refresh();//刷新
timer1Interval = 2;
timer1Enabled = true;
timer3Interval = 1;
timer3Enabled = true;
manFootpoint = new Point(aliftdownmostX + 50, aliftdownmostY -20);
//初始化任务的坐标
button1Enabled = false;//Button1被锁
}
private void timer1_Tick(object sender, EventArgs e)
{
aliftliftrise();//伸梯子
if (aliftdownmostY <= 260)
aliftaddstep();//加梯子
pictureBox1Refresh();
}
private void Form1_KeyDown(object sender, KeyEventArgs e)
{
//用A控制向左,S控制向右
if (eKeyCode == KeysA)
{
timer2Interval = 1;
timer2Enabled = true;
j = 2;
}
if (eKeyCode == KeysS)
{
timer2Interval = 1;
timer2Enabled = true;
j = 3;
}
}
private void Form1_KeyUp(object sender, KeyEventArgs e)
{
//KeyUp事件控制人物左右移动的停止
if (eKeyCode == KeysA)
{
timer2Enabled = false ;
j = 1;
}
if (eKeyCode == KeysS)
{
timer2Enabled = false ;
j = 1;
}
}
private void timer2_Tick(object sender, EventArgs e)
{
if (j == 2)
manmoveleft();//人物向左移动
else if (j == 3)
manmoveright();//人物向右移动
}
private void timer3_Tick(object sender, EventArgs e)
{
manmovedown();//人物下落
if (aliftmanland(man))
{
timer3Enabled = false;
timer4Interval = 2;
timer4Enabled = true;
}
}
private void timer4_Tick(object sender, EventArgs e)
{
manmoverise();//人物随梯子上升
if (aliftmanout(man))
{
timer3Enabled = true;
timer4Enabled = false;
}
}
}
public class step//台阶类是梯子的一个单位
{
private Point safep;//台阶的坐标
public Point Safep
{
get
{
return safep;
}
set
{
safep = value;
}
}
public void drawstep(Graphics g)//画台阶[实心长方形]
{
SolidBrush b = new SolidBrush(ColorDarkSlateBlue);
Rectangle rect = new Rectangle(safepX, safepY, 100, 10);
gFillRectangle(b, rect);
}
public int getpointx(int i)//台阶的随机X坐标
{
Random rdm = new Random(SystemDateTimeNowMillisecond+i);
return rdmNext(0,240);
}
public Point risepoint()//新伸起来的台阶坐标
{
Random rdm = new Random(SystemDateTimeNowMillisecond123456 );
int x= rdmNext(0, 240);
Point p = new Point(x, 340);
return p;
}
}
public class lift//梯子类
{
public ArrayList alist = new ArrayList(5);//先进先出表
public Point downmost;//最下面台阶的坐标
public lift()//构造函数
{
step astep;
int x=1,y=10;
for (int i = 0; i < 5; i++)
{
astep = new step();
x = astepgetpointx(x);
astep = new step();
astepSafep =new Point(x, y);
alistAdd(astep);
y = y + 80;
if (i == 4)
downmost = astepSafep;
}
}
public void drawlift(Graphics g)//画梯子
{
for (int i=0;i<5;i++)
{
step astep=(step) alist[i];
astepdrawstep (g);
}
}
public void liftrise()//梯子上升
{
//表中的每个Y坐标加1
//并把新的台阶存在表的尾部
for (int i = 0; i < 5; i++)
{
step astep = (step)alist[i];
Point p = new Point(astepSafepX, astepSafepY - 1);
astepSafep = p;
alistAdd(astep);
if (i == 4)
downmost = astepSafep;
}
for (int i = 0; i < 5; i++)//删除表的前5个数据
{
alistRemove(alist[i]);
}
}
public void addstep()//伸起来的一节梯子
{
step astep=new step ();
astepSafep=asteprisepoint();
alistAdd(astep);
alistRemove(alist[0]);
downmost = astepSafep; //始终保证最下面的一节为downmost
}
public bool manland(people man)//人物登陆事件
{
step s;
for (int a = 0; a < 5; a++)
{
s = (step)alist[a];
if (MathAbs( sSafepY -(manFootpointY+10))<2&&
sSafepX <= manFootpointX+10 &&
manFootpointX <= sSafepX + 100)
return true;
}
return false;
}
public bool manout(people man)//人物冲出事件
{
step s;
for (int a = 0; a < 5; a++)
{
s = (step)alist[a];
if (MathAbs(sSafepY - (manFootpointY + 10)) < 3)
{
if (sSafepX-10 > manFootpointX ||
manFootpointX > sSafepX + 100)
return true;
}
}
return false;
}
}
public class people//人物类
{
private Point footpoint;//人物的坐标
public Point Footpoint
{
get
{
return footpoint;
}
set
{
footpoint = value;
}
}
public void drawpeople(Graphics g)//画个实心圆代表人物
{
SolidBrush b = new SolidBrush(ColorIndianRed);
Rectangle rect = new Rectangle(footpointX, footpointY, 10, 10);
gFillEllipse(b, rect);
}
public void moveleft()//人物向左移动
{
Point p = new Point(footpointX - 2, footpointY);
footpoint = p;
}
public void moveright()//人物向右移动
{
Point p = new Point(footpointX + 2, footpointY);
footpoint = p;
}
public void moverise()//人物随梯子上升
{
Point p = new Point(footpointX, footpointY-1);
footpoint = p;
}
public void movedown()//人物下落
{
Point p = new Point(footpointX, footpointY + 1);
footpoint = p;
}
public bool mandead()//人物死亡
{
if ( footpointY<0 ||footpointY>340)
return true ;
return false ;
}
}
}
发两个玩玩吧。 都测试过可行的
我的楼主可以自己玩一下
试试吧
#define N 200
#include <graphicsh>
#include <stdlibh>
#include <dosh>
#define LEFT 0x4b00
#define RIGHT 0x4d00
#define DOWN 0x5000
#define UP 0x4800
#define ESC 0x011b
int i,key;
int score=0;/得分/
int gamespeed=50000;/游戏速度自己调整/
struct Food
{
int x;/食物的横坐标/
int y;/食物的纵坐标/
int yes;/判断是否要出现食物的变量/
}food;/食物的结构体/
struct Snake
{
int x[N];
int y[N];
int node;/蛇的节数/
int direction;/蛇移动方向/
int life;/ 蛇的生命,0活着,1死亡/
}snake;
void Init(void);/图形驱动/
void Close(void);/图形结束/
void DrawK(void);/开始画面/
void GameOver(void);/结束游戏/
void GamePlay(void);/玩游戏具体过程/
void PrScore(void);/输出成绩/
/主函数/
void main(void)
{
Init();/图形驱动/
DrawK();/开始画面/
GamePlay();/玩游戏具体过程/
Close();/图形结束/
}
/图形驱动/
void Init(void)
{
int gd=DETECT,gm;
initgraph(&gd,&gm,"c:\\tc");
cleardevice();
}
/开始画面,左上角坐标为(50,40),右下角坐标为(610,460)的围墙/
void DrawK(void)
{
/setbkcolor(LIGHTGREEN);/
setcolor(11);
setlinestyle(SOLID_LINE,0,THICK_WIDTH);/设置线型/
for(i=50;i<=600;i+=10)/画围墙/
{
rectangle(i,40,i+10,49); /上边/
rectangle(i,451,i+10,460);/下边/
}
for(i=40;i<=450;i+=10)
{
rectangle(50,i,59,i+10); /左边/
rectangle(601,i,610,i+10);/右边/
}
}
/玩游戏具体过程/
void GamePlay(void)
{
randomize();/随机数发生器/
foodyes=1;/1表示需要出现新食物,0表示已经存在食物/
snakelife=0;/活着/
snakedirection=1;/方向往右/
snakex[0]=100;snakey[0]=100;/蛇头/
snakex[1]=110;snakey[1]=100;
snakenode=2;/节数/
PrScore();/输出得分/
while(1)/可以重复玩游戏,压ESC键结束/
{
while(!kbhit())/在没有按键的情况下,蛇自己移动身体/
{
if(foodyes==1)/需要出现新食物/
{
foodx=rand()%400+60;
foody=rand()%350+60;
while(foodx%10!=0)/食物随机出现后必须让食物能够在整格内,这样才可以让蛇吃到/
foodx++;
while(foody%10!=0)
foody++;
foodyes=0;/画面上有食物了/
}
if(foodyes==0)/画面上有食物了就要显示/
{
setcolor(GREEN);
rectangle(foodx,foody,foodx+10,foody-10);
}
for(i=snakenode-1;i>0;i--)/蛇的每个环节往前移动,也就是贪吃蛇的关键算法/
{
snakex[i]=snakex[i-1];
snakey[i]=snakey[i-1];
}
/1,2,3,4表示右,左,上,下四个方向,通过这个判断来移动蛇头/
switch(snakedirection)
{
case 1:snakex[0]+=10;break;
case 2: snakex[0]-=10;break;
case 3: snakey[0]-=10;break;
case 4: snakey[0]+=10;break;
}
for(i=3;i<snakenode;i++)/从蛇的第四节开始判断是否撞到自己了,因为蛇头为两节,第三节不可能拐过来/
{
if(snakex[i]==snakex[0]&&snakey[i]==snakey[0])
{
GameOver();/显示失败/
snakelife=1;
break;
}
}
if(snakex[0]<55||snakex[0]>595||snakey[0]<55||
snakey[0]>455)/蛇是否撞到墙壁/
{
GameOver();/本次游戏结束/
snakelife=1; /蛇死/
}
if(snakelife==1)/以上两种判断以后,如果蛇死就跳出内循环,重新开始/
break;
if(snakex[0]==foodx&&snakey[0]==foody)/吃到食物以后/
{
setcolor(0);/把画面上的食物东西去掉/
rectangle(foodx,foody,foodx+10,foody-10);
snakex[snakenode]=-20;snakey[snakenode]=-20;
/新的一节先放在看不见的位置,下次循环就取前一节的位置/
snakenode++;/蛇的身体长一节/
foodyes=1;/画面上需要出现新的食物/
score+=10;
PrScore();/输出新得分/
}
setcolor(4);/画出蛇/
for(i=0;i<snakenode;i++)
rectangle(snakex[i],snakey[i],snakex[i]+10,
snakey[i]-10);
delay(gamespeed);
setcolor(0);/用黑色去除蛇的的最后一节/
rectangle(snakex[snakenode-1],snakey[snakenode-1],
snakex[snakenode-1]+10,snakey[snakenode-1]-10);
} /endwhile(!kbhit)/
if(snakelife==1)/如果蛇死就跳出循环/
break;
key=bioskey(0);/接收按键/
if(key==ESC)/按ESC键退出/
break;
else
if(key==UP&&snakedirection!=4)
/判断是否往相反的方向移动/
snakedirection=3;
else
if(key==RIGHT&&snakedirection!=2)
snakedirection=1;
else
if(key==LEFT&&snakedirection!=1)
snakedirection=2;
else
if(key==DOWN&&snakedirection!=3)
snakedirection=4;
}/endwhile(1)/
}
/游戏结束/
void GameOver(void)
{
cleardevice();
PrScore();
setcolor(RED);
settextstyle(0,0,4);
outtextxy(200,200,"GAME OVER");
getch();
}
/输出成绩/
void PrScore(void)
{
char str[10];
setfillstyle(SOLID_FILL,YELLOW);
bar(50,15,220,35);
setcolor(6);
settextstyle(0,0,2);
sprintf(str,"score:%d",score);
outtextxy(55,20,str);
}
/图形结束/
void Close(void)
{
getch();
closegraph();
}
以上就是关于求几C语言个小游戏代码,简单的,要注释、、谢谢了、全部的内容,包括:求几C语言个小游戏代码,简单的,要注释、、谢谢了、、c# 小游戏代码、求C语言小游戏源程序等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)