Post by B+ on Feb 1, 2019 4:46:43 GMT
Review the Bulls and Cows Game here: rosettacode.org/wiki/Bulls_and_cows
The code below has a Bulls and Cows Game commented out, if you want to try your hand at playing the game and to help appreciate the AI program.
Now for the AI player:
Sample Output:
The code below has a Bulls and Cows Game commented out, if you want to try your hand at playing the game and to help appreciate the AI program.
Now for the AI player:
'Bulls and Cows AI.txt for JB v2 B+ started 2019-01-31
global secret$
DIM possibles(9876) 'non repeating digits permutation: 1 or 0
'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 10 digits
'taken 4 at a time = 10*9*8*7 possibles = 5040.
'Loading such that if a nonrepeating 4 digit number then 1 else leave at 0
FOR i = 123 TO 9876
IF noRepeat(RIGHT$("000" + str$(i), 4)) THEN possibles(i) = 1: count = count + 1
'check noRepeat function
'PRINT i, possibles(i)
'INPUT "OK press enter "; wate$
NEXT
'check possibles count
PRINT "Possible permutations of 4 non repeating digits = "; count 'equals 5040?
call createSecretNumber
'test score$ with a Regular Game of Bulls and Cows
'PRINT secret$ 'or NOT
'DO
' INPUT "Please enter your 4 digit guess "; guess$
' guesses = guesses + 1
' sc$ = score$(secret$, guess$)
' PRINT "Scores: "; LEFT$(sc$, 1); " bull(s), "; RIGHT$(sc$, 1); " cow(s)"
' PRINT
'LOOP WHILE secret$ <> guess$
'PRINT "You found the number in "; guesses; " guesses."
'end
'OK now for the main event
PRINT "Here is the secret number so you can follow along: "; secret$
PRINT
DO
'INPUT "Please enter your 4 digit guess "; guess$
'insert AI player here
r = INT(RND(0) * 9876) + 1
WHILE possibles(r) = 0
r = r + 1
IF r > 9876 THEN r = 123
WEND
guess$ = RIGHT$("000" + STR$(r), 4)
'display the guess and it's score
PRINT "AI guesses "; guess$
guesses = guesses + 1
sc$ = score$(secret$, guess$)
PRINT "Scores: "; LEFT$(sc$, 1); " bull(s), "; RIGHT$(sc$, 1); " cow(s)"
IF sc$ <> "40" THEN
'now AI will use that score to eliminate all possibles that would not yeild the cattle count
changes = 0: remainder = 0
FOR i = 123 TO 9876
IF possibles(i) THEN
is$ = RIGHT$("000" + STR$(i), 4)
'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
end if
'INPUT "OK press enter "; wate$
END IF
NEXT
PRINT "AI reduced number of possible permutations by "; changes; ", so "; remainder; " possible(s) remain."
INPUT "OK press enter "; wate$
END IF
PRINT
LOOP WHILE secret$ <> guess$
PRINT "AI found the number in "; guesses; " guesses."
SUB createSecretNumber
'secret$ is global
digits$ = "0123456789": secret$ = ""
FOR i = 1 TO 4
p = INT(RND(0) * LEN(digits$)) + 1
secret$ = secret$ + MID$(digits$, p, 1)
digits$ = MID$(digits$, 1, p - 1) + MID$(digits$, p + 1)
NEXT
END SUB
FUNCTION noRepeat (d$)
IF LEN(d$) <> 4 THEN EXIT FUNCTION
FOR i = 1 TO 3
c$ = MID$(d$, i, 1)
FOR j = i + 1 TO 4
IF c$ = MID$(d$, j, 1) THEN EXIT FUNCTION
NEXT
NEXT
noRepeat = -1
END FUNCTION
FUNCTION score$ (std$, test$)
FOR i = 1 TO 4
c$ = MID$(test$, i, 1)
IF MID$(std$, i, 1) = c$ THEN
bulls = bulls + 1
ELSE
if INSTR(std$, c$) THEN cows = cows + 1
END IF
NEXT
score$ = STR$(bulls);STR$(cows)
END FUNCTION
Sample Output:
Possible permutations of 4 non repeating digits = 5040
Here is the secret number so you can follow along: 3975
AI guesses 1203
Scores: 0 bull(s), 1 cow(s)
AI reduced number of possible permutations by 3600, so 1440 possible(s) remain.
OK press enter
AI guesses 4516
Scores: 0 bull(s), 1 cow(s)
AI reduced number of possible permutations by 1062, so 378 possible(s) remain.
OK press enter
AI guesses 8924
Scores: 1 bull(s), 0 cow(s)
AI reduced number of possible permutations by 354, so 24 possible(s) remain.
OK press enter
AI guesses 5937
Scores: 1 bull(s), 3 cow(s)
AI reduced number of possible permutations by 23, so 1 possible(s) remain.
OK press enter
AI guesses 3975
Scores: 4 bull(s), 0 cow(s)
AI found the number in 5 guesses.
Here is the secret number so you can follow along: 3975
AI guesses 1203
Scores: 0 bull(s), 1 cow(s)
AI reduced number of possible permutations by 3600, so 1440 possible(s) remain.
OK press enter
AI guesses 4516
Scores: 0 bull(s), 1 cow(s)
AI reduced number of possible permutations by 1062, so 378 possible(s) remain.
OK press enter
AI guesses 8924
Scores: 1 bull(s), 0 cow(s)
AI reduced number of possible permutations by 354, so 24 possible(s) remain.
OK press enter
AI guesses 5937
Scores: 1 bull(s), 3 cow(s)
AI reduced number of possible permutations by 23, so 1 possible(s) remain.
OK press enter
AI guesses 3975
Scores: 4 bull(s), 0 cow(s)
AI found the number in 5 guesses.