forked from andrei-punko/java-interview-coding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlatformer.java
More file actions
75 lines (68 loc) · 2.41 KB
/
Copy pathPlatformer.java
File metadata and controls
75 lines (68 loc) · 2.41 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 by.andd3dfx.common;
import java.util.ArrayDeque;
import java.util.Deque;
/**
* A character in a platformer game is standing on a single row of floor tiles numbered 0 to N, at position X.
* <p>
* When the character moves, the tile at the previous position disappears. The character can only move left and right,
* and always jump over one tile, and any holes. The character will not move if there are no tiles left to move to
* (you do not need to implement this in the code).
* <p>
* Implement a class that models this behavior and can report the character's position.
* <p>
* For example, new Platformer(6, 3) creates a row of 6 tiles (numbered 0 to 5) and a character positioned
* on the 3 {0 1 2 [3] 4 5}. A call to jumpLeft() moves the character two tiles to the left and the tile at position 3
* disappears {0 [1] 2 4 5}. A subsequent call to jumpRight() moves the character two tiles to the right and the tile
* at position 1 disappears, skipping tiles that have disappeared {0 2 [4] 5}.
* <pre>
* public class Platformer {
*
* public Platformer(int n, int position) {
* throw new UnsupportedOperationException("Waiting to be implemented");
* }
*
* public void jumpLeft() {
* throw new UnsupportedOperationException("Waiting to be implemented");
* }
*
* public void jumpRight() {
* throw new UnsupportedOperationException("Waiting to be implemented");
* }
*
* public int position() {
* throw new UnsupportedOperationException("Waiting to be implemented");
* }
* }
* </pre>
*/
public class Platformer {
private Deque<Integer> left = new ArrayDeque<>();
private Deque<Integer> right = new ArrayDeque<>();
private Integer position;
public Platformer(int n, Integer position) {
for (int i = 0; i < position; i++) {
left.add(i);
}
for (int i = position + 1; i < n; i++) {
right.add(i);
}
this.position = position;
}
public void jumpLeft() {
if (left.size() < 2) {
return;
}
right.addFirst(left.pollLast());
position = left.pollLast();
}
public void jumpRight() {
if (right.size() < 2) {
return;
}
left.addLast(right.pollFirst());
position = right.pollFirst();
}
public int position() {
return position;
}
}