#include <stdio.h>

#define MAX_TRAJECTORY_LENGTH 100

struct Robot {
    int* x;
    int* y;
    int trajectory[MAX_TRAJECTORY_LENGTH];
    int health;
    int power;
};

void Robot_move(struct Robot* robot, char direction);
void Path_print(const struct Robot* robot);
void Obstacle_set(int* map, int x, int y);

int main() {
    int map[6][6] = {0};
    struct Robot robot;
    int robot_x = 0, robot_y = 0;
    robot.x = &robot_x;
    robot.y = &robot_y;
    robot.health = 100;
    robot.power = 50;
    
    // Set obstacles
    Obstacle_set(&map[0][0], 1, 2);
    Obstacle_set(&map[0][0], 2, 2);
    Obstacle_set(&map[0][0], 3, 2);
    Obstacle_set(&map[0][0], 4, 2);
    
    Robot_move(&robot, 'D');
    Robot_move(&robot, 'D');
    Robot_move(&robot, 'D');
    Robot_move(&robot, 'D');
    Robot_move(&robot, 'D');
    Robot_move(&robot, 'D');
    Robot_move(&robot, 'D');
    Robot_move(&robot, 'D');
    
    Path_print(&robot);
    
    return 0;
}

void Robot_move(struct Robot* robot, char direction) {
    if (robot->power >= 10) {
        int new_x = *robot->x;
        int new_y = *robot->y;
        switch (direction) {
            case 'W':
                new_y--;
                break;
            case 'S':
                new_y++;
                break;
            case 'A':
                new_x--;
                break;
            case 'D':
                new_x++;
                break;
            default:
                return;
        }
        
        if (new_x >= 0 && new_x <= 5 && new_y >= 0 && new_y <= 5) {
            if (map[new_x][new_y] == 0) {
                robot->x = &new_x;
                robot->y = &new_y;
                robot->trajectory[robot->power / 10] = direction;
                robot->power -= 10;
                
                if (new_y > *robot->y) {
                    robot->health -= 8;
                } else if (new_y < *robot->y) {
                    robot->health -= 8;
                } else if (new_x > *robot->x) {
                    robot->health -= 5;
                } else if (new_x < *robot->x) {
                    robot->health -= 5;
                }
                
                if (robot->power > robot->power_limit) {
                    printf("功率过高,机器人已停止移动!\n");
                }
            }
        }
    }
}

void Path_print(const struct Robot* robot) {
    for (int i = 0; i < robot->power / 10; i++) {
        char direction = robot->trajectory[i];
        printf("%c ", direction);
    }
    printf("\n");
}

void Obstacle_set(int* map, int x, int y) {
    *(map + 6 * x + y) = 1;
}
``
用c语言设计一个机器人系统包含机器人的移动和攻击功能并考虑机器人功率限制。机器人移动功能:有一个6 x 6的二维空间以二维数组int map66表示其中包含了一些障碍物阻碍机器人的移动。定义一个结构体Robot包含以下成员:1	指向机器人的当前 x 坐标的指针2	指向机器人的当前 y 坐标的指针3	机器人的移动轨迹的数组4	机器人的血量 机器人的功率机器人的初始位置为0 0初始血量为10

原文地址: http://www.cveoy.top/t/topic/hNyB 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录