[Webinar] Streamline your web hosting managementRegister Today


AI.  String parsing.

Posted on 2003-03-09
Medium Priority
Last Modified: 2013-12-26
I was wondering ig you could help me.  I am trying to get an AI program that will simulate a Heads and tails guessing game.  The program is supposed to be played 200 times by the user.  The user and the computer pick heads or tails.  If the computer and the user pick the same thing the computer wins.  The coumputer bases its guess on the previously played heads or tails... so I was wondering how I could get a computer to parse a string and find the best possable guess..
Question by:cmcrae

Expert Comment

ID: 8112961
Short answer is not possible in all cases. You might assume that the user will tend to favor a response, based on either the current win/lose ratio, some of the more freq responses or some other strategy; but the user could be flipping a coin or using a sequence which does not repeat within 200 plays.

But lets assume that the user  to give the best possiable guess without making assumations which was not stated in the problem.

I considered how often a sequence appears, how often it wins/loses, and how much variation was in the sequence.

Need a feedback loop. Interesting problem if you want to feedback more than one decision.

LVL 85

Accepted Solution

ozo earned 200 total points
ID: 8140795
#!/usr/bin/perl -w
use strict;
my $p='';
my @s=(0,0);
my $g = qw(H T)[rand 2];
print "H or T ?";
while( <> ){
    my $u;
    exit if /q/;
    unless( ($u) = /([ht])/i ){
        print "I don't know $_, please enter H or T\n";
    print "My guess $g\n";
    $s[lc($g) eq lc($u)] += 1;
    print $s[1]/($s[1]+$s[0])," correct\n";
    $p .= lc($u).$g;
    $g = g($p);
    print "H or T ?";
sub g{
    my $p = shift;
    my $i;
    my $h=1;
    my $t=1;
    for( 0..(length$p)/2 ){
        my $c = substr($p,-$_);
        $h += (()=$p=~ /${c}h/g)*1.5**$_;
        $t += (()=$p=~ /${c}t/g)*1.5**$_;
    return qw(H T)[rand($h+$t)>$h];

Expert Comment

ID: 8144291
Does it really make sense to have the computer attempt to predict what is supposedly a completely random event?  If you want it to be fair, the computer should probably guess randomly (that's what the human player is doing [even if he doesn't think so]).  Otherwise, I would suggest giving the computer a "skill" which represents a small percentage chance that he guess correctly (cheating).  Possibly, you could determine outside factors responsible for the final position of the coin using a neural network.  This really isn't an answer, more of an intellectual response.  Thank you for indulging me.
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.


Expert Comment

ID: 8274898
Humans don't usually guess things randomly, even if they try to, so it would make some sense trying to predict what the human player will guess next. However, there isn't really any 'best' way to do this unless you make assumptions about the human player. It could for example be that the human player is more likely to pick tails if a majority of the previous guesses was heads. These assumptions will determine what it considered the best thing to do for the computer.

Expert Comment

ID: 8350700
What I would do is assign weights to certain things, like percieved patterns.  Then have the computer generate a random number and add (or subtract) the weighted values and guess heads or tails based on the results.  This make it so the computer leans one way but is still inherently random.
LVL 31

Expert Comment

ID: 8492879
(1) Humans do not choose randomly

(2)Most coins are not fair they split 49% Tails and 51% Head roughly.

So there are probably going to be patterns to identify.

(a) A very simple technique is to count the heads (H) and tails (T) over the last say six tosses. Generate a random number 0<= r <=1. If r > T/H then choosee head else choose tails. Simple to program worth a try. (Or even the other way around as the user may subconciously try to compensate for a lack of heads or tails in their previous choices, you can check to see which is the best way around during the running of the game and behave adaptively)

(b) More complex is to search for occurrances of the pattern of say the previous 4 or 5 results and calculating the probablity of a head or a tail for the next. This is more complex as there may not be occurances of the previous pattern in which case you must then search for the next shortest previous pattern.

Featured Post

Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Recently, in one of the tech-blogs I usually read, I saw a post about the best-selling video games through history. The first place in the list is for the classic, extremely addictive Tetris. Well, a long time ago, in a galaxy far far away, I was…
Performance in games development is paramount: every microsecond counts to be able to do everything in less than 33ms (aiming at 16ms). C# foreach statement is one of the worst performance killers, and here I explain why.
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?

590 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question