IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> 双向链表实现贪吃蛇 -> 正文阅读

[数据结构与算法]双向链表实现贪吃蛇

#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;
				line* new_head1 = (line*)malloc(sizeof(line));
				new_head1 = board[head->row][head->column + 1];
				new_head1->a = 2;
				new_head1->next = head;
				head->prior = new_head1;
				head = new_head1;				
				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;
				line* new_head1 = (line*)malloc(sizeof(line));
				new_head1 = board[head->row][head->column - 1];
				new_head1->a = 2;
				new_head1->next = head;
				head->prior = new_head1;
				head = new_head1;
				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;
				line* new_head1 = (line*)malloc(sizeof(line));
				new_head1 = board[head->row+1][head->column ];
				new_head1->a = 2;
				new_head1->next = head;
				head->prior = new_head1;
				head = new_head1;
				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;
				line* new_head1 = (line*)malloc(sizeof(line));
				new_head1 = board[head->row - 1][head->column];
				new_head1->a = 2;
				new_head1->next = head;
				head->prior = new_head1;
				head = new_head1;
				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;
		}
	}
}

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

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

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-10-12 23:43:35  更:2021-10-12 23:45:42 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/26 7:43:55-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码