jcblocks 0.1.0

Components for constructing block games such as Tetris/BlockBlast.
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
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
use std::collections::HashMap;
use std::fmt::{Debug, Display, Formatter};

use rand::distr::{Distribution, StandardUniform};
use rand::{Rng, random};

/// The smallest component of a peice.
/// ```
/// ┌─┐
/// └─┘
/// ```
#[derive(PartialEq, Debug, Clone)]
pub struct Point {
    pub x: i32,
    pub y: i32,
}

impl Point {
    /// Rotate right 90 degrees about the origin.
    pub fn rotate_right(&mut self) -> &mut Self {
        let tmp = self.x;
        self.x = self.y;
        self.y = 0 - tmp;
        self
    }

    /// Rotate left 90 degrees about the origin.
    pub fn rotate_left(&mut self) -> &mut Self {
        let tmp = self.x;
        self.x = 0 - self.y;
        self.y = tmp;
        self
    }
}

impl Default for Point {
    fn default() -> Self {
        Self { x: 0, y: 0 }
    }
}

pub const MAX_RECTANGLE_EDGE: usize = 3;
pub const MAX_LINE_LENGTH: usize = 5;
pub const MIN_ELLE_EDGE: usize = 2;
pub const MAX_ELLE_EDGE: usize = 3;

#[derive(Debug, Clone)]
pub enum Variant {
    /// The following shapes can be created as a Rectangle:
    /// ```
    /// ┌─┐ ┌─┬─┐ ┌─┬─┐ ┌─┬─┬─┐ ┌─┬─┬─┐
    /// └─┘ ├─┼─┤ ├─┼─┤ ├─┼─┼─┤ ├─┼─┼─┤
    ///     └─┴─┘ ├─┼─┤ └─┴─┴─┘ ├─┼─┼─┤
    ///           └─┴─┘         └─┴─┴─┘
    /// ```
    /// Where the origin (0,0) is the lower leftmost block.
    ///
    /// NOTE: Though you are free to create `Line`s this way, prefer using `Line` explicitly.
    Rectangle,

    /// ```
    ///   ┌─┐  
    /// ┌─┼─┼─┐
    /// └─┴─┴─┘
    /// ```
    /// Where the origin (0,0) is the lower leftmost block.
    Tee,
    Diagonal,
    Elle,

    /// ┌─┬─┐    ┌─┬─┬─┐   ┌─┬─┬─┬─┐  ┌─┬─┬─┬─┬─┐
    /// └─┴─┘    └─┴─┴─┘   └─┴─┴─┴─┘  └─┴─┴─┴─┴─┘
    /// Where the origin (0,0) is the lower leftmost block.
    Line,
}

impl Distribution<Variant> for StandardUniform {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Variant {
        match rng.random_range(0..=4) {
            0 => Variant::Rectangle,
            1 => Variant::Tee,
            2 => Variant::Diagonal,
            3 => Variant::Elle,
            _ => Variant::Line,
        }
    }
}

impl Display for Variant {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let name = match self {
            Variant::Diagonal => "Diagonal",
            Variant::Elle => "Elle",
            Variant::Tee => "Tee",
            Variant::Rectangle => "Rectangle",
            Variant::Line => "Line",
        };
        write!(f, "{name}")
    }
}

pub struct Dimension {
    pub height: usize,
    pub width: usize,
}

#[derive(Clone)]
pub struct Block {
    coords: Vec<Point>,
    variant: Variant,
}

impl Block {
    /// Tee constructor. Tees are always the same size.
    pub fn tee() -> Self {
        let mut coords = Vec::new();

        for i in 0..3 {
            coords.push(Point { x: i, y: 0 });
        }
        coords.push(Point { x: 1, y: 1 });

        Self {
            coords,
            variant: Variant::Tee,
        }
    }

    /// Width/Height are restricted to the range [1, `MAX_RECTANGLE_EDGE`].
    pub fn rectangle(width: usize, height: usize) -> Self {
        let mut coords = Vec::new();

        for i in 0..width.clamp(1, MAX_RECTANGLE_EDGE) {
            for j in 0..height.clamp(1, MAX_RECTANGLE_EDGE) {
                coords.push(Point {
                    x: i as i32,
                    y: j as i32,
                });
            }
        }

        Self {
            coords,
            variant: Variant::Rectangle,
        }
    }

    /// Length is restricted to the range [2, `MAX_LINE_LENGTH`].
    pub fn line(length: usize) -> Self {
        let mut coords = Vec::new();

        for i in 0..length.clamp(2, MAX_LINE_LENGTH) {
            coords.push(Point {
                x: i as i32,
                y: 0 as i32,
            });
        }

        Self {
            coords,
            variant: Variant::Line,
        }
    }

    pub fn diagonal(width: usize) -> Self {
        let mut coords = Vec::new();

        for i in 0..width {
            coords.push(Point {
                x: i as i32,
                y: i as i32,
            });
        }

        Self {
            coords,
            variant: Variant::Diagonal,
        }
    }

    /// Width/Height are restricted to the range [`MIN_ELLE_EDGE`, `MAX_ELLE_EDGE`].
    pub fn elle(width: usize, height: usize) -> Self {
        let mut coords = Vec::new();

        coords.push(Point { x: 0, y: 0 });
        for i in 1..height.clamp(MIN_ELLE_EDGE, MAX_ELLE_EDGE) {
            coords.push(Point { x: i as i32, y: 0 });
        }

        for i in 1..width.clamp(MIN_ELLE_EDGE, MAX_ELLE_EDGE) {
            coords.push(Point { x: 0, y: i as i32 });
        }

        Self {
            coords,
            variant: Variant::Elle,
        }
    }

    pub fn coordinates(&self) -> &Vec<Point> {
        &self.coords
    }

    pub fn coordinates_mut(&mut self) -> &mut Vec<Point> {
        &mut self.coords
    }

    pub fn dimensions(&self) -> Dimension {
        // diagonals can be computed trivially
        if let Variant::Diagonal = self.variant {
            let points = self.coords.len();
            return Dimension {
                width: points,
                height: points,
            };
        }

        // Create a histogram of x/y values then just grab the max.
        let height: i32 = self
            .coordinates()
            .iter()
            .fold(&mut HashMap::new(), |acc, coord| {
                if let Some(val) = acc.get(&coord.x) {
                    acc.insert(coord.x, val + 1);
                } else {
                    acc.insert(coord.x, 1);
                };

                acc
            })
            .drain()
            .map(|(_k, v)| v)
            .max()
            .unwrap_or(0);

        let width: i32 = self
            .coordinates()
            .iter()
            .fold(&mut HashMap::new(), |acc, coord| {
                if let Some(val) = acc.get(&coord.y) {
                    acc.insert(coord.y, val + 1);
                } else {
                    acc.insert(coord.y, 1);
                };

                acc
            })
            .drain()
            .map(|(_k, v)| v)
            .max()
            .unwrap_or(0);

        Dimension {
            width: width as usize,
            height: height as usize,
        }
    }

    /// Rotate 90 degrees to the right about the origin.
    pub fn rotate_right(&mut self) -> &mut Self {
        self.coordinates_mut().iter_mut().for_each(|p| {
            p.rotate_right();
        });
        self
    }

    /// Rotate 90 degrees to the left about the origin.
    pub fn rotate_left(&mut self) -> &mut Self {
        self.coordinates_mut().iter_mut().for_each(|p| {
            p.rotate_left();
        });
        self
    }
}

impl Distribution<Block> for StandardUniform {
    fn sample<R: Rng + ?Sized>(&self, rng: &mut R) -> Block {
        let variant: Variant = random();
        let width = rng.random::<u8>() as usize % MAX_RECTANGLE_EDGE + 1;
        let height = rng.random::<u8>() as usize % MAX_RECTANGLE_EDGE + 1;

        match variant {
            Variant::Rectangle => Block::rectangle(width, height),
            Variant::Tee => Block::tee(),
            Variant::Elle => Block::elle(width, height),
            Variant::Diagonal => Block::diagonal(width),
            Variant::Line => Block::line(width),
        }
    }
}

impl Display for Block {
    /// Textual (unicode) representation of a block.
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        // Printing a block requires the allocation of a large enough rectangle to fit it plus some
        // whitespace in between points and new lines at the end of each row.
        let dimensions = self.dimensions();
        let display_repr_height = dimensions.height;
        let display_repr_width = dimensions.width * 2 + 1;

        // Blocks are encoded assuming a standard coordinate system, where x grows right and y
        // grows up. Printing to stdout naturally occurs top to bottom, so a bit of translation is
        // required.
        let min_y = self.coordinates().iter().map(|p| p.y).min().unwrap_or(0);
        let min_x = self.coordinates().iter().map(|p| p.x).min().unwrap_or(0);
        let coord_to_index = |p: &Point| -> usize {
            // normalize all shapes to be in the first quadrant
            let norm_x = (p.x - min_x) as usize;
            let norm_y = (p.y - min_y) as usize;

            display_repr_width * (display_repr_height - 1 - norm_y) + norm_x * 2
        };

        let mut buf = vec![' '; display_repr_width * display_repr_height];
        for row in 1..=display_repr_height {
            // 2x2 Rectangle, view vs buffer index
            //  ▅ _ ▅ _ \n
            //  0 1 2 3 4
            //  ▅ _ ▅ _ \n
            //  5 6 7 8 9
            let end_of_row_position = display_repr_width * row - 1;
            buf[end_of_row_position] = '\n';
        }

        for c in self.coordinates().iter() {
            let index = coord_to_index(c);
            buf[index] = '';
        }

        let block_str_view: String = buf.into_iter().collect();
        write!(f, "{}", block_str_view)
    }
}

impl Debug for Block {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        let dimensions = self.dimensions();
        write!(
            f,
            "{}: {}x{}\n{}",
            self.variant, dimensions.width, dimensions.height, self
        )
    }
}

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

    macro_rules! test_can_create {
        ( $name:ident, $width:expr, $height:expr, $expected_coords:expr ) => {
            #[test]
            fn $name() {
                let block = Block::rectangle($width, $height);
                assert_eq!($height * $width, block.coordinates().len());

                for c in $expected_coords {
                    assert!(block.coordinates().contains(&c));
                }
            }
        };
    }

    // Exhaustively test all blocks, as there are a small set of them and they are core to the
    // puzzle.

    // ┌─┐
    // └─┘
    test_can_create!(can_create_1x1, 1, 1, vec![Point { x: 0, y: 0 }]);

    // ┌─┬─┐
    // ├─┼─┤
    // └─┴─┘
    test_can_create!(can_create_2x2, 2, 2, vec![Point { x: 0, y: 0 }]);

    // ┌─┬─┐
    // ├─┼─┤
    // ├─┼─┤
    // └─┴─┘
    test_can_create!(
        can_create_2x3_rect,
        2,
        3,
        vec![
            Point { x: 0, y: 0 },
            Point { x: 0, y: 1 },
            Point { x: 0, y: 2 },
            Point { x: 1, y: 0 },
            Point { x: 1, y: 1 },
            Point { x: 1, y: 2 },
        ]
    );

    // ┌─┬─┬─┐
    // ├─┼─┼─┤
    // └─┴─┴─┘
    test_can_create!(
        can_create_3x2_rect,
        3,
        2,
        vec![
            Point { x: 0, y: 0 },
            Point { x: 0, y: 1 },
            Point { x: 1, y: 0 },
            Point { x: 1, y: 1 },
            Point { x: 2, y: 0 },
            Point { x: 2, y: 1 },
        ]
    );

    // ┌─┬─┬─┐
    // ├─┼─┼─┤
    // ├─┼─┼─┤
    // └─┴─┴─┘
    test_can_create!(
        can_create_3x3_rect,
        3,
        3,
        vec![
            Point { x: 0, y: 0 },
            Point { x: 0, y: 1 },
            Point { x: 0, y: 2 },
            Point { x: 1, y: 0 },
            Point { x: 1, y: 1 },
            Point { x: 1, y: 2 },
            Point { x: 2, y: 0 },
            Point { x: 2, y: 1 },
            Point { x: 2, y: 2 },
        ]
    );

    macro_rules! test_dimensions {
        ( $name:ident, $block:expr, $expected_width:expr, $expected_height:expr ) => {
            #[test]
            fn $name() {
                let dimensions = $block.dimensions();
                assert_eq!($expected_width, dimensions.width);
                assert_eq!($expected_height, dimensions.height);
            }
        };
    }

    test_dimensions!(test_dimensions_1x1_rect, Block::rectangle(1, 1), 1, 1);
    test_dimensions!(
        test_dimensions_1x1_rect_rot,
        Block::rectangle(1, 1).rotate_right(),
        1,
        1
    );
    test_dimensions!(test_dimensions_2x2_rect, Block::rectangle(2, 2), 2, 2);
    test_dimensions!(
        test_dimensions_2x2_rect_rot,
        Block::rectangle(2, 2).rotate_left(),
        2,
        2
    );
    test_dimensions!(test_dimensions_len2_line, Block::line(2), 2, 1);
    test_dimensions!(
        test_dimensions_len2_line_rot,
        Block::line(2).rotate_left(),
        1,
        2
    );
    test_dimensions!(test_dimensions_len3_line, Block::line(3), 3, 1);
    test_dimensions!(test_dimensions_len5_line, Block::line(5), 5, 1);
    test_dimensions!(test_dimensions_0deg_tee, Block::tee(), 3, 2);
    test_dimensions!(test_dimensions_90deg_tee, Block::tee().rotate_left(), 2, 3);
    test_dimensions!(test_dimensions_3x2_0deg_elle, Block::elle(2, 3), 3, 2);
    test_dimensions!(
        test_dimensions_3x2_90deg_elle,
        Block::elle(2, 3).rotate_right(),
        2,
        3
    );
    test_dimensions!(test_dimensions_2x2_0deg_elle, Block::elle(2, 2), 2, 2);
    test_dimensions!(
        test_dimensions_2x2_90deg_elle,
        Block::elle(2, 2).rotate_right(),
        2,
        2
    );
    test_dimensions!(test_dimensions_2_0deg_diag, Block::diagonal(2), 2, 2);
    test_dimensions!(
        test_dimensions_2_90deg_diag,
        Block::diagonal(2).rotate_left(),
        2,
        2
    );

    test_dimensions!(test_dimensions_5_0deg_diag, Block::diagonal(5), 5, 5);
    test_dimensions!(
        test_dimensions_5_90deg_diag,
        Block::diagonal(5).rotate_left(),
        5,
        5
    );

    macro_rules! test_rotate_right {
        ( $name:ident, $block:expr, $num_rotations:expr, $expected_coords:expr ) => {
            #[test]
            fn $name() {
                let mut shape = $block;
                for _ in 0..$num_rotations {
                    shape.rotate_right();
                }

                assert_eq!($expected_coords.len(), shape.coordinates().len());
                for p in $expected_coords {
                    assert!(
                        shape.coordinates().contains(&p),
                        "Expected {:?} in coordinates after {} right rotation(s). Got {:?}",
                        p,
                        $num_rotations,
                        shape.coordinates(),
                    );
                }
            }
        };
    }

    test_rotate_right!(
        can_rotate_right_once,
        Block::tee(),
        1,
        vec![
            Point { x: 0, y: 0 },
            Point { x: 0, y: -1 },
            Point { x: 0, y: -2 },
            Point { x: 1, y: -1 },
        ]
    );

    test_rotate_right!(
        can_rotate_right_twice,
        Block::tee(),
        2,
        vec![
            Point { x: 0, y: 0 },
            Point { x: -1, y: 0 },
            Point { x: -2, y: 0 },
            Point { x: -1, y: -1 },
        ]
    );

    test_rotate_right!(
        can_rotate_right_thrice,
        Block::tee(),
        3,
        vec![
            Point { x: 0, y: 0 },
            Point { x: 0, y: 1 },
            Point { x: 0, y: 2 },
            Point { x: -1, y: 1 },
        ]
    );

    test_rotate_right!(
        can_rotate_right_completely,
        Block::tee(),
        4,
        Block::tee().coordinates()
    );

    macro_rules! test_rotate_left {
        ( $name:ident, $block:expr, $num_rotations:expr, $expected_coords:expr ) => {
            #[test]
            fn $name() {
                let mut shape = $block;
                for _ in 0..$num_rotations {
                    shape.rotate_left();
                }

                assert_eq!($expected_coords.len(), shape.coordinates().len());
                for p in $expected_coords {
                    assert!(
                        shape.coordinates().contains(&p),
                        "Expected {:?} in coordinates after {} left rotation(s). Got {:?}",
                        p,
                        $num_rotations,
                        shape.coordinates(),
                    );
                }
            }
        };
    }

    test_rotate_left!(
        can_rotate_left_once,
        Block::tee(),
        1,
        vec![
            Point { x: 0, y: 0 },
            Point { x: 0, y: 1 },
            Point { x: 0, y: 2 },
            Point { x: -1, y: 1 },
        ]
    );

    test_rotate_left!(
        can_rotate_left_twice,
        Block::tee(),
        2,
        vec![
            Point { x: 0, y: 0 },
            Point { x: -1, y: 0 },
            Point { x: -2, y: 0 },
            Point { x: -1, y: -1 },
        ]
    );

    test_rotate_left!(
        can_rotate_left_thrice,
        Block::tee(),
        3,
        vec![
            Point { x: 0, y: 0 },
            Point { x: 0, y: -1 },
            Point { x: 0, y: -2 },
            Point { x: 1, y: -1 },
        ]
    );

    test_rotate_left!(
        can_rotate_left_completely,
        Block::tee(),
        4,
        Block::tee().coordinates()
    );
}