1

I need to create a heads or tails project where the computer will guess randomly up to 5 times, but on the sixth time it will look into the playersGuessHistory variable setup as a string to see if it can find a match for a pattern of 4 entires. If there is a pattern found the computer will guess the next character after the pattern.

For example, given the sequence HHTTH the pattern is HHTT so the computer would guess H for the sixth turn. My only problem is that I'm having difficulty setting up the project so that it will look through the playersguesshistory and find the patterns and guess the next character in the history. Any suggestions?

3
  • 2
    Sounds as if this should be tagged as homework? Commented Apr 29, 2010 at 6:59
  • Incomplete question. Did not mention - sample test cases and results, what's the challenge faced by the OP, why should it not tagged homework (because it clearly seems to be). Commented Apr 29, 2010 at 7:12
  • Would you like to post the code you have already written in your question? Commented Apr 29, 2010 at 8:21

3 Answers 3

1

Create a List<string> and throw the history into this, so that each item in the list is a string of 4 characters (like you show in your text). Then when the computer should guess select the items (there should be several) from the list that starts with (myList.StartsWith - method) your string, then you should sum up the amount of times that H is the next character, and the amount of times that T is the next character - calculate the probability of each of them and let the computer choose the one with the highest probability...

Does it make sense?

Sign up to request clarification or add additional context in comments.

5 Comments

Yeah the logic behind that makes sense but trying to code it is kind of where i'm stuck i know what its supposed to be doing it, the syntax how ever is where i'm stuck.
basically here's what i have going: private void headsButton_Click /computer randomly guess if guesscount <= 5 ++GuessCount; if (GuessCount <= 5) CpuGuess = RandomGuess(x, y); x = (int)Guess.Head; /add players guess to the string that will be searched /show players history of guesses label1.Text = PlayerGuessHistory +=x; /check to see if computer has guessed right if (CpuGuess == x) cpuPointsLabel.Text = (++CpuPoints).ToString(); else /check to see if computer has guessed incorrectly if (CpuGuess != x) PlayersPointsLabel.Text = (++PlayerPoints).ToString();
else { //if guesscount > 5 have computer look at guess history //have computer guess according to the pattern of players guess history MessageBox.Show("next guess should be taken from the player guess history"); } }
@lo3: Instead of putting your code into the comment of one answer (even it is the only one) makes it hard to find and read. Instead edit your question and put the code there.
Sorry oliver, i'm new to stackoverflow didn't know that i had the ability to edit my initial question.
0

This is a little snippet based on what I understand of your requirement. The below method will return a string of guesses of 'H' heads or 'T' tails. The first 5 guesses are random, and then if any sequence of 4 guesses is HHTT the final guess will be 'H'.

     static string HeadsOrTails()
    {
        string guessHistory = String.Empty;
        // Guess heads or tails 5 times
        Random random = new Random();
        for (int currentGuess = 0; currentGuess < 5; currentGuess++)
        {
            if (random.Next(2) == 0)
                guessHistory += 'H';
            else
                guessHistory += 'T';
        }
        // Analyse pattern of guesses
        if (guessHistory.Substring(0, 4) == "HHTT" || guessHistory.Substring(1, 4) == "HHTT")
        {
            // If guess history contains HHTT then make the 6th guess = H
            guessHistory += 'H';
        }

        return guessHistory;
    }

This is a very simple implementation and will only work for 5 random initial guesses, but it should be quite easy to enhance as needed.

2 Comments

Thanks phil i'm going to go ahead and try this out and see how it works, thank you very much for responding :D
@lo3: Welcome. I deliberately kept it simple, so hopefully it's easy to understand. Ordinarily the aim would be for a more generic and reusable approach but I didn't want to take all the fun parts from you.
0

First of all, if the heads and tails are really random, like results from flipping an actual coin, this task is pointless. The computer will always get the next throw right with probability 1/2, regardless of any perceived patters in the history. (See "Independence".)

Now, if the heads and tails are not really random (e.g. they are created by a person calling heads or tails in a way he thinks is random), then we can maybe get the computer to have a higher success quote than 1/2.

I'd try the following: To start, check how often in the history.

  • heads are followed by heads
  • heads are followed by tails

and use these number for a guess on the transition probability H->H and H->T, do the same with the tails, and guess the next outcome based on the last one, choosing whatever seems more probable..

Says in the sequence "HHHTH", you find - H->H: 2 of 3 - H->T: 1 of 3 - T->H: 1 of 1 Since the last throw came up heads, the computer should choose heads as the guess for the next throw.

Now, you can experiment with taking longer parts of the history into account, by counting the transitions "HH->T" and so on and try to improve your success rate.

1 Comment

I think lo3 already knows what to implement - the question seems to be more about how to implement it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.