-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlateau.java
More file actions
37 lines (31 loc) · 1.17 KB
/
Copy pathPlateau.java
File metadata and controls
37 lines (31 loc) · 1.17 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
import java.util.HashSet;
public class Plateau {
private int row;
private int col;
private HashSet<String> plateau = new HashSet<>();
public Plateau(int row, int col) {
this.row = row;
this.col = col;
}
public Boolean positionAllowed(int x, int y) {
Boolean rowInBounds = x >= 0 && x <= this.row;
Boolean colInBounds = y >= 0 && y <= this.col;
if (!rowInBounds || !colInBounds) {
System.out.println("Position (" + x + "," + y + ") is out of bounds");
return false;
}
// check the avaliability of the new position (x, y)
String newPos = Integer.toString(x) + ',' + Integer.toString(y);
if (this.plateau.contains(newPos)) {
System.out.println("Position (" + x + "," + y + ") is already ocuppied");
return false;
}
return true;
}
public void setPosition(Sonda s, int x, int y) {
String previous = Integer.toString(s.getRow()) + ',' + Integer.toString(s.getCol());
String newPos = Integer.toString(x) + ',' + Integer.toString(y);
plateau.remove(previous);
plateau.add(newPos);
}
}