Bulls
Count the bulls — digits that are right and in the right place — by walking the two arrays together and comparing position by position.
With the code and the guess sitting in two arrays, the first clue is the straightforward one: a bull — a digit that is correct and in the right position. Counting them is the simplest kind of array comparison: walk both arrays in step and tally where they agree.
10 BORDER 0: PAPER 0: INK 7: CLS
130 RANDOMIZE
140 DIM c(4)
150 FOR i = 1 TO 4: LET c(i) = INT (RND * 6) + 1: NEXT i
160 CLS
170 PRINT "The code is: ";
180 FOR i = 1 TO 4: PRINT c(i);: NEXT i
190 PRINT
220 INPUT "Your guess (4 digits): "; g$
230 IF LEN g$ <> 4 THEN GO TO 220
240 IF g$(1) < "1" OR g$(1) > "6" OR g$(2) < "1" OR g$(2) > "6" OR g$(3) < "1" OR g$(3) > "6" OR g$(4) < "1" OR g$(4) > "6" THEN GO TO 220
250 DIM g(4)
260 FOR i = 1 TO 4: LET g(i) = VAL g$(i): NEXT i
270 LET bulls = 0
280 FOR i = 1 TO 4
290 IF g(i) = c(i) THEN LET bulls = bulls + 1
300 NEXT i
310 PRINT "Bulls: "; bulls
Comparing two arrays in step
Lines 270–300 are the whole idea. Start bulls at 0, then FOR i = 1 TO 4 walks both arrays
at once: IF g(i) = c(i) THEN LET bulls = bulls + 1. Position 1 of the guess against position 1
of the code, position 2 against position 2, and so on — a tally of the places where guess and
code agree exactly. Two arrays, one index, compared slot for slot: this is the workhorse of
every game that matches one sequence against another, from this code-breaker to a spelling
checker.
A bull is unambiguous — same digit, same place — which is why it comes first. The hard clue is the cow: a digit that is in the code but somewhere else. That needs more than a position check, because you must not count a digit you have already credited as a bull, and you must not count it more times than it appears. That is the next unit.
Next: cows — the clue that takes real thought.