File size: 21,229 Bytes
d5bfab8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
//! Cellular Automaton (singular) and Cellular Automata (plural)
//! https://en.wikipedia.org/wiki/Cellular_automaton
//! 
//! Conway's Game of Life is one cellular automaton.
//! https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
//! 
//! Ideas for more cellular automata:
//! https://en.wikipedia.org/wiki/Langton%27s_ant
//! https://conwaylife.com/wiki/OCA:Maze
//! https://conwaylife.com/wiki/OCA:Life_without_death
//! https://conwaylife.com/wiki/OCA:Seeds
//! https://en.wikipedia.org/wiki/Brian's_Brain
use super::{Image, ImageSize, HtmlLog};
use std::marker::PhantomData;

/// `CARule` is a trait that defines the behavior of a single cell within a cellular automaton
/// based on its current state and the states of its eight neighboring cells.
///
/// This trait can be implemented for different rule sets, allowing for the simulation
/// of various cellular automata beyond Conway's Game of Life, such as Highlife or Wireworld.
pub trait CARule {
    fn apply(center: u8, neighbors: &[u8; 8]) -> u8;
}

#[derive(Debug)]
pub struct CellularAutomaton<R: CARule> {
    current: Image,
    next: Image,
    outside_color: Option<u8>,
    _rule: PhantomData<R>,
}

impl<R: CARule> CellularAutomaton<R> {
    #[allow(dead_code)]
    pub fn new(size: ImageSize, outside_color: Option<u8>) -> Self {
        Self {
            current: Image::zero(size.width, size.height),
            next: Image::zero(size.width, size.height),
            outside_color,
            _rule: PhantomData,
        }
    }

    #[allow(dead_code)]
    pub fn with_image(image: &Image, outside_color: Option<u8>) -> Self {
        Self {
            current: image.clone(),
            next: image.clone_zero(),
            outside_color,
            _rule: PhantomData,
        }
    }

    #[allow(dead_code)]
    pub fn step(&mut self, count: u8) {
        for _ in 0..count {
            self.step_once();
        }
    }

    #[allow(dead_code)]
    pub fn step_and_log(&mut self, count: u8) {
        HtmlLog::image(&self.current);
        for _ in 0..count {
            self.step_once();
            HtmlLog::image(&self.current);
        }
    }

    #[allow(dead_code)]
    pub fn images_for_n_steps(&mut self, count: u8) -> Vec<Image> {
        let mut images = Vec::<Image>::new();
        images.push(self.current.clone());
        for _ in 0..count {
            self.step_once();
            images.push(self.current.clone());
        }
        images
    }

    pub fn step_once(&mut self) {
        for y in 0..self.current.height() {
            for x in 0..self.current.width() {

                // Obtain the 8 neighbor values
                let mut neighbors: [u8; 8] = [0; 8];
                let mut index: usize = 0;
                for i in -1..=1 {
                    for j in -1..=1 {
                        if i == 0 && j == 0 {
                            continue;
                        }
                        let value: u8 = if let Some(outside_color) = self.outside_color {
                            self.current.get(x as i32 + i, y as i32 + j).unwrap_or(outside_color)
                        } else {
                            self.current.get_wrap(x as i32 + i, y as i32 + j).unwrap_or(0)
                        };
                        neighbors[index] = value;
                        index += 1;
                    }
                }

                // Get center value
                let center: u8 = self.current.get(x as i32, y as i32).unwrap_or(0);

                // Apply the rules
                let set_value: u8 = R::apply(center, &neighbors);

                _ = self.next.set(x as i32, y as i32, set_value);
            }
        }
        std::mem::swap(&mut self.current, &mut self.next);
    }

    #[allow(dead_code)]
    pub fn image(&self) -> &Image {
        &self.current
    }
}

pub mod rule {
    /// https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
    /// 
    /// - Any live cell with fewer than two live neighbours dies, as if by underpopulation.
    /// - Any live cell with two or three live neighbours lives on to the next generation.
    /// - Any live cell with more than three live neighbours dies, as if by overpopulation.
    /// - Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
    /// 
    /// These rules, which compare the behavior of the automaton to real life, can be condensed into the following:
    /// 
    /// - Any live cell with two or three live neighbours survives.
    /// - Any dead cell with three live neighbours becomes a live cell.
    /// - All other live cells die in the next generation. Similarly, all other dead cells stay dead.
    pub struct GameOfLife;

    impl super::CARule for GameOfLife {
        fn apply(center: u8, neighbors: &[u8; 8]) -> u8 {
            let alive_count: usize = neighbors.iter().filter(|&&value| value > 0).count();
            match (center, alive_count) {
                (0, 3) => 1,
                (1, 2) => 1,
                (1, 3) => 1,
                _ => 0,
            }
        }
    }

    /// GameOfLife with extra states
    /// 
    /// 0 = dead
    /// 1 = alive
    /// 2 = just born
    /// 3 = just dead
    pub struct GameOfLifeExtra;

    impl super::CARule for GameOfLifeExtra {
        fn apply(center: u8, neighbors: &[u8; 8]) -> u8 {
            let alive_count = neighbors.iter().filter(|&&state| state == 1 || state == 2).count();

            if (center == 1 || center == 2) && (alive_count < 2 || alive_count > 3) {
                // Any live cell with fewer than two live neighbours dies, as if by underpopulation.
                // Any live cell with more than three live neighbours dies, as if by overpopulation.
                return 3; // just dead
            }

            if (center == 1 || center == 2) && (alive_count == 2 || alive_count == 3) {
                // Any live cell with two or three live neighbours lives on to the next generation.
                return 1; // alive
            }    

            if (center == 0 || center == 3) && (alive_count == 3) {
                // Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
                return 2; // just born
            }

            if center == 0 {
                // dead stays dead
                return 0; // dead
            }

            if center == 3 {
                // "just dead" becomes fully dead
                return 0; // dead
            }

            // anything else becomes "just dead"
            3 // just dead
        }
    }


    /// https://conwaylife.com/wiki/OCA:HighLife
    /// 
    /// Cells survive from one generation to the next if they have 2 or 3 neighbours, and are born if they have 3 or 6 neighbours.
    pub struct HighLife;

    impl super::CARule for HighLife {
        fn apply(center: u8, neighbors: &[u8; 8]) -> u8 {
            let alive_count: usize = neighbors.iter().filter(|&&value| value > 0).count();
            match (center, alive_count) {
                // A dead cell with exactly three or six neighbors becomes a live cell
                (0, 3) | (0, 6) => 1,
                // A live cell with two or three neighbors stays alive
                (1, 2) | (1, 3) => 1,
                // In all other cases, the cell dies or remains dead
                _ => 0,
            }
        }
    }

    /// https://en.wikipedia.org/wiki/Wireworld
    /// 
    /// 0 = empty
    /// 1 = electron head
    /// 2 = electron tail
    /// 3 = conductor
    pub struct Wireworld;

    impl super::CARule for Wireworld {
        fn apply(center: u8, neighbors: &[u8; 8]) -> u8 {
            if center == 1 { // electron head
                return 2; // electron tail
            }

            if center == 2 { // electron tail
                return 3; // conductor
            }
            
            let electron_head_count: usize = neighbors.iter().filter(|&&value| value == 1).count();
            if center == 3 && (electron_head_count == 1 || electron_head_count == 2) { // conductor with 1 or 2 electron heads
                return 1; // electron head
            }

            center
        }
    }

    /// https://conwaylife.com/wiki/OCA:Serviettes
    /// also known as "Persian rugs"
    pub struct Serviettes;

    impl super::CARule for Serviettes {
        fn apply(center: u8, neighbors: &[u8; 8]) -> u8 {
            let alive_count: usize = neighbors.iter().filter(|&&value| value > 0).count();
            match (center, alive_count) {
                // A dead cell with 2, 3, 4 becomes alive
                (0, 2) | (0, 3) | (0, 4) => 1,
                // In all other cases, the cell dies or remains dead
                _ => 0,
            }
        }
    }

    /// Create a cave system
    /// https://www.roguebasin.com/index.php/Cellular_Automata_Method_for_Generating_Random_Cave-Like_Levels
    /// http://pixelenvy.ca/wa/ca_cave.html
    pub struct Cave;

    impl super::CARule for Cave {
        fn apply(center: u8, neighbors: &[u8; 8]) -> u8 {
            let alive_count: usize = neighbors.iter().filter(|&&value| value > 0).count();
            if alive_count < 4 {
                return 0;
            }
            if alive_count > 5 {
                return 1;
            }
            center
        }
    }

    /// Create a maze
    /// https://conwaylife.com/wiki/OCA:Maze
    pub struct Maze;

    impl super::CARule for Maze {
        fn apply(center: u8, neighbors: &[u8; 8]) -> u8 {
            let alive_count: usize = neighbors.iter().filter(|&&value| value > 0).count();
            if alive_count == 3 {
                return 1;
            }
            if alive_count < 1 || alive_count > 5 {
                return 0;
            }
            center
        }
    }
} // mod rule

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

    #[test]
    fn test_10000_gameoflife_glider() {
        // Act
        let pixels: Vec<u8> = vec![
            0, 0, 0, 0, 0, 0,
            0, 0, 0, 1, 0, 0,
            0, 1, 0, 1, 0, 0,
            0, 0, 1, 1, 0, 0,
            0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0,
        ];
        let input: Image = Image::try_create(6, 6, pixels).expect("image");
        let mut ca: CellularAutomaton<_> = CellularAutomaton::<rule::GameOfLife>::with_image(&input, None);

        // Act
        ca.step_once();
        let actual: Image = ca.current;
        
        // Assert
        let expected_pixels: Vec<u8> = vec![
            0, 0, 0, 0, 0, 0,
            0, 0, 1, 0, 0, 0,
            0, 0, 0, 1, 1, 0,
            0, 0, 1, 1, 0, 0,
            0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0,
        ];
        let expected: Image = Image::try_create(6, 6, expected_pixels).expect("image");
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_10001_gameoflife_glider_wraparound() {
        // Act
        let pixels: Vec<u8> = vec![
            0, 1, 0, 0, 0,
            1, 1, 0, 0, 1,
            0, 0, 0, 0, 0,
            0, 0, 0, 0, 0,
            1, 0, 0, 0, 0,
        ];
        let input: Image = Image::try_create(5, 5, pixels).expect("image");
        let mut ca: CellularAutomaton<_> = CellularAutomaton::<rule::GameOfLife>::with_image(&input, None);

        // Act
        ca.step_once();
        let actual: Image = ca.current;
        
        // Assert
        let expected_pixels: Vec<u8> = vec![
            0, 1, 0, 0, 1,
            1, 1, 0, 0, 0,
            1, 0, 0, 0, 0,
            0, 0, 0, 0, 0,
            0, 0, 0, 0, 0,
        ];
        let expected: Image = Image::try_create(5, 5, expected_pixels).expect("image");
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_10002_gameoflife_glider_use_outside_color() {
        // Act
        let pixels: Vec<u8> = vec![
            0, 1, 0, 0, 0,
            1, 1, 0, 0, 1,
            0, 0, 0, 0, 0,
            0, 0, 0, 0, 0,
            1, 0, 0, 0, 0,
        ];
        let input: Image = Image::try_create(5, 5, pixels).expect("image");
        let mut ca: CellularAutomaton<_> = CellularAutomaton::<rule::GameOfLife>::with_image(&input, Some(0));

        // Act
        ca.step_once();
        let actual: Image = ca.current;
        
        // Assert
        let expected_pixels: Vec<u8> = vec![
            1, 1, 0, 0, 0,
            1, 1, 0, 0, 0,
            0, 0, 0, 0, 0,
            0, 0, 0, 0, 0,
            0, 0, 0, 0, 0,
        ];
        let expected: Image = Image::try_create(5, 5, expected_pixels).expect("image");
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_20000_gameoflife_extra_glider() {
        // Act
        let pixels: Vec<u8> = vec![
            0, 0, 0, 0, 0, 0,
            0, 0, 0, 1, 0, 0,
            0, 1, 0, 1, 0, 0,
            0, 0, 1, 1, 0, 0,
            0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0,
        ];
        let input: Image = Image::try_create(6, 6, pixels).expect("image");
        let mut ca: CellularAutomaton<_> = CellularAutomaton::<rule::GameOfLifeExtra>::with_image(&input, None);

        // Act
        ca.step_once();
        let actual: Image = ca.current;
        
        // Assert
        let expected_pixels: Vec<u8> = vec![
            0, 0, 0, 0, 0, 0,
            0, 0, 2, 3, 0, 0,
            0, 3, 0, 1, 2, 0,
            0, 0, 1, 1, 0, 0,
            0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0,
        ];
        let expected: Image = Image::try_create(6, 6, expected_pixels).expect("image");
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_20001_gameoflife_extra_glider() {
        // Act
        let pixels: Vec<u8> = vec![
            0, 0, 0, 0, 0, 0,
            0, 0, 2, 3, 0, 0,
            0, 3, 0, 1, 2, 0,
            0, 0, 1, 1, 0, 0,
            0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0,
        ];
        let input: Image = Image::try_create(6, 6, pixels).expect("image");
        let mut ca: CellularAutomaton<_> = CellularAutomaton::<rule::GameOfLifeExtra>::with_image(&input, None);

        // Act
        ca.step_once();
        let actual: Image = ca.current;
        
        // Assert
        let expected_pixels: Vec<u8> = vec![
            0, 0, 0, 0, 0, 0,
            0, 0, 3, 2, 0, 0,
            0, 0, 0, 3, 1, 0,
            0, 0, 1, 1, 2, 0,
            0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0,
        ];
        let expected: Image = Image::try_create(6, 6, expected_pixels).expect("image");
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_30000_highlife_predecessor_replicator() {
        // Act
        let pixels: Vec<u8> = vec![
            0, 0, 0, 0, 0, 0,
            0, 0, 1, 1, 1, 0,
            0, 1, 0, 0, 0, 0,
            0, 1, 0, 0, 0, 0,
            0, 1, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0,
        ];
        let input: Image = Image::try_create(6, 6, pixels).expect("image");
        let mut ca: CellularAutomaton<_> = CellularAutomaton::<rule::HighLife>::with_image(&input, None);

        // Act
        ca.step_once();
        let actual: Image = ca.current;
        
        // Assert
        let expected_pixels: Vec<u8> = vec![
            0, 0, 0, 1, 0, 0,
            0, 0, 1, 1, 0, 0,
            0, 1, 0, 1, 0, 0,
            1, 1, 1, 0, 0, 0,
            0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0,
        ];
        let expected: Image = Image::try_create(6, 6, expected_pixels).expect("image");
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_40000_wireworld_diode() {
        // Act
        let pixels: Vec<u8> = vec![
            0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 3, 3, 0, 0, 0,
            3, 2, 1, 0, 3, 3, 3, 3,
            0, 0, 0, 3, 3, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0,
        ];
        let input: Image = Image::try_create(8, 5, pixels).expect("image");
        let mut ca: CellularAutomaton<_> = CellularAutomaton::<rule::Wireworld>::with_image(&input, None);

        // Act
        ca.step_once();
        let actual: Image = ca.current;
        
        // Assert
        let expected_pixels: Vec<u8> = vec![
            0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 1, 3, 0, 0, 0,
            3, 3, 2, 0, 3, 3, 3, 3,
            0, 0, 0, 1, 3, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0,
        ];
        let expected: Image = Image::try_create(8, 5, expected_pixels).expect("image");
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_40001_wireworld_diode() {
        // Act
        let pixels: Vec<u8> = vec![
            0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 3, 3, 0, 0, 0,
            3, 2, 1, 3, 0, 3, 3, 3,
            0, 0, 0, 3, 3, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0,
        ];
        let input: Image = Image::try_create(8, 5, pixels).expect("image");
        let mut ca: CellularAutomaton<_> = CellularAutomaton::<rule::Wireworld>::with_image(&input, None);

        // Act
        ca.step_once();
        let actual: Image = ca.current;
        
        // Assert
        let expected_pixels: Vec<u8> = vec![
            0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 1, 3, 0, 0, 0,
            3, 3, 2, 1, 0, 3, 3, 3,
            0, 0, 0, 1, 3, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0,
        ];
        let expected: Image = Image::try_create(8, 5, expected_pixels).expect("image");
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_50000_serviettes() {
        // Act
        let pixels: Vec<u8> = vec![
            0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0,
            0, 0, 1, 1, 0, 0,
            0, 0, 1, 1, 0, 0,
            0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0,
        ];
        let input: Image = Image::try_create(6, 6, pixels).expect("image");
        let mut ca: CellularAutomaton<_> = CellularAutomaton::<rule::Serviettes>::with_image(&input, None);

        // Act
        ca.step_once();
        let actual: Image = ca.current;
        
        // Assert
        let expected_pixels: Vec<u8> = vec![
            0, 0, 0, 0, 0, 0,
            0, 0, 1, 1, 0, 0,
            0, 1, 0, 0, 1, 0,
            0, 1, 0, 0, 1, 0,
            0, 0, 1, 1, 0, 0,
            0, 0, 0, 0, 0, 0,
        ];
        let expected: Image = Image::try_create(6, 6, expected_pixels).expect("image");
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_60000_cave() {
        // Act
        let pixels: Vec<u8> = vec![
            1, 1, 0, 1, 0, 1, 0, 1, 1, 1,
            1, 0, 0, 1, 0, 0, 0, 1, 0, 1,
            0, 1, 0, 1, 1, 0, 1, 1, 0, 1,
            0, 1, 1, 1, 0, 0, 1, 1, 1, 1,
            0, 0, 1, 1, 1, 0, 0, 0, 1, 1,
            0, 0, 1, 1, 1, 1, 0, 1, 0, 0,
            0, 1, 1, 0, 0, 0, 0, 1, 1, 0,
            0, 0, 0, 0, 1, 0, 1, 0, 1, 1,
            1, 1, 1, 1, 0, 1, 1, 0, 0, 0,
            1, 1, 0, 1, 0, 0, 0, 0, 0, 0
        ];
        let input: Image = Image::try_create(10, 10, pixels).expect("image");
        let mut ca: CellularAutomaton<_> = CellularAutomaton::<rule::Cave>::with_image(&input, Some(0));

        // Act
        ca.step_once();
        let actual: Image = ca.current;
        
        // Assert
        let expected_pixels: Vec<u8> = vec![
            0, 0, 0, 0, 0, 0, 0, 0, 1, 0,
            0, 0, 0, 0, 0, 0, 0, 1, 1, 0,
            0, 0, 1, 1, 0, 0, 1, 1, 1, 0,
            0, 0, 1, 1, 0, 0, 0, 1, 1, 1,
            0, 0, 1, 1, 1, 0, 0, 0, 1, 0,
            0, 0, 1, 1, 1, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 1, 1, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 1, 1, 0, 0, 0, 0, 0, 0, 0,
            0, 1, 0, 0, 0, 0, 0, 0, 0, 0
        ];
        let expected: Image = Image::try_create(10, 10, expected_pixels).expect("image");
        assert_eq!(actual, expected);
    }

    #[test]
    fn test_70000_maze() {
        // Act
        let pixels: Vec<u8> = vec![
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 1, 1, 0, 1, 1, 0, 0,
            0, 0, 1, 1, 0, 0, 1, 1, 0, 0,
            0, 0, 1, 1, 1, 0, 0, 0, 0, 0,
            0, 0, 1, 1, 1, 1, 0, 1, 0, 0,
            0, 0, 1, 0, 0, 0, 0, 1, 0, 0,
            0, 0, 0, 0, 1, 0, 1, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
        ];
        let input: Image = Image::try_create(10, 10, pixels).expect("image");
        let mut ca: CellularAutomaton<_> = CellularAutomaton::<rule::Maze>::with_image(&input, Some(0));

        // Act
        ca.step_once();
        let actual: Image = ca.current;
        
        // Assert
        let expected_pixels: Vec<u8> = vec![
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 1, 1, 1, 1, 1, 1, 0, 0,
            0, 0, 1, 0, 0, 0, 1, 1, 0, 0,
            0, 1, 1, 0, 1, 0, 0, 1, 0, 0,
            0, 1, 1, 0, 1, 1, 1, 1, 0, 0,
            0, 0, 1, 0, 0, 0, 0, 1, 0, 0,
            0, 0, 0, 0, 0, 0, 1, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
            0, 0, 0, 0, 0, 0, 0, 0, 0, 0
        ];
        let expected: Image = Image::try_create(10, 10, expected_pixels).expect("image");
        assert_eq!(actual, expected);
    }
}