summaryrefslogtreecommitdiffstats
path: root/examples/game_of_life/src/preset.rs
blob: 05157b6a962d9deb6395376e326a267b9657cfef (plain) (blame)
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
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Preset {
    Custom,
    XKCD,
    Glider,
    SmallExploder,
    Exploder,
    TenCellRow,
    LightweightSpaceship,
    Tumbler,
    GliderGun,
    Acorn,
}

pub static ALL: &[Preset] = &[
    Preset::Custom,
    Preset::XKCD,
    Preset::Glider,
    Preset::SmallExploder,
    Preset::Exploder,
    Preset::TenCellRow,
    Preset::LightweightSpaceship,
    Preset::Tumbler,
    Preset::GliderGun,
    Preset::Acorn,
];

impl Preset {
    pub fn life(self) -> Vec<(isize, isize)> {
        #[rustfmt::skip]
        let cells = match self {
            Preset::Custom => vec![],
            Preset::XKCD => vec![
                "  xxx  ",
                "  x x  ",
                "  x x  ",
                "   x   ",
                "x xxx  ",
                " x x x ",
                "   x  x",
                "  x x  ",
                "  x x  ",
            ],
            Preset::Glider => vec![
                " x ",
                "  x",
                "xxx"
            ],
            Preset::SmallExploder => vec![
                " x ",
                "xxx",
                "x x",
                " x ",
            ],
            Preset::Exploder => vec![
                "x x x",
                "x   x",
                "x   x",
                "x   x",
                "x x x",
            ],
            Preset::TenCellRow => vec![
                "xxxxxxxxxx",
            ],
            Preset::LightweightSpaceship => vec![
                " xxxxx",
                "x    x",
                "     x",
                "x   x ",
            ],
            Preset::Tumbler => vec![
                " xx xx ",
                " xx xx ",
                "  x x  ",
                "x x x x",
                "x x x x",
                "xx   xx",
            ],
            Preset::GliderGun => vec![
                "                        x           ",
                "                      x x           ",
                "            xx      xx            xx",
                "           x   x    xx            xx",
                "xx        x     x   xx              ",
                "xx        x   x xx    x x           ",
                "          x     x       x           ",
                "           x   x                    ",
                "            xx                      ",
            ],
            Preset::Acorn => vec![
                " x     ",
                "   x   ",
                "xx  xxx",
            ],
        };

        let start_row = -(cells.len() as isize / 2);

        cells
            .into_iter()
            .enumerate()
            .flat_map(|(i, cells)| {
                let start_column = -(cells.len() as isize / 2);

                cells
                    .chars()
                    .enumerate()
                    .filter(|(_, c)| !c.is_whitespace())
                    .map(move |(j, _)| {
                        (start_row + i as isize, start_column + j as isize)
                    })
            })
            .collect()
    }
}

impl Default for Preset {
    fn default() -> Preset {
        Preset::XKCD
    }
}

impl std::fmt::Display for Preset {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "{}",
            match self {
                Preset::Custom => "Custom",
                Preset::XKCD => "xkcd #2293",
                Preset::Glider => "Glider",
                Preset::SmallExploder => "Small Exploder",
                Preset::Exploder => "Exploder",
                Preset::TenCellRow => "10 Cell Row",
                Preset::LightweightSpaceship => "Lightweight spaceship",
                Preset::Tumbler => "Tumbler",
                Preset::GliderGun => "Gosper Glider Gun",
                Preset::Acorn => "Acorn",
            }
        )
    }
}