Advertisement
nuit

Game of Life

Mar 20th, 2011
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 3.20 KB | None | 0 0
  1. #!/usr/bin/perl -w
  2.  
  3. my @cells; # $cells[x][y]
  4. my @old;
  5. my $x=5; # X-Value for the Coordination-system
  6. my $y=5; # Y-Value for the Coordination-system
  7. #my $file = $ARGV[0];
  8.  
  9. my $generation = 1; # Generations...
  10.  
  11.  
  12. # Function for getting the status of a Zell (Dead or Alive)
  13. sub getStatus {
  14.     if($_[1] == 0 or $_[0] == 0) {
  15.         return 0;
  16.     }
  17.     return ($cells[$_[0]][$_[1]]{'status'} ? 1 : 0);
  18. }
  19.  
  20. # set the status
  21. sub setStatus {
  22.     $cells[$_[0]][$_[1]]{'status'} = $_[2];
  23. }
  24.  
  25. # Create Statii for the Cells randomly
  26. sub createCells {
  27.     for(my $i = 1; $i <= $y; $i++) {
  28.         for(my $z = 1; $z <= $x; $z++) {
  29.             $random = rand;
  30.             $random = $random*10;
  31.             $random = substr($random,0,1);
  32.             $status = ($random%2 == 0 ? 0 : 1);
  33.  
  34.             &setStatus($z,$i,$status);
  35.         }
  36.     }
  37.     &saveGeneration();
  38. }
  39.  
  40. # Get All Neighbours of a cell and save them in $cell[x][y]{'neighbours'}
  41. sub getNeighbours {
  42.     my $count = 0;
  43.     for(my $i = 1; $i <= $y; $i++) {
  44.         for(my $z = 1; $z <= $x; $z++) {
  45.             for(my $u = $i-1; $u <= $i+1; $u++) {
  46.                 for(my $j = $z-1; $j <= $z+1; $j++) {
  47.                     $count += &getStatus($j,$u);
  48.                 }
  49.             }
  50.             $cells[$z][$i]{'neighbours'} = $count;
  51.             $count = 0;
  52.         }
  53.     }
  54. }
  55.  
  56. # draw the cells
  57. sub drawCells {
  58.     print "\n ";
  59.     for($i = 1; $i <= $x; $i++) {
  60.         print "_";
  61.     }
  62.     print "\n";
  63.  
  64.     for(my $i = 1; $i <= $y; $i++) {
  65.         print "|";
  66.         for(my $z = 1; $z <= $x; $z++) {
  67.             print (&getStatus($z,$i) ? '*' : '-');
  68.         }
  69.         print "|\n";
  70.     }
  71.  
  72.     print " ";
  73.     for($i = 1; $i <= $x; $i++) {
  74.         print "-";
  75.     }
  76. }
  77.  
  78. # Change the Statii for the differenet cells after Rules
  79. sub changeStatii {
  80.     &getNeighbours;
  81.     for(my $i = 1; $i <= $y; $i++) {
  82.         for(my $z = 1; $z <= $x; $z++) {
  83.             $neighbours = $cells[$z][$i]{'neighbours'};
  84.  
  85.             if($neighbours < 2 or $neighbours > 3) {
  86.                 &setStatus($z,$i,0);
  87.             }
  88.             elsif($neighbours == 3) {
  89.                 &setStatus($z,$i,1);
  90.             }
  91.         }
  92.     }
  93. }
  94.  
  95. # check if the generation is dead
  96. sub deadGeneration {
  97.     my $allDead = 1;
  98.     for(my $i = 1; $i <= $y; $i++) {
  99.         for(my $z = 1; $z <= $x; $z++) {
  100.             if(&getStatus($z,$i) == 1) {
  101.                 $allDead = 0;
  102.             }
  103.         }
  104.     }
  105.  
  106.     return $allDead;
  107. }
  108.  
  109. # check if the population stays constant
  110. sub constantGeneration {
  111.     my $constant = 1;
  112.     for(my $i = 1; $i <= $y; $i++) {
  113.         for(my $z = 1; $z <= $x; $z++) {
  114.             if($old[$z][$i]{'status'} != &getStatus($z,$i)) {
  115.                 $constant = 0;
  116.             }
  117.         }
  118.     }
  119.  
  120.     return $constant;
  121. }
  122.  
  123. # save the generation in another variable
  124. sub saveGeneration {
  125.     for(my $i = 1; $i <= $y; $i++) {
  126.         for(my $z = 1; $z <= $x; $z++) {
  127.             if($generation != 1) {
  128.                 $old[$z][$i]{'status'} = &getStatus($z,$i);
  129.             } else {
  130.                 $old[$z][$i]{'status'} = 1;
  131.             }
  132.         }
  133.     }
  134. }
  135.  
  136. &createCells();
  137.  
  138. while(1) {
  139.     system('cls');
  140.  
  141.     if(&deadGeneration) {
  142.         print "Your Population died out in the ".$generation.". Generation";
  143.     }
  144.     elsif(&constantGeneration) {
  145.         print "The Population stays constant";
  146.  
  147.         &drawCells;
  148.     }
  149.     else {
  150.         print "\nDie ".$generation." Generation.\n\n";
  151.         print " ";
  152.         &drawCells;
  153.         &saveGeneration;
  154.         &changeStatii;
  155.         $generation++;
  156.     }
  157.     print "\n\n";
  158.     $in = <>;
  159.  
  160.     if($in =~ m/set \((\d+),(\d+)\) ([0-1])/i) {
  161.         $cells[$1][$2]{'status'} = $3;
  162.     }
  163.     elsif($in =~ m/restart/i) {
  164.         &createCells();
  165.         $generation = 1;
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement