c/c++学习 No.5 AI贪吃蛇(二)
来源:互联网 发布:淘宝金丝楠木手串真假 编辑:程序博客网 时间:2024/04/30 05:02
先写一个寻路函数
stack<int> bfs(pair<int, int> start, pair<int, int> end) { stack<int> path; int dist[101][101]; int dist2[101][101]; for (int i = 0; i < X; i++) { for (int j = 0; j < Y; j++) { dist[i][j] = 0x7f7f; dist2[i][j] = -1; } } queue<pair<int, int> > que; dist[start.first][start.second] = 0; que.push(start); int fx[4] = {0, 0, -1, 1}; int fy[4] = {-1, 1, 0, 0}; while (que.size()) { pair<int, int> now; now = que.front(); que.pop(); if (now == end) { break; } for (int i = 0; i < 4; i++) { int nx = now.first + fx[i]; int ny = now.second + fy[i]; if (nx >= 0 && ny >= 0 && nx < X && ny < Y && (gameMap[nx][ny] == 0 || gameMap[nx][ny] == gameMap[end.first][end.second]) && dist[nx][ny] == 0x7f7f) { que.push(pair<int, int>(nx, ny)); dist2[nx][ny] = i; dist[nx][ny] = dist[now.first][now.second] + 1; } } } int d = dist2[end.first][end.second]; while (d != -1) { path.push(d); switch(d) { case UP_: d = dist2[end.first][++end.second]; break; case DOWN_: d = dist2[end.first][--end.second]; break; case LEFT_: d = dist2[++end.first][end.second]; break; case RIGHT_: d = dist2[--end.first][end.second]; break; } } return path;}
接着调用这个函数即可
void moveFace(pair<int, int> &nowPos, int face) { switch(face) { case UP_: nowPos.second--; break; case DOWN_: nowPos.second++; break; case LEFT_: nowPos.first--; break; case RIGHT_: nowPos.first++; break; }}pair<int, int> findNear(int tx, int ty) { pair<int, int> p; int fx[4] = {1, -1, 0, 0}; int fy[4] = {0, 0, -1, 1}; for (int i = 0; i < 4; i++) { if (gameMap[tx + fx[i]][ty + fy[i]] == gameMap[tx][ty] + 1) { p.first = tx + fx[i]; p.second = ty + fy[i]; return p; } }}bool goAway() { for (int i = 0; i < 4; i++) { pair<int, int> nowPos(snake.x, snake.y); moveFace(nowPos, i); stack<int> path = bfs(nowPos, findNear(snake.tx, snake.ty)); if (!path.empty()) { snake.setFace((FACE)path.top()); snake.move(gameMap); display(gameMap); } }}sbool autoMove(int gameMap[X][Y]) { pair<int, int> nowPos(snake.x, snake.y); stack<int> path = bfs(nowPos, apple); if (!path.empty()) { moveFace(nowPos, (FACE)path.top()); if (snake.tx != -1) { stack<int> path2 = bfs(nowPos, pair<int, int>(snake.tx, snake.ty)); if (!path2.empty()) { snake.setFace((FACE)path.top()); snake.move(gameMap); display(gameMap); } else { goAway(); } } else { snake.setFace((FACE)path.top()); snake.move(gameMap); display(gameMap); } } else { goAway(); } return true;}
0 0
- c/c++学习 No.5 AI贪吃蛇(二)
- c\c++学习 No.4 AI贪吃蛇(一)
- 每日一题 No.37 AI贪吃蛇(二)
- AI贪吃蛇(二)
- 贪吃蛇(c++)
- 贪吃蛇(C++)
- 每日一题 No.36 AI贪吃蛇(一)
- 每日一题 No.38 AI贪吃蛇(三)
- C语言-手把手教你写贪吃蛇AI(上)
- C语言-手把手教你写贪吃蛇AI(中)
- C语言-手把手教你写贪吃蛇AI(下)
- C语言贪吃蛇的一个简单AI
- 练手: 贪吃蛇(c++)
- 贪吃蛇C实现
- 贪吃蛇C代码
- C代码 贪吃蛇
- [C++] Console 贪吃蛇
- c 贪吃蛇
- 直击物联网安全痛点,青莲云亮相2017中国(上海)国际物联网大会
- Java File类应用:递归遍历文件夹和递归删除文件
- 利用JavaScript、Ajax及CSS3实现图片预加载
- 使用高德地图的时候获取SHA1值和调试SHA1值
- 使用iconv批量转换文件编码为utf-8
- c/c++学习 No.5 AI贪吃蛇(二)
- 海量数据常用技巧之位图法
- php 面向对象之构造方法
- Android开发之--判断当前手机上是否存在QQ或微信应用
- 自定义表格,选取表格,获取里面的内容
- 魅族 Android 6.0 以上系统获取相机权限的坑
- 前端面试相关
- Jquery为相同ID的button绑定click
- Bootstrap常用导航条