贪吃蛇AI人工智能C++实现
c++,贪吃蛇,人工智能,算法,ai2016-07-12
贪吃蛇是一款非常经典的游戏,规则想必大部分人都知道,这里就不再叙述了。最近写了一个贪吃蛇的人工智能版本,想方设法让蛇吃掉更多的食物。
先上效果图:
这个项目并没有完全结束,目前的AI算法并不算很好,所以有兴趣的人可以加入我的项目一起完成,项目链接以及代码下载:Github: Snake-AI
// Console color type enum ColorType { BLACK, RED, GREEN, BLUE, YELLOW, CYAN, MAGENTA, WHITE, }; // Console color class struct ConsoleColor { ConsoleColor(const ColorType foreColor_, const ColorType backColor_, const bool &foreIntensified_ = false, const bool &backIntensified_ = false); ColorType foreColor; ColorType backColor; bool foreIntensified; bool backIntensified; }; /* A cross-platform class to control the output attributes of the console(terminal). */ class Console { public: /* Set console cursor position. The origin is at the left-top corner. Axis x extends to the right and axis y extends to the bottom. @param console x coordinate @param console y coordinate */ static void setCursor(const int &x = 0, const int &y = 0); /* Clear the console. */ static void clear(); /* Write string to console @param str the string to write */ static void write(const std::string &str); /* Write string to console with a given color. In linux platform, the intensified console color attribute is not supported. Reference: 1. http://stackoverflow.com/questions/2616906/how-do-i-output-coloured-text-to-a-linux-terminal @param str the string to write @param color the output color */ static void writeWithColor(const std::string &str, const ConsoleColor &consoleColor); /* A cross-platform getch() method. Reference: 1. http://stackoverflow.com/questions/3276546/how-to-implement-getch-function-of-c-in-linux */ static char getch(); /* A cross-platform kbhit() method. Reference: 1. http://cboard.cprogramming.com/c-programming/63166-kbhit-linux.html */ static int kbhit(); private: #ifdef WIN32 /* Set console output color. Only available in windows platform. @param color the output color @return the origin console attribute */ static WORD setColor(const ConsoleColor &consoleColor); /* Reset console output color to default. Only available in windows platform. @param attr the console attribute to restore */ static void resetColor(const WORD &attr); #endif };
int main() { auto game = GameCtrl::getInstance(); // Set map's size. Default is 20*20 // The minimum size is 4*4. game->setMapRow(8); game->setMapColumn(8); // Set FPS. Default is 60.0 game->setFPS(59.0); // Set whether to make the snake automove. Default is true game->setAutoMoveSnake(true); // Set interval time(ms) for automove. Default is 200 ms. // If setAutoMoveSnake(false), this code is useless. game->setAutoMoveInterval(50); // Set whether to enable the second snake. Default is false game->setEnableSecondSnake(false); // Set whether to enable the snake AI. Default is false // If setAutoMoveSnake(false), this code is useless. game->setEnableAI(true); // Set whether to run the test program. Default is false // Set the map size to 20*40 before setting this to true. game->setRunTest(false); // Set whether to write the map content to the file. Default is false // If set this attribute to true, the game map content will be written // to a file named "movements.txt" after each snake's movement. // PS: This is designed for debugging. Open this method may make the // snake move slower. game->setWriteToFile(false); return game->run(); }