cli_2048 0.2.0

2048 for the command line
Documentation
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
use rand::Rng;
use phf::phf_map;
use std::fmt;

/// Holds the game state.
pub struct Grid {
    //rows contain cols
    rows: Vec<Vec<u8>>,
    pipes: &'static PipeMap,
}

impl Default for Grid {
    fn default() -> Self {
        Grid {
            rows: vec![vec![0; 4]; 4],
            pipes: &PIPEMAP_THICK,
        }
    }
}

impl Grid {
    /// Creates a new grid with the given size and adds two numbers on random positions.
    /// The size must be greater than 0 and greater than 1 in at least one dimension.
    /// # Examples
    ///
    /// ```
    /// use cli_2048::Grid;
    /// 
    /// //Create a 4x4 grid
    /// let grid = Grid::new(4, 4);
    ///
    /// ```
    pub fn new(x_size: usize, y_size: usize) -> Grid {
        if x_size < 1 || y_size < 1 || x_size < 2 && y_size < 2 {
            panic!("Grid size cannot be 0");
        }
        let grid = Grid {
            rows: vec![vec![0; x_size]; y_size],
            ..Default::default()
        };
        //add two starting numbers
        grid.add_random_number().unwrap().add_random_number().unwrap()
    }

    /// Creates a new grid from a predefined grid.
    ///
    /// # Examples
    ///
    /// ```
    /// use cli_2048::Grid;
    /// 
    /// //Create a 4x4 vector
    /// let rows = vec![vec![0; 4]; 4];
    /// //Create a grid from the vector
    /// let grid = Grid::from_rows(rows);
    ///
    /// ```
    pub fn from_rows(rows: Vec<Vec<u8>>) -> Grid {
        Grid {
            rows,
            ..Default::default()
        }
    }

    /// Returns a new instance of the grid, with a custom pipe-map for the borders.
    ///
    /// # Examples
    ///
    /// ```
    /// use cli_2048::Grid;
    /// use cli_2048::PIPEMAPS;
    /// 
    /// //Create a 4x4 grid
    /// let grid = Grid::new(4, 4);
    /// //Create a grid with a custom pipe-map
    /// let grid = grid.with_pipes(PIPEMAPS.get("Medium").unwrap());
    /// 
    /// //Or like this
    /// let grid = Grid::new(4, 4).with_pipes(PIPEMAPS.get("Medium").unwrap());
    /// 
    /// ```
    pub fn with_pipes(&self, pipes: &'static PipeMap) -> Grid {
        Grid {
            rows: self.rows.clone(),
            pipes,
        }
    }
    /// Gets the size of the grid in characters and with borders.
    ///
    /// # Examples
    ///
    /// ```
    /// use cli_2048::Grid;
    /// 
    /// //Create a 4x4 grid
    /// let grid = Grid::new(4, 4);
    /// 
    /// let x_size = 4*2 + (4-1) + 2;
    /// let y_size = 4 + (4-1) + 2;
    ///
    /// assert_eq!(grid.get_size(), (x_size, y_size));
    /// 
    /// ```
    pub fn get_size(&self) -> (usize, usize) {
        (self.rows.len() * self.formatted_numbers()[0][0].len() + self.rows.len() + 1, self.rows[0].len() * 2 + 1)
    }
    /// Returns a new Grid from the previous.
    /// Slides and combines the grid in the given direction.
    /// If the tiles change a new tile will be added at a random empty position.
    /// If the grid is full the game is over (Err("no more options")).
    ///
    /// # Examples
    ///
    /// ```
    /// use cli_2048::Grid;
    /// use cli_2048::Direction;
    /// 
    /// //Create a 4x4 grid
    /// let grid = Grid::new(4, 4);
    /// let grid = grid.slide(Direction::Up);
    /// let grid = grid.slide(Direction::Down);
    /// 
    /// ```
    pub fn slide(&self, dir: Direction) -> Result<Grid, &'static str> {
        let mut rows: Vec<Vec<u8>> = self.rows.clone();

        (|| {
            match dir {
                Direction::LEFT => {
                    //Rotate
                    // -
                    //Operate
                    rows = rows.iter().map(|row| self.combine_row(row)).collect();
                    //Rotate back
                    // -
                    //Return
                    return Ok(());
                }
                
                Direction::RIGHT => {
                    //Rotate
                    rows = rows.iter().map(|row| row.iter().rev().cloned().collect()).collect();
                    //Operate
                    rows = rows.iter().map(|row| self.combine_row(row)).collect();
                    //Rotate back
                    rows = rows.iter().map(|row| row.iter().rev().cloned().collect()).collect();
                    //Return
                    return Ok(());
                }
                
                Direction::UP => {
                    //Rotate
                    rows = (0..rows[0].len()).map(|col| rows.iter().map(|row| row[col]).collect()).collect();
                    //Operate
                    rows = rows.iter().map(|row| self.combine_row(row)).collect();
                    //Rotate back
                    rows = (0..rows[0].len()).map(|col| rows.iter().map(|row| row[col]).collect()).collect();
                    //Return
                    return Ok(());
                }
                Direction::DOWN => {
                    //Rotate
                    rows = (0..rows[0].len()).map(|col| rows.iter().map(|row| row[col]).collect()).collect();
                    rows = rows.iter().map(|row| row.iter().rev().cloned().collect()).collect();
                    //Operate
                    rows = rows.iter().map(|row| self.combine_row(row)).collect();
                    //Rotate back
                    rows = rows.iter().map(|row| row.iter().rev().cloned().collect()).collect();
                    rows = (0..rows[0].len()).map(|col| rows.iter().map(|row| row[col]).collect()).collect();
                    //Return
                    return Ok(());
                }
            }
        })()?;

        let new_grid = Grid { rows, ..Default::default() }; 
        let new_grid_with_new_number = new_grid.add_random_number()?;
        //see if grid has changed
        if new_grid.rows != self.rows {
            return Ok(new_grid_with_new_number);
        }
        Ok(new_grid)
    }
    
    fn compress_row(&self, row: &Vec<u8>) -> Vec<u8> {
        let mut new_row = row.iter().filter(|&x| *x != 0).cloned().collect::<Vec<u8>>();
        new_row.append(&mut vec![0; row.len() - new_row.len()]);
        new_row
    }

    fn combine_row(&self, row: &Vec<u8>) -> Vec<u8> {
        let mut row = self.compress_row(&row);
        for i in 0..(row.len() - 1) {
            if row[i] == row[i+1] && row[i] != 0 {
                row[i] += 1;
                row[i+1] = 0;
            }
        }
        self.compress_row(&row)
    }

    fn add_random_number(&self) -> Result<Grid, &'static str> {
        //get index of all 0 cells
        let options: Vec<(usize, usize)> = self.rows.iter().enumerate().flat_map(|(x, row)| {
            row.iter().enumerate().filter(|(_, &cell)| cell == 0).map(move |(y, _)| (x, y))
        }).collect();
        
        //check for no options (GAME OVER)
        if options.is_empty() {
            return Err("no more options");
        }

        let mut rng = rand::thread_rng();

        //get random option
        let option = options[rng.gen_range(0..options.len())];

        let mut power = 1;
        //1 in 10 chance of getting 4
        if rng.gen_range(1..10) == 10 {
            power = 2;
        }

        let mut new_rows = self.rows.clone();
        new_rows[option.0][option.1] = power;

        Ok(Grid { rows: new_rows, ..Default::default() })
    }
    fn formatted_numbers(&self) -> Vec<Vec<String>> {

        let mut longest_string_len = 2;
        for number in self.rows.iter().flatten() {
            if format_number(number, 0).len() > longest_string_len {
                longest_string_len = format_number(number, 0).len();
            }
        }

        return self.rows.iter().map(|row| {
            row.iter().map(|number| format_number(number, longest_string_len)).collect()
        }).collect();

        fn format_number(&number: &u8, len: usize) -> String {
            let digits = format_digits(&number);
            if len == 0 {
                return digits;
            }
            return (0..len-digits.len()).map(|_| " ".to_string()).collect::<Vec<String>>().join("") + &digits;

            fn format_digits(&number: &u8) -> String {
                match number {
                    0 => " ".to_string(),
                    x => format!("{}", (2 as usize).pow(x as u32)),
                }
            }
        }
    }
}

impl fmt::Debug for Grid {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut grid_str = format!("{} by {} Grid:\n[\n", self.rows.len(), self.rows[0].len());
        for row in &self.rows {
            grid_str.push_str("  [");
            for val in row {
                grid_str.push_str(&format!(" {} ", val));
            }
            grid_str.push_str("]\n");
        }
        grid_str.push_str("]\n");
        
        
        Ok(
            write!(f, "{}", grid_str)?
        )
    }
}

impl fmt::Display for Grid {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {

        //set pipes
        let pipes = &self.pipes;

        let grid_str = self.formatted_numbers();

        //draw top border
        write!(f, "{}", pipes.get("top_left").unwrap())?;
        for i in 0..grid_str[0].len() {
            for _ in 0..grid_str[0][0].len() {
                write!(f, "{}", pipes.get("horizontal").unwrap())?;
            }
            if i != grid_str[0].len() - 1 {
                write!(f, "{}", pipes.get("top_horizontal").unwrap())?;
            }
        }
        write!(f, "{}\n", pipes.get("top_right").unwrap())?;

        //draw row
        for i in 0..grid_str.len() {
            for col in &grid_str[i] {
                write!(f, "{}{}", pipes.get("vertical").unwrap(), col)?;
            }
            write!(f, "{}\n", pipes.get("vertical").unwrap())?;

            //Draw bottom border or cross-line
            if i == grid_str.len() - 1 {
                //bottom border
                write!(f, "{}", pipes.get("bottom_left").unwrap())?;
                for i in 0..grid_str[0].len() {
                    for _ in 0..grid_str[0][0].len() {
                        write!(f, "{}", pipes.get("horizontal").unwrap())?;
                    }
                    if i != grid_str[0].len() - 1 {
                        write!(f, "{}", pipes.get("bottom_horizontal").unwrap())?;
                    }
                }
                write!(f, "{}\n", pipes.get("bottom_right").unwrap())?;
            } else {
                //cross-line
                write!(f, "{}", pipes.get("left_vertical").unwrap())?;
                for i in 0..grid_str[0].len() {
                    for _ in 0..grid_str[0][0].len() {
                        write!(f, "{}", pipes.get("horizontal").unwrap())?;
                    }
                    if i != grid_str[0].len() - 1 {
                        write!(f, "{}", pipes.get("cross").unwrap())?;
                    }
                }
                write!(f, "{}\n", pipes.get("right_vertical").unwrap())?;
            }
        }

        Ok(())
    }
}

    /// Used as parameters to the slide function.
    /// # Examples
    ///
    /// ```
    /// use cli_2048::Grid;
    /// use cli_2048::Direction;
    /// 
    /// //Create a 4x4 grid
    /// let grid = Grid::new(4, 4);
    /// let grid = grid.slide(Direction::Up);
    /// let grid = grid.slide(Direction::Down);
    /// 
    /// ```
pub enum Direction {
    LEFT,
    RIGHT,
    UP,
    DOWN,
}

type PipeMap = phf::Map<&'static str, &'static str>;

    /// Contains three pipe-map presets:
    /// Thin
    /// Medium
    /// Thick
    /// 
    /// # Examples
    ///
    /// ```
    /// use cli_2048::Grid;
    /// use cli_2048::PIPEMAPS;
    /// 
    /// //Create a 4x4 grid
    /// let grid = Grid::new(4, 4);
    /// //Create a grid with a custom pipe-map
    /// let grid = grid.with_pipes(PIPEMAPS.get("Medium").unwrap());
    /// 
    /// //Or like this
    /// let grid = Grid::new(4, 4).with_pipes(PIPEMAPS.get("Medium").unwrap());
    /// 
    /// ```
pub static PIPEMAPS: phf::Map<&'static str, &'static PipeMap> = phf_map! {
    "Thin" => &PIPEMAP_THIN,
    "Medium" => &PIPEMAP_MEDIUM,
    "Thick" => &PIPEMAP_THICK,
};

static PIPEMAP_THIN: PipeMap = phf_map! {
    "horizontal" => "",
    "vertical" => "",
    "top_left" => "",
    "top_right" => "",
    "bottom_left" => "",
    "bottom_right" => "",
    "top_horizontal" => "",
    "bottom_horizontal" => "",
    "left_vertical" => "",
    "right_vertical" => "",
    "cross" => "",
};
static PIPEMAP_MEDIUM: PipeMap = phf_map! {
    "horizontal" => "",
    "vertical" => "",
    "top_left" => "",
    "top_right" => "",
    "bottom_left" => "",
    "bottom_right" => "",
    "top_horizontal" => "",
    "bottom_horizontal" => "",
    "left_vertical" => "",
    "right_vertical" => "",
    "cross" => "",
};
static PIPEMAP_THICK: PipeMap = phf_map! {
    "horizontal" => "",
    "vertical" => "",
    "top_left" => "",
    "top_right" => "",
    "bottom_left" => "",
    "bottom_right" => "",
    "top_horizontal" => "",
    "bottom_horizontal" => "",
    "left_vertical" => "",
    "right_vertical" => "",
    "cross" => "",
};


//tests
#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    #[should_panic]
    fn zero_grid() {
        let _grid = Grid::new(1, 0);
    }
    #[test]
    fn add_random_number() {
        let grid = Grid::new(4, 4);
        let grid = grid.add_random_number().unwrap();
        println!("{:?}", grid);
    }

    #[test]
    fn compress_row() {
        let grid = Grid::new(4, 4);
        let row = grid.compress_row(&vec![2, 0, 4, 0]);
        assert_eq!(row, vec![2, 4, 0, 0]);
        let row = grid.compress_row(&vec![4, 0, 2, 8]);
        assert_eq!(row, vec![4, 2, 8, 0]);
    }

    #[test]
    fn combine_row() {
        let grid = Grid::new(4, 4);
        let row = grid.combine_row(&vec![1, 0, 1, 0]);
        assert_eq!(row, vec![2, 0, 0, 0]);
        let row = grid.combine_row(&vec![2, 2, 3, 4, 6, 6, 5, 0, 6]);
        assert_eq!(row, vec![3, 3, 4, 7, 5, 6, 0, 0, 0]);
    }

    #[test]
    fn overwrite_rows() {
        let grid = Grid::new(2, 2);
        assert_eq!(grid.rows.len(), 2);
        let new_rows = vec![vec![1; 4]; 4];
        let grid = Grid::from_rows(new_rows.clone());
        assert_eq!(grid.rows, new_rows);
    }
}