| 1 | use v6-alpha; |
|---|
| 2 | |
|---|
| 3 | # life.pl adopted for perl6 after: |
|---|
| 4 | # |
|---|
| 5 | # // life.cola |
|---|
| 6 | # // |
|---|
| 7 | # // Game of life |
|---|
| 8 | # // |
|---|
| 9 | # // Copyright (C) 2002 Melvin Smith |
|---|
| 10 | # // |
|---|
| 11 | # |
|---|
| 12 | # (c) 2002 by Leopold Toetsch |
|---|
| 13 | |
|---|
| 14 | # Input / output are int arrays - slooow - needs a rewrite |
|---|
| 15 | # |
|---|
| 16 | sub print_world($world) { |
|---|
| 17 | for ($world) -> $row { |
|---|
| 18 | say $row.map:{ +$_ ?? '*' !! ' '}.join(""); |
|---|
| 19 | } |
|---|
| 20 | say "----------------"; |
|---|
| 21 | } |
|---|
| 22 | |
|---|
| 23 | sub neighbors($cell_x, $cell_y, $input) { |
|---|
| 24 | my $neighbors; |
|---|
| 25 | for -1,0,1 -> $x_off { |
|---|
| 26 | for -1,0,1 -> $y_off { |
|---|
| 27 | $neighbors += $input[$cell_x + $x_off][$cell_y + $y_off]; |
|---|
| 28 | } |
|---|
| 29 | } |
|---|
| 30 | return $neighbors; |
|---|
| 31 | } |
|---|
| 32 | |
|---|
| 33 | sub sycle($input) { |
|---|
| 34 | |
|---|
| 35 | my @death = (0,0,1,1,0,0,0,0,0); |
|---|
| 36 | my $output; |
|---|
| 37 | for 0..15 -> $x { |
|---|
| 38 | print "."; |
|---|
| 39 | for 0 .. 15 -> $y { |
|---|
| 40 | my $neighbors = neighbors($x,$y,$input); |
|---|
| 41 | if ($input[$x][$y]) { |
|---|
| 42 | if (@death[$neighbors]) { |
|---|
| 43 | $output[$x][$y] = 1; |
|---|
| 44 | |
|---|
| 45 | } |
|---|
| 46 | else { |
|---|
| 47 | $output[$x][$y] = 0; |
|---|
| 48 | } |
|---|
| 49 | } else { |
|---|
| 50 | if ($neighbors == 3) { |
|---|
| 51 | $output[$x][$y] = 1; |
|---|
| 52 | } |
|---|
| 53 | } |
|---|
| 54 | } |
|---|
| 55 | } |
|---|
| 56 | say ""; |
|---|
| 57 | return $output; |
|---|
| 58 | } |
|---|
| 59 | |
|---|
| 60 | my $world = ( |
|---|
| 61 | [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,], |
|---|
| 62 | [0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,], |
|---|
| 63 | [0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,], |
|---|
| 64 | [1,1,1,0,0,0,0,0,0,0,0,0,0,0,0,0,], |
|---|
| 65 | [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,], |
|---|
| 66 | [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,], |
|---|
| 67 | [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,], |
|---|
| 68 | [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,], |
|---|
| 69 | [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,], |
|---|
| 70 | [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,], |
|---|
| 71 | [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,], |
|---|
| 72 | [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,], |
|---|
| 73 | [0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,], |
|---|
| 74 | [0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,], |
|---|
| 75 | [0,0,1,1,1,0,0,0,0,0,0,0,0,0,0,0,], |
|---|
| 76 | [0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0] |
|---|
| 77 | ); |
|---|
| 78 | |
|---|
| 79 | my $gen = @*ARGS[0] || 100; |
|---|
| 80 | say "Running ", $gen, " generations"; |
|---|
| 81 | my $ts = time; |
|---|
| 82 | |
|---|
| 83 | for 1 .. $gen { |
|---|
| 84 | print_world($world); |
|---|
| 85 | $world = sycle($world); |
|---|
| 86 | } |
|---|
| 87 | my $tdelta = time() - $ts + 1; |
|---|
| 88 | |
|---|
| 89 | my $ratio = $gen / $tdelta; |
|---|
| 90 | say "Gens/s: ", $ratio; |
|---|