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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

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 84

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.

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.


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

Will your db performance match your db growth?

In Percona’s white paper “Performance at Scale: Keeping Your Database on Its Toes,” we take a high-level approach to what you need to think about when planning for database scalability.

Question has a verified solution.

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

Artificial Intelligence comes in many forms, and for game developers, Path-Finding is an important ability for making an NPC (Non-Playable Character) maneuver through terrain.  A* is a particularly easy way to approach it.  I’ll start with the algor…
As game developers, we quickly learn that Artificial Intelligence (AI) doesn’t need to be so tough.  To reference Space Ghost: “Moltar, I have a giant brain that is able to reduce any complex machine into a simple yes or no answer. (http://www.youtu…
In this brief tutorial Pawel from AdRem Software explains how you can quickly find out which services are running on your network, or what are the IP addresses of servers responsible for each service. Software used is freeware NetCrunch Tools (https…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
Suggested Courses
Course of the Month15 days, 14 hours left to enroll

743 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