-
Notifications
You must be signed in to change notification settings - Fork 111
/
design-snake-game.cc
50 lines (48 loc) · 1.57 KB
/
design-snake-game.cc
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
// Design Snake Game
class SnakeGame {
int w, h, x = 0, y = 0;
vector<pair<int, int>> food;
vector<vector<bool>> occ;
deque<pair<int, int>> a{{0, 0}};
public:
/** 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]. */
SnakeGame(int width, int height, vector<pair<int, int>> food) {
w = width;
h = height;
occ.assign(h, vector<bool>(w, false));
occ[0][0] = true;
reverse(food.begin(), food.end());
this->food = std::move(food);
}
/** 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. */
int move(string direction) {
if (x < 0) return -1;
if (direction == "U") y--;
if (direction == "D") y++;
if (direction == "L") x--;
if (direction == "R") x++;
if (x < 0 || x >= w || y < 0 || y >= h) return x = -1;
a.emplace_back(x, y);
if (food.size() && x == food.back().second && y == food.back().first)
food.pop_back();
else {
occ[a.front().second][a.front().first] = false;
a.pop_front();
if (occ[y][x]) return x = -1;
}
occ[y][x] = true;
return a.size()-1;
}
};
/**
* Your SnakeGame object will be instantiated and called as such:
* SnakeGame obj = new SnakeGame(width, height, food);
* int param_1 = obj.move(direction);
*/