### Author Topic: Question - AI Guessing Game in SpecBAS  (Read 2172 times)

#### Richly

• Guest
##### Question - AI Guessing Game in SpecBAS
« on: August 14, 2017, 10:24:43 PM »
I have come up with the following code in SpecBAS in response to a programming challenge to create an AI guessing game.

The aim is to get the computer to generate a random number and then for the computer to try and guess the number.

I can get the computer to eventually guess the randomly generated number but only through increments or decrements of 1.

Is there a way of getting the computer to reach the randomly generated number through a process of elimination as a human would, which may or may not necessarily be done through increments / decrements of 1?

Here is my code...

Code: [Select]
5 REM AI Guessing Game
10 PRINT "I'M CHOOSING A NUMBER BETWEEN 1 AND 100"
20 LET AIChoose=INT(RND*100)
30 PRINT "The number is ";AIChoose
40 PRINT "I'M TRYING TO GUESS THE NUMBER"
60 LET AIGuess=INT(RND*100)
80 DO
90 IF AIGuess<AIChoose THEN INC AIGuess,AIGuess TO 100
95 IF AIGuess>AIChoose THEN DEC AIGuess,AIGuess TO AIChoose
110 PRINT "Is it ";AIGuess;"?"
120 IF AIGuess=AIChoose THEN EXIT
130 LOOP UNTIL AIGuess=AIChoose
140 PRINT "Yes, the number I guessed was ";AIGuess;" and the number you chose was ";AIChoose
150 STOP

Many thanks in advance for any help. Any other suggestions for improvements would also be greatly appreciated.

#### B+

• Guest
##### Re: Question - AI Guessing Game in SpecBAS
« Reply #1 on: August 15, 2017, 02:13:41 AM »
Hi Richey,

I would setup a Hi and Lo variable, starting the Hi at 101 and the Lo at 0 then the guess is (Hi - Lo)/2 + Lo, so first guess is 101/2 + 0 = 50 (You might need INT to round down to integer guesses.)

From the feed back on the guess, raise the Lo or Lower the Hi.
So say 50 was too high, then your new Hi is 50 and your next guess is 50/2 + 0 = 25

If 50 was too low then the Lo is made 50 and (101 - 50)/2 + 50 = 75 is your next guess.

Continue in this way until the guess is correct.

You would find a word in a sorted list in the same way.

Append: This works in SmallBASIC
Code: [Select]
' Hi Lo AI.bas SmallBASIC 0.12.9 (B+=MGA) 2017-08-14

secret = int(rnd*100) + 1 ' < 1 to 100
Hi = 101 : Lo = 0   '< make these higher and lower than what the secret number can be (just tested secret = 100, oops!)
label anotherGuess
guess = int((Hi - Lo)/2) + Lo
print "Guessing: ";guess
if guess = secret then
print "Yes! The secret number was ";guess
? : input "Want to see another, y + Enter for yes ";yes
if yes = "y" then
? : secret = int(rnd*100) + 1 : Hi = 101 : Lo = 0
else
stop
fi
elif guess > secret then
Print "Too high!"
Hi = guess
else
print "Too Low!"
Lo = guess
fi
goto anotherGuess

EDIT: Hi start at 101 not 100

« Last Edit: August 15, 2017, 02:41:54 AM by B+ »

#### Richly

• Guest
##### Re: Question - AI Guessing Game in SpecBAS
« Reply #2 on: August 30, 2017, 06:04:40 PM »
Thanks B+

I'll look to re-write my code when I have time for programming again