3、 c语言题目题目:代码命名格式为23设计一个机器人系统包含机器人的移动和攻击功能并考虑机器人功率限制。机器人移动功能:有一个6 x 6的二维空间以二维数组int map66表示其中包含了一些障碍物阻碍机器人的移动。定义一个结构体Robot包含以下成员:1 指向机器人的当前 x 坐标的指针2 指向机器人的当前 y 坐标的指针3 机器人的移动轨迹的数组4 机器人的血量 机器人的功率机器人
#include <stdio.h>
#include <stdlib.h>
#define MAX_TRAJECTORY_LENGTH 100
struct Robot {
int* x; // 指向机器人的当前 x 坐标的指针
int* y; // 指向机器人的当前 y 坐标的指针
char trajectory[MAX_TRAJECTORY_LENGTH]; // 机器人的移动轨迹的数组
int health; // 机器人的血量
int power; // 机器人的功率
};
void Robot_move(struct Robot* robot, char direction) {
if (robot->power > 0) {
int new_x = *robot->x;
int new_y = *robot->y;
switch (direction) {
case 'W':
new_x -= 1;
break;
case 'S':
new_x += 1;
break;
case 'A':
new_y -= 1;
break;
case 'D':
new_y += 1;
break;
}
if (new_x >= 0 && new_x < 6 && new_y >= 0 && new_y < 6) {
if (map[new_x][new_y] == 0) {
*robot->x = new_x;
*robot->y = new_y;
robot->power -= 10;
robot->trajectory[strlen(robot->trajectory)] = direction;
robot->trajectory[strlen(robot->trajectory) + 1] = '\0';
if (direction == 'W' || direction == 'S') {
robot->health -= 8;
} else {
robot->health -= 5;
}
if (robot->power > power_limit) {
printf("功率过高,机器人已停止移动!\n");
}
}
}
}
}
void Path_print(const struct Robot* robot) {
printf("机器人的移动轨迹:\n%s\n", robot->trajectory);
printf("机器人当前坐标:\n(%d,%d)\n", *robot->x, *robot->y);
printf("机器人当前血量:\n%d\n", robot->health);
printf("机器人当前功率:\n%d\n", robot->power);
}
void Obstacle_set(int* map, int x, int y) {
map[x][y] = 1;
}
int main() {
int power_limit;
printf("请输入机器人功率上限设置:\n");
scanf("%d", &power_limit);
int map[6][6] = {0};
int obstacle_count;
printf("请输入障碍物设置个数:\n");
scanf("%d", &obstacle_count);
printf("请输入障碍物坐标:\n");
for (int i = 0; i < obstacle_count; i++) {
int x, y;
scanf("%d%d", &x, &y);
Obstacle_set(map, x, y);
}
struct Robot robot;
int x = 0;
int y = 0;
robot.x = &x;
robot.y = &y;
strcpy(robot.trajectory, "");
robot.health = 100;
robot.power = 50;
int move_count;
printf("请输入机器人移动次数:\n");
scanf("%d", &move_count);
printf("请输入机器人移动操作:\n");
for (int i = 0; i < move_count; i++) {
char direction;
scanf(" %c", &direction);
Robot_move(&robot, direction);
}
Path_print(&robot);
return 0;
}
``
原文地址: http://www.cveoy.top/t/topic/hNzp 著作权归作者所有。请勿转载和采集!