数据结构Java课设火星基地
时间: 2023-11-26 17:47:40 浏览: 156
数据结构课设
以下是一个使用Java实现的火星基地数据结构课设的示例:
```java
import java.util.Scanner;
public class MarsBase {
private static final int MAX_ROVERS = 10;
private static final int MAX_COMMANDS = 100;
private Rover[] rovers;
private int numRovers;
public MarsBase() {
rovers = new Rover[MAX_ROVERS];
numRovers = 0;
}
public void addRover(Rover rover) {
if (numRovers < MAX_ROVERS) {
rovers[numRovers] = rover;
numRovers++;
} else {
System.out.println("Cannot add rover - maximum number of rovers reached.");
}
}
public void runMission() {
Scanner scanner = new Scanner(System.in);
for (int i = 0; i < numRovers; i++) {
System.out.println("Enter commands for rover " + (i + 1) + ":");
String commands = scanner.nextLine();
for (int j = 0; j < commands.length(); j++) {
char command = commands.charAt(j);
rovers[i].executeCommand(command);
}
System.out.println("Final position of rover " + (i + 1) + ":");
System.out.println(rovers[i].getPosition());
}
}
public static void main(String[] args) {
MarsBase base = new MarsBase();
Rover rover1 = new Rover(1, 1, 'N');
rover1.addCommand('L');
rover1.addCommand('M');
rover1.addCommand('L');
rover1.addCommand('M');
rover1.addCommand('L');
rover1.addCommand('M');
rover1.addCommand('L');
rover1.addCommand('M');
rover1.addCommand('M');
Rover rover2 = new Rover(3, 3, 'E');
rover2.addCommand('M');
rover2.addCommand('M');
rover2.addCommand('R');
rover2.addCommand('M');
rover2.addCommand('M');
rover2.addCommand('R');
rover2.addCommand('M');
rover2.addCommand('R');
rover2.addCommand('R');
rover2.addCommand('M');
base.addRover(rover1);
base.addRover(rover2);
base.runMission();
}
}
class Rover {
private int x;
private int y;
private char direction;
private char[] commands;
private int numCommands;
public Rover(int x, int y, char direction) {
this.x = x;
this.y = y;
this.direction = direction;
commands = new char[MAX_COMMANDS];
numCommands = 0;
}
public void addCommand(char command) {
if (numCommands < MAX_COMMANDS) {
commands[numCommands] = command;
numCommands++;
} else {
System.out.println("Cannot add command - maximum number of commands reached.");
}
}
public void executeCommand(char command) {
switch (command) {
case 'L':
turnLeft();
break;
case 'R':
turnRight();
break;
case 'M':
move();
break;
default:
System.out.println("Invalid command: " + command);
}
}
private void turnLeft() {
switch (direction) {
case 'N':
direction = 'W';
break;
case 'E':
direction = 'N';
break;
case 'S':
direction = 'E';
break;
case 'W':
direction = 'S';
break;
}
}
private void turnRight() {
switch (direction) {
case 'N':
direction = 'E';
break;
case 'E':
direction = 'S';
break;
case 'S':
direction = 'W';
break;
case 'W':
direction = 'N';
break;
}
}
private void move() {
switch (direction) {
case 'N':
y++;
break;
case 'E':
x++;
break;
case 'S':
y--;
break;
case 'W':
x--;
break;
}
}
public String getPosition() {
return x + " " + y + " " + direction;
}
}
```
这个示例实现了一个火星基地,其中有两个机器人漫游车。每个漫游车都有一个初始位置和方向,并且可以接收一系列指令来移动和转向。在这个示例中,我们创建了两个漫游车,并为它们添加了一些指令,然后运行了整个任务,输出了每个漫游车的最终位置。
阅读全文