Author Topic: Hi Lo without Secret Number?  (Read 7677 times)

0 Members and 1 Guest are viewing this topic.

Offline bplus

  • Global Moderator
  • Forum Resident
  • Posts: 8053
  • b = b + ...
    • View Profile
Re: Hi Lo without Secret Number?
« Reply #30 on: October 15, 2020, 09:17:25 pm »
Before the river of time travels too far, here is my revised version of Hi Lo Game that has no secret number from start. The number gets established later by careful applications of RND s.t. the final secret numbers established cover fairly evenly the number range 1 to 100 like int(rnd*100) + 1 for secret number would have done.

Code: QB64: [Select]
  1. _TITLE "Hi Lo Game without Secret Number v2" 'B+ 2020-10-13
  2. ' trans from JB 2020-10-11 minimal changes
  3. DEFINT A-Z
  4. DIM hi, lo, guesses$, playerGuess, guessed, range, again$
  5. restart:
  6. hi = 101: lo = 0 '<<<<  make these higher and lower than what the secret number can be
  7. guesses$ = ""
  8. WHILE hi - lo > 1
  9.     INPUT "Guess Number 1 to 100, Enter your guess "; playerGuess
  10.     'if PlayerGuess = 0 then goto [restart]  ' GOOD! we split first guess of 50 both ways!
  11.     IF playerGuess < 1 OR playerGuess > 100 THEN
  12.         PRINT "Your guess is out of range of this game."
  13.         GOTO cont
  14.     END IF
  15.     IF INSTR(guesses$, RIGHT$("000" + STR$(playerGuess), 3)) THEN
  16.         PRINT "You've guessed that number already."
  17.         GOTO cont
  18.     END IF
  19.     guesses$ = guesses$ + "," + RIGHT$("000" + STR$(playerGuess), 3)
  20.     IF playerGuess > lo AND playerGuess < hi THEN
  21.         ' must be inside the Lo and Hi range ie not inclusive boundaries.
  22.         IF RND <= 1 / (hi - lo - 1) THEN
  23.             PRINT "You Guessed!"
  24.             guessed = 1
  25.             EXIT WHILE
  26.         ELSE
  27.             IF playerGuess = lo + 1 THEN
  28.                 PRINT "Your guess was low."
  29.                 lo = lo + 1
  30.             ELSE
  31.                 IF playerGuess = hi - 1 THEN
  32.                     PRINT "Your guess was high."
  33.                     hi = hi - 1
  34.                 ELSE
  35.                     'p < guess = (guess - lo -1) / range
  36.                     range = hi - lo - 1
  37.                     IF RND * range <= playerGuess - lo - .5 THEN
  38.                         PRINT lo, hi, "Your guess was too high."
  39.                         hi = playerGuess
  40.                     ELSE
  41.                         PRINT lo, hi, "Your guess was too low."
  42.                         lo = playerGuess
  43.                     END IF
  44.                 END IF
  45.             END IF
  46.         END IF
  47.     ELSE
  48.         IF playerGuess >= hi THEN PRINT "Your guess was too high." ELSE PRINT "Your guess was too low."
  49.     END IF
  50.     cont:
  51. IF guessed <> 1 THEN PRINT "Yikes! Programming error, did not find secret number."
  52. PRINT: INPUT "press Enter to go again, any other + Enter to quit "; again$
  53. IF LEN(again$) THEN END ELSE GOTO restart
  54.  
  55.  

And here are 2 frequency testers in one, just change what is commented out for PlayerGuess:
Code: QB64: [Select]
  1. _TITLE "Hi Lo Game wo secret Test Distribution" '  B+ 2020-10-10 works now
  2. DEFINT A-Z
  3. DIM secrets(100) 'check distribution of answers, also checking if we fail to arrive at answer
  4. DIM hi, lo, guesses$, alo, ahi, guessed, playerGuess, range, programErrors, trial, low, i, tot, saveLowI, high, saveHighI, w$
  5.  
  6. restart:
  7. hi = 101: lo = 0 '<<<<  make these higher and lower than what the secret number can be
  8. guesses$ = ""
  9. guessed = 0
  10. ahi = 101
  11. WHILE hi - lo > 1
  12.     'input "Guess Number 1 to 100, Enter your guess "; PlayerGuess
  13.  
  14.     'use AI to guess
  15.  
  16.     'make all the guesses way too high ie ride the AHI down 1 by 1
  17.     'ahi = ahi - 1
  18.     'playerGuess = ahi
  19.  
  20.     playerGuess = INT((hi - lo) / 2) + lo 'debug what the heck is going on with ALO AHI??
  21.     'PRINT "Player Guesses "; playerGuess
  22.  
  23.     'if PlayerGuess = 0 then goto [restart]  ' GOOD! we split first guess of 50 both ways!
  24.     IF playerGuess < 1 OR playerGuess > 100 THEN
  25.         PRINT "Your guess is out of range of this game."
  26.         GOTO cont
  27.     END IF
  28.     IF INSTR(guesses$, RIGHT$("000" + STR$(playerGuess), 3)) THEN
  29.         PRINT "You've guessed that number already."
  30.         GOTO cont
  31.     END IF
  32.     guesses$ = guesses$ + "," + RIGHT$("000" + STR$(playerGuess), 3)
  33.     IF playerGuess > lo AND playerGuess < hi THEN
  34.         ' must be inside the Lo and Hi range ie not inclusive boundaries.
  35.         IF RND <= 1 / (hi - lo - 1) THEN
  36.             PRINT "You Guessed!"
  37.             guessed = 1
  38.         ELSE
  39.             IF playerGuess = lo + 1 THEN
  40.                 'PRINT "Your guess was low."
  41.                 lo = lo + 1
  42.             ELSE
  43.                 IF playerGuess = hi - 1 THEN
  44.                     'PRINT "Your guess was high."
  45.                     hi = hi - 1
  46.                 ELSE
  47.                     'p < guess = (guess - lo -1) / range
  48.                     range = hi - lo - 1
  49.                     IF RND * range <= playerGuess - lo - .5 THEN
  50.                         ' PRINT lo, hi, "Your guess was too high."
  51.                         hi = playerGuess
  52.                     ELSE
  53.                         'PRINT lo, hi, "Your guess was too low."
  54.                         lo = playerGuess
  55.                     END IF
  56.                 END IF
  57.             END IF
  58.         END IF
  59.     ELSE 'going wrong way with guessing, not narrowing down with current guess
  60.         'IF playerGuess >= hi THEN PRINT "Your guess was too high." ELSE PRINT "Your guess was too low."
  61.     END IF
  62.  
  63.     cont:
  64.     'PRINT lo, hi, alo, ahi 'OK that blunder was pretty dumb!
  65.     'INPUT "Press enter to continue..."; w$
  66.     IF guessed THEN EXIT WHILE
  67. IF guessed <> 1 THEN
  68.     BEEP
  69.     PRINT "Yikes! Programming error, did not find secret number."
  70.     programErrors = programErrors + 1
  71.     secrets(playerGuess) = secrets(playerGuess) + 1
  72. trial = trial + 1
  73. IF trial < 10000 THEN GOTO restart
  74.  
  75. PRINT "Secret Numbers results:"
  76. PRINT "Program errors = "; programErrors
  77. low = 10000
  78. FOR i = 1 TO 100
  79.     PRINT i, secrets(i)
  80.     tot = tot + secrets(i)
  81.     IF secrets(i) < low THEN low = secrets(i): saveLowI = i
  82.     IF secrets(i) > high THEN high = secrets(i): saveHighI = i
  83.     IF i MOD 20 = 0 THEN
  84.         INPUT "Press enter to continue... "; w$
  85.         CLS
  86.     END IF
  87. PRINT "Total "; tot
  88. PRINT " Lowest count "; low; " for i = "; saveLowI
  89. PRINT "Highest count "; high; " for i = "; saveHighI
  90.  

And this is the frequency tester in which the V2 Game code (the AI deciding if guess too high or low or correct) was developed using a deck of numbers from 1 to 100 shuffled for guessing randomly without repetition.
Code: QB64: [Select]
  1. ' Hi Lo Game WO Secret Number v2 Test Random Guessing Distribution  B+ 2020-10-13
  2.  
  3. ' Check tsh73 report of wackiness for pure random guessing.
  4. ' So we aren't here all day waiting for results of 1000 tests
  5. ' let us store all the guess numbers into a deck.
  6. ' Shuffle the deck before each round and use 100 numbers WITHOUT REPEAT
  7. ' to get that secret number established! That should save us a ship load
  8. ' of time and still produce results of pure random guesses.
  9.  
  10. ' 2020-10-13 trans JB code to QB64
  11.  
  12. DEFINT A-Z
  13. DIM secrets(100) ' Check distribution of answers, also checking if we fail to arrive at answer.
  14. DIM rg(100) '      Get our random guesses in a row! Here is our Deck!
  15. FOR i = 1 TO 100 ' Create our deck of random numbers to test, rg stands for Random Guess
  16.     rg(i) = i
  17.  
  18. restart:
  19. ' reinitialize all that is necessary start by reshuffle rg()
  20. FOR i = 100 TO 2 STEP -1 'forget about 0 leave it alone
  21.     tmp = rg(i)
  22.     r = INT(RND * i) + 1
  23.     rg(i) = rg(r)
  24.     rg(r) = tmp
  25. rIdx = 1 'reset deck index
  26. Hi = 101: Lo = 0 '  reset, make these higher and lower than what the secret number can be
  27. guesses$ = "" '     track what has been guessed
  28. Guessed = 0 '     flag the number has been established!
  29.  
  30. WHILE Hi - Lo > 1 ' Begin rounds of guesses for number
  31.  
  32.     PlayerGuess = rg(rIdx) ' 1 of 100 pure random guesses
  33.     rIdx = rIdx + 1
  34.     'PRINT "Player Guesses "; PlayerGuess
  35.  
  36.     'if PlayerGuess = 0 then goto restart  ' GOOD! we split first guess of 50 both ways!
  37.     IF PlayerGuess < 1 OR PlayerGuess > 100 THEN 'not going to happen
  38.         GOTO cont
  39.     END IF
  40.     IF INSTR(guesses$, RIGHT$("000" + STR$(PlayerGuess), 3)) THEN 'not going to happen
  41.         GOTO cont
  42.     END IF
  43.     guesses$ = guesses$ + "," + RIGHT$("000" + STR$(PlayerGuess), 3) 'wasting time but...
  44.     IF PlayerGuess > Lo AND PlayerGuess < Hi THEN
  45.         r = RND: p = 1 / (Hi - Lo - 1) ' must be inside the Lo and Hi range ie not inclusive boundaries.
  46.         IF r <= p THEN
  47.             'PRINT "You guessed it!"
  48.             Guessed = 1
  49.             EXIT WHILE
  50.         ELSE
  51.             IF PlayerGuess = Lo + 1 THEN
  52.                 'PRINT "Your guess was low."
  53.                 Lo = Lo + 1
  54.             ELSE
  55.                 IF PlayerGuess = Hi - 1 THEN
  56.                     'PRINT "Your guess was high."
  57.                     Hi = Hi - 1
  58.                 ELSE
  59.                     'p < guess = (guess - lo -1) / range
  60.                     range = Hi - Lo - 1
  61.                     IF RND * range <= PlayerGuess - Lo - .5 THEN
  62.                         'PRINT Lo, Hi, "Your guess was too high."
  63.                         Hi = PlayerGuess
  64.                     ELSE
  65.                         'PRINT Lo, Hi, "Your guess was too low."
  66.                         Lo = PlayerGuess
  67.                     END IF
  68.                 END IF
  69.             END IF
  70.         END IF
  71.     ELSE 'going wrong way with guessing, not narrowing down with current guess this will happen allot!!!
  72.         IF PlayerGuess >= Hi THEN
  73.             'Print "Your guess was too high."
  74.         ELSE
  75.             'print "Your guess was too low."
  76.         END IF
  77.     END IF
  78.     cont:
  79.     'INPUT "Press enter to continue..."; wait$
  80. IF Guessed <> 1 THEN
  81.     'PRINT "Yikes! Programming error, did not find secret number."
  82.     programErrors = programErrors + 1
  83.     secrets(PlayerGuess) = secrets(PlayerGuess) + 1
  84. trial = trial + 1
  85. IF trial < 10000 THEN GOTO restart
  86.  
  87. 'report results
  88. PRINT "Secret Numbers results:"
  89. PRINT "Program errors = "; programErrors
  90. low = 10000
  91. FOR i = 1 TO 100
  92.     PRINT i, secrets(i) 'that's right in JB you can scroll output screen to review all 100
  93.     tot = tot + secrets(i)
  94.     IF secrets(i) < low THEN low = secrets(i): saveLowI = i
  95.     IF secrets(i) > high THEN high = secrets(i): saveHighI = i
  96.     IF i MOD 20 = 0 THEN
  97.         INPUT " Press enter to cont..."; w$
  98.         CLS
  99.     END IF
  100. PRINT "Total "; tot
  101. PRINT " Lowest count "; low; " for i = "; saveLowI
  102. PRINT "Highest count "; high; " for i = "; saveHighI
  103.  
  104.  




« Last Edit: October 15, 2020, 09:23:41 pm by bplus »