Skip to content

Bug: Infinite loop in Word-Building botTurn when all words for a letter are exhausted #1304

Description

@vipul674

Description

The botTurn function in games/Word-Building/word_building.py contains an infinite loop bug. When all words for a given starting letter have already been used by both players, the while True loop never exits because there are no unused words to select.

Root Cause

The bare except: pass on line 20 only catches the KeyError when a letter doesn't exist in the word list. However, when the letter IS in the word list but ALL words for that letter are in used_words, the loop runs forever because:

  1. random.randint picks a word from data.words[letter]
  2. The word IS in used_words, so the break is never reached
  3. The loop repeats indefinitely with no exit condition

Impact

The game becomes permanently stuck when the bot runs out of words for a given letter. The user must force-kill the process. This is a runtime crash risk that makes the game unplayable after enough rounds.

Reproduction

  1. Play Word Building until many words have been used
  2. When the bot gets a letter where all words are exhausted, the game hangs forever

Expected Behavior

The function should return None (or handle the case) when no unused words remain for the letter.

Actual Behavior

Infinite loop with no exit condition.

Proposed Fix

Add a maximum iteration count or check if all words are used:

def botTurn(letter, used_words):
    import data
    word = None
    try:
        available = [w for w in data.words.get(letter, []) if w not in used_words]
        if available:
            word = random.choice(available)
    except:
        pass
    return word

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions