#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#include<windows.h>
#include<time.h>
#include<conio.h>


#define up 'w'
#define down 's'
#define left 'a'
#define right 'd'

HANDLE hOut;

void welcome();   //欢迎界面
void Finish();     //结束界面
void creattu();   //方格
void gotoxy(int x,int y);
void gotoprint(int x, int y);
void gotodelete(int x, int y);
void creatfood();  //食物小方格
int keyboard();   //输入 w s a d 
int Judge();    //判断是否结束
void Movebody();  //身体移动
void Eat();    //吃后增加一个方格
void newbody(int x,int y);   //打印新身体

int main() {
	system("color 0A");
	welcome();
	creattu();
	creatfood();
	if (keyboard() == 0) return 0;
	return 0;

}
void gotoxy(int x, int y) {     
	COORD pos;
	HANDLE hOutput = GetStdHandle(STD_OUTPUT_HANDLE);
	pos.X = x;
	pos.Y = y;
	SetConsoleCursorPosition(hOutput, pos);
}

char name[21];
int score = 0;
char click;
int speed;

void welcome() {
	gotoxy(15, 10);
	printf("/-------------------------------------------/");
	gotoxy(15, 25);
	printf("/-------------------------------------------/");
	gotoxy(15, 13);
	printf("         welcome to 贪吃蛇");
	gotoxy(20, 18);
	printf("Please enter your name:\n");
	gotoxy(27, 21);
	gets(name);
	system("cls");
}
typedef struct Snakes {
	int x;
	int y;
	struct Snakes* next;
}snake;

snake* head;
struct Food {
	int x;
	int y;
}food;

void gotoprint(int x, int y) {
	gotoxy(x, y);
	printf("■");
}

void gotodelete(int x, int y) {
	gotoxy(x, y);
	printf("  ");
}

void creattu() {
	for (int i = 0; i < 58; i += 2) {
		gotoprint(i, 0);
		gotoprint(i, 26);
	}
	for (int j = 1; j < 26; j++) {
		gotoprint(0, j);
		gotoprint(56, j);
	}
	gotoxy(63, 10);
	printf("Hello %s,欢迎来到贪吃蛇", name);
	gotoxy(63, 15);
	printf("你的分数是: %d  ", score);
	gotoxy(63, 20);
	printf("This game was created by X,F,H");

	head = (snake*)malloc(sizeof(snake));
	snake* p = (snake*)malloc(sizeof(snake));
	snake* q = (snake*)malloc(sizeof(snake));
	head->x = 16;
	head->y = 15;
	p->x = 16;
	p->y = 16;
	q->x = 16;
	q->y = 17;

	head->next = p;
	p->next = q;
	q->next = NULL;
	
}

void creatfood() {
	int flag = 0;
	while (!flag) {
		flag = 1;
		srand((int)time(NULL));
		food.y = rand() % 25+1;
		food.x = rand() % 54+2;
		if (food.x % 2 != 0) {
			food.x = food.x + 1;
		}
		snake* judge = head;
		while(1) {
			if (judge->next == NULL) break;
			if (food.x == judge->x && food.y == judge->y) flag = 0;
			judge = judge->next;
		}

	}
	gotoxy(food.x, food.y);
	printf("■");

}

void Finish() {
	system("cls");
	gotoxy(15, 10);
	printf("/------------------------------------------------/");
	gotoxy(15, 20);
	printf("/------------------------------------------------/");
	gotoxy(18, 14);
	printf("Game over!");
	gotoxy(20, 16);
	printf("你的分数是 : %d ", score);
	gotoxy(18, 18);
	printf("还不错哦!");
	
	system("pause");
}
int Judge() {  //判断是否结束
	if (head->x == 0 || head->x == 56 || head->y == 0 || head->y == 26) {
		Finish();
		return 0;
	}
	snake* p = head->next;
	while (1) {
		if (p == NULL) break;
		if (head->x == p->x && head->y == p->y) {
			Finish();
			return 0;
		}
		p = p->next;
	}
	return 1;
}
void newbody(int x, int y) {  //吃一个食物,更新身体
	snake* p = head;
	while (p->next->next != NULL) {
		p = p->next;
	}
	free(p->next);
	p->next = NULL;
	snake* new_head = (snake*)malloc(sizeof(snake));
	new_head->x = x;
	new_head->y = y;
	new_head->next = head;
	head = new_head;

}
int keyboard() {
	//char c;
	while (1) {
		if (Judge() == 0) return 0;
		if (_kbhit()) {                //_kbhit() ,_getch()
			click = _getch();

		}
		Movebody();
		Eat();
	}
	return 1;
}
void Movebody() {
	int x = head->x;
	int y = head->y;
	snake* p = head;
	while (p->next != NULL) {
		p = p->next;
	}
	gotodelete(p->x, p->y);
	switch (click) {
	case up:
		y = y - 1; break;
	case down:
		y = y + 1; break;
	case left:
		x = x - 2; break;
	case right:
		x = x + 2; break;
	default:
		break;
	}
	if (x != head->x || y != head->y) {
		newbody(x, y);
	}
	p = head;
	gotoprint(p->x, p->y);

	int count = score / 10;
	if (count <= 10) speed = 200;
	else if (count > 10 && count <= 20) speed = 100;
	else if (count > 20 && count <= 40) speed = 50;
	else speed = 10;
	Sleep(speed);
}
void Eat() {
	if (head->x == food.x && head->y == food.y) {
		creatfood();
		snake* _new = (snake*)malloc(sizeof(snake));
		snake* p;
		p = head;
		while (1) {
			if (p->next == NULL)break;
			p = p->next;
		}
		p->next = _new;
		_new->next = NULL;
		score += 10;
		gotoxy(77, 15);
		printf(" %d ", score);
	}
}