Webstatt.org - Community seit 2006 - 2012 (2024?)

Game of Life

Avatar user-271
18.06.2006 13:50

Ich hab das Spiel in einem Java Buch gesehn zwinkern und dann dachte ich mir...implementier es doch mal in perl

es kann sein, dass kleine fehler dirn sind...also nicht strict ist zwinkern aber es funktioniert und hab jetzt auch keine Lust, dass noch auszubügeln zwinkern

Vielleicht kennt das Spiel der ein oder andere...mein Vater sagt es gabs schon auf dem Commodore in Basic geschrieben ;D es ist lustig zuzuschauen...wielange so eine Population von Zellen hält *fg*

#!/usr/bin/perl -w

my @cells; # $cells[x][y]
my @old;
my $x=5; # X-Value for the Coordination-system
my $y=5; # Y-Value for the Coordination-system
#my $file = $ARGV[0];

my $generation = 1; # Generations...


# Function for getting the status of a Zell (Dead or Alive)
sub getStatus {
if($_[1] == 0 or $_[0] == 0) {
return 0;
}
return ($cells[$_[0]][$_[1]]{'status'} ? 1 : 0);
}

# set the status
sub setStatus {
$cells[$_[0]][$_[1]]{'status'} = $_[2];
}

# Create Statii for the Cells randomly
sub createCells {
for(my $i = 1; $i <= $y; $i++) {
for(my $z = 1; $z <= $x; $z++) {
$random = rand;
$random = $random*10;
$random = substr($random,0,1);
$status = ($random%2 == 0 ? 0 : 1);

&setStatus($z,$i,$status);
}
}
&saveGeneration();
}

# Get All Neighbours of a cell and save them in $cell[x][y]{'neighbours'}
sub getNeighbours {
my $count = 0;
for(my $i = 1; $i <= $y; $i++) {
for(my $z = 1; $z <= $x; $z++) {
for(my $u = $i-1; $u <= $i+1; $u++) {
for(my $j = $z-1; $j <= $z+1; $j++) {
$count += &getStatus($j,$u);
}
}
$cells[$z][$i]{'neighbours'} = $count;
$count = 0;
}
}
}

# draw the cells
sub drawCells {
print "\n ";
for($i = 1; $i <= $x; $i++) {
print "_";
}
print "\n";

for(my $i = 1; $i <= $y; $i++) {
print "|";
for(my $z = 1; $z <= $x; $z++) {
print (&getStatus($z,$i) ? '*' : '-'zwinkern;
}
print "|\n";
}

print " ";
for($i = 1; $i <= $x; $i++) {
print "-";
}
}

# Change the Statii for the differenet cells after Rules
sub changeStatii {
&getNeighbours;
for(my $i = 1; $i <= $y; $i++) {
for(my $z = 1; $z <= $x; $z++) {
$neighbours = $cells[$z][$i]{'neighbours'};

if($neighbours < 2 or $neighbours > 3) {
&setStatus($z,$i,0);
}
elsif($neighbours == 3) {
&setStatus($z,$i,1);
}
}
}
}

# check if the generation is dead
sub deadGeneration {
my $allDead = 1;
for(my $i = 1; $i <= $y; $i++) {
for(my $z = 1; $z <= $x; $z++) {
if(&getStatus($z,$i) == 1) {
$allDead = 0;
}
}
}

return $allDead;
}

# check if the population stays constant
sub constantGeneration {
my $constant = 1;
for(my $i = 1; $i <= $y; $i++) {
for(my $z = 1; $z <= $x; $z++) {
if($old[$z][$i]{'status'} != &getStatus($z,$i)) {
$constant = 0;
}
}
}

return $constant;
}

# save the generation in another variable
sub saveGeneration {
for(my $i = 1; $i <= $y; $i++) {
for(my $z = 1; $z <= $x; $z++) {
if($generation != 1) {
$old[$z][$i]{'status'} = &getStatus($z,$i);
} else {
$old[$z][$i]{'status'} = 1;
}
}
}
}

&createCells();

while(1) {
system('cls'zwinkern;

if(&deadGeneration) {
print "Your Population died out in the ".$generation.". Generation";
}
elsif(&constantGeneration) {
print "The Population stays constant";

&drawCells;
}
else {
print "\nDie ".$generation." Generation.\n\n";
print " ";
&drawCells;
&saveGeneration;
&changeStatii;
$generation++;
}
print "\n\n";
$in = <>;

if($in =~ m/set \((\d+),(\d+)\) ([0-1])/i) {
$cells[$1][$2]{'status'} = $3;
}
elsif($in =~ m/restart/i) {
&createCells();
$generation = 1;
}
}


Ps.: man könnte mal geshi implementieren zwinkern

#!/bin/bash
traurig){ neutral:& };: