Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
From a totally raw point of view, I would keep track of "x previous moves" and "current move".

Warning: Spoiler Code Ahead

#!/usr/bin/perl -w # roshambo # usage: roshambo [look-behind] # look-behind number of previous turns AI examines use strict; my $look_behind = shift || 10; my @beat = (1, 2, 0); my @names = qw( Rock Paper Scissors ); my @chars = qw( R P S ); my (%freq,@last,$uw,$cw,$t); while (1) { my $rps; ### UPDATE ### FIXED SELECTION for my $i (0 .. $#last) { if (my $c = $freq{"@last[$i .. $#last]"}) { $rps = $beat[ weighted(@$c) ]; last; } } $rps = int rand 3 if not defined $rps; print "Rock (1), Paper (2), Scissors (3): "; chomp(my $user = <STDIN>); last if !$user or $user > 3; ### UPDATE ### FIXED WEIGHTING $user--; for (0 .. $#last) { $freq{"@last[0 .. $_]"}[$user]++; } shift @last if @last == $look_behind; push @last, $user; print "$names[$user] vs. $names[$rps] => "; if ($rps == $user) { $t++; print "tied\n" } elsif ($rps == $beat[$user]) { $cw++; print "AI wins\n" } else { $uw++; print "you win\n" } } sub weighted { my ($choice,$sum); for my $i (0 .. $#_) { $_[$i] ||= 0; $choice = $i if rand($sum += $_[$i]) < $_[$i]; } return $choice; } print "You won: $uw\n"; print "AI won: $cw\n"; print "Ties: $t\n\n"; for (sort { length($a) <=> length($b) } keys %freq) { for (split ' ') { print $chars[$_] } print " => ( "; for my $i (0 .. 2) { print "$chars[$i] = ", $freq{$_}[$i] || 0, ", "; } print ")\n"; }
All that code does is remember what move you made when you'd made a certain list of previous moves. It selects a weighted choice. The bottom section is output to show how well the AI did.

japhy -- Perl and Regex Hacker

In reply to Re: Rock, Paper, Scissors by japhy
in thread Rock, Paper, Scissors by Sprad

Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":

  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?

What's my password?
Create A New User
Domain Nodelet?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (4)
As of 2023-01-29 21:57 GMT
Find Nodes?
    Voting Booth?

    No recent polls found