C語言手把手教你實現(xiàn)貪吃蛇AI(上)
本文實例為大家分享了手把手教你實現(xiàn)貪吃蛇AI的具體步驟,供大家參考,具體內(nèi)容如下
1. 目標
編寫一個貪吃蛇AI,也就是自動繞過障礙,去尋找最優(yōu)路徑吃食物。
2. 問題分析
為了達到這一目的,其實很容易,總共只需要兩步,第一步抓一條蛇,第二步給蛇裝一個腦子。具體來說就是,首先我們需要有一條普通的貪吃蛇,也就是我們常玩兒的,手動控制去吃食物的貪吃蛇;然后給這條蛇加入AI,也就是通過算法控制,告訴蛇怎么最方便的繞開障礙去吃食物。為了講清楚這個問題,文章將分為三部分:上,寫一個貪吃蛇程序;中,算法基礎(需要運用到什么算法);下,運用算法基礎中的算法編寫一個貪吃蛇AI。
在動手寫貪吃蛇之前,我們需要想清楚以下幾個問題,就非常容易了:
a. 蛇身。由于蛇在吃食物的過程中會不斷的長大,所以很適合用單鏈表表示,并且吃食物的過程就是用頭插法插入元素的過程
b. 食物。食物直接用隨機生成函數(shù),隨機生成食物,但是需要檢查,所生成的食物的位置不可以和蛇身重合
c. 顯示。我們需要實時的顯示出蛇身的移動,但事實上,我們不用每次都打印整個蛇身,因為蛇身每走一步,僅僅是蛇頭和蛇尾的位置移動一格,其他的地方都沒有變化,所以只需要打印一個新的蛇頭,并把蛇尾的位置抹掉,那么視覺效果就是蛇身先前移動了一格,這個過程中,我們需要用到SetConsoleCursorPosition(),將光標移到到指定的位置(比如蛇尾),完成相應的操作(比如打印空格抹掉蛇尾)
d.控制。我們需要用鍵盤來控制蛇身的移動,這個程序中是利用上下左右方向鍵來實現(xiàn)的,這里需要用到GetAsyncKeyState(),來實時監(jiān)測按鍵的狀態(tài)
3. 運行效果
4. 源代碼
總共由三個文件組成gluttonous.h,source.c & main.cpp。由于這個貪吃蛇是用于后面加AI,所以并沒有加入一些錯誤檢測,比如是否撞到邊界,是否撞到蛇身等。
需要注意的是,這個程序中用到了比較特殊的字符('■')來表示游戲空間的邊界,在VS2013中可以正常編譯,但是在codeblock中會亂碼。
另外還有一點容易混淆的是,我們通常都是用(x,y)坐標表示第x行,第y列,但是在SetConsoleCursorPosition(x,y)中,表示把光標移動到第y行,第x列
4.1 gluttonous.h
#ifndef SNAKE_H_ #define SNAKE_H_ #include<stdio.h> #include<Windows.h> //SetConsoleCursorPosition, sleep函數(shù)的頭函數(shù) #include<time.h> //time()的頭函數(shù) #include<malloc.h> //malloc()的頭函數(shù) #define N 32 //地圖大小 #define snake_mark '#'//表示蛇身 #define food_mark '$' #define sleeptime 500 /*表示蛇身坐標的結(jié)構(gòu)體*/ typedef struct SNAKE{ int x; //行坐標 int y; //列坐標 struct SNAKE* next; }snake_body, *psnake; extern psnake food; typedef enum Direction{ U,D,L,R} direction;//蛇頭的朝向 extern direction snake_direction; void set_cursor_position(int x, int y); void initial_map(); psnake initial_snake(); void create_food(psnake snake,psnake food); void printe_map(psnake snake, psnake food); int is_food(psnake snake_head, psnake food); int is_boundary(psnake snake_head, psnake food); int is_snakebody(psnake snake_head, psnake food); psnake snake_move(psnake sanke, psnake food); void control_snake(); #endif
4.2 source.cpp
#include"gluttonous.h" void set_cursor_position(int x, int y) { COORD coord = { x, y };//x表示列,y表示行。 SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), coord); } /*初始化后的地圖為 N列 N/2行*/ /*游戲的空間為2至N+1列,1至N/2行*/ void initial_map() { int i = 0; //打印上下邊框(每個■占用一行兩列) for (i = 0; i<N/2+2; i++) { set_cursor_position(2*i, 0); printf("■"); set_cursor_position(2*i, N/2+1); printf("■"); } for (i = 0; i<N/2+2; i++) //打印左右邊框 { set_cursor_position(0, i); printf("■"); set_cursor_position(N+2, i); printf("■"); } } /*初始化蛇身*/ /*蛇身初始化坐標為(5,8),(4,8), (3,8) */ psnake initial_snake() { int i=5;//列 int j = N / 4;//行 psnake snake = NULL, tsnake = NULL, temp = NULL; snake = (psnake)malloc(sizeof(snake_body)); (snake)->x = i; (snake)->y = j; (snake)->next = NULL; tsnake = snake; for (i = 4; i >2; i--) { temp = (psnake)malloc(sizeof(snake_body)); (temp)->x = i; (temp)->y = j; (temp)->next = NULL; (tsnake)->next = (temp); (tsnake) = (tsnake)->next; } return snake; } void create_food(psnake snake, psnake food) { static int i=1; psnake head = snake; srand((unsigned)time(NULL)); food->x = rand() % N + 2; food->y = rand() % (N/2) + 1; //檢查食物是否和蛇身重回 while (head) { if (head->x == food->x && head->y == food->y) { free(food); food = NULL; create_food(snake,food); } else { head = head->next; } } } void printe_map(psnake snake, psnake food) { psnake temp=snake; while (temp) { set_cursor_position(temp->x, temp->y); printf("%c",snake_mark); temp = temp->next; } if (food) set_cursor_position(food->x,food->y ); printf("%c",food_mark); set_cursor_position(0, N/2+2); } //判斷是否吃到食物,吃到食物返回 1,否則返回 0; int is_food(psnake snake_head, psnake food) { if (snake_head->x == food->x && snake_head->y == food->y) return 1; return 0; } //判斷是否撞到墻,撞到墻返回 1,否則返回 0; int is_boundary(psnake snake_head) { if (snake_head->y <= 0 || snake_head->y >= N / 2 + 1 || snake_head->x <= 1 || snake_head->x >= N + 1) return 1; return 0; } //判斷是否撞到自己,撞到自己返回 1,否則返回 0; int is_snakebody(psnake snake_head) { psnake temp=snake_head->next; while (temp) { if (snake_head->x == temp->x && snake_head->y == temp->y) return 1; else temp = temp->next; } return 0; } //將蛇身移動到合適的位置,并打印出來 psnake snake_move(psnake snake, psnake food) { psnake snake_head = (psnake)malloc(sizeof(snake_body)); if (snake_direction == U) { snake_head->y = snake->y-1; snake_head->x = snake->x; snake_head->next = snake; } else if (snake_direction == D) { snake_head->y = snake->y + 1; snake_head->x = snake->x; snake_head->next = snake; } else if (snake_direction == L) { snake_head->y = snake->y; snake_head->x = snake->x - 1; snake_head->next = snake; } else if (snake_direction == R) { snake_head->y = snake->y; snake_head->x = snake->x + 1; snake_head->next = snake; } if (is_food(snake_head, food))//如果是食物 { create_food(snake_head, food); printe_map(snake_head, food); } else if (is_boundary(snake_head) == 0 && is_snakebody(snake_head) == 0)//不是食物,不是邊界,也不是蛇身 { psnake temp = snake_head; while (temp->next->next)//尋找蛇尾 { temp = temp->next; } set_cursor_position(temp->next->x, temp->next->y); printf(" ");//把蛇尾用空格消掉 free(temp->next);//釋放蛇尾的內(nèi)存空間 temp->next = NULL;//將temp的next置成NULL printe_map(snake_head, food); } else { free(snake_head); snake_head = NULL; } return snake_head; } void control_snake() { if (GetAsyncKeyState(VK_UP) && snake_direction != D) { snake_direction = U; } else if (GetAsyncKeyState(VK_DOWN) && snake_direction != U) { snake_direction = D; } else if (GetAsyncKeyState(VK_LEFT) && snake_direction != R) { snake_direction = L; } else if (GetAsyncKeyState(VK_RIGHT) && snake_direction != L) { snake_direction = R; } }
4.3 main.cpp
#include"gluttonous.h" direction snake_direction; psnake food; int main(void) { psnake snake; initial_map(); snake=initial_snake(); food = (psnake)malloc(sizeof(snake_body)); food->next = NULL; create_food(snake, food); printe_map(snake, food); snake_direction = R; while (1) { Sleep(sleeptime); control_snake(); snake=snake_move(snake, food); } return 0; }
以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。
相關文章
C++實現(xiàn)LeetCode(23.合并k個有序鏈表)
這篇文章主要介紹了C++實現(xiàn)LeetCode(23.合并k個有序鏈表),本篇文章通過簡要的案例,講解了該項技術的了解與使用,以下就是詳細內(nèi)容,需要的朋友可以參考下2021-07-07解析c語言中"函數(shù)調(diào)用中缺少哨兵"的情況分析
本篇文章是對c語言中"函數(shù)調(diào)用中缺少哨兵"的情況進行了詳細的分析介紹,需要的朋友參考下2013-05-05Qt數(shù)據(jù)庫應用之實現(xiàn)圖片轉(zhuǎn)pdf
這篇文章主要為大家詳細介紹了如何利用Qt實現(xiàn)圖片轉(zhuǎn)pdf功能,文中的示例代碼講解詳細,對我們學習或工作有一定參考價值,需要的可以了解一下2022-06-06C++圖像加載之libpng、FreeImage、stb_image詳解
libpng、FreeImage、stb_image都是圖像解析的開源庫,這篇文章主要為大家詳細介紹了這三者的使用方法,文中的示例代碼講解詳細,需要的可以參考一下2023-06-06