Task

Write a player of the Bulls and Cows game, rather than a scorer. The player should give intermediate answers that respect the scores to previous attempts.

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.



Related tasks

   Bulls and cows
   Guess the number
   Guess the number/With Feedback (Player)


      secret$ = ""
      REPEAT
        c$ = CHR$(&30 + RND(9))
        IF INSTR(secret$, c$) = 0 secret$ += c$
      UNTIL LEN(secret$) = 4
 
      FOR i% = 1234 TO 9876
        possible$ += STR$(i%)
      NEXT
 
      PRINT "Guess a four-digit number with no digit used twice."
      guesses% = 0
      REPEAT
 
        IF LEN(possible$) = 4 THEN
          guess$ = possible$
        ELSE
          guess$ = MID$(possible$, 4*RND(LEN(possible$) / 4) - 3, 4)
        ENDIF
 
        PRINT '"Computer guesses " guess$
        guesses% += 1
 
        IF guess$ = secret$ PRINT "Correctly guessed after "; guesses% " guesses!" : END
 
        PROCcount(secret$, guess$, bulls%, cows%)
        PRINT "giving " ;bulls% " bull(s) and " ;cows% " cow(s)."
 
        i% = 1
        REPEAT
          temp$ = MID$(possible$, i%, 4)
          PROCcount(temp$, guess$, testbulls%, testcows%)
          IF bulls%=testbulls% IF cows%=testcows% THEN
            i% += 4
          ELSE
            possible$ = LEFT$(possible$, i%-1) + MID$(possible$, i%+4)
          ENDIF
        UNTIL i% > LEN(possible$)
 
        IF INSTR(possible$, secret$) = 0 STOP
 
      UNTIL FALSE
      END
 
      DEF PROCcount(secret$, guess$, RETURN bulls%, RETURN cows%)
      LOCAL i%, c$
      bulls% = 0
      cows% = 0
      FOR i% = 1 TO 4
        c$ = MID$(secret$, i%, 1)
        IF MID$(guess$, i%, 1) = c$ THEN
          bulls% += 1
        ELSE IF INSTR(guess$, c$) THEN
            cows% += 1
          ENDIF
        ENDIF
      NEXT i%
      ENDPROC
 