@@ -38,32 +38,59 @@ fn grid_from_ascii(ascii: &str) -> Grid {
3838 grid
3939}
4040
41+ fn randomly_fill_grid ( grid : & mut Grid , probability : & [ Cell ] , seed : u32 ) -> ( ) {
42+ // Pseudorandom number generator from the "Xorshift RNGs" paper by George Marsaglia.
43+ // https://github.com/rust-lang/rust/blob/1.55.0/library/core/src/slice/sort.rs#L559-L573
44+ fn random_numbers ( seed : u32 ) -> impl Iterator < Item = u32 > {
45+ let mut random = seed;
46+ std:: iter:: repeat_with ( move || {
47+ random ^= random << 13 ;
48+ random ^= random >> 17 ;
49+ random ^= random << 5 ;
50+ random
51+ } )
52+ }
53+
54+ let mut rng = random_numbers ( seed) ;
55+
56+ for x in 0 ..( grid. width as i8 ) {
57+ for y in 0 ..( grid. height as i8 ) {
58+ let random = rng. next ( ) . unwrap ( ) ;
59+ let cell = probability[ random as usize % probability. len ( ) ] ;
60+ grid. set_cell ( & Point { x, y } , cell) ;
61+ }
62+ }
63+ }
64+
4165pub enum SampleGrid {
4266 Empty ,
4367 OneDot ,
4468 Realistic ,
4569 Labyrinthe ,
70+ RandomPack ,
4671}
4772pub fn get_grid_sample ( g : SampleGrid ) -> Grid {
48- grid_from_ascii ( match g {
49- SampleGrid :: Empty => {
73+ match g {
74+ SampleGrid :: Empty => grid_from_ascii (
5075 & r#"
5176 _
5277 _
5378 _
5479 _
55- "#
56- }
57- SampleGrid :: OneDot => {
80+ "# ,
81+ ) ,
82+
83+ SampleGrid :: OneDot => grid_from_ascii (
5884 & r#"
5985 _
6086 _
6187 . _
6288 _
6389 _
64- "#
65- }
66- SampleGrid :: Realistic => {
90+ "# ,
91+ ) ,
92+
93+ SampleGrid :: Realistic => grid_from_ascii (
6794 & r#"
6895231 412 12213 13 213 421 121131 32123112 332 _
6996412 12 4 331213 12214431 412 413 42133123 23 21
@@ -72,9 +99,10 @@ pub fn get_grid_sample(g: SampleGrid) -> Grid {
729934122 3 2144 31 31234 212 2121 211 12 3 123 3123 12
73100442 12122122 12331123 33443 3311121 111 223 333_
7410131413 31231 2 213321 123 32123 3332 12312 3 33 2 3
75- "#
76- }
77- SampleGrid :: Labyrinthe => {
102+ "# ,
103+ ) ,
104+
105+ SampleGrid :: Labyrinthe => grid_from_ascii (
78106 & r#"
79107################################################## #
80108# #
@@ -83,7 +111,17 @@ pub fn get_grid_sample(g: SampleGrid) -> Grid {
83111################################################## #
84112#. #
85113####################################################
86- "#
114+ "# ,
115+ ) ,
116+
117+ SampleGrid :: RandomPack => {
118+ let mut grid = Grid :: create_empty ( 52 , 7 ) ;
119+ randomly_fill_grid (
120+ & mut grid,
121+ & [ Cell :: Empty , Cell :: Empty , Cell :: Color1 , Cell :: Color4 ] ,
122+ 92u32 ,
123+ ) ;
124+ grid
87125 }
88- } )
126+ }
89127}
0 commit comments