jcblocks 0.1.1

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
use std::fmt;

use crate::block::Block;

#[derive(Debug, Clone)]
pub enum PointStatus {
    Occupied,
    Empty,
    MarkedForRemoval,
}

#[derive(Debug, Clone)]
pub struct PlayableBlock {
    block: Block,
    row: i32,
    column: i32,
}

/// Canvas holds the state of the board.
#[derive(Clone)]
pub struct Canvas {
    pub columns: usize,
    pub rows: usize,
    contents: Vec<PointStatus>,
}

pub const DEFAULT_CANVAS_HEIGHT: usize = 8;
pub const DEFAULT_CANVAS_WIDTH: usize = 8;

impl Canvas {
    /// Create an empty board.
    pub fn new(rows: usize, columns: usize) -> Self {
        Canvas {
            columns,
            rows,
            contents: vec![PointStatus::Empty; usize::from(rows * columns)],
        }
    }

    /// Returns a the status for each point on the canvas.
    pub fn contents(&self) -> &Vec<PointStatus> {
        &self.contents
    }

    /// Remove all pieces from the canvas.
    pub fn clear_all(&mut self) -> &mut Self {
        self.contents.fill(PointStatus::Empty);
        self
    }

    /// Translate from row/col domain to 1d-array with stride domain.
    ///
    /// Returns `None` for invalid positions.
    fn position_to_index(&self, x: i32, y: i32) -> Option<usize> {
        if x < 0 || y < 0 || x >= self.columns as i32 || y >= self.rows as i32 {
            return None;
        }

        Some(self.columns * y as usize + x as usize)
    }

    /// Returns true if `block`'s coordinates would fit if the origin of the block was placed at
    /// the specified row/column.
    pub fn can_fit_at(&self, block: &Block, row: i32, column: i32) -> bool {
        for p in block.coordinates() {
            let Some(index) = self.position_to_index(column + p.x, row + p.y) else {
                return false;
            };

            if let PointStatus::Occupied = self.contents[index] {
                return false;
            }
        }

        true
    }

    pub fn can_fit(&self, block: &Block) -> Option<PlayableBlock> {
        for column in 0..self.columns {
            for row in 0..self.rows {
                if self.can_fit_at(&block, row as i32, column as i32) {
                    return Some(PlayableBlock {
                        block: block.clone(),
                        row: row as i32,
                        column: column as i32,
                    });
                }
            }
        }

        None
    }

    /// Returns None if the block is not playable.
    pub fn try_make_playable(&self, block: &Block, row: i32, column: i32) -> Option<PlayableBlock> {
        if !self.can_fit_at(block, row, column) {
            return None;
        }

        Some(PlayableBlock {
            block: block.clone(),
            row,
            column,
        })
    }

    /// Add `block` to the canvas.
    pub fn add(&mut self, block: &PlayableBlock) -> &mut Self {
        for p in block.block.coordinates() {
            if let Some(index) = self.position_to_index(block.column + p.x, block.row + p.y) {
                self.contents[index] = PointStatus::Occupied;
            }
        }

        self
    }

    /// Clear all completed rows and columns then returns number of rows and columns removed.
    pub fn clear_completed_lines(&mut self) -> usize {
        let mut removed = 0;

        // mark cols
        for col in 0..self.columns {
            if let Some(true) = self.is_complete_column(col) {
                for row in 0..self.rows {
                    if let Some(index) = self.position_to_index(col as i32, row as i32) {
                        self.contents[index] = PointStatus::MarkedForRemoval;
                    }
                }
                removed += 1;
            }
        }

        // mark rows
        for row in 0..self.rows {
            if let Some(true) = self.is_complete_row(row) {
                for col in 0..self.columns {
                    if let Some(index) = self.position_to_index(col as i32, row as i32) {
                        self.contents[index] = PointStatus::MarkedForRemoval;
                    }
                }
                removed += 1;
            }
        }

        // mark empty
        for p in self.contents.iter_mut() {
            if let PointStatus::MarkedForRemoval = *p {
                *p = PointStatus::Empty;
            }
        }

        removed
    }

    /// Return `Some(true)` if the row is completely occupied.
    pub fn is_complete_row(&self, row: usize) -> Option<bool> {
        // Invalid row selection.
        if self.rows <= row {
            return None;
        }

        let mut sum = 0;
        for col in 0..self.columns {
            if let Some(index) = self.position_to_index(col as i32, row as i32) {
                sum = match self.contents[index] {
                    PointStatus::Occupied => sum + 1,
                    PointStatus::MarkedForRemoval => sum + 1,
                    PointStatus::Empty => sum,
                };
            }
        }

        if sum != self.columns {
            return Some(false);
        }

        Some(true)
    }

    /// Return `Some(true)` if the column is completely occupied.
    pub fn is_complete_column(&self, column: usize) -> Option<bool> {
        // Invalid column selection.
        if self.columns <= column {
            return None;
        }

        let mut sum = 0;

        for row in 0..self.rows {
            if let Some(index) = self.position_to_index(column as i32, row as i32) {
                sum = match self.contents[index] {
                    PointStatus::Occupied => sum + 1,
                    PointStatus::MarkedForRemoval => sum + 1,
                    PointStatus::Empty => sum,
                };
            }
        }

        if sum != self.rows {
            return Some(false);
        }

        Some(true)
    }
}

impl Default for Canvas {
    fn default() -> Self {
        Canvas::new(DEFAULT_CANVAS_HEIGHT, DEFAULT_CANVAS_WIDTH)
    }
}

impl fmt::Debug for Canvas {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        let mut canvas_char_view = Vec::new();
        for row in (0..self.rows).rev() {
            canvas_char_view.push(char::from_digit(row as u32, 10).unwrap());
            canvas_char_view.push(' ');
            for col in 0..self.columns {
                let content_index = self.position_to_index(col as i32, row as i32).unwrap();
                let marker = match self.contents[content_index] {
                    PointStatus::Occupied => '',
                    PointStatus::MarkedForRemoval => '',
                    PointStatus::Empty => '.',
                };
                canvas_char_view.push(marker);
                canvas_char_view.push(' ');
            }
            canvas_char_view.push('\n');
        }

        // whitespace before x labels
        for _ in 0..2 {
            canvas_char_view.push(' ');
        }

        // x labels
        for c in "01234567".chars() {
            canvas_char_view.push(c);
            canvas_char_view.push(' ');
        }
        canvas_char_view.push('\n');

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

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

    use super::*;

    macro_rules! test_position_to_index {
        ( $name:ident, $x:expr, $y:expr, $expected:expr) => {
            #[test]
            fn $name() {
                let board = Canvas::new(8, 8);
                let index = board.position_to_index($x, $y);
                if let Some(i) = index {
                    assert_eq!($expected, i);
                } else {
                    assert!(false, "Expected a valid index from a value position.");
                }
            }
        };
    }

    macro_rules! test_position_to_index_fail {
        ( $name:ident, $x:expr, $y:expr) => {
            #[test]
            fn $name() {
                let board = Canvas::new(8, 8);
                let index = board.position_to_index($x, $y);
                if let Some(_) = index {
                    assert!(false, "Expected a invalid position to fail.");
                }
            }
        };
    }

    test_position_to_index!(pos_to_idx_x0_y0_maps_to_0, 0, 0, 0);
    test_position_to_index!(pos_to_idx_x1_y0_maps_to_1, 1, 0, 1);
    test_position_to_index!(pos_to_idx_x0_y1_maps_to_8, 0, 1, 8);
    test_position_to_index!(pos_to_idx_x0_y2_maps_to_16, 0, 2, 16);
    test_position_to_index!(pos_to_idx_x1_y2_maps_to_17, 1, 2, 17);
    test_position_to_index!(pos_to_idx_x8_y8_maps_to_63, 7, 7, 63);

    test_position_to_index_fail!(pos_to_idx_negative_x, -1, 0);
    test_position_to_index_fail!(pos_to_idx_negative_y, 0, -1);
    test_position_to_index_fail!(pos_to_idx_negative_x_and_y, -3, -3);
    test_position_to_index_fail!(pos_to_idx_large_x, 10, 1);
    test_position_to_index_fail!(pos_to_idx_large_y, 1, 10);
    test_position_to_index_fail!(pos_to_idx_large_x_and_y, 8, 8);

    macro_rules! test_add_blocks {
        ( $name:ident, $blocks:expr, $should_add:expr, $where_to_add:expr ) => {
            #[test]
            fn $name() {
                let mut board = Canvas::new(8, 8);

                // validate the test input
                assert!(
                    $blocks.len() == $should_add.len(),
                    "All lists should be equal length."
                );
                assert!(
                    $blocks.len() == $where_to_add.len(),
                    "All lists should be equal length."
                );

                for (i, b) in $blocks.into_iter().enumerate() {
                    let maybe_playable =
                        board.try_make_playable(&b, $where_to_add[i].y, $where_to_add[i].x);

                    if let Some(playable) = maybe_playable {
                        board.add(&playable);
                    } else {
                        assert!(!$should_add[i], "Unable to add block[{i}]\n{board:?}");
                    }
                }
            }
        };
    }

    test_add_blocks!(
        can_add_one_and_only_one_1x1_in_a_position,
        [Block::rectangle(1, 1), Block::rectangle(1, 1)],
        [true, false],
        [Point { x: 0, y: 0 }, Point { x: 0, y: 0 }]
    );

    test_add_blocks!(
        can_add_many_1x1s_to_different_positions,
        [
            Block::rectangle(1, 1),
            Block::rectangle(1, 1),
            Block::rectangle(1, 1),
            Block::rectangle(1, 1),
            Block::rectangle(1, 1),
        ],
        [true, true, true, true, true],
        [
            Point { x: 0, y: 0 },
            Point { x: 0, y: 1 },
            Point { x: 1, y: 0 },
            Point { x: 4, y: 4 },
            Point { x: 7, y: 7 },
        ]
    );

    test_add_blocks!(
        can_add_many_rectangles,
        [
            Block::rectangle(1, 1),
            Block::rectangle(2, 2),
            Block::rectangle(3, 3),
            Block::rectangle(5, 1),
            Block::rectangle(5, 1),
        ],
        [true, true, true, true, true],
        [
            Point { x: 0, y: 0 },
            Point { x: 0, y: 1 },
            Point { x: 0, y: 3 },
            Point { x: 0, y: 6 },
            Point { x: 0, y: 7 },
        ]
    );

    test_add_blocks!(
        can_fill_board,
        [
            Block::rectangle(1, 5),
            Block::rectangle(1, 5),
            Block::rectangle(1, 5),
            Block::rectangle(1, 5),
            Block::rectangle(1, 5),
            Block::rectangle(1, 5),
            Block::rectangle(1, 5),
            Block::rectangle(1, 5),
            Block::rectangle(1, 3),
            Block::rectangle(1, 3),
            Block::rectangle(1, 3),
            Block::rectangle(1, 3),
            Block::rectangle(1, 3),
            Block::rectangle(1, 3),
            Block::rectangle(1, 3),
            Block::rectangle(1, 3),
        ],
        [
            true, true, true, true, true, true, true, true, true, true, true, true, true, true,
            true, true,
        ],
        [
            Point { x: 0, y: 0 },
            Point { x: 1, y: 0 },
            Point { x: 2, y: 0 },
            Point { x: 3, y: 0 },
            Point { x: 4, y: 0 },
            Point { x: 5, y: 0 },
            Point { x: 6, y: 0 },
            Point { x: 7, y: 0 },
            Point { x: 0, y: 5 },
            Point { x: 1, y: 5 },
            Point { x: 2, y: 5 },
            Point { x: 3, y: 5 },
            Point { x: 4, y: 5 },
            Point { x: 5, y: 5 },
            Point { x: 6, y: 5 },
            Point { x: 7, y: 5 },
        ]
    );

    #[test]
    fn cant_fit_when_full() {
        let mut original = Canvas::new(8, 8);
        for c in original.contents.iter_mut() {
            *c = PointStatus::Occupied;
        }

        let all_blocks: [Block; 14] = [
            Block::rectangle(3, 3),
            Block::rectangle(3, 2),
            Block::rectangle(2, 3),
            Block::rectangle(2, 2),
            Block::rectangle(1, 1),
            Block::tee(),
            Block::line(2),
            Block::line(3),
            Block::line(4),
            Block::line(5),
            Block::elle(3, 3),
            Block::elle(3, 2),
            Block::elle(2, 3),
            Block::elle(2, 2),
        ];
        for block in all_blocks {
            assert!(original.can_fit(&block).is_none());
        }
    }

    #[test]
    fn can_fit_when_barely_empty() {
        let mut original = Canvas::new(8, 8);
        original.contents.fill(PointStatus::Occupied);
        original.contents[63] = PointStatus::Empty;

        let wont_fit: [Block; 13] = [
            Block::rectangle(3, 3),
            Block::rectangle(3, 2),
            Block::rectangle(2, 3),
            Block::rectangle(2, 2),
            Block::tee(),
            Block::line(2),
            Block::line(3),
            Block::line(4),
            Block::line(5),
            Block::elle(3, 3),
            Block::elle(3, 2),
            Block::elle(2, 3),
            Block::elle(2, 2),
        ];

        for block in wont_fit {
            assert!(
                original.can_fit(&block).is_none(),
                "Expected {} not to fit!",
                block
            );
        }

        // the only one that should fit
        assert!(
            original.can_fit(&Block::rectangle(1, 1)).is_some(),
            "Expected 1x1 to fit!"
        );
    }

    #[test]
    fn can_clone() {
        let mut original = Canvas::new(3, 3);
        original.contents[0] = PointStatus::Occupied;
        original.contents[1] = PointStatus::Occupied;
        original.contents[2] = PointStatus::Occupied;

        let duplicate = original.clone();
        for i in 0..3 {
            if let PointStatus::Occupied = duplicate.contents[i] {
            } else {
                assert!(false, "Expected contents to be cloned");
            }
        }

        if let PointStatus::Empty = duplicate.contents[3] {
        } else {
            assert!(false, "Expected contents to be cloned");
        }
    }
}