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

Richey

  • Full Member
  • ***
  • Posts: 144
    • View Profile
Question - AI Guessing Game in SpecBAS
« on: 15. August 2017, 00:24:43 »
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+

  • Sr. Member
  • ****
  • Posts: 436
    • View Profile
Re: Question - AI Guessing Game in SpecBAS
« Reply #1 on: 15. August 2017, 04:13:41 »
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: 15. August 2017, 04:41:54 by B+ »

Richey

  • Full Member
  • ***
  • Posts: 144
    • View Profile
Re: Question - AI Guessing Game in SpecBAS
« Reply #2 on: 30. August 2017, 20:04:40 »
Thanks B+

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