Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: AI in Perl - proof of concept

by Abigail-II (Bishop)
on Jul 15, 2002 at 09:33 UTC ( #181764=note: print w/replies, xml ) Need Help??


in reply to AI in Perl - proof of concept

Here's the regex variant.
#!/usr/bin/perl -w use strict; use warnings 'all'; use re 'eval'; use vars qw /%owns %valuable @thief/; %owns = (merlyn => [qw /gold/], ovid => [qw /pocketLint cheapWhiskey/], kudra => [qw /dominationFund/], badguy => [qw /water/], Abigail => [qw /pearls rubies/],) ; %valuable = map {$_ => 1} qw /gold dominationFund pearls/; @thief = qw /badguy Abigail/; my $regex; my $thief_rule = join "|\n" => map {"(?{ \$thief = qq !$_! })"} @thie +f; $regex .= "(?: $thief_rule )\n"; my $victim_rule = join "|\n" => map {"(?{ \$victim = qq !$_! })"} keys + %owns; $regex .= "(?: $victim_rule )\n"; # Don't steal from yourself. $regex .= "(?(?{ \$thief eq \$victim }) fail)\n"; # Victim needs to be rich. $regex .= "(?(?{ \@loot = grep {\$valuable {\$_}} \@{\$owns {\$victim}} } +) |\n" . "(?{ print qq !\$thief does not steal from \$victim\n!}) fail +)\n"; $regex .= "(?(?{ print qq !\$thief steals \@loot from \$victim +\n! })" . "fail)\n"; "" =~ /$regex/x;
Output:
badguy does not steal from ovid
badguy steals pearls from Abigail
badguy steals dominationFund from kudra
badguy steals gold from merlyn
Abigail does not steal from ovid
Abigail does not steal from badguy
Abigail steals dominationFund from kudra
Abigail steals gold from merlyn
I'm not quite satisfied with the regex though; it would be nice if the regex could be independent of the content of the variables. I haven't been able to find a way to say "this (sub)expressions just failed. Let's try the same thing again".

Abigail

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (3)
As of 2019-07-23 05:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If you were the first to set foot on the Moon, what would be your epigram?






    Results (24 votes). Check out past polls.

    Notices?