The Best of Creative Computing Volume 1 (published 1976)

Page 257 << PREVIOUS >> NEXT Jump to page:
Go to contents Go to thumbnails

Guess (BASIC computer game, comparison, searching a list, BASIC program listing, sample run)

graphic of page

[image]
GUESS
[image]

GUESS is a simple introduction to the idea of a computer program and computer
games. It also introduces the notions of comparison and searching a list.

Before playing GUESS on the computer, you may wish to play it in class at the
chalkboard. One student is selected to be the computer. He is given the
flowchart below and is asked to follow it and write on the chalkboard the
statements in parentheses as well as each guess. Students take turns being the
computer.

[image]
FLOWCHART

START

CHOOSE ANY NUMBER BETWEEN 1 AND 100

WRITE ON CHALKBOARD, "I'M THINKING OF A NUMBER"

WRITE ON CHALKBOARD, "WHAT'S YOUR GUESS?"

ACCEPT GUESS FROM CLASS MEMBER AND WRITE IT ON THE CHALKBOARD

COMPARE GUESS TO NUMBER
WRITE "TOO HIGH"

WRITE "YOU GOT IT!"

WRITE "TOO LOW"

STOP

[image]

After playing the game manually, play GUESS on the computer. Divide the class
into small teams of 2 or 3 members each. Have each team play 10 games on the
computer and keep track of the number of guesses required to get each number.
Compute the average guesses for each team and for the entire class. After
playing the class should consider the following questions:

1. Why should it never take more than 7 guesses to find the number?

2. What was the average guesses for the class? How does this compare to the
theoretical average (using the correct guessing strategy) of 5.3?

3. What are the maximum number of guesses required to find a mystery number
between:

1 and 10    1 and 64

1 and 63    1 and 1000

PROGRAM LISTING

1 PRI "THIS IS A NUMBER GUESSING GAME. I'LL THINK"

2 PRI "OF A NUMBER BETWEEN 1 AND ANY LIMIT YOU WANT. "

3 PRI "THEN YOU HAVE TO GUESS WHAT IT IS."

4 PRI

5 PRI "WHAT LIMIT DO YOU WANT"

6 INPL

7 PRI 

8 L1=INT(LOG(L)/LOG(2))+1

10 PRI "I'M THINKING OF A NUMBER BETWEEN 1 AND"L

11 G=1

14 PRI "NOW YOU TRY TO GUESS WHAT IT IS"

15 M=INT(L*RND(0))+1

20 INP N

21 IF N>0 THEN 25

22 GOSUB70

23 GOTO1

25 IF N=M THEN 50

30 G=G+1

31 IF N>M THEN 40

32 PRI "TOO LOW. GUESS AGAIN. "

33 GOTO 20

40 PRI "TOO HIGH. GUESS AGAIN. "

42 GOTO20

50 PRI "THAT'S IT! YOU GOT IT IN*G*TRIES.

52 IF G<L1 THEN 50

54 IF G=L1 THEN 60

56 PRI "YOU SHOULD HAVE BEEN ABLE TO GET IT IN ONLY*L1*

57 GOT 65

58 PRI "VERY ";

60 PRI "GOOD!

65 GOSUB70

66 GOTO10

70 FOR H=1 TO 5

71 PRI

72 NEXT H

73 RETURN

99 END

Note: BASIC statements in this program are abbreviated to their first 3 letters.

SAMPLE RUN

THIS IS A NUMBER GUESSING GAME. I'LL THINK 

OF A NUMBER BETWEEN 1 AND ANY LIMIT YOU WANT.

THEN YOU HAVE TO GUESS WHAT IT IS.

WHAT LIMIT DO YOU WANT? 100

I'M THINKING OF A NUMBER BETWEEN 1 AND 100

NOW TRY TO GUESS WHAT IT IS.

? 50

TOO HIGH. GUESS AGAIN.

? 25

THAT'S IT! YOU GOT IT IN 2 TRIES.

VERY GOOD!

I'M THINKING OF A NUMBER BETWEEN 1 AND 100

NOW TRY TO GUESS WHAT IT IS.

? 50

TOO HIGH. GUESS AGAIN.

? 25

TOO LOW. GUESS AGAIN.

? 37 

TOO HIGH. GUESS AGAIN. 

? 31 

TOO HIGH. GUESS AGAIN.

? 28

TOO LOW. GUESS AGAIN.

? 29

TOO LOW. GUESS AGAIN.

? 30

THAT'S IT! YOU GOT IT IN 7 TRIES.

GOOD!

Page 257 << PREVIOUS >> NEXT Jump to page:
Go to contents Go to thumbnails