tetrs-tui 0.1.0

A terminal-based Tetris clone built using the Cursive TUI library.
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
use crate::{
    board,
    text_art::BLOCK_CHAR,
    tile::{Block, Tile},
};
use cursive::{
    Printer, View,
    theme::{BaseColor, Color},
};
use rand::Rng;

#[derive(Clone, Copy)]
pub enum PieceType {
    I,
    O,
    J,
    L,
    S,
    Z,
    T,
}

const LAYOUT_LEN: usize = 4;
pub type PieceLayout = [[Tile; LAYOUT_LEN]; LAYOUT_LEN];

pub enum Rotation {
    Left,
    Right,
}

impl PieceType {
    fn get_colored_block(&self) -> Block {
        match self {
            PieceType::I => Block::Cyan,
            PieceType::O => Block::Yellow,
            PieceType::J => Block::Blue,
            PieceType::L => Block::Orange,
            PieceType::S => Block::Green,
            PieceType::Z => Block::Red,
            PieceType::T => Block::Magenta,
        }
    }
    // for checking if rotation is possible
    fn get_rot_diameter(&self) -> usize {
        match self {
            PieceType::I => 4,
            PieceType::O => 4,
            _ => 3,
        }
    }
    fn get_layout(&self) -> PieceLayout {
        type Lay = [[u8; LAYOUT_LEN]; LAYOUT_LEN];

        static I_LAYOUT: Lay = [
            [0, 0, 0, 0],
            [1, 1, 1, 1],
            [0, 0, 0, 0],
            [0, 0, 0, 0],
        ];
        static O_LAYOUT: Lay = [
            [0, 0, 0, 0],
            [0, 1, 1, 0],
            [0, 1, 1, 0],
            [0, 0, 0, 0],
        ];
        static J_LAYOUT: Lay = [
            [1, 0, 0, 0],
            [1, 1, 1, 0],
            [0, 0, 0, 0],
            [0, 0, 0, 0],
        ];
        static L_LAYOUT: Lay = [
            [0, 0, 1, 0],
            [1, 1, 1, 0],
            [0, 0, 0, 0],
            [0, 0, 0, 0],
        ];
        static S_LAYOUT: Lay = [
            [0, 1, 1, 0],
            [1, 1, 0, 0],
            [0, 0, 0, 0],
            [0, 0, 0, 0],
        ];
        static Z_LAYOUT: Lay = [
            [1, 1, 0, 0],
            [0, 1, 1, 0],
            [0, 0, 0, 0],
            [0, 0, 0, 0],
        ];
        static T_LAYOUT: Lay = [
            [0, 1, 0, 0],
            [1, 1, 1, 0],
            [0, 0, 0, 0],
            [0, 0, 0, 0],
        ];

        let bitmap_layout: Lay = match self {
            PieceType::I => I_LAYOUT,
            PieceType::O => O_LAYOUT,
            PieceType::J => J_LAYOUT,
            PieceType::L => L_LAYOUT,
            PieceType::S => S_LAYOUT,
            PieceType::Z => Z_LAYOUT,
            PieceType::T => T_LAYOUT,
        };

        self.to_tilemap(bitmap_layout)
    }
    fn to_tilemap(&self, bitmap: [[u8; LAYOUT_LEN]; LAYOUT_LEN]) -> PieceLayout {
        let mut layout: PieceLayout = [[None; LAYOUT_LEN]; LAYOUT_LEN];
        for i in 0..LAYOUT_LEN {
            for j in 0..LAYOUT_LEN {
                if bitmap[i][j] == 1 {
                    layout[i][j] = Some(self.get_colored_block());
                }
            }
        }
        layout
    }
}
#[derive(Clone, Copy)]
pub struct Piece {
    piece_type: PieceType,
    layout: PieceLayout,
    // of top left, signed so piece itself can go to edge even when top left of 4x4 layout is at
    // some 0 coord
    coord: (i8, i8),
}

impl Piece {
    pub fn piece_type(&self) -> PieceType {
        self.piece_type
    }
    pub fn layout(&self) -> &PieceLayout {
        &self.layout
    }
    pub fn layout_mut(&mut self) -> &mut PieceLayout {
        &mut self.layout
    }
    pub fn coord(&self) -> (i8, i8) {
        self.coord
    }
    pub fn random_new() -> Self {
        let mut rng = rand::rng();
        let piece_type = match rng.random_range(0..=6) {
            0 => PieceType::I,
            1 => PieceType::O,
            2 => PieceType::J,
            3 => PieceType::L,
            4 => PieceType::S,
            5 => PieceType::Z,
            6 => PieceType::T,
            _ => PieceType::I, // shouldn't happen
        };
        let layout = piece_type.get_layout();
        Self {
            piece_type: piece_type,
            layout: layout,
            coord: (0, 0),
        }
    }
    pub fn new(piece_type: PieceType) -> Self {
        let layout = piece_type.get_layout();
        Self {
            piece_type: piece_type,
            layout: layout,
            coord: (0, 0),
        }
    }

    pub fn at(mut self, x: i8, y: i8) -> Self {
        self.coord = (x, y);
        self
    }
    pub fn set_at(&mut self, x: i8, y: i8) -> &Self {
        self.coord = (x, y);
        self
    }
    pub fn move_left(&mut self) {
        self.coord.0 -= 1
    }

    pub fn move_right(&mut self) {
        self.coord.0 += 1;
    }
    pub fn move_down(&mut self) {
        self.coord.1 += 1;
    }

    pub fn move_by(&mut self, x: i8, y: i8) {
        self.coord.0 += x;
        self.coord.1 += y;
    }
    pub fn rotate(&mut self, rotation: Rotation) {
        match rotation {
            Rotation::Left => {
                self.rotate_left();
            }
            Rotation::Right => {
                self.rotate_right();
            }
        }
    }
    pub fn rotate_left(&mut self) {
        let mut temp: PieceLayout = [[None; 4]; 4];
        match self.piece_type.get_rot_diameter() {
            4 => {
                temp[0][0] = self.layout[0][3];
                temp[0][1] = self.layout[1][3];
                temp[0][2] = self.layout[2][3];
                temp[0][3] = self.layout[3][3];

                temp[1][0] = self.layout[0][2];
                temp[1][1] = self.layout[1][2];
                temp[1][2] = self.layout[2][2];
                temp[1][3] = self.layout[3][2];

                temp[2][0] = self.layout[0][1];
                temp[2][1] = self.layout[1][1];
                temp[2][2] = self.layout[2][1];
                temp[2][3] = self.layout[3][1];

                temp[3][0] = self.layout[0][0];
                temp[3][1] = self.layout[1][0];
                temp[3][2] = self.layout[2][0];
                temp[3][3] = self.layout[3][0];
            }
            3 => {
                temp[0][0] = self.layout[0][2];
                temp[0][1] = self.layout[1][2];
                temp[0][2] = self.layout[2][2];

                temp[1][0] = self.layout[0][1];
                temp[1][1] = self.layout[1][1];
                temp[1][2] = self.layout[2][1];

                temp[2][0] = self.layout[0][0];
                temp[2][1] = self.layout[1][0];
                temp[2][2] = self.layout[2][0];
            }
            _ => {
                //impossible, but we'll know it fails because the piece will be empty
            }
        }
        self.layout = temp;
    }
    pub fn rotate_right(&mut self) {
        let mut temp: PieceLayout = [[None; 4]; 4];

        match self.piece_type.get_rot_diameter() {
            4 => {
                temp[0][0] = self.layout[3][0];
                temp[0][1] = self.layout[2][0];
                temp[0][2] = self.layout[1][0];
                temp[0][3] = self.layout[0][0];

                temp[1][0] = self.layout[3][1];
                temp[1][1] = self.layout[2][1];
                temp[1][2] = self.layout[1][1];
                temp[1][3] = self.layout[0][1];

                temp[2][0] = self.layout[3][2];
                temp[2][1] = self.layout[2][2];
                temp[2][2] = self.layout[1][2];
                temp[2][3] = self.layout[0][2];

                temp[3][0] = self.layout[3][3];
                temp[3][1] = self.layout[2][3];
                temp[3][2] = self.layout[1][3];
                temp[3][3] = self.layout[0][3];
            }
            3 => {
                temp[0][2] = self.layout[0][0];
                temp[1][2] = self.layout[0][1];
                temp[2][2] = self.layout[0][2];

                temp[0][1] = self.layout[1][0];
                temp[1][1] = self.layout[1][1];
                temp[2][1] = self.layout[1][2];

                temp[0][0] = self.layout[2][0];
                temp[1][0] = self.layout[2][1];
                temp[2][0] = self.layout[2][2];
            }
            _ => {
                //impossible, but we'll know it fails because the piece will be empty
            }
        }
        self.layout = temp;
    }
    // checks if piece is out of bounds for movement purposes, but pieces above the board are not
    // considered out of bounds
    pub fn is_out_of_bounds(&self) -> bool {
        for i in 0..self.layout.len() {
            for j in 0..self.layout[i].len() {
                let tile = self.layout[i][j];
                if tile.is_none() {
                    continue; // we do not care, no block
                }
                let x = j as i8 + self.coord.0;
                let y = i as i8 + self.coord.1;
                if x < 0 || x >= board::BOARD_WIDTH as i8 || y >= board::BOARD_HEIGHT as i8 {
                    return true;
                }
            }
        }
        false
    }
}

pub struct PieceView {
    piece: Option<Piece>,
    large: bool,
}

impl PieceView {
    pub fn new() -> Self {
        PieceView {
            piece: None,
            large: true,
        }
    }

    pub fn set_piece(&mut self, piece: Piece) {
        self.piece = Some(piece);
        // shift up O (square) so he can fit into 2x4
        if let Some(ref mut p) = self.piece {
            match p.piece_type() {
                PieceType::O => {
                    for i in 0..PIECEVIEW_HEIGHT - 1 {
                        p.layout_mut()[i] = p.layout_mut()[i + 1];
                    }
                }
                _ => {}
            }
        }
    }
    pub fn set_piece_optional(&mut self, opt_piece: Option<Piece>) {
        match opt_piece {
            Some(p) => self.set_piece(p),
            None => self.piece = None,
        }
    }
    fn get_scale(&self) -> usize {
        match self.large {
            true => 2,
            false => 1,
        }
    }
    pub fn set_scale(&mut self, make_large: bool) {
        self.large = make_large;
    }
    fn draw_tile(&self, printer: &Printer, tile: Tile, row: usize, col: usize) {
        let i = self.get_scale() * row;
        // constant 2 to account for characters inheritantly being narrow
        let j = self.get_scale() * col * 2;
        const SMALL_SHIFT: usize = 2; // adjust for center alignment when small
        match self.large {
            false => {
                // 2 chars wide, 1 char tall
                for dx in 0..2 {
                    Self::draw_tile_char(printer, tile, (j + dx + SMALL_SHIFT, i));
                }
            }
            true => {
                for dx in 0..4 {
                    // 4 chars wide
                    for dy in 0..2 {
                        // 2 chars tall
                        Self::draw_tile_char(printer, tile, (j + dx, i + dy));
                    }
                }
            }
        }
    }
    // helper
    fn draw_tile_char(printer: &Printer, tile: Option<Block>, coord: (usize, usize)) {
        match tile {
            Some(block) => printer.with_style(block.get_color(), |p| p.print(coord, BLOCK_CHAR)),
            None => printer.with_style(Color::Dark(BaseColor::Black), |p| {
                p.print(coord, BLOCK_CHAR)
            }),
        }
    }
}

const PIECEVIEW_WIDTH: usize = 4;
const PIECEVIEW_HEIGHT: usize = 2;

impl View for PieceView {
    fn required_size(&mut self, _constraint: cursive::XY<usize>) -> cursive::XY<usize> {
        // scale should be externally managed by the owning view by passing in a bool for large as
        // true/false
        let dimen_x = PIECEVIEW_WIDTH * 2 * self.get_scale();
        let dimen_y = PIECEVIEW_HEIGHT * self.get_scale();
        (dimen_x, dimen_y).into()
    }
    fn draw(&self, printer: &Printer) {
        // rendering logic for static board
        for i in 0..PIECEVIEW_HEIGHT {
            for j in 0..PIECEVIEW_WIDTH {
                match self.piece {
                    None => {
                        self.draw_tile(printer, None, i, j);
                    }
                    Some(piece) => {
                        let tile = piece.layout()[i][j];
                        self.draw_tile(printer, tile, i, j);
                    }
                }
            }
        }
    }
}

const PIECE_BAG_SIZE: usize = 7;
pub struct PieceBag {
    pieces: [Piece; PIECE_BAG_SIZE],
    curr: usize,
}

impl PieceBag {
    pub fn new() -> Self {
        PieceBag {
            pieces: [(); PIECE_BAG_SIZE]
                .map(|_| Piece::random_new().at(board::PIECE_START_X, board::PIECE_START_Y)),
            curr: 0,
        }
    }
    #[inline]
    pub fn pop(&mut self) -> Piece {
        let piece = self.pieces[self.curr];
        self.pieces[self.curr] = Piece::random_new().at(board::PIECE_START_X, board::PIECE_START_Y);
        self.curr = (self.curr + 1) % PIECE_BAG_SIZE;
        piece
    }

    #[inline]
    pub fn get(&self, idx: usize) -> Piece {
        assert!(idx < PIECE_BAG_SIZE);
        let true_pos = (self.curr + idx) % PIECE_BAG_SIZE;
        self.pieces[true_pos]
    }
}