Angel
Angel

Reputation: 132

Wordle code doesn't work properly? Python 3.10.7

I'm currently writing a small Wordle game, which analyzes a guess word against a randomly selected word from a dictionary; however, there's an issue with it.

Here's my code:

SQUARES = {
    'correct_place': '🟩',
    'correct_letter': '🟨',
    'incorrect_letter': '⬜'
}

def check_guess(guess: str, word: str):
    guessed = []
    wordle_pattern = []
    for i, letter in enumerate(guess):
        if word[i] == guess[i]:
            guessed.append(letter)
            wordle_pattern.append(SQUARES['correct_place'])
        elif (word.count(guess[i]) == 1 and guess[i] not in guessed and guess.count(guess[i]) == 1) or (word.count(guess[i]) > 1 and guessed.count(guess[i]) < word.count(guess[i])):
            guessed.append(letter)
            wordle_pattern.append(SQUARES['correct_letter'])
        else:
            guessed.append(letter)
            wordle_pattern.append(SQUARES['incorrect_letter'])
    return ''.join(wordle_pattern)

This code mostly works, but it has a problem, in which it breaks down whenever the guess word has a repeated letter; for example:

print(check_guess(guess='burbs', word='urban'))

Prints ⬜🟨🟨⬜⬜ instead of the intended 🟨🟨🟨⬜⬜.

I've tried adding many conditions to fix it; however, none of them seem to work.

Upvotes: -1

Views: 184

Answers (2)

imburningbabe
imburningbabe

Reputation: 792

This is the best I came up with (2.0):

SQUARES = {
    'correct_place': '🟩',
    'correct_letter': '🟨',
    'incorrect_letter': '⬜'
}

def check_guess(guess: str, word: str):
    wordle_pattern = []
    word_list = [*word]
    temp = ''
    for i, letter in enumerate(guess):
        if __is_correct_place(word[i], letter):
            wordle_pattern.append(SQUARES['correct_place'])
            word_list[i] = '/'
            temp += '_'
        else:
            wordle_pattern.append(SQUARES['incorrect_letter'])
            temp += letter
    for i, letter in enumerate(temp):
        if letter in word_list:
            wordle_pattern[i] = SQUARES['correct_letter']
            word_list.remove(letter)
    return ''.join(wordle_pattern)

def __is_correct_place(letter: str, guessed_letter: str):
    return letter == guessed_letter

Upvotes: 1

tabeqc
tabeqc

Reputation: 1

I cannot reproduce your mentioned output, as running your code returns your desired EGEGG on my machine.

However, as @Sembei Norimaki mentioned, YGEGG should be the correct output, where E is incorrect letter, G is correct place and Y is incorrect place.

It seems like your conditions to check for incorrect place are unnecessary complicated, as you only need to check whether the current letter of the guessed word appears anywhere in the word.

Here is a refactored version that delivers the desired output YGEGG for

print(check_guess(guess='raver', word='pager'))
SQUARES = {
    'correct_place': '🟩',
    'correct_letter': '🟨',
    'incorrect_letter': '⬜'
}

def check_guess(guess: str, word: str):
        wordle_pattern = []
        for i, letter in enumerate(guess):
            if __is_correct_place(word[i], letter):
                wordle_pattern.append(SQUARES['correct_place'])
                continue
            if __is_correct_letter(word, letter):
                wordle_pattern.append(SQUARES['correct_letter'])
                continue
            wordle_pattern.append(SQUARES['incorrect_letter'])
        return ''.join(wordle_pattern)

def __is_correct_place(letter: str, guessed_letter: str):
    return letter == guessed_letter

def __is_correct_letter(word: str, guessed_letter: str):
    return word.count(guessed_letter) >= 1

Hope this helps.

Upvotes: 0

Related Questions