-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathn02_boards.rs
54 lines (46 loc) · 1.75 KB
/
n02_boards.rs
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
use std::str::FromStr;
use bitris::prelude::*;
fn main() {
// Make boards. Select the bit width.
let board8: Board8 = Board8::blank();
let board16: Board16 = Board16::blank();
let board32: Board32 = Board32::blank();
let board64: Board64 = Board64::blank();
// Depending on the bit width, the ceiling height will vary.
assert_eq!(board8.ceiling(), 8);
assert_eq!(board16.ceiling(), 16);
assert_eq!(board32.ceiling(), 32);
assert_eq!(board64.ceiling(), 64);
// Boards can be created from strings.
let board64: Board64 = Board64::from_str("
..........
####....##
####...###
####..####
####...###
").expect("Failed to create a board");
assert_eq!(board64.count_blocks(), 28);
assert_eq!(board64.well_top(), 4);
// If you want to extend the bit width, you can use Into to convert.
let board32: Board32 = Board8::blank().into();
assert_eq!(board32.ceiling(), 32);
// If you want to shrink the bit width, you must explicitly convert them.
let mut board64 = Board64::blank();
board64.set_at(xy(0, 8));
assert!(board64.ceiling() == 64 && board64.count_blocks() == 1);
let board8: Board8 = Board8::shrink_from(board64);
assert!(board8.ceiling() == 8 && board8.count_blocks() == 0);
// Set and unset a block. Manipulation of the board changes itself (it means mutable).
// ..........
// ..........
// ..........
// ..#.......
// ..........
let mut board = Board64::blank();
board.set_at(xy(2, 1));
assert!(board.is_occupied_at(xy(2, 1)));
assert!(!board.is_free_at(xy(2, 1)));
board.unset_at(xy(2, 1));
assert!(!board.is_occupied_at(xy(2, 1)));
assert!(board.is_free_at(xy(2, 1)));
}