求一个基于mfc或api的c++程序(像贪吃蛇的小游戏之类)

含程序的文件夹,要有完整源文件麻烦发到我邮箱610034980@qq.com... 含程序的文件夹,要有完整源文件
麻烦发到我邮箱610034980@qq.com
展开
 我来答
百度网友3df8970f5
2012-07-01 · TA获得超过918个赞
知道小有建树答主
回答量:1268
采纳率:50%
帮助的人:1072万
展开全部
100块一个
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
guohong932257
2012-07-13
知道答主
回答量:31
采纳率:0%
帮助的人:10.1万
展开全部
lianliankan.h
view plaincopy to clipboardprint?
#ifndef LIANLIANKAN_H_
#define LIANLIANKAN_H_

const int LEN = 8;

struct pos
{
pos(){}
pos(int _x, int _y) : x(_x), y(_y){}
int x;
int y;
};

struct unit
{
unit(){}
pos parent;
int cross;
int distance;
};

class LianLianKan
{
public:
LianLianKan();
~LianLianKan();
void RandomMap();
int Dist(pos &x, pos &y);
int GetMinCrossing(pos &x);
int GetMinDistance(pos &x);
void SetMinCrossing(pos &x, int c);
void SetMinDistance(pos &x, int d);
void SetParent(pos &x, pos p);
void Updata(pos &x, pos &y);
bool Find(pos &p);
void UnitMapInit(pos &p);
bool InUnitMap(pos &p);
void DisplayPath(pos &p);
bool GameProcess(pos& first, pos& second);
void DisplayMap();
void Run();
private:
unit unitmap[LEN + 2][LEN + 2];
};

#endif // LIANLIANKAN_H_
lianliankan.cpp
view plaincopy to clipboardprint?
#include "stdafx.h"
#include "lianliankan.h"
#include <iostream>
#include <stdlib.h>
#include <time.h>

using namespace std;

int map[LEN + 2][LEN + 2] = {
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
};

LianLianKan::LianLianKan()
{
RandomMap();
}

LianLianKan::~LianLianKan()
{

}

void LianLianKan::RandomMap()
{
srand(time(0));
int row;
int col;
bool b = false;
for (int i = 1; i <= 9; i++)
{
while (!b)
{
row = rand() % 8 + 1;
col = rand() % 8 + 1;
if (!map[row][col])
{
map[row][col] = i;
b = 1;
}
}
b = 0;
while (!b)
{
row = rand() % 8 + 1;
col = rand() % 8 + 1;
if (!map[row][col])
{
map[row][col] = i;
b = 1;
}
}
b = 0;
while (!b)
{
row = rand() % 8 + 1;
col = rand() % 8 + 1;
if (!map[row][col])
{
map[row][col] = i;
b = 1;
}
}
b = 0;
while (!b)
{
row = rand() % 8 + 1;
col = rand() % 8 + 1;
if (!map[row][col])
{
map[row][col] = i;
b = 1;
}
}
b = 0;
}
}

int LianLianKan::Dist(pos &x, pos &y)
{
if (x.x == y.x)
{
return abs(x.y - y.y);
}
else return abs(x.x - y.x);
}

int LianLianKan::GetMinCrossing(pos &x)
{
return unitmap[x.x][x.y].cross;
}

int LianLianKan::GetMinDistance(pos &x)
{
return unitmap[x.x][x.y].distance;
}

void LianLianKan::SetMinCrossing(pos &x, int c)
{
unitmap[x.x][x.y].cross = c;
}

void LianLianKan::SetMinDistance(pos &x, int d)
{
unitmap[x.x][x.y].distance = d;
}

void LianLianKan::SetParent(pos &x, pos p)
{
unitmap[x.x][x.y].parent = p;
}

void LianLianKan::Updata(pos &x, pos &y)
{
if (GetMinCrossing(x) + 1 < GetMinCrossing(y) ||
(GetMinCrossing(x) + 1 == GetMinCrossing(y) &&
GetMinDistance(x) + Dist(x, y) < GetMinDistance(y)))
{
SetMinCrossing(y, GetMinCrossing(x) + 1);
SetMinDistance(y, GetMinDistance(x) + Dist(x, y));
SetParent(y, x);
}
}

bool LianLianKan::Find(pos &p)
{
if (p.x >= 0 && p.x <= LEN + 1 && p.y >= 0 && p.y <= LEN + 1)
{
int i = p.x - 1;
while (i >= 0 && !map[i][p.y])
{
Updata(p, pos(i, p.y));
i--;
}
if (i != -1)
{
Updata(p, pos(i, p.y));
}

i = p.x + 1;
while (i <= LEN + 1 && !map[i][p.y])
{
Updata(p, pos(i, p.y));
i++;
}
if (i != LEN + 2)
{
Updata(p, pos(i, p.y));
}

i = p.y - 1;
while (i >= 0 && !map[p.x][i])
{
Updata(p, pos(p.x, i));
i--;
}
if (i != -1)
{
Updata(p, pos(p.x, i));
}

i = p.y + 1;
while (i <= LEN + 1 && !map[p.x][i])
{
Updata(p, pos(p.x, i));
i++;
}
if (i != LEN + 2)
{
Updata(p, pos(p.x, i));
}
return 1;
}
return 0;
}

void LianLianKan::UnitMapInit(pos &p)
{
memset(unitmap, 0, (LEN + 2) * (LEN + 2) * sizeof(unit));
for (int i = 0; i < LEN + 2; i++)
{
for (int j = 0; j < LEN + 2; j++)
{
unitmap[i][j].cross = INT_MAX;
unitmap[i][j].distance = INT_MAX;
}
}
unitmap[p.x][p.y].cross = 0;
unitmap[p.x][p.y].distance = 0;
}

bool LianLianKan::InUnitMap(pos &p)
{
return unitmap[p.x][p.y].cross < INT_MAX;
}

void LianLianKan::DisplayPath(pos &p)
{
int d = unitmap[p.x][p.y].distance;
pos child = p;
pos parent = unitmap[p.x][p.y].parent;
while (d > 0)
{
if (child.x < parent.x)
{
while (child.x != parent.x)
{
cout<<"("<<child.x<<","<<child.y<<") ";
child.x++;
d--;
}
if (d == 0)
{
break;
}
child = parent;
parent = unitmap[child.x][child.y].parent;
}
if (child.x > parent.x)
{
while (child.x != parent.x)
{
cout<<"("<<child.x<<","<<child.y<<") ";
child.x--;
d--;
}
if (d == 0)
{
break;
}
child = parent;
parent = unitmap[child.x][child.y].parent;
}
if (child.y < parent.y)
{
while (child.y != parent.y)
{
cout<<"("<<child.x<<","<<child.y<<") ";
child.y++;
d--;
}
if (d == 0)
{
break;
}
child = parent;
parent = unitmap[child.x][child.y].parent;
}
if (child.y > parent.y)
{
while (child.y != parent.y)
{
cout<<"("<<child.x<<","<<child.y<<") ";
child.y--;
d--;
}
if (d == 0)
{
break;
}
child = parent;
parent = unitmap[child.x][child.y].parent;
}
}
cout<<"("<<parent.x<<","<<parent.y<<") \n";
}

bool LianLianKan::GameProcess(pos& first, pos& second)
{
UnitMapInit(first);
Find(first);
if (InUnitMap(second))
{
DisplayPath(second);
return 1;
}

for (int i = 0; i < LEN + 2; i++)
{
for (int j = 0; j < LEN + 2; j++)
{
if (unitmap[i][j].cross == 1 && !map[i][j])
{
Find(pos(i, j));
}
}
}
if (InUnitMap(second))
{
DisplayPath(second);
return 1;
}

for (int i = 0; i < LEN + 2; i++)
{
for (int j = 0; j < LEN + 2; j++)
{
if (unitmap[i][j].cross == 2 && !map[i][j])
{
Find(pos(i, j));
}
}
}
if (InUnitMap(second))
{
DisplayPath(second);
return 1;
}
return 0;
}

void LianLianKan::DisplayMap()
{
cout<<"*****************\n";
for (int i = 1; i < LEN + 1; i++)
{
for (int j = 1; j < LEN + 1; j++)
{
cout << ' ' << map[i][j];
}
cout << endl;
}
cout<<"*****************\n";
}

void LianLianKan::Run()
{
pos f, s;
DisplayMap();
while (cin>>f.x>>f.y>>s.x>>s.y)
{
if (map[f.x][f.y] != map[s.x][s.y] ||
!map[f.x][f.y] || !map[s.x][s.y])
{
cout<<"Wrong Input!"<<endl;
continue;
}
bool b = GameProcess(f, s);
if (!b)
{
cout<<"Can't do it!"<<endl;
continue;
}
else
{
map[f.x][f.y] = map[s.x][s.y] = 0;
}
system("cls");
DisplayMap();
}
}
main.cpp
view plaincopy to clipboardprint?
// ch1_14_连连看.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "lianliankan.h"

int _tmain(int argc, _TCHAR* argv[])
{
LianLianKan *game = new LianLianKan();
game->Run();
return 0;
}
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式