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
原创粉丝点击