Author Topic: Stars (Number guessing program)  (Read 3173 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Stars (Number guessing program)
« on: November 06, 2021, 11:06:35 pm »
I found this program surfing old Basic code and wondered what it was about:
Code: QB64: [Select]
  1. 10 Print Tab(34); "STARS"
  2. 20 Print Tab(15); "CREATIVE COMPUTING  MORRISTOWN, NEW JERSEY"
  3. 100 Rem *** STARS - PEOPLE'S COMPUTER CENTER, MENLO PARK, CA
  4. 140 Rem *** A IS LIMIT ON NUMBER, M IS NUMBER OF GUESSES
  5. 150 A = 100: M = 7
  6. 170 Input "DO YOU WANT INSTRUCTIONS"; A$
  7. 190 If Left$(A$, 1) = "N" Then 280
  8. 200 Rem *** INSTRUCTIONS ON HOW TO PLAY
  9. 210 Print "I AM THINKING OF A WHOLE NUMBER FROM 1 TO"; A
  10. 220 Print "TRY TO GUESS MY NUMBER.  AFTER YOU GUESS, I"
  11. 230 Print "WILL TYPE ONE OR MORE STARS (*).  THE MORE"
  12. 240 Print "STARS I TYPE, THE CLOSER YOU ARE TO MY NUMBER."
  13. 250 Print "ONE STAR (*) MEANS FAR AWAY, SEVEN STARS (*******)"
  14. 260 Print "MEANS REALLY CLOSE!  YOU GET"; M; "GUESSES."
  15. 270 Rem *** COMPUTER THINKS OF A NUMBER
  16. 280 Print
  17. 290 Print
  18. 300 X = Int(A * Rnd(1) + 1)
  19. 310 Print "OK, I AM THINKING OF A NUMBER, START GUESSING."
  20. 320 Rem *** GUESSING BEGINS, HUMAN GETS M GUESSES
  21. 330 For K = 1 To M
  22.    340 Print
  23.    350 Print "YOUR GUESS";
  24.    360 Input G
  25.    370 If G = X Then 600
  26.    380 D = Abs(G - X)
  27.    390 If D >= 64 Then 510
  28.    400 If D >= 32 Then 500
  29.    410 If D >= 16 Then 490
  30.    420 If D >= 8 Then 480
  31.    430 If D >= 4 Then 470
  32.    440 If D >= 2 Then 460
  33.    450 Print "*";
  34.    460 Print "*";
  35.    470 Print "*";
  36.    480 Print "*";
  37.    490 Print "*";
  38.    500 Print "*";
  39.    510 Print "*";
  40.    520 Print
  41. 530 Next K
  42. 540 Rem *** DID NOT GUESS IN M GUESSES
  43. 550 Print
  44. 560 Print "SORRY, THAT'S"; M; "GUESSES. THE NUMBER WAS"; X
  45. 580 GoTo 650
  46. 590 Rem *** WE HAVE A WINNER
  47. 600 Print: For N = 1 To 79
  48.    610 Print "*";
  49. 620 Next N
  50. 640 Print "YOU GOT IT IN"; K; "GUESSES!!!  LET'S PLAY AGAIN..."
  51. 650 GoTo 280
  52. 660 End
  53.  
  54.  

I translated to more modern Basic and learned a little about LOGs.
Code: QB64: [Select]
  1. _Title "Stars: Guess number 1 to 100, get star rating 1 to 7, get 7 guesses, ...zzz = press any." 'b+ 2021-11-06
  2. 1 Cls: Randomize Timer: GoSub StarChart ' start over
  3. N = Int(100 * Rnd) + 1: nG = 1: Print ' N 'secret number : nG = number of guesses up to 7 allowed
  4. 2 Print " Guess #"; _Trim$(Str$(nG));: Input " enter > ", G
  5. If G = N Then Print "That's it in"; nG; "guesses...zzz": Sleep: GoTo 1
  6. Print " Rating: "; String$(7 - Int(Log(Abs(G - N)) / Log(2)), "* ")
  7. If nG = 7 Then Print "Sorry, that's 7 guesses, the number was"; N; "...zzz": Sleep: GoTo 1
  8. nG = nG + 1: GoTo 2 ' another guess
  9. StarChart: ' number of stars for number you are off
  10. Print Spc(35); "Star Chart:"
  11. For i = 1 To 100 ' this also tests my formula to compare to original game
  12.     If 7 - Int(Log(i) / Log(2)) <> last Then Print Spc(28); "For"; 7 - Int(Log(i) / Log(2)); "Stars, min off ="; i
  13.     last = 7 - Int(Log(i) / Log(2))
  14.  
  15. 'Test code for Star Chart LOG and convert to LOG(2) to convert to Star Rating
  16. 'For p = 0 To 6
  17. '    Print p, Log(2 ^ p) / Log(2) ' yes! When you are of 2 ^ x from number you get 7 - x stars
  18. 'Next
  19.  
  20.  

With the help of the Star Chart (and rewriting the program) it's not hard to pick up the strategy to play the game and usually get the number in 7 guesses.

I should probably reduce the density of the code. It's starting to look like MasterGy's ;-))

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Stars (Number guessing program)
« Reply #1 on: November 07, 2021, 06:32:15 pm »
Code easier to read and Star Rating easier to count:
Code: QB64: [Select]
  1. _Title "Stars: Guess number 1 to 100, get star rating 1 to 7, get 7 guesses, ...zzz = press any." 'b+ 2021-11-06
  2. ' 2021-11-07 make code more readable and star rating easier to count
  3.     Cls
  4.     GoSub StarChart '                         very helpful for handling star ratings
  5.     N = Int(100 * Rnd) + 1 '                  secret number
  6.     nG = 1 '                                  reset number of guesses
  7.     While nG <= 7 '                           allowing 7 guesses
  8.         Print " Guess #"; _Trim$(Str$(nG)); ' show what guess we're on
  9.         Input " enter > ", G '                enter guess
  10.         If G = N Then Print Spc(26); "That's it in"; nG; "guesses...zzz": Sleep: Exit While
  11.         Print " Rating: "; xCopy$(7 - Int(Log(Abs(G - N)) / Log(2)), "* ")
  12.         nG = nG + 1
  13.     Wend
  14.     If nG = 8 Then Print "Sorry, that's 7 guesses, the number was"; N; "...zzz": Sleep
  15.  
  16. StarChart: '                                  number of stars for number you are off
  17. Print Spc(35); "Star Chart:"
  18. For i = 1 To 100 '                            this also tests my formula to compare to original game
  19.     If 7 - Int(Log(i) / Log(2)) <> last Then Print Spc(28); "For"; 7 - Int(Log(i) / Log(2)); "Stars, min off ="; i
  20.     last = 7 - Int(Log(i) / Log(2)) '         convert number difference into star ratimg, save changes as last
  21.  
  22. 'Test code for Star Chart LOG and convert to LOG(2) to convert to Star Rating
  23. 'For p = 0 To 6
  24. '    Print p, Log(2 ^ p) / Log(2) ' yes! When you are of 2 ^ x from number you get 7 - x stars
  25. 'Next
  26.  
  27. Function xCopy$ (x, strng$) ' make x copies of string
  28.     Dim i As Long, b$
  29.     For i = 1 To x
  30.         b$ = b$ + strng$
  31.     Next
  32.     xCopy$ = b$
  33.  
  34.