-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSnakeGame.java
More file actions
107 lines (99 loc) · 3.09 KB
/
SnakeGame.java
File metadata and controls
107 lines (99 loc) · 3.09 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
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
package Interview;
import java.util.Deque;
import java.util.HashSet;
import java.util.LinkedList;
public class SnakeGame {
public static void main(String[] args){
SnakeGame obj = new SnakeGame(3, 3, new int[][]{{2,0},{0,0},{0,2},{0,1},{2,2},{0,1}});
int param_1 = obj.move("D");
int param_2 = obj.move("D");
int param_3 = obj.move("R");
int param_4 = obj.move("U");
int param_5 = obj.move("U");
int param_6 = obj.move("L");
int param_7 = obj.move("D");
int param_8 = obj.move("R");
int param_9 = obj.move("R");
int param_10 = obj.move("U");
int param_11 = obj.move("L");
int param_12 = obj.move("L");
int param_13 = obj.move("D");
int param_14 = obj.move("R");
int param_15 = obj.move("U");
}
private HashSet<int[]> bodySet;
private Deque<int[]> bodyQueue;
private int score;
private int[][] food;
private int height;
private int width;
private int foodIndex;
/** Initialize your data structure here.
@param width - screen width
@param height - screen height
@param food - A list of food positions
E.g food = [[1,1], [1,0]] means the first food is positioned at [1,1], the second is at [1,0]. */
public SnakeGame(int width, int height, int[][] food) {
bodySet = new HashSet<int[]>();
bodyQueue = new LinkedList<>();
score = 0;
foodIndex = 0;
this.height = height;
this.width = width;
this.food = food;
bodySet.add(new int[]{0,0});
bodyQueue.offerFirst(new int[]{0,0});
}
/** Moves the snake.
@param direction - 'U' = Up, 'L' = Left, 'R' = Right, 'D' = Down
@return The game's score after the move. Return -1 if game over.
Game over when snake crosses the screen boundary or bites its body. */
public int move(String direction) {
if(score==-1){
return -1;
}
int x = (bodyQueue.peekFirst())[0];
int y = (bodyQueue.peekFirst())[1];
switch(direction){
case "U":{
x--;
break;
}
case "D":{
x++;
break;
}
case "L":{
y--;
break;
}
case "R":{
y++;
break;
}
}
if(x<0||x>height-1||y<0||y>width-1){
return -1;
}
if(foodIndex<food.length&&x==food[foodIndex][0]&&y==food[foodIndex][1]){
score++;
foodIndex++;
bodySet.add(bodyQueue.peekFirst());
bodyQueue.offerFirst(new int[]{x,y});
}else{
bodySet.remove(bodyQueue.peekLast());
bodyQueue.pollLast();
bodySet.add(bodyQueue.peekFirst());
bodyQueue.offerFirst(new int[]{x,y});
}
if(bodySet.contains(bodyQueue.peekFirst())){
return -1;
}
return score;
}
}
/**
* Your SnakeGame object will be instantiated and called as such:
* SnakeGame obj = new SnakeGame(width, height, food);
* int param_1 = obj.move(direction);
*/