Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Life, in Perl.

by Mr_Person (Hermit)
on Jul 21, 2003 at 20:56 UTC ( [id://276461]=note: print w/replies, xml ) Need Help??


in reply to Life, in Perl.

Okay, here's my go at it. Uses POE so there can be many people "living" at once and creating new children. Reality has been sped up a bit so that each year of a person's life only takes 1/10 of a second. Actually runs too, though you may not want to leave it going for too long as the "population" increases rapidly. :-)

While this one may be more functional, the OP is a lot more fun to read, partially because I wanted to use strict and warnings which leaves less room for stuff that reads well, but doesn't actually do anything.

This also has a big friggin comet feature, just press Ctrl+C after it's been running for a while :-)

Enjoy!

#!/usr/bin/perl use strict; use warnings; use POE; *MATE = *STDOUT; POE::Session->create( inline_states => { _start => \&_start, make_baby => \&make_baby, }, ); $poe_kernel->run; sub _start { my $kernel = $_[ KERNEL ]; $kernel->alias_set('God'); $kernel->post('God', 'make_baby'); } sub make_baby { my $session = $_[ SESSION ]; POE::Session->create( inline_states => { _start => \&born, _stop => \&die, live => \&live }, args => [ $session ], ); } sub born { my ($heap, $person, $kernel, $init) = @_[ HEAP, SESSION, KERNEL, A +RG0 ]; $heap->{age} = 0; $heap->{gender} = (int(rand(2)) ? 'male' : 'female'); $heap->{person} = $person->ID - 2; $heap->{kids} = int(rand(5)); @{$heap->{actions}} = qw/Crying Sleeping Eating Pooping/; print "Child $heap->{person} born, age $heap->{age}, gender $heap- +>{gender}. Likely to have $heap->{kids} kids. Actions: " . join(', +', @{$heap->{actions}}) . "\n"; $kernel->delay('live', .1, $init); } sub live { my ($heap, $kernel, $init) = @_[ HEAP, KERNEL, ARG0 ]; $heap->{age}++; if ($heap->{'age'} == 2) { @{$heap->{actions}} = qw/Screaming Crying/; } elsif (($heap->{'age'} >= 3) && ($heap->{'age'} <= 12)) { @{$heap->{actions}} = qw/Running Playing "Getting into Trouble +"/; } elsif (($heap->{'age'} >= 13) && ($heap->{'age'} <= 15)) { if ($heap->{'gender'} eq "male") { @{$heap->{actions}} = qw/Eat Eat Eat Eat Eat Sleep/; } else { @{$heap->{actions}} = qw/Talk Talk Talk Shop Shop Sleep/; } } elsif ( ($heap->{'age'} >= 16) && ($heap->{'age'} <= 20) ) { @{$heap->{actions}} = qw/Flirt Flirt Flirt Flirt Flirt/; } elsif ($heap->{'age'} >= 21 && $heap->{'age'} <= ($heap->{'gende +r'} eq 'male' ? 74.1 : 79.5)) { select MATE; @{$heap->{actions}} = 'Making and raising kids'; if(int(rand(5)) == 1 && $heap->{kids}) { print $init->ID . "\n"; $kernel->post('God', 'make_baby') or die $!; $heap->{kids}--; } } print "Person $heap->{person} turned $heap->{age}. Their activiti +es are now: " . join(', ', @{$heap->{actions}}) . "\n"; $kernel->delay('live', .1, $init); if ($heap->{'age'} >= ($heap->{'gender'} eq 'male' ? 74.1 : 79.5)) + { print my $will = "I, Person $heap->{person}, do make and decla +re my last will and testament as follows: ...\n"; $kernel->delay('live'); } } sub die { my $heap = $_[ HEAP ]; print "Person $heap->{person} died.\n"; }

Update: Slightly restructed with "God" session because the children were keeping the parents alive after they were supposed to be dead :-)

Replies are listed 'Best First'.
Re: Re: Life, in Perl.
by djbiv (Scribe) on Jul 24, 2003 at 18:41 UTC
    Very nice, ran effortlessly for me. The only comment I have is that you have "Person 1 turned 79. Their activities are now: Making and raising kids"... I hope that I can still 'make kids' at that age 8^)
    djbiv

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://276461]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others wandering the Monastery: (2)
As of 2024-04-20 03:16 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found