Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
4.1 kB
3
Indexable
Never
import string

WORD_LENGTH = 5

# Code for functions
def read_dictionary(file_name):
    new_dictionary_list = []
    with open(file_name, 'r') as file:
        for line in file:
            new_dictionary_list.append(line.strip().lower())
    return new_dictionary_list  # a list of strings with lowercase letters

def enter_a_word(word_type, num_letters):
    a_word = input(f"Enter the {num_letters}-letter {word_type} word: ").lower()
    return a_word  # a string with lowercase letters

def is_it_a_word(input_word, dictionary_list):
    return input_word in dictionary_list  # Boolean variable

def enter_and_check(word_type, dictionary_list):
    while True:
        in_word = enter_a_word(word_type, WORD_LENGTH)
        in_dict = is_it_a_word(in_word, dictionary_list)
        w_length = len(in_word)

        if w_length != WORD_LENGTH:
            if not in_dict:
                print(f"You entered a {w_length}-letter word that is not in the dictionary. Please try again!")
            else:
                print()
                print(f"You entered a {w_length}-letter word that is in the dictionary. Please try again!")
            continue

        if in_dict:
            return in_word  # a string - valid input word
        else:
            print()
            print(f"You entered a {w_length}-letter word that is not in the dictionary. Please try again!")

def compare_words(player, secret):
    global in_secret_word_correct_spot
    global in_secret_word_somewhere
    global not_in_secret_word

    final = ''
    in_correct_spot = 0

    for i in range(len(player)):
        if player[i] == secret[i]:
            final += player[i]
        elif player[i] in secret:
            final += f'({player[i]})'
        else:
            final += '_'

    for i in range(len(player)):
        if player[i] == secret[i]:
            in_correct_spot += 1
            in_secret_word_correct_spot.append(player[i])
        elif player[i] in secret:
            in_secret_word_somewhere.append(player[i])
        else:
            not_in_secret_word.append(player[i])
            for i in not_in_secret_word:
                list2 = not_in_secret_word[:]
                list2.remove(i)
                for k in list2:
                    if i == k:
                        not_in_secret_word.remove(i)         

    #Remove used letters and show remaining letters in alphabet
    for letter in remaining_alphabet[:]:
        if letter not in in_secret_word_correct_spot and letter in player_word:
            remaining_alphabet.remove(letter)

    return final, in_correct_spot

# Program code
print('Welcome to new and improved Wordle - CECS 174 edition!')
alphabet_string = string.ascii_lowercase
remaining_alphabet = list(alphabet_string)

dictionary_file = 'project4_dictionary.txt'
words_list = read_dictionary(dictionary_file)

in_secret_word_correct_spot = []
in_secret_word_somewhere = []
not_in_secret_word = []
attempts = 0

secret_word = enter_and_check('secret', words_list)

N = int(input("Input allowed number of attempts: "))

while attempts < N:
    print(f"Enter your attempt #{attempts + 1}")
    player_word = enter_and_check('player', words_list)

    result, correct_spot = compare_words(player_word, secret_word)

    print()
    print(f"letter in the right spot: {correct_spot}")
    print(f"You guessed letters of the secret_word: {result}")
    print(f"Previously attempted letters that are in the correct spot of secret_word:")
    print(in_secret_word_correct_spot)
    print(f"Previously attempted letters that are in some spot of secret_word:")
    print(in_secret_word_somewhere)
    print(f"Previously attempted letters that are not in the secret_word:")
    print(not_in_secret_word)
    print(f"Remaining letters of the alphabet that have not been tried:")
    print(remaining_alphabet)

    attempts += 1
    if correct_spot == WORD_LENGTH:
        print(f"Congrats you won using {attempts} attempt(s)")
        break
    elif attempts == N:
        print(f"You already used #{attempts} attempts. Better luck tomorrow!")
Leave a Comment