Python开发Cows and Bulls游戏:AI猜数逻辑优化需求咨询
Hey there! Great job getting the player interaction and basic AI logic working for your Cows and Bulls game—slow guess times are a common pain point here, so let’s break down some concrete, actionable optimizations to speed up the AI’s guessing process:
1. Maintain a Dynamic Candidate Pool with Strict Pruning
Instead of only handling edge cases (no cows/bulls, all bulls), treat every guess result as a filter for your pool of possible target numbers:
- Start by generating the full set of valid 4-digit numbers (assuming no repeated digits, that’s 10×9×8×7 = 5040 possibilities).
- After each guess and resulting (cow, bull) pair, eliminate every candidate in the pool that could not produce the same (cow, bull) result when compared to your guess.
For example: If your guess is1234and you get 2 cows + 1 bull, any valid candidate must share exactly 2 digits in the same position as1234, and a total of 3 unique digits with1234(2 cows + 1 bull).
This pruning step will drastically reduce the candidate pool size with every guess, far beyond the limited logic you’re using now.
2. Use the Minimax Strategy to Pick Optimal Guesses
Don’t just pick a random candidate from the pool—choose the guess that minimizes the worst-case number of remaining candidates:
- For each potential guess (even ones not in the current candidate pool, sometimes!), calculate how many candidates would remain for every possible (cow, bull) outcome.
- Select the guess where the largest remaining candidate count across all outcomes is as small as possible.
This ensures that no matter what result you get, you’re left with the smallest possible pool to work with next.
3. Maximize Information Entropy for Faster Convergence
Another powerful approach is to select guesses that maximize the information gained from each result:
- For each potential guess, calculate the entropy of its possible outcomes using the formula:
Whereentropy = -sum(probability(outcome) * log2(probability(outcome)))probability(outcome)is the percentage of candidates that would produce that (cow, bull) pair with the guess. - Pick the guess with the highest entropy—this guess splits the candidate pool into the most balanced possible groups, letting you eliminate the maximum number of candidates on average with each guess.
4. Cache Precomputed Cow/Bull Counts
Calculating cow and bull counts for pairs of numbers can add up, especially with large candidate pools. Precompute these values once and store them in a dictionary:
- Create a cache where the key is a tuple of two 4-digit numbers (e.g.,
(1234, 5678)), and the value is their (cow, bull) pair. - When filtering candidates, look up the precomputed value instead of recalculating it every time. This saves significant CPU time as the pool gets smaller (and you’re doing more comparisons).
5. Handle Edge Cases Efficiently
Build on your existing logic with more targeted optimizations for specific results:
- If you get 0 cows and N bulls, generate all unique permutations of your guess’s digits (excluding the original guess if it’s already been tried) and add them directly to a prioritized candidate list—no need to check the full pool.
- If a guess returns 4 cows, you’re done! No extra processing needed.
By combining these strategies, your AI will go from taking dozens of guesses to consistently solving the game in 5-7 guesses max, with minimal computational overhead.
内容的提问来源于stack exchange,提问作者Nishie

