#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;
}
}
}
缺陷:与边界或蛇身碰撞前瞬间结束游戏
移动的主要实现方式:顺着蛇头移动的方向增加节点(新蛇头),同时删除蛇尾
|