-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWorld.java
166 lines (146 loc) · 4.95 KB
/
World.java
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import java.awt.Point;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.Collections;
import java.util.List;
import java.util.Random;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.Scanner;
/** Simulation of the robots in their world */
public class World {
public static final int SIZE = 12;
protected Set<Point> availableFuel = Collections.newSetFromMap(new ConcurrentHashMap<Point, Boolean>());
private Random rand = new Random();
protected Robot[] robots;
public World() {
robots = new Robot[] { null, new Robot(this, 0, 0, "red", false), new Robot(this, SIZE - 1, SIZE - 1, "blue", false) };
}
/**
* This world can no longer be used after this call.
*/
public void reset() {
availableFuel.clear();
for (int i = 1; i <= 2; i++) {
robots[i].updatePending();
robots[i].cancel();
}
}
public Set<Point> getAvailableFuel() {
return availableFuel;
}
public void updateWorld() {
addFuel(false);
for (int i = 1; i <= 2; i++) {
robots[i].updatePending();
}
}
public void loadRobotProgram(int id, File code) {
try{
Scanner scanner = new Scanner(code);
ProgramNode prog = new Parser().parse(scanner);
if (prog == null) {
System.out.println("Robot " + id + " was given an empty program and ignored it.");
}
else {
System.out.println("Robot " + id + " now has program: ");
System.out.println(prog);
robots[id].setProgram(prog);
}
}
catch (FileNotFoundException e) {
System.out.println("Robot program source file not found");
}
catch (ParserFailureException e) {
System.out.println("Parser error:");
System.out.println(e.getMessage());
}
}
public void start() {
// add some initial fuel
if (availableFuel.isEmpty()) {
addFuel(true);
addFuel(true);
}
new Thread(()-> {
try {robots[1].run();}
catch (RobotInterruptedException e) {}
robots[1].setFinished(true);
}).start();
/* new Thread(new Runnable() {
@Override
public void run() {
try {
robots[1].run();
} catch (RobotInterruptedException e) {
}
robots[1].setFinished(true);
}
}).start();
*/
new Thread(() -> {
try {
robots[2].run();
} catch (RobotInterruptedException e) {
}
robots[2].setFinished(true);
}).start();
}
public Robot getRobot(int id) {
if (id <= 0 || id > robots.length) {
return null;
}
return robots[id];
}
/** Returns the opponent robot of the argument */
public Robot getOtherRobot(Robot robot) {
if (robot == robots[2])
return robots[1];
if (robot == robots[1])
return robots[2];
return null;
}
private void addFuel(boolean definitely) {
if (definitely || rand.nextDouble() < 0.2) {
int x = rand.nextInt(12);
int y = rand.nextInt(12);
Point fuel = new Point(x, y);
availableFuel.add(fuel);
}
}
/*
public static class RoboGamePrinter extends World {
public RoboGamePrinter() {
super();
// we remake the robots to have noWait set to true, this makes the program run without the timer.
// the blue robot is never used, but the red robot does call some methods on it so we can't make it null.
this.robots = new Robot[] { null, new Robot(this, 0, 0, "red", true), new Robot(this, SIZE - 1, SIZE - 1, "blue", true) };
}
public static void main(String[] args) throws IOException {
if (args.length == 0) {
// for the students.
System.out.println("WRONG FILE");
System.out.println("Use the main function in RoboGame.java to run the assignment.");
}
RoboGamePrinter rgp = new RoboGamePrinter();
// load the program.
RobotProgramNode prog = Parser.parseFile(new File(args[0]));
rgp.robots[1].setProgram(prog);
// load fuel placement from file.
List<String> fuel = Files.readAllLines(Paths.get(args[1]), StandardCharsets.UTF_8);
for (String line : fuel) {
String[] coords = line.split("\\s+");
int x = Integer.parseInt(coords[0]);
int y = Integer.parseInt(coords[1]);
rgp.availableFuel.add(new Point(x, y));
}
// run the robot.
rgp.robots[1].run();
}
}
*/
}