Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Perl meets CoreWars

by JPaul (Hermit)
on Jan 28, 2002 at 08:19 UTC ( [id://142013]=CUFP: print w/replies, xml ) Need Help??

Greetings all,
Many years ago I became famaliar with a pretty cool game called CoreWars. I was never terribly good at it, being a young fellow with little real programming behind me in anything but QuickBASIC, and never really took it anywhere - but the concept always seemed very cool to me.

I'm not even entirely sure what prompted me to think of it the other day, but I suddenly thought 'How about perl CoreWars?', and a week later, so it was... In one form or another.
I've kinda crossed it with a little of a Mac game 'RoboWar', in which you programme little robots to blast each other away, and UNIXed it a bit with processes instead of memory blocks, but its the same basic idea.
It's still a little simplistic, but its got potential - I figured I could see what you guys think of it, and where you think it could go.
Posting the source would be a little untidy, so for all who are interested the JProcWars homepage is up.
Tell me what you think.

JP,
-- Alexander Widdlemouse undid his bellybutton and his bum dropped off --

Replies are listed 'Best First'.
Re: Perl meets CoreWars
by hossman (Prior) on Jan 28, 2002 at 11:32 UTC
    This is pretty cool, but it doesn't seem to be very fair. Most of the times I tried running it, only one class of warrior (usually whichever on is listed last on the commandline) is ever getting to take turns -- even though the launcher says they're all loaded).

    At one point, the launcher decided the game was over before any warrior had taken a turn.

    A couple of random suggestions:

    • You might consider "seeding" the playing feild, creating N of each warrior before sending them all a "go" command.
    • the syntax for warriors is a little awkward (especially all the warnings about "No section for line __") what about just letting each file contain whatever "global" declarations people want, plus a "sub warrior_loop" that will get called over and over
    • a FindBrotherWarriors would be helpfull.

    Anyway, here's my first attempt at a warrior...

    ;predecl # hoss1 # if the ratio of enemies warriors to the total num # of warriors is greater # then N, then attack. Otherwise, Spawn. my $ratio = 2 / 3; ;main # Check other warriors my $enemies = JProcRoutines::FindEnemyWarriors; my $all = JProcRoutines::FindWarriors; my $magic_num = (keys %$enemies) / (keys %$all); print "$me : ratio is $magic_num\n"; if ($magic_num > $ratio) { # Pick a random enemy to hit my $rand_enemy = (keys %$enemies)[rand(keys %$enemies)]; # Hit them. &JProcRoutines::Hit($rand_enemy); } else { # Replicate, and if we get an undef back, set up a wait &JProcRoutines::Replicate; }
      Greetings,
      I'm terribly embaressed - I always found it extremely irritating when developers would push something out the door without testing it one last time - and what did I do? Oh the shame.

      However:

      • I can't replicate your problem with only one warrior playing on - no matter which order I put the warriors in, warrior3 always kicks butt - not that I'm entirely sure why, but it appears to Replicate more (I'll investigate, if anyone should know, it should be me). There is always all the warriors specified playing (for me).
      • The bizarre complaints about things not being in a section is fixed. Doofus to me.
      • The even stranger one when the server decides the game is over - yet everyone else plays on regardless is also fixed. Another doofus for me.
      • In theory the warriors should all start around one second of each other (which, admittedly, in clock speed is a LONG time, but) as they all have to wait for the server to come up.
      • FindBrotherWarriors() implemented
      • I'll think a bit more about making the section thing nicer, you're right - it is a bit cac-handed.

      JProcWars v0.2.1 released ;)

      JP,
      -- Alexander Widdlemouse undid his bellybutton and his bum dropped off --

Re: Perl meets CoreWars
by hossman (Prior) on Jan 29, 2002 at 14:10 UTC
    I think my problem with warriors not getting a "turn" may have had to do with having $JProcRoutines::MaxProcs set too low ... i bumped it up to 20, and now it works great.

    We can rebuild him, stronger, faster, better...

    ;predecl # hoss2 # # if there are less then $bromin brothers, spawn. # else: if pid is even, attack # if pid is odd, spawn # # only check our brother list periodicly to save time # use a wait counter if spawning was prevously unsuccesful # my $brothers = undef; my $bromin = 10; my $browait = 0; my $spawnwait = -1; # ;main # if ($browait <= 0) { $brothers = &JProcRoutines::FindBrotherWarriors(); $browait = 5; } if ( (0 > $spawnwait) and ((scalar(keys %$brothers) < $bromin) or ($$ % 2 == 1)) ) { # spawn if ( ! defined &JProcRoutines::Replicate()) { $spawnwait = 5; } } else { # attack! my $warriors = &JProcRoutines::FindEnemyWarriors(); if (keys %$warriors > 0) { # Pick a random enemy to hit my $rand_enemy = (keys %$warriors)[rand(keys %$warriors)]; # Hit them. &JProcRoutines::Hit($rand_enemy); } } # cycle counters... $spawnwait--; $browait--;

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (5)
As of 2024-04-18 09:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found