如何实现猜词游戏中字符位置校验与数字结果输出功能?
Alright, let's tackle this word guess validation logic—this is exactly the feedback system you see in games like Wordle, so I'll break it down into clear, actionable steps with code to back it up.
First, let's lock in the rules we need to enforce:
2: Character is present and in the correct position1: Character exists in the target string, but in the wrong position0: Character does not appear in the target string
The biggest gotcha is handling duplicate characters (e.g., target is "AAB", guess is "ABA")—we can't overcount matches. Here's the step-by-step approach to avoid that:
Step 1: Mark Exact Matches (2s)
First, loop through each character position. For every spot where the guess character matches the target character exactly, mark it as 2 and flag those characters as "used" so we don't reuse them in later checks.
Step 2: Track Remaining Target Characters
After accounting for exact matches, count how many times each unused character appears in the target string. This helps us avoid matching the same target character multiple times for partial matches.
Step 3: Mark Partial Matches (1s) and Misses (0s)
For each position that wasn't marked as 2, check if the guess character exists in our remaining target character count. If it does, mark it as 1 and decrement the count for that character. If not, leave it as 0.
Let's turn this logic into a reusable function that handles duplicates correctly and returns the numeric sequence as a string:
def get_guess_feedback(target: str, guess: str) -> str: # Initialize feedback with all '0's feedback = ['0'] * len(target) target_chars = list(target) guess_chars = list(guess) # Step 1: Handle exact matches (mark as '2') for i in range(len(target)): if guess_chars[i] == target_chars[i]: feedback[i] = '2' # Mark these characters as used to avoid reprocessing target_chars[i] = None guess_chars[i] = None # Step 2: Count remaining unused characters in target remaining_counts = {} for char in target_chars: if char is not None: remaining_counts[char] = remaining_counts.get(char, 0) + 1 # Step 3: Handle partial matches (1) and misses (0) for i in range(len(target)): if guess_chars[i] is not None: char = guess_chars[i] if char in remaining_counts and remaining_counts[char] > 0: feedback[i] = '1' remaining_counts[char] -= 1 # Else, keep as '0' return ''.join(feedback)
Let's run through some common cases to make sure the logic works:
- Target:
"APPLE", Guess:"PAPER"→ Returns"11210" - Target:
"AAB", Guess:"ABA"→ Returns"211" - Target:
"TEST", Guess:"TEAM"→ Returns"2200" - Target:
"XYZ", Guess:"ABC"→ Returns"000"
- Length Check: Add a validation step first to ensure the guess and target are the same length (return an error or handle it based on your app's requirements).
- Language Flexibility: This logic translates directly to other languages—just adjust syntax (e.g., use objects for counts in JavaScript, or HashMaps in Java).
- Database Integration: When pulling random strings from your database, make sure to fetch one at a time and store it temporarily (e.g., in a user session) to keep the target consistent for each user's guess attempt.
内容的提问来源于stack exchange,提问作者geartzen9

