root/examples/life.pl

Revision 11293, 2.1 kB (checked in by Darren_Duncan, 3 years ago)

replaced all 'use v6;' lines with 'use v6-alpha;' in 330 files (examples/, ext/, t/, t_disabled/) ... more remain to do

  • Property svn:mime-type set to text/plain; charset=UTF-8
  • Property svn:eol-style set to native
Line 
1use 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#
16sub print_world($world) { 
17    for ($world) -> $row {
18          say $row.map:{ +$_ ?? '*' !! ' '}.join("");
19    }
20    say "----------------";
21}
22
23sub 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
33sub 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
60my  $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
79my $gen = @*ARGS[0] || 100;
80say "Running ", $gen, " generations";
81my $ts = time;
82
83for 1 .. $gen {
84  print_world($world);
85  $world = sycle($world);
86}
87my $tdelta = time() - $ts + 1;
88
89my $ratio = $gen / $tdelta;
90say "Gens/s: ", $ratio;
Note: See TracBrowser for help on using the browser.