-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAbstractWorldMap.java
More file actions
78 lines (55 loc) · 1.51 KB
/
AbstractWorldMap.java
File metadata and controls
78 lines (55 loc) · 1.51 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
76
77
78
package agh.cs.lab2;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
abstract class AbstractWorldMap implements IWorldMap,IPositionChangeObserver {
protected List<Car> cars = new ArrayList<>();
Map<Position, Car> hashMapa = new HashMap<>();
@Override
public boolean canMoveTo(Position position) {
if (isOccupied(position) == false)
return true;
else
return false;
}
@Override
public boolean add(Car car) {
Position pozycja;
pozycja = car.getPosition();
if (canMoveTo(pozycja))
cars.add(car);
else
throw new IllegalArgumentException("Pozycja zajeta");
return canMoveTo(pozycja);
}
@Override
public boolean isOccupied(Position position) {
if (objectAt(position) != null)
return true;
else
return false;
}
@Override
public Object objectAt(Position position) {
return hashMapa.get(position);
}
public void positionChanged(Position old, Position _new){
Car auto=hashMapa.get(old.hashCode());
hashMapa.remove(old.hashCode());
hashMapa.put(_new, auto);
}
public void run(MoveDirection[] directions) {
int n=cars.size();
Position poz;
for (int i = 0; i < directions.length; i++) {
poz=cars.get(i%n).getPosition();
cars.get(i % n).move(directions[i]);
if(cars.get(i%n).getPosition().equals(poz)) continue;
else {
hashMapa.remove(poz);
hashMapa.put(cars.get(i%n).getPosition(), cars.get(i%n));
}
}
}
}