Small 2048 game in Rust
| 1 | use std::io; |
| 2 | use std::io::{Read, Write, stdin}; |
| 3 | |
| 4 | mod term; |
| 5 | use term::TermMode; |
| 6 | |
| 7 | #[derive(Debug, PartialEq)] |
| 8 | enum Direction { |
| 9 | Up, |
| 10 | Down, |
| 11 | Right, |
| 12 | Left, |
| 13 | None, |
| 14 | } |
| 15 | |
| 16 | fn print_field(field: [[u16; 4]; 4]) { |
| 17 | println!("┏━━━━┯━━━━┯━━━━┯━━━━┓"); |
| 18 | |
| 19 | for i in 0..field.len() { |
| 20 | print!("┃"); |
| 21 | |
| 22 | for j in 0..field.len() { |
| 23 | match field[i][j] { |
| 24 | 0 => print!(" "), |
| 25 | n => print!("{:4}", n), |
| 26 | }; |
| 27 | |
| 28 | match j { |
| 29 | 3 => println!("┃"), |
| 30 | _ => print!("│") |
| 31 | }; |
| 32 | |
| 33 | } |
| 34 | |
| 35 | if i != 3 { |
| 36 | println!("┠────┼────┼────┼────┨"); |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | println!("┗━━━━┷━━━━┷━━━━┷━━━━┛"); |
| 41 | |
| 42 | io::stdout().flush().unwrap(); |
| 43 | } |
| 44 | |
| 45 | fn move_field(direction: Direction, field: [[u16; 4]; 4]) -> [[u16; 4]; 4] { |
| 46 | if direction == Direction::None { |
| 47 | return field; |
| 48 | } |
| 49 | |
| 50 | let mut new_field = field; |
| 51 | |
| 52 | for i in 0..field.len() { |
| 53 | let mut lock = false; |
| 54 | for j in 1..field.len() { |
| 55 | for k in (1..=j).rev() { |
| 56 | let this = match direction { |
| 57 | Direction::Up => (k, i), |
| 58 | Direction::Down => (4 - k - 1, i), |
| 59 | Direction::Right => (i, 4 - k - 1), |
| 60 | Direction::Left => (i, k), |
| 61 | _ => (i, k), |
| 62 | }; |
| 63 | let next = match direction { |
| 64 | Direction::Up => (k - 1, i), |
| 65 | Direction::Down => (4 - k, i), |
| 66 | Direction::Right => (i, 4 - k), |
| 67 | Direction::Left => (i, k - 1), |
| 68 | _ => (i, k), |
| 69 | }; |
| 70 | |
| 71 | if new_field[this.0][this.1] == 0 { |
| 72 | break; |
| 73 | } |
| 74 | |
| 75 | if new_field[this.0][this.1] == new_field[next.0][next.1] { |
| 76 | if lock { |
| 77 | lock = false; |
| 78 | break; |
| 79 | } |
| 80 | new_field[this.0][this.1] = 0; |
| 81 | new_field[next.0][next.1] *= 2; |
| 82 | lock = true; |
| 83 | break; |
| 84 | } else if new_field[next.0][next.1] == 0 { |
| 85 | new_field[next.0][next.1] = new_field[this.0][this.1]; |
| 86 | new_field[this.0][this.1] = 0; |
| 87 | } |
| 88 | } |
| 89 | } |
| 90 | } |
| 91 | new_field |
| 92 | } |
| 93 | |
| 94 | fn make_rand_num(field: &mut [[u16; 4]; 4]) { |
| 95 | let what_value = rand::random_range(0..10); |
| 96 | |
| 97 | let mut coord = rand::random_range(0..16); |
| 98 | |
| 99 | while field[coord / 4][coord % 4] != 0 { |
| 100 | coord = rand::random_range(0..16); |
| 101 | } |
| 102 | |
| 103 | field[coord / 4][coord % 4] = match what_value { |
| 104 | 0 => 4, |
| 105 | _ => 2, |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | fn field_has_empty(field: [[u16; 4]; 4]) -> bool { |
| 110 | for i in 0..field.len() { |
| 111 | for j in 0..field.len() { |
| 112 | if field[i][j] == 0 { |
| 113 | return true; |
| 114 | } |
| 115 | } |
| 116 | } |
| 117 | false |
| 118 | } |
| 119 | |
| 120 | fn has_lost(field: [[u16; 4]; 4]) -> bool { |
| 121 | if field_has_empty(field) { |
| 122 | return false; |
| 123 | } |
| 124 | |
| 125 | for i in 0..field.len() { |
| 126 | for j in 0..field.len() { |
| 127 | if i > 0 && field[i][j] == field[i - 1][j] { |
| 128 | return false; |
| 129 | } |
| 130 | |
| 131 | if j > 0 && field[i][j] == field[i][j - 1] { |
| 132 | return false; |
| 133 | } |
| 134 | |
| 135 | if i < 3 && field[i][j] == field[i + 1][j] { |
| 136 | return false; |
| 137 | } |
| 138 | |
| 139 | if j < 3 && field[i][j] == field[i][j + 1] { |
| 140 | return false; |
| 141 | } |
| 142 | } |
| 143 | } |
| 144 | |
| 145 | true |
| 146 | } |
| 147 | |
| 148 | fn get_direction(input: u8) -> Option<Direction> { |
| 149 | match input { |
| 150 | 0x41 => Some(Direction::Up), |
| 151 | 0x42 => Some(Direction::Down), |
| 152 | 0x43 => Some(Direction::Right), |
| 153 | 0x44 => Some(Direction::Left), |
| 154 | _ => None, |
| 155 | } |
| 156 | } |
| 157 | |
| 158 | fn main() { |
| 159 | let _term = TermMode::new().unwrap(); |
| 160 | |
| 161 | let mut field: [[u16; 4]; 4] = [[0; 4]; 4]; |
| 162 | |
| 163 | for _ in 0..2 { |
| 164 | make_rand_num(&mut field); |
| 165 | }; |
| 166 | |
| 167 | let mut stdin = stdin(); |
| 168 | let mut buf = [0u8; 10]; |
| 169 | |
| 170 | print!("\x1b[?25l\x1b[?1049h\x1b[2J\x1b[H"); |
| 171 | io::stdout().flush().unwrap(); |
| 172 | |
| 173 | loop { |
| 174 | print!("\x1b[1;1H"); |
| 175 | print_field(field); |
| 176 | |
| 177 | let n = stdin.read(&mut buf).unwrap(); |
| 178 | |
| 179 | if n > 0 && (buf[0] == b'q' || buf[0] == b'Q') { |
| 180 | break; |
| 181 | } |
| 182 | |
| 183 | if n == 3 && buf[0] == 0x1b && buf[1] == 0x5b { |
| 184 | let tmp = match get_direction(buf[2]) { |
| 185 | Some(direction) => move_field(direction, field), |
| 186 | None => continue, |
| 187 | }; |
| 188 | |
| 189 | if tmp != field { |
| 190 | field = tmp; |
| 191 | |
| 192 | if field_has_empty(field) { |
| 193 | make_rand_num(&mut field); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | if has_lost(field) { |
| 198 | break; |
| 199 | } |
| 200 | } |
| 201 | } |
| 202 | |
| 203 | print!("\x1b[?1049l\x1b[?25h"); |
| 204 | print_field(field); |
| 205 | io::stdout().flush().unwrap(); |
| 206 | } |