Small Basic Small Basic Books > Basic Computer Games Small Basic Edition > Bagels

In this game, the computer picks a 3 digit secret number using the digits 0 to 9 and you attempt to guess what it is. You are allowed up to twenty guesses. No digit is repeated. After each guess the computer will give you clues about your guess as follows:

PICO One digit is correct, but in the wrong place.

FERMI One digit is in the correct place.

BAGELS No digit is correct.

You will learn to draw inferences from the clues and, with practice, you’ll learn to improve your score. There are several good strategies for playing Bagels. After you have found a good strategy, see if you can improve it or try a different strategy altogether and see if it is any better. While the program allows up to twenty guesses, if you use a good strategy it should not take more than eight guesses to get any number.

The original authors of this program are D. Resek and P. Rowe of the Lawrence Hall of Science, Berkeley, California.

Code Listing (Small Basic File: bagels.sb):

TextWindow.CursorLeft = 33
TextWindow.WriteLine("BAGELS")
TextWindow.CursorLeft = 15
TextWindow.WriteLine("BIBLEBYTE BOOKS, 
MAPLE VALLEY, WASHINGTON") TextWindow.WriteLine("") TextWindow.WriteLine("") ' *** BAGLES NUMBER GUESSING GAME ' *** ORIGINAL SOURCE UNKNOWN BUT SUSPECTED
TO BE ' *** LAWRENCE HALL OF SCIENCE, U.C. BERKELY Y=0 T=255 TextWindow.WriteLine("") TextWindow.WriteLine("") TextWindow.WriteLine("") TextWindow.Write("WOULD YOU LIKE THE RULES
(YES OR NO) ") AD = TextWindow.Read() If Text.GetSubText(AD,1,1)="N" Then Goto LN150 EndIf TextWindow.WriteLine("") TextWindow.WriteLine("I AM THINKING OF A
THREE-DIGIT NUMBER.
TRY TO GUESS") TextWindow.WriteLine("MY NUMBER AND I WILL
GIVE YOU CLUES AS FOLLOWS:") TextWindow.WriteLine(" PICO -
ONE DIGIT CORRECT
BUT IN THE WRONG POSITION") TextWindow.WriteLine(" FERMI -
ONE DIGIT CORRECT
AND IN THE RIGHT POSITION") TextWindow.WriteLine(" BAGELS -
NO DIGITS CORRECT") LN150: For I=1 To 3 LN160: A[I]=Math.GetRandomNumber(10) - 1 If I-1=0 Then Goto LN200 EndIf For J=1 To I-1 If A[I]=A[J] Then Goto LN160 EndIf EndFor LN200: EndFor TextWindow.WriteLine("") TextWindow.WriteLine("O.K. I HAVE A NUMBER
IN MIND.") For I=1 To 20 LN230: TextWindow.WriteLine("GUESS #"+I) Ad = TextWindow.Read() If Text.GetLength(AD)<>3 Then Goto LN630 EndIf For Z=1 To 3 A1[z]=Text.GetCharacterCode
(Text.GetSubText(AD,Z,1)) EndFor For J=1 To 3 If A1[J]<48 Then Goto LN300 EndIf If A1[J]>57 Then Goto LN300 EndIf B[J]=A1[J]-48 EndFor Goto LN320 LN300: TextWindow.WriteLine("WHAT?") Goto LN230 LN320: If B[1]=B[2] Then Goto LN650 EndIf If B[2]=B[3] Then Goto LN650 EndIf If B[3]=B[1] Then Goto LN650 EndIf C=0 D=0 For J=1 To 2 If A[J]<>B[J+1] Then Goto LN390 EndIf C=C+1 LN390: If A[J+1]<>B[J] Then Goto LN410 EndIf C=C+1 LN410: EndFor If A[1]<>B[3] Then Goto LN440 EndIf C=C+1 LN440: If A[3]<>B[1] Then Goto LN460 EndIf C=C+1 LN460: For J=1 To 3 If A[J]<>B[J] Then Goto LN490 EndIf D=D+1 LN490: EndFor If D=3 Then Goto LN680 EndIf If C=0 Then Goto LN545 EndIf For J=1 To C TextWindow.Write("PICO ") EndFor LN545: If D=0 Then Goto LN580 EndIf For J=1 To D TextWindow.Write("FERMI ") EndFor LN580: If C+D<>0 Then Goto LN600 EndIf TextWindow.Write("BAGELS") LN600: TextWindow.WriteLine("") EndFor TextWindow.WriteLine("OH WELL.") TextWindow.WriteLine("THAT'S TWENTY GUESSES.
MY NUMBER WAS "+100*A[1]+10*A[2]+A[3]) Goto LN700 LN630: TextWindow.WriteLine("TRY GUESSING A
THREE-DIGIT NUMBER.") Goto LN230 LN650: TextWindow.WriteLine("OH, I FORGOT TO TELL YOU
THAT THE NUMBER I HAVE IN MIND") TextWindow.WriteLine("HAS NO TWO DIGITS
THE SAME.") Goto LN230 LN680: TextWindow.WriteLine("YOU GOT IT!!!") TextWindow.WriteLine("") Y=Y+1 LN700: TextWindow.Write("PLAY AGAIN (YES OR NO) ") AD = TextWindow.Read() If Text.GetSubText(AD,1,1)="Y" Then Goto LN150 EndIf If Y=0 Then Goto LN750 EndIf TextWindow.WriteLine("") TextWindow.WriteLine("A "+Y+" POINT BAGELS
BUFF!!") LN750: TextWindow.WriteLine("HOPE YOU HAD FUN.
BYE.")

This chapter is adapted from the book Basic Computer Games Small Basic Edition published by Kidware Software.

To purchase this book in its entirety, please see the Computer Science For Kids web site.


Sample Run:

Bagels

Next Chapter > >

Excerpt © Copyright 2010-2013 By Kidware Software LLC All Rights Reserved. Computer Science For Kids, the Computer Science For Kids logo, and related trade dress are trademarks or registered trademarks of Kidware Software LLC. Philip Conrod & Lou Tylee have co-authored dozens of books and tutorials for beginning Microsoft Basic, Small Basic, Visual Basic, and Visual C# developers of all ages for over 25 years.