liveBook · Manning

Secret code

As explained in Learn AI-Assisted Python Programming, Second Edition

A secret code is a hidden sequence of digits that a player must identify in the Bulls and Cows-style game.

A secret code is a hidden sequence of four distinct digits that one player must identify. The computer generates it randomly, while the human player submits guesses. section 10.3.1

Each guess produces two kinds of feedback:

  • Correct digits occupy the same position in the guess and the secret code. section 10.3.1
  • Misplaced digits occur in the secret code but appear in a different position in the guess. section 10.3.1
  • The program separates code generation, guess validation, and result calculation into subtasks, then makes code length and guess limits configurable. section 10.3.2 section 10.3.3

Example feedback

For secret code 1862 and guess 3821, the 8 is correct, while 2 and 1 are misplaced. The result is 1 correct digit and 2 misplaced digits. section 10.3.1

Table 10.1 Example of playing the game
Guess
Misplaced
Correct
Comments
0123
1
0
One of 0, 1, 2, 3 is in the answer; none are in the correct location.
4567
3
0
Three of 4, 5, 6, 7 are in the answer; none are in the correct location.
9045
0
1
Because one number from 0123 and three numbers from 4567 are in the answer, we know 8 and 9 aren’t in the answer. We know at least one of the numbers 4 or 5 must be in the answer from prior guesses and that 0 could be in the answer. One correct means that either 4 or 5 is in the correct location, either 4 or 5 isn’t present in the solution, and 0 isn’t in the solution.
9048
0
0
We know 8, 9, and 0 aren’t in the answer from prior guesses. Zero correct and zero misplaced tells us 4 is also not in the answer, and from the previous guess, we now know that 5 is the last digit.
1290
1
0
Going back to the original guess, we want to know which digit of 1, 2, and 3 is in the answer. We know 9 and 0 aren’t in the answer, so one misplaced means either 1 or 2 is in the answer and 3 isn’t in the answer. In addition, whichever of the numbers 1 and 2 are in the answer, it’s currently in the wrong spot.
6715
2
1
Because 4 isn’t in the solution, we know from the second guess that 5, 6, and 7 are. Our guess here tells us that 1 isn’t in the answer and that 6 and 7 are in the wrong place. Since 1 isn’t in the answer, 2 must be (from the previous guess). Because 5 is at the end and we’ve tried 2 in the second and third position previously with zero correct, 2 must be in the first position. Because we’ve tried 6 in the first and third position and neither were correct, 6 must be in the second position. That leaves 7 for the third position. We’ve got it.
2675
0
4
Yes, this is correct.

Use the example table to follow how successive guesses narrow the possible secret code. section 10.3.1

How Learn AI-Assisted Python Programming, Second Edition covers it

How deep it goes
Defines generation, validation, feedback, and representation, but stops short of detailing the complete implementation.
Examples
Uses the 1862 secret code, the 3821 guess, and a deduction table showing successive guesses.
What you can do after
Build it from scratch by decomposing gameplay into generation, input validation, and feedback functions.
How it connects
Connects Bulls and Cows with Wordle, top-down design, functions, parameters, and string representation.

This explainer was generated by Manning's knowledge graph from the text of Learn AI-Assisted Python Programming, Second Edition. The section links point to the parts of the book it draws on.