-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRobotFileManager.java
More file actions
75 lines (62 loc) · 3.1 KB
/
RobotFileManager.java
File metadata and controls
75 lines (62 loc) · 3.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package RobotSimulation;
import javax.swing.*;
import java.io.*;
import java.util.Scanner;
import java.util.NoSuchElementException;
public class RobotFileManager {
//save arena to a file
public static void saveArena(RobotArena arena) {
//activate suggested file chooser
JFileChooser fileChooser = new JFileChooser();
int option = fileChooser.showSaveDialog(null);
//check if file selection was approved
if (option == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try (PrintWriter writer = new PrintWriter(file)) {
//save arena dimensions
writer.println(arena.getWidth() + " " + arena.getHeight());
//save each robot's position and direction
for (Robot robot : arena.getRobots()) {
writer.println(robot.getX() + " " + robot.getY() + " " + robot.getDirection());
}
JOptionPane.showMessageDialog(null, "Arena saved, do not worry :) .");
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, "Error: Could not save file, please try again :( .");
}
}
}
//load arena from a save file
public static RobotArena loadArena() {
JFileChooser fileChooser = new JFileChooser();
int option = fileChooser.showOpenDialog(null);
if (option == JFileChooser.APPROVE_OPTION) {
File file = fileChooser.getSelectedFile();
try (Scanner scanner = new Scanner(file)) {
//read arena dimensions
int width = scanner.nextInt();
int height = scanner.nextInt();
RobotArena arena = new RobotArena(width, height);
//read each robot's position and direction
while (scanner.hasNext()) {
int x = scanner.nextInt();
int y = scanner.nextInt();
String directionString = scanner.next();
Direction direction = Direction.valueOf(directionString);
arena.getRobots().add(new Robot(x, y, direction));
}
//success load message
JOptionPane.showMessageDialog(null, "Arena loaded from save file. Have fun! ");
return arena;
} catch (FileNotFoundException e) {
JOptionPane.showMessageDialog(null, "Error: We could not load your file, please try again :( .");
} catch (IllegalArgumentException | NoSuchElementException e) {
JOptionPane.showMessageDialog(null, "Error: This file format is invalid, please try a different file :( .");
}
}
//if loading fails, create a default arena to make life easy
int defaultWidth = 20;
int defaultHeight = 10;
JOptionPane.showMessageDialog(null, "Loading failed or canceled. A new arena has been created for ease :) .");
return new RobotArena(defaultWidth, defaultHeight);
}
}