c/c++学习 No.5 AI贪吃蛇(二)

先写一个寻路函数


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);
        }
    }
}s

bool 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
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值