標(biāo)題:
A*尋路測試用貪吃蛇c++程序
[打印本頁]
作者:
51hei單片
時(shí)間:
2016-3-13 00:18
標(biāo)題:
A*尋路測試用貪吃蛇c++程序
#include "stdio.h"
#include "stdlib.h"
struct she {
// 貪吃蛇節(jié)點(diǎn)
int fx; // 貪吃蛇當(dāng)前節(jié)點(diǎn)的運(yùn)動(dòng)方向
struct she *next; // 下一個(gè)節(jié)點(diǎn)
};
class tanchishe {
// 貪吃蛇類
public:
tanchishe(); // 構(gòu)造函數(shù)創(chuàng)建并初始化貪吃蛇
void chi(int fx); // 吃并按方向增加一節(jié)長度
void U(); // 向上運(yùn)動(dòng)一步
void D(); // 向下運(yùn)動(dòng)一步
void L(); // 向左運(yùn)動(dòng)一步
void R(); // 向右運(yùn)動(dòng)一步
void print(); // 打印函數(shù)
// private:
struct she *tou = NULL; // 蛇頭
};
tanchishe::tanchishe() {
// 構(gòu)造函數(shù)
tou = (she *) malloc(sizeof(she));
if (tou == NULL) {
printf("貪吃蛇創(chuàng)建失��!");
}
tou->next = NULL;
tou->fx = 4; // 1—上 2—下 3—左
// 4—右
// 頭節(jié)點(diǎn)初始化為4
}
void tanchishe::chi(int fx) {
// 吃一節(jié)
struct she *node = (she *) malloc(sizeof(she));
if (node == NULL) {
printf("添加新節(jié)失��!");
}
node->fx = fx;
node->next = tou;
tou = node;
}
void tanchishe::U() {
// 向上運(yùn)動(dòng)一步
struct she *p;
struct she *q;
int a, b;
p = tou;
q = tou->next;
a = p->fx;
while (q != NULL) {
b = q->fx;
q->fx = a;
a = b;
p = p->next;
q = q->next;
}
tou->fx=1;
}
void tanchishe::D() {
// 向下運(yùn)動(dòng)一步
struct she *p;
struct she *q;
int a, b;
p = tou;
q = tou->next;
a = p->fx;
while (q != NULL) {
b = q->fx;
q->fx = a;
a = b;
p = p->next;
q = q->next;
}
tou->fx=2;
}
void tanchishe::L() {
// 向左運(yùn)動(dòng)一步
struct she *p;
struct she *q;
int a, b;
p = tou;
q = tou->next;
a = p->fx;
while (q != NULL) {
b = q->fx;
q->fx = a;
a = b;
p = p->next;
q = q->next;
}
tou->fx=3;
}
void tanchishe::R() {
// 向右運(yùn)動(dòng)一步
struct she *p;
struct she *q;
int a, b;
p = tou;
q = tou->next;
a = p->fx;
while (q != NULL) {
b = q->fx;
q->fx = a;
a = b;
p = p->next;
q = q->next;
}
tou->fx=4;
}
void tanchishe::print() {
// 打印函數(shù)
}
#include "tanchishe.h"
#include<conio.h>.
#include<time.h>
class ditu {
public:
ditu(); // 初始化
void shiwu(); // 隨機(jī)生成一個(gè)食物
void print(); // 打印地圖
void U(); // 向上一步
void D(); // 向下一步
void L(); // 向左一步
void R(); // 向右一步
// private:
int she_X, she_Y; // 蛇在地圖上的坐標(biāo)
int shiwu_X, shiwu_Y; // 食物在地圖上的坐標(biāo)
int dt[20][20]; // 地圖數(shù)組
tanchishe she; // 貪吃蛇對(duì)象
};
ditu::ditu() {
she_X = 0;
she_Y = 1;
shiwu();
}
void ditu::shiwu() {
srand(time(0));
int B = 1;
while (B) {
shiwu_Y = rand() % 18 + 1;
shiwu_X = rand() % 18 + 1;
if (dt[shiwu_Y][shiwu_X] == 0)
B = 0;
}
dt[shiwu_Y][shiwu_X] = 6;
}
void ditu::U() {
if (dt[she_Y - 1][she_X] == 0 && ((she_Y - 1) >= 0)) {
she_Y--;
she.U();
} else if (dt[she_Y - 1][she_X] == 6 && ((she_Y - 1) >= 0)) {
she_Y--;
she.chi(1);
shiwu();
}
}
void ditu::D() {
if (dt[she_Y + 1][she_X] == 0 && ((she_Y + 1) < 20)) {
she_Y++;
she.D();
} else if (dt[she_Y + 1][she_X] == 6 && ((she_Y + 1) < 20)) {
she_Y++;
she.chi(2);
shiwu();
}
}
void ditu::L() {
if (dt[she_Y][she_X - 1] == 0 && ((she_X - 1) >= 0)) {
she_X--;
she.L();
} else if (dt[she_Y][she_X - 1] == 6 && ((she_X - 1) >= 0)) {
she_X--;
she.chi(3);
shiwu();
}
}
void ditu::R() {
if (dt[she_Y][she_X + 1] == 0 && ((she_X + 1) < 20)) {
she_X++;
she.R();
} else if (dt[she_Y][she_X + 1] == 6 && ((she_X + 1) < 20)) {
she_X++;
she.chi(4);
shiwu();
}
}
void ditu::print() {
// 打印函數(shù)
int x = she_X;
int y = she_Y;
for (int i = 0; i < 20; i++)
for (int j = 0; j < 20; j++)
dt[i][j] = 0;
dt[shiwu_Y][shiwu_X] = 6;
struct she *p = she.tou;
dt[y][x] = 88; // 蛇頭
if (p->fx == 1)
y++;
else if (p->fx == 2)
y--;
else if (p->fx == 3)
x++;
else if (p->fx == 4)
x--;
p = p->next;
while (p != NULL) {
dt[y][x] = 8; // 蛇身
if (p->fx == 1)
y++;
else if (p->fx == 2)
y--;
else if (p->fx == 3)
x++;
else if (p->fx == 4)
x--;
p = p->next;
}
for (int i = 0; i < 20; i++) {
for (int j = 0; j < 20; j++) {
if (dt[i][j] == 8)
printf("o");
else if (dt[i][j] == 88)
printf("@");
else if (dt[i][j] == 6)
printf("X");
else
printf(" ");
}
printf("\n");
}
}
復(fù)制代碼
歡迎光臨 (http://www.torrancerestoration.com/bbs/)
Powered by Discuz! X3.1