Small 2048 game in Rust
Commit 59d388c5a3ed
tarball1 file changed+14-9
@@ -43,7 +43,7 @@ fn move_field(direction: Direction, field: [[u16; 4]; 4]) -> [[u16; 4]; 4] {
4343
return field;4444
}4545
46
- let mut new_field = field.clone();46
+ let mut new_field = field;4747
4848
for i in 0..4 {4949
let mut lock = false;@@ -141,6 +141,16 @@ fn has_lost(field: [[u16; 4]; 4]) -> bool {
141141
true142142
}143143
144
+fn get_direction(input: u8) -> Option<Direction> {145
+ match input {146
+ 0x41 => Some(Direction::Up),147
+ 0x42 => Some(Direction::Down),148
+ 0x43 => Some(Direction::Right),149
+ 0x44 => Some(Direction::Left),150
+ _ => None,151
+ }152
+}153
+144154
fn main() {145155
let fd = stdin().as_raw_fd();146156
@@ -175,16 +185,11 @@ fn main() {
175185
}176186
177187
if n == 3 && buf[0] == 0x1b && buf[1] == 0x5b {178
- let direction: Direction = match buf[2] {179
- 0x41 => Direction::Up,180
- 0x42 => Direction::Down,181
- 0x43 => Direction::Right,182
- 0x44 => Direction::Left,183
- _ => Direction::None,188
+ let tmp = match get_direction(buf[2]) {189
+ Some(direction) => move_field(direction, field),190
+ None => continue,184191
};185192
186
- let tmp = move_field(direction, field);187
-188193
if tmp != field {189194
field = tmp;