-
Notifications
You must be signed in to change notification settings - Fork 1
/
Trotinetes.java
148 lines (130 loc) · 4.76 KB
/
Trotinetes.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
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.locks.ReentrantLock;
import java.lang.*;
public class Trotinetes {
private PositionsList trotinetesAvailable;
private ReentrantLock l;
private boolean alteracao;
private Recompensas recompensas;
public Trotinetes(int c) {
this.trotinetesAvailable = new PositionsList();
initializeTrotinetesArray();
this.l = new ReentrantLock();
this.alteracao = false;
}
public PositionsList getTrotinetesAvailable() {
return trotinetesAvailable;
}
public boolean getAlteracao() {
l.lock();
try {
return this.alteracao;
} finally {
l.unlock();
}
}
public void setAlteracao(boolean alteracao) {
l.lock();
try {
this.alteracao = alteracao;
} finally {
l.unlock();
}
}
public void setRecompensas(Recompensas recompensas) {
this.recompensas = recompensas;
}
public static int manhattanDist(int X1, int Y1, int X2, int Y2) {
int dist = Math.abs(X2 - X1) + Math.abs(Y2 - Y1);
return dist;
}
public void lockTrotinetes() {
l.lock();
}
public void unlockTrotinetes() {
l.unlock();
}
public void initializeTrotinetesArray() {
this.trotinetesAvailable.add(new Positions(1, 2));
this.trotinetesAvailable.add(new Positions(1, 2));
this.trotinetesAvailable.add(new Positions(3, 4));
this.trotinetesAvailable.add(new Positions(6, 4));
this.trotinetesAvailable.add(new Positions(8, 9));
this.trotinetesAvailable.add(new Positions(2, 2));
this.trotinetesAvailable.add(new Positions(10, 2));
this.trotinetesAvailable.add(new Positions(15, 9));
this.trotinetesAvailable.add(new Positions(16, 9));
this.trotinetesAvailable.add(new Positions(11, 11));
this.trotinetesAvailable.add(new Positions(16, 7));
this.trotinetesAvailable.add(new Positions(9, 17));
this.trotinetesAvailable.add(new Positions(8, 13));
}
public void addTrotinete(Positions aP) { // recebe as coordenadas de uma trotinete que deixou de estar reservada
// pelo que deve ser adicionada à lista - devolve o array original mais a
// trotinete
l.lock();
try {
this.trotinetesAvailable.add(aP);
alteracao = true;
recompensas.lock();
recompensas.signal();
recompensas.unlock();
} finally {
l.unlock();
}
}
public void removeTrotinete(Positions rP) { // recebe as coordenadas de uma trotinete que ficou reservada e deve
// ser removida do array - devolve o array original sem aquela
// coordenada
l.lock();
try {
if (this.trotinetesAvailable.contains(rP)) {
this.trotinetesAvailable.remove(rP);// remove a 1ª instância da posição no array
alteracao = true;
recompensas.lock();
recompensas.signal();
recompensas.unlock();
}
} finally {
l.unlock();
}
}
public PositionsList getClosestTrotinetes(Positions newpos) { // recebe as coordenadas do user, e devolve a lista
// de trotinetes mais próximas, posicao do cliente
PositionsList closest = new PositionsList();
l.lock();
for (int i = 0; i < this.trotinetesAvailable.size(); i++) {
Positions atualP = this.trotinetesAvailable.get(i);
if ((atualP.getX() == newpos.getX()) && (atualP.getY() == newpos.getY())) {
closest.add(this.trotinetesAvailable.get(i));
} else if (manhattanDist(atualP.getX(), atualP.getY(), newpos.getX(), newpos.getY()) <= 2) {
closest.add(this.trotinetesAvailable.get(i));
}
}
l.unlock(); // can be improved
return closest;
}
public boolean moreThanOneTrotinete(Positions t) {
int contador = 0;
l.lock();
for (int i = 0; i < this.trotinetesAvailable.size(); i++) {
Positions atualP = this.trotinetesAvailable.get(i);
if (atualP.equals(t)) {
contador++;
//System.out.print("contador" + contador );
}
}
l.unlock();
if (contador > 1)
return true;
else
return false;
}
public Boolean isClosestEmpty(Positions p) {
if (getClosestTrotinetes(p).size() == 0)
return true;
else
return false;
}
}