Repository navigation
Expand file tree
/
Copy pathlib.rs
More file actions
223 lines (188 loc) · 5.71 KB
/
Copy pathlib.rs
File metadata and controls
223 lines (188 loc) · 5.71 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
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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
use wasm_bindgen::prelude::*;
use wee_alloc::WeeAlloc;
#[global_allocator]
static ALLOC: WeeAlloc = WeeAlloc::INIT;
#[wasm_bindgen(module = "/www/utils/rnd.js")]
extern "C" {
fn rnd(max: usize) -> usize;
}
#[wasm_bindgen]
#[derive(PartialEq)]
pub enum Direction {
Up,
Right,
Down,
Left,
}
#[wasm_bindgen]
#[derive(Clone, Copy)]
pub enum GameStatus {
Won,
Lost,
Played,
}
#[derive(PartialEq, Clone, Copy)]
pub struct SnakeCell(usize);
struct Snake {
body: Vec<SnakeCell>,
direction: Direction,
}
impl Snake {
fn new(spawn_index: usize, size: usize) -> Snake {
let mut body = vec![];
for i in 0..size {
body.push(SnakeCell(spawn_index - i));
}
Snake {
body,
direction: Direction::Right,
}
}
}
#[wasm_bindgen]
pub struct World {
width: usize,
size: usize,
8000
snake: Snake,
next_cell: Option<SnakeCell>,
reward_cell: Option<usize>,
status: Option<GameStatus>,
points: usize,
}
#[wasm_bindgen]
impl World {
pub fn new(width: usize, snake_idx: usize) -> World {
let snake = Snake::new(snake_idx, 3);
let size = width * width;
World {
width,
size,
reward_cell: World::gen_reward_cell(size, &snake.body),
snake,
next_cell: None,
status: None,
points: 0,
}
}
fn gen_reward_cell(max: usize, snake_body: &Vec<SnakeCell>) -> Option<usize> {
let mut reward_cell;
loop {
reward_cell = rnd(max);
if !snake_body.contains(&SnakeCell(reward_cell)) {
break;
}
}
Some(reward_cell)
}
pub fn width(&self) -> usize {
self.width
}
pub fn points(&self) -> usize {
self.points
}
pub fn reward_cell(&self) -> Option<usize> {
self.reward_cell
}
pub fn snake_head_idx(&self) -> usize {
self.snake.body[0].0
}
pub fn start_game(&mut self) {
self.status = Some(GameStatus::Played);
}
pub fn game_status(&self) -> Option<GameStatus> {
self.status
}
pub fn game_status_text(&self) -> String {
match self.status {
Some(GameStatus::Won) => String::from("You have won!"),
Some(GameStatus::Lost) => String::from("You have lost!"),
Some(GameStatus::Played) => String::from("Playing"),
None => String::from("No Status"),
}
}
pub fn change_snake_dir(&mut self, direction: Direction) {
let next_cell = self.gen_next_snake_cell(&direction);
if self.snake.body[1].0 == next_cell.0 {
return;
}
self.next_cell = Some(next_cell);
self.snake.direction = direction;
}
pub fn snake_length(&self) -> usize {
self.snake.body.len()
}
pub fn snake_cells(&self) -> *const SnakeCell {
self.snake.body.as_ptr()
}
pub fn step(&mut self) {
match self.status {
Some(GameStatus::Played) => {
let temp = self.snake.body.clone();
match self.next_cell {
Some(cell) => {
self.snake.body[0] = cell;
self.next_cell = None;
}
None => {
self.snake.body[0] = self.gen_next_snake_cell(&self.snake.direction);
}
}
for i in 1..self.snake_length() {
self.snake.body[i] = SnakeCell(temp[i - 1].0);
}
if self.snake.body[1..self.snake_length()].contains(&self.snake.body[0]) {
self.status = Some(GameStatus::Lost)
}
if self.reward_cell == Some(self.snake_head_idx()) {
if self.snake_length() < self.size {
self.points += 1;
self.reward_cell = World::gen_reward_cell(self.size, &self.snake.body);
} else {
self.reward_cell = None;
self.status = Some(GameStatus::Won)
}
self.snake.body.push(SnakeCell(self.snake.body[1].0));
}
}
_ => {}
}
}
fn gen_next_snake_cell(&self, direction: &Direction) -> SnakeCell {
let snake_idx = self.snake_head_idx();
let row = snake_idx / self.width;
return match direction {
Direction::Right => {
let treshold = (row + 1) * self.width;
if snake_idx + 1 == treshold {
SnakeCell(treshold - self.width)
} else {
SnakeCell(snake_idx + 1)
}
}
Direction::Left => {
let treshold = row * self.width;
if snake_idx == treshold {
SnakeCell(treshold + (self.width - 1))
} else {
SnakeCell(snake_idx - 1)
}
}
Direction::Up => {
let treshold = snake_idx - (row * self.width);
if snake_idx == treshold {
SnakeCell((self.size - self.width) + treshold)
} else {
SnakeCell(snake_idx - self.width)
}
}
Direction::Down => {
let treshold = snake_idx + ((self.width - row) * self.width);
if snake_idx + self.width == treshold {
SnakeCell(treshold - ((row + 1) * self.width))
} else {
SnakeCell(snake_idx + self.width)
}
}
};
}
}