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?
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?
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.
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.