Crates.io | ca-formats |
lib.rs | ca-formats |
version | 0.3.5 |
source | src |
created_at | 2020-01-26 07:08:53.351322 |
updated_at | 2023-03-18 04:20:12.24907 |
description | Parsing pattern files for Conway's Game of Life. |
homepage | https://github.com/AlephAlpha/ca-formats |
repository | https://github.com/AlephAlpha/ca-formats |
max_upload_size | |
id | 202076 |
size | 71,042 |
Parsing pattern files for Conway's Game of Life.
The parsers read a string and return an iterator of coordinates of living cells.
use ca_formats::rle::Rle;
const GLIDER: &str = r"#N Glider
#O Richard K. Guy
#C The smallest, most common, and first discovered spaceship. Diagonal, has period 4 and speed c/4.
#C www.conwaylife.com/wiki/index.php?title=Glider
x = 3, y = 3, rule = B3/S23
bob$2bo$3o!";
let glider = Rle::new(GLIDER).unwrap();
assert_eq!(glider.header_data().unwrap().x, 3);
assert_eq!(glider.header_data().unwrap().y, 3);
assert_eq!(glider.header_data().unwrap().rule, Some(String::from("B3/S23")));
let cells = glider.map(|cell| cell.unwrap().position).collect::<Vec<_>>();
assert_eq!(cells, vec![(1, 0), (2, 1), (0, 2), (1, 2), (2, 2)]);
use std::fs::File;
use ca_formats::rle::Rle;
let file = File::open("tests/sirrobin.rle").unwrap();
let sirrobin = Rle::new_from_file(file).unwrap();
assert_eq!(sirrobin.count(), 282);
When the unknown
feature is enabled. the Rle
type will provide an extra method with_unknown
, which turns the RLE into a special variant of RLE format. In this variant there is another symbol, ?
, which represents unknown cells. Now unknown cells are the background. Dead cells at the end of each line must not be omitted. The iterator will also explicitly output the dead cells.
This is only supported for RLE.