通过动态链表实现
自定义蛇身长度,速度,地图大小,规则
蛇自动吃苹果时:
无视蛇吃自己死亡规则分数可刷到无限大(蛇可叠加),加上蛇吃自己平均分15分左右
测试版,待改善
以下是演示效果
动态链表贪吃蛇,实现自动追踪食物,分可刷到无限大
规则控制内容
#define LEN 5//初始长度
int speed = 0;//初始速度
int autotexta = 1;//蛇吃后是否增长,1为增长
int autotextb = 0;//判定蛇吃自己开关,1为打开
/*刷测试模块(及游戏结束后立刻重新开始)*/
int texta = 1;//刷测试次数开关,1为开
int times = 100;//刷几次
int limit = 0;//得分最高限制,仅限于刷次数时有效,0为无限次
int pause = 0;//刷测试时结束暂停
/*自动控制模块*/
int autoall = 1;//自动控制总开关
int auto1 = 0;
int auto2 = 0;
移动蛇模块
void movesnake() {
if (_kbhit()) {
fflush(stdin);
ch = _getch();
}
struct snake* lsd = tail;
if (change) {
gotoxy(lsd->x, lsd->y);
color(blue);
printf("■");
}
// if (scor >= 1) {
// gotoxy(0, 0);
// color(blue);
// printf("□");
// }
struct snake* lsa = lsd;
struct snake* lsb = lsd->last;
for (int i = len - 1; i > 0; i--) {
lsa->x = lsb->x;
lsa->y = lsb->y;
lsa = lsa->last;
lsb = lsb->last;
}
switch (ch) {
case 'w':
case 'W': {head->y--; break; }
case'S':
case's': {head->y++; break; }
case'D':
case'd': {head->x++; break; }
case'a':
case'A': {head->x--; break; }
}
gotoxy(head->x, head->y);
color(yellow);
printf("¤");
struct snake* lsc = head->next;
gotoxy(lsc->x, lsc->y);
color(yellow);
printf("★");
change = 1;
gotoxy(MAP, MAP-1);
}
控制蛇自动吃食物且保证蛇不碰壁或不吃自己函数
automove1为基础模块(必须)
automove2,3为带改善模块(可关掉),多次试验发现打开2与3后蛇吃自己平均分没有多大改善
void automove3() {
struct snake* lsa = head->next;
if (head->x - 1 == lsa->x && head->x + 1 == MAP)
ch = 'w';
if (head->x + 1 == lsa->x && head->x - 1 == 0)
ch = 's';
if (head->y + 1 == lsa->y && head->y - 1 == 0)
ch = 'a';
if (head->y - 1 == lsa->y && head->y + 1 == MAP)
ch = 'd';
}
void automove2() {
struct snake* lsa = head->next;
struct snake* lsb = lsa->next;
for (int i = 1; i < len - 2; i++) {
if (head->x - 1 == lsa->x) {
l = 0;
if (head->x + 1 == lsb->x && head->y == lsb->y) {
r = 0;
}
else r = 1;
if (head->y - 1 == lsb->y && head->x == lsb->x) {
u = 0;
}
else u = 1;
if (head->y + 1 == lsb->y && head->x == lsb->x) {
d = 0;
}
else d = 1;
if (r == 0 || u == 0 || d == 0)
break;
}
if (head->x + 1 == lsa->x) {
r = 0;
if (head->x - 1 == lsb->x && head->y == lsb->y) {
l = 0;
}
else l = 1;
if (head->y - 1 == lsb->y && head->x == lsb->x) {
u = 0;
}
else u = 1;
if (head->y + 1 == lsb->y && head->x == lsb->x) {
d = 0;
}
else d = 1;
if (l == 0 || u == 0 || d == 0)
break;
}
if (head->y - 1 == lsa->y) {
u = 0;
if (head->x + 1 == lsb->x && head->y == lsb->y) {
r = 0;
}
else r = 1;
if (head->x - 1 == lsb->x && head->y == lsb->y) {
l = 0;
}
else l = 1;
if (head->y + 1 == lsb->y && head->x == lsb->x) {
d = 0;
}
else d = 1;
if (l == 0 || r == 0 || d == 0)
break;
}
if (head->y + 1 == lsa->y) {
d = 0;
if (head->x + 1 == lsb->x && head->y == lsb->y) {
r = 0;
}
else r = 1;
if (head->x - 1 == lsb->x && head->y == lsb->y) {
l = 0;
}
else l = 1;
if (head->y - 1 == lsb->y && head->x == lsb->x) {
u = 0;
}
else u = 1;
if (l == 0 || r == 0 || u == 0)
break;
}
lsb = lsb->next;
}
if (l == 0 && r == 0 && d == 0)
ch = 'w';
if (l == 0 && r == 0 && u == 0)
ch = 's';
if (r == 0 && u == 0 && d == 0)
ch = 'a';
if (l == 0 && u == 0 && d == 0)
ch = 'd';
//还有lrud其中有两个为0的情况,其中有八种情况
}
void automove1() {
struct snake* lsa = head->next;
if (head->x - 1 != lsa->x|| head->x + 1 != lsa->x) {
if (head->y > appley&&u==1)
ch = 'w';
else if (head->y < appley&&d==1)
ch = 's';
}
if (head->y - 1 != lsa->y || head->y + 1 != lsa->y) {
if (head->x > applex&&l==1)
ch = 'a';
else if (head->x < applex&&r==1)
ch = 'd';
}
}
void automove() {
automove1();
if(auto1)
automove2();
if (auto2)
automove3();
}
全文
#include<conio.h>
#include<stdlib.h>
#include<stdio.h>
#include<Windows.h>
#include<time.h>
#include<malloc.h>
#include<math.h>
#define red 12
#define blue 9
#define yellow 14
#define MAP 30
#define applex apple[0]
#define appley apple[1]
#define LEN 5
int speed = 0;
int autotexta = 1;//蛇吃后是否增长,1为增长
int autotextb = 0;//判定蛇吃自己开关,1为打开
/*刷测试模块*/
int texta = 1;//刷测试次数开关,1为开
int times = 100;//刷几次
int limit = 0;//得分最高限制,仅限于刷次数时有效,0为无限次
int pause = 0;//刷测试时结束暂停
/*自动控制模块*/
int autoall = 1;//自动控制总开关
int auto1 = 0;
int auto2 = 0;
struct snake {
int x;
int y;
struct snake* next;
struct snake* last;
};
struct snake* p1 = (struct snake*)malloc(sizeof(struct snake));
struct snake* p2 = p1;
struct snake* head = p1;
struct snake* tail;
int apple[2];
int len = LEN;
int **wall= (int**)malloc(sizeof(int*) * MAP);
void basic() {
for (int i = 0; i < MAP; i++) {
wall[i]= (int*)malloc(sizeof(int) * MAP);
}
}
char ch = 'w';
int change = 1;
int scor = 0;
void gotoxy(int x, int y)//位置函数
{
COORD pos;
pos.X = 2 * x;
pos.Y = y;
SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), pos);
}
void color(int a)//颜色函数
{
SetConsoleTextAttribute(GetStdHandle(STD_OUTPUT_HANDLE), a);
}
void wall_info() {
for (int q = 0; q < MAP; q++) {
for (int w = 0; w < MAP; w++) {
wall[q][w] = 1;
}
}
for (int t = 0; t < MAP; t++) {
wall[0][t] = 0;
wall[MAP - 1][t] = 0;
wall[t][0] = 0;
wall[t][MAP - 1] = 0;
}
for (int j = 0; j < MAP; j++) {
for (int k = 0; k < MAP; k++) {
if (wall[j][k]) {
gotoxy(j, k);
color(blue);
printf("■");
}
else {
gotoxy(j, k);
color(blue);
printf("□");
}
}
}
}
void snake_apple() {
for (int i = 0; i < LEN - 1; i++) {
p1 = (struct snake*)malloc(sizeof(struct snake));
p2->next = p1;
p1->last = p2;
p2 = p1;
}
tail = p1;
srand((unsigned int)time(NULL));
head->x = (MAP - 1) / 2;
head->y = (MAP - 1) / 2;
struct snake* lsa = head->next, * lsb = head;
for (int i = 1; i < len; i++) {
lsa->x = lsb->x;
lsa->y = lsb->y + 1;
gotoxy(lsa->x, lsb->y);
color(yellow);
printf("★");
lsa = lsa->next;
lsb = lsb->next;
}
gotoxy(head->x, head->y);
color(yellow);
printf("¤");
int flag = 0;
while (1) {
struct snake* lsa = head;
applex = rand() % (MAP - 2) + 1;
appley = rand() % (MAP - 2) + 1;
for (int j = 0; j < len; j++) {
if (lsa->x == applex && lsa->y == appley) {
flag = 1;
break;
}
lsa = lsa->next;
}
if (!flag) break;
}
gotoxy(applex, appley);
color(red);
printf("●");
}
void movesnake() {
if (_kbhit()) {
fflush(stdin);
ch = _getch();
}
struct snake* lsd = tail;
if (change) {
gotoxy(lsd->x, lsd->y);
color(blue);
printf("■");
}
// if (scor >= 1) {
// gotoxy(0, 0);
// color(blue);
// printf("□");
// }
struct snake* lsa = lsd;
struct snake* lsb = lsd->last;
for (int i = len - 1; i > 0; i--) {
lsa->x = lsb->x;
lsa->y = lsb->y;
lsa = lsa->last;
lsb = lsb->last;
}
switch (ch) {
case 'w':
case 'W': {head->y--; break; }
case'S':
case's': {head->y++; break; }
case'D':
case'd': {head->x++; break; }
case'a':
case'A': {head->x--; break; }
}
gotoxy(head->x, head->y);
color(yellow);
printf("¤");
struct snake* lsc = head->next;
gotoxy(lsc->x, lsc->y);
color(yellow);
printf("★");
change = 1;
gotoxy(MAP, MAP-1);
}
void eat_apple() {
if (head->x == applex && head->y == appley) {
if (autotexta) {
len++;
p1 = (struct snake*)malloc(sizeof(struct snake));
p2->next = p1;
p1->last = p2;
p2 = p1;
tail = p1;
}
scor++;
int flag = 0;
if (autoall) {
int i = 0;
while (1) {
applex = rand() % (MAP - 2) + 1;
appley = rand() % (MAP - 2) + 1;
if (applex != head->x && appley != head->y)
i = 1;
if (i)
break;
}
}
else {
while (1) {
struct snake* lsa = head;
applex = rand() % (MAP - 2) + 1;
appley = rand() % (MAP - 2) + 1;
for (int j = 0; j < len - 1; j++) {
if (lsa->x == applex && lsa->y == appley) {
flag = 1;
break;
}
lsa = lsa->next;
}
if (!flag) break;
}
}
gotoxy(applex, appley);
color(red);
printf("●");
change = 1;
}
}
int if_wall() {
if (head->x == 0 || head->x == MAP - 1 || head->y == 0 || head->y == MAP - 1) {
return 1;
}
if (autotextb) {
struct snake* lsa = head->next;
for (int i = 1; i < len-1; i++) {
if (head->x == lsa->x && head->y == lsa->y) {
return 1;
break;
}
lsa = lsa->next;
}
}
return 0;
}
int l = 1, r = 1, u = 1, d = 1;
void automove3() {
struct snake* lsa = head->next;
if (head->x - 1 == lsa->x && head->x + 1 == MAP)
ch = 'w';
if (head->x + 1 == lsa->x && head->x - 1 == 0)
ch = 's';
if (head->y + 1 == lsa->y && head->y - 1 == 0)
ch = 'a';
if (head->y - 1 == lsa->y && head->y + 1 == MAP)
ch = 'd';
}
void automove2() {
struct snake* lsa = head->next;
struct snake* lsb = lsa->next;
for (int i = 1; i < len - 2; i++) {
if (head->x - 1 == lsa->x) {
l = 0;
if (head->x + 1 == lsb->x && head->y == lsb->y) {
r = 0;
}
else r = 1;
if (head->y - 1 == lsb->y && head->x == lsb->x) {
u = 0;
}
else u = 1;
if (head->y + 1 == lsb->y && head->x == lsb->x) {
d = 0;
}
else d = 1;
if (r == 0 || u == 0 || d == 0)
break;
}
if (head->x + 1 == lsa->x) {
r = 0;
if (head->x - 1 == lsb->x && head->y == lsb->y) {
l = 0;
}
else l = 1;
if (head->y - 1 == lsb->y && head->x == lsb->x) {
u = 0;
}
else u = 1;
if (head->y + 1 == lsb->y && head->x == lsb->x) {
d = 0;
}
else d = 1;
if (l == 0 || u == 0 || d == 0)
break;
}
if (head->y - 1 == lsa->y) {
u = 0;
if (head->x + 1 == lsb->x && head->y == lsb->y) {
r = 0;
}
else r = 1;
if (head->x - 1 == lsb->x && head->y == lsb->y) {
l = 0;
}
else l = 1;
if (head->y + 1 == lsb->y && head->x == lsb->x) {
d = 0;
}
else d = 1;
if (l == 0 || r == 0 || d == 0)
break;
}
if (head->y + 1 == lsa->y) {
d = 0;
if (head->x + 1 == lsb->x && head->y == lsb->y) {
r = 0;
}
else r = 1;
if (head->x - 1 == lsb->x && head->y == lsb->y) {
l = 0;
}
else l = 1;
if (head->y - 1 == lsb->y && head->x == lsb->x) {
u = 0;
}
else u = 1;
if (l == 0 || r == 0 || u == 0)
break;
}
lsb = lsb->next;
}
if (l == 0 && r == 0 && d == 0)
ch = 'w';
if (l == 0 && r == 0 && u == 0)
ch = 's';
if (r == 0 && u == 0 && d == 0)
ch = 'a';
if (l == 0 && u == 0 && d == 0)
ch = 'd';
//还有lrud其中有两个为0的情况,其中有八种情况
}
void automove1() {
struct snake* lsa = head->next;
if (head->x - 1 != lsa->x|| head->x + 1 != lsa->x) {
if (head->y > appley&&u==1)
ch = 'w';
else if (head->y < appley&&d==1)
ch = 's';
}
if (head->y - 1 != lsa->y || head->y + 1 != lsa->y) {
if (head->x > applex&&l==1)
ch = 'a';
else if (head->x < applex&&r==1)
ch = 'd';
}
}
void automove() {
automove1();
if(auto1)
automove2();
if (auto2)
automove3();
}
int main() {
basic();
if (!texta) {
wall_info();
snake_apple();
while (1) {
movesnake();
Sleep(speed);
eat_apple();
if (autoall) {
automove();
}
if (if_wall()) {
break;
}
gotoxy(MAP+2, 10);
color(yellow);
printf("得分%4d分", scor);
gotoxy(0, 0);
}
gotoxy(MAP / 2, MAP / 2);
color(red);
printf("游戏结束,得分%d", scor);
}
if (texta) {
int line = 0,lie=0;
struct scors
{
int x;
struct scors* next;
};
struct scors* p3, * p4, * hind;
hind = p3 = p4 = (struct scors*)malloc(sizeof(struct scors));
int i = 0;
while (i < times) {
if (i % 25 == 0) {
line++;
lie = 0;
}
lie++;
wall_info();
snake_apple();
gotoxy(MAP+2, 1);
color(yellow);
printf("第 %d 次测验,总共有 %d 次", i + 1, times);
if (i != 0) {
gotoxy(MAP+2, lie + 2);
color(yellow);
printf("第%d次测试成绩%d", i ,scor );
}
scor = 0;
while (1) {
movesnake();
Sleep(speed);
eat_apple();
if (autoall) {
automove();
}
if (if_wall()) {
break;
}
gotoxy(MAP+2, 2);
color(yellow);
printf("得分%.4d分", scor);
gotoxy(0, 0);
if (limit != 0) {
if (scor > limit)
break;
}
}
gotoxy(applex, appley);
color(red);
printf("●");
if(pause)
system("pause");
p3->x = scor;
p3= (struct scors*)malloc(sizeof(struct scors));
p4->next = p3;
p4 = p3;
struct snake* end = tail->last;
for (int i = 2; i < len; i++) {
free(end->next);
end = end->last;
}
p1 = p2 = head;
len = LEN;
i++;
}
p4->next = NULL;
int all = 0;
while (hind->next != NULL) {
all += hind->x;
hind = hind->next;
}
gotoxy(MAP+2, lie + 2);
color(yellow);
printf("平均成绩 %d", all / times);
}
system("pause");
}