[CursorBase] split out sys_find_module from sys_load_module
[pugs.git] / examples / life.pl
blob6732e7afcdfdbc6d719dd22c385653412d65f824
1 use v6;
3 # life.pl adopted for perl6 after:
5 # // life.cola
6 # //
7 # // Game of life
8 # //
9 # // Copyright (C) 2002 Melvin Smith
10 # //
12 # (c) 2002 by Leopold Toetsch
14 # Input / output are int arrays - slooow - needs a rewrite
16 sub print_world($world) {
17 for ($world) -> $row {
18 say $row.map: { +$_ ?? '*' !! ' '}.join("");
20 say "----------------";
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];
30 return $neighbors;
33 sub sycle($input) {
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;
46 else {
47 $output[$x][$y] = 0;
49 } else {
50 if ($neighbors == 3) {
51 $output[$x][$y] = 1;
56 say "";
57 return $output;
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]
79 my $gen = @*ARGS[0] || 100;
80 say "Running ", $gen, " generations";
81 my $ts = time;
83 for 1 .. $gen {
84 print_world($world);
85 $world = sycle($world);
87 my $tdelta = time() - $ts + 1;
89 my $ratio = $gen / $tdelta;
90 say "Gens/s: ", $ratio;