双向链表实现贪吃蛇

缺陷:与边界或蛇身碰撞前瞬间结束游戏

移动的主要实现方式:顺着蛇头移动的方向增加节点(新蛇头),同时删除蛇尾

#define _CRT_SECURE_NO_WARNINGS 1
#define __STDC_WANT_LIB_EXT1_ 1
#pragma warning(disable : 4996)
#include<stdio.h>
#include<string.h>
#include <stdio.h>
#include <conio.h>
#include<time.h>
#include<windows.h>
typedef struct Line
{
	int a;
	struct Line* next;
	struct Line* prior;
	int row;
	int column;
}line;
line* board[20][20];
void ini_board();
void move();
void display();
void food();
line* find_tail(line*);
int judge(int Move, line* head);
int score = 0;
int main()
{
	ini_board();
	display();
	move();
	return 0;
}
void ini_board()//   1-□  2-■  3-★
{
	for (int i = 0; i < 20; i++)
	{
		for (int k = 0; k < 20; k++)
		{
			board[i][k] = (line*)malloc(sizeof(line));
			board[i][k]->a = 1;
			board[i][k]->row = i;
			board[i][k]->column  = k;
		}
	}
	//初始时放一条长度为4的蛇
	line* head = (line*)malloc(sizeof(line));
	head = board[10][10];
	head->a = 2;
	for (int i = 1; i < 4; i++)
	{
		line* body = malloc(sizeof(line));
		body = board[10][10 - i];
		body->a = 2;
		head->next = body;
		body->prior = head;
		head = body;
	}
	head->next = NULL;//此时head指向最后一个,next指向NULL;
}
void display()
{
	system("cls");
	for (int i = 0; i < 20; i++)
	{
		for (int k = 0; k < 20; k++)
		{
			if (board[i][k]->a  == 1)
				printf("□");
			if (board[i][k]->a  == 2)
				printf("■");
			if(board[i][k]->a==3)
				printf("★");
		}
		printf("\n");
	}
	printf("                               玩家: 极简       分数: %d", score);
}
void move()
{
	char Move = 77;//初始化的蛇向右移动
	line* head = (line*)malloc(sizeof(line));
	head = board[10][10];
	food();
	while (1)
	{
		Sleep(200);
		if (kbhit())//上下左右键对应ASCII码值: 72-up 80-down 75-left 77-right
		{
			Move = getch();
		}
		if (Move == 77)
		{
			if (judge(Move, head) == 1)
			{
				continue;				
			}
			if (judge(Move, head) == 2)
			{
				line* new_head = (line*)malloc(sizeof(line));
				new_head = board[head->row][head->column + 1];
				new_head->a = 2;
				new_head->next = head;
				head->prior = new_head;
				head = new_head;
				line* tail = (line*)malloc(sizeof(line));
				tail = find_tail(head);
				tail->a = 1;				
				tail->prior->next = NULL;
				tail->prior = NULL;		
				display();
			}
			if (judge(Move, head) == 3)
			{
				line* new_head = (line*)malloc(sizeof(line));
				new_head = board[head->row][head->column + 1];
				new_head->a = 2;
				new_head->next = head;
				head->prior = new_head;
				head = new_head;
				
				display();
				food();
			}
		}
		if (Move == 75)
		{
			if (judge(Move, head) == 1)
			{
				continue;
			}
			if (judge(Move, head) == 2)
			{
				line* new_head = (line*)malloc(sizeof(line));
				new_head = board[head->row][head->column - 1];
				new_head->a = 2;
				new_head->next = head;
				head->prior = new_head;
				head = new_head;
				line* tail = (line*)malloc(sizeof(line));
				tail = find_tail(head);
				tail->a = 1;
				tail->prior->next = NULL;
				tail->prior = NULL;
				display();				
			}
			if (judge(Move, head) == 3)
			{
				line* new_head = (line*)malloc(sizeof(line));
				new_head = board[head->row][head->column - 1];
				new_head->a = 2;
				new_head->next = head;
				head->prior = new_head;
				head = new_head;
				
				display();
				food();
			}
		}

		if (Move == 80)
		{
			if (judge(Move, head) == 1)
			{
				continue;
			}
			if (judge(Move, head) == 2)
			{
				line* new_head = (line*)malloc(sizeof(line));
				new_head = board[head->row+1][head->column];
				new_head->a = 2;
				new_head->next = head;
				head->prior = new_head;
				head = new_head;
				line* tail = (line*)malloc(sizeof(line));
				tail = find_tail(head);
				tail->a = 1;
				tail->prior->next = NULL;
				tail->prior = NULL;
				display();
			}
			if (judge(Move, head) == 3)
			{
				line* new_head = (line*)malloc(sizeof(line));
				new_head = board[head->row+1][head->column ];
				new_head->a = 2;
				new_head->next = head;
				head->prior = new_head;
				head = new_head;
				
				display();
				food();
			}
		}
		if (Move == 72)
		{
			if (judge(Move, head) == 1)
			{
				continue;
			}
			if (judge(Move, head) == 2)
			{
				line* new_head = (line*)malloc(sizeof(line));
				new_head = board[head->row - 1][head->column];
				new_head->a = 2;
				new_head->next = head;
				head->prior = new_head;
				head = new_head;
				line* tail = (line*)malloc(sizeof(line));
				tail = find_tail(head);
				tail->a = 1;
				tail->prior->next = NULL;
				tail->prior = NULL;
				display();
			}
			if (judge(Move, head) == 3)
			{
				line* new_head = (line*)malloc(sizeof(line));
				new_head = board[head->row - 1][head->column];
				new_head->a = 2;
				new_head->next = head;
				head->prior = new_head;
				head = new_head;
				
				display();
				food();
			}
		}
	}

}
int judge(int Move,line* head)
{
	if (Move == 77)
	{
		if (head->column == 19)
		{
			printf("\n游戏结束!");
			exit(0);
		}
		if (((board[head->row][head->column+1])->a == 2) && (head->next != board[head->row][head->column + 1]))
		{
			printf("\n游戏结束!");
			exit(0);
		}
		if (((board[head->row][head->column + 1])->a  == 2) && (head->next == board[head->row][head->column + 1]))
			return 1;
		if(board[head->row][head->column+1]->a ==1)
			return 2;
		if (board[head->row][head->column + 1]->a  == 3)
			return 3;
	}
	if (Move == 75)
	{
		if (head->column == 0)
		{
			printf("\n游戏结束!");
			exit(0);
		}
		if (((board[head->row][head->column - 1])->a == 2) && (head->next != board[head->row][head->column - 1]))
		{
			printf("\n游戏结束!");
			exit(0);
		}
		if (((board[head->row][head->column - 1])->a == 2) && (head->next == board[head->row][head->column - 1]))
			return 1;
		if (board[head->row][head->column - 1]->a == 1)
			return 2;
		if (board[head->row][head->column - 1]->a == 3)
			return 3;
	}
	if (Move == 72)
	{
		if (head->row == 0)
		{
			printf("\n游戏结束!");
			exit(0);
		}
		if (((board[head->row-1][head->column])->a == 2) && (head->next != board[head->row-1][head->column]))
		{
			printf("\n游戏结束!");
			exit(0);
		}
		if (((board[head->row-1][head->column])->a == 2) && (head->next == board[head->row-1][head->column]))
			return 1;
		if (board[head->row-1][head->column]->a == 1)
			return 2;
		if (board[head->row-1][head->column]->a == 3)
			return 3;
	}
	if (Move == 80)
	{
		if (head->row == 19)
		{
			printf("\n游戏结束!");
			exit(0);
		}
		if (((board[head->row + 1][head->column])->a == 2) && (head->next != board[head->row + 1][head->column]))
		{
			printf("\n游戏结束!");
			exit(0);
		}
		if (((board[head->row + 1][head->column])->a == 2) && (head->next == board[head->row + 1][head->column]))
			return 1;
		if (board[head->row + 1][head->column]->a == 1)
			return 2;
		if (board[head->row + 1][head->column]->a == 3)
			return 3;
	}
}
line* find_tail(line* head)
{
	while (head->next != NULL)
	{
		head = head->next;
	}
	return head;
}
void food()
{
	while (1)
	{
		srand((unsigned)time(NULL));
		int row = rand()%18+1;
		int column = rand()%18+1;
		if (board[row][column]->a == 2)
			continue;
		if (board[row][column]->a == 1)
		{
			board[row][column]->a = 3;
			score = score + 100;
			break;
		}
	}
}

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值