_TITLE "Bulls and Cows AI for digits 1 to 9" ' for QB64 B+ started 2019-01-31 '2019-02-01 fix for digits 1 to 9
'As suggested by Rosetta,
' "One method is to generate a list of all possible numbers that could be the answer,"
' "then to prune the list by keeping only those numbers that would give an equivalent"
' "score to how your last guess was scored. Your next guess can be any number from the"
' "pruned list. Either you guess correctly or run out of numbers to guess, which"
' "indicates a problem with the scoring."
'Creating a list of all possible permutations of the 9 digits
'taken 4 at a time = 9*8*7*6 possibles = 3024.
'Loading such that if a nonrepeating 4 digit number then -1 else leave at 0
'check possibles count
PRINT "Possible permutations of 4 non repeating digits ="; count
'equals 5040?
createSecretNumber
'test score$ with a regular game of Bulls and Cows
'PRINT secret$
'guesses = 0
'DO
' INPUT "Please enter your 4 digit guess "; guess$
' guesses = guesses + 1
' PRINT score$(secret$, guess$)
' PRINT
'LOOP WHILE secret$ <> guess$
'PRINT "You found the number in"; guesses; " guesses."
'OK now for the main event
PRINT "Here is the secret number so you can follow along: "; secret$
'INPUT "Please enter your 4 digit guess "; guess$
'insert AI player here
r = r + 1
'display the guess and it's score
PRINT "AI guesses "; guess$
guesses = guesses + 1
sc$ = score$(secret$, guess$)
'now AI will use that score to eliminate all possibles that would not yeild the cattle count
changes = 0: remainder = 0
' tsc$ = score$(guess$, is$)
'PRINT "i string$ "; is$; " score is "; tsc$
IF score$
(guess$
, is$
) <> sc$
THEN possibles
(i
) = 0: changes
= changes
+ 1 ELSE remainder
= remainder
+ 1 'INPUT "OK press enter "; wate$
PRINT "AI reduced number of possible permutations by";
RTRIM$(STR$(changes
));
", so"; remainder;
"possible(s) remain." INPUT "OK press enter "; wate$
PRINT "AI found the number in"; guesses;
"guesses."
digits$ = "123456789": secret$ = ""
secret$
= secret$
+ MID$(digits$
, p
, 1) digits$
= MID$(digits$
, 1, p
- 1) + MID$(digits$
, p
+ 1)
noRepeat = -1
bulls = bulls + 1
cows = cows + 1