views:

107

answers:

3

Ok i'm working on a project for a 200 level C# course, we are required to create a heads or tails project. Basically the project is setup so that 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 EX: [HHTT]H [HHTTH]H HHTT being the pattern then the computer would guess H for the next 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?

+1  A: 

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?

H4mm3rHead
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.
lo3
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 guesseslabel1.Text = PlayerGuessHistory +=x;/check to see if computer has guessed rightif (CpuGuess == x)cpuPointsLabel.Text = (++CpuPoints).ToString();else/check to see if computer has guessed incorrectlyif (CpuGuess != x)PlayersPointsLabel.Text = (++PlayerPoints).ToString();
lo3
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
@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.
Oliver
Sorry oliver, i'm new to stackoverflow didn't know that i had the ability to edit my initial question.
lo3
A: 

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.

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

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.

Phil
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
@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.
Phil