Algorithm:
(imin + imax) / 2 = guess
Code:
If intImax = 0 Then
intImax = 100
End If
If intImin = 0 Then
intImin = 1
End If
If intNumberOfGuesses = 0 Then
AIguess = 50
Else
AIguess = Int((intImin + intImax) / 2)
End If
Okay i'll take one bit at a time.
If intImax = 0 Then
intImax = 100
End If
what happens here is that if intImax is zero it'll be set to 100
If intImin = 0 Then
intImin = 1
End If
and here the same happens with Imin but it's set to 1.
If intNumberOfGuesses = 0 Then
AIguess = 50
these 2 lines of code tells the program that if it's the first guess of the game, then the A.I should guess 50.
Else
AIguess = Int((intImin + intImax) / 2)
End If
and here if it isn't the first guess of the game, the program runs the algorithm.
But that doesn't work, if it keeps running this algorithm, it'll keep guessing, so we need to keep changing Imin and Imax, here's the code that does this for us.
Select Case result
Case "Lower"
If Guess < gintImax Or gintImax = 0 Then
gintImax = Guess
End If
Case "Higher"
If Guess > gintImin Then
gintImin = Guess
End If
So here again i'll take one bit of code at a time.
Select Case result
Case "Lower"
If Guess < gintImax Or gintImax = 0 Then
gintImax = Guess
End If
Here if a guess is lower then the random number, it says that if the guess is lower then Imax or if Imax is 0, then set the Guess to Imax.
Case "Higher"
If Guess > gintImin Then
gintImin = Guess
End If
Here it's pretty much the same, but with a few alterations, this code is activated if the guess is higher then the random number, what it does is, if the guess is larger then Imin, then set Imin to the guess.
And it's that simple.
-Rasmus Iversen
No comments:
Post a Comment