5. Design and implement a program to implement the 'CECS 174-style new and improved Wordle' game without using any GUI. One player will enter a five-letter secret word and the other player will try to guess it in N attempts. 6. Both the secret word and the player words need to be 'valid words.'A'valid word' is a five-letter English word. To check if a word is an English word, you will implement a list that contains valid words. (There is No need for nitk anymore). You will check if the secret word is in your own 'custom-made' English dictionary). Additionally, you will break down the implementation such that it uses functions to perform each task. Follow the step-by-step instructions: a. Please use the code below to import the string module and define a constant for the word length (we just used 5 in our code in P3, but having a constant is way better) import string WORD_LENGTH = 5 b. Write a function named read_dictionary (file_name) that takes as an input a file name and returns a list of strings named new_dictionary_list. Valid English words are stored in the file project4_dictionary.txt which is available for download on BeachBoard. The file contains one word per line. Warning: Each line contains a string that ends with '\n'. In\ does not print and will need to be removed from each string. Feel free to modify the file by adding new valid words for your testing. There is no guarantee that all words in the file contain only lowercase letters, so write the code for the function to perform conversion to lowercase letters. SAMPLE RUN To test the function read_dictionary use code: words_list = read_dictionary ('project4_dictionary.txt') ' =
print (words_list) The expected output for the file provided on BB: ['skill', skull', 'movie', 'apple', 'world', 'audio', 'their', 'banana', 'five', 'word', 'sock', 'shoe', 'table', 'orange', 'handle', 'chalk', 'quick', 'attempt', 'three', 'drink', 'style', 'drink', 'sound', 'round', 'green', 'chair', 'quiet', 'shine', 'mirror', 'place', 'demon', 'fire', 'hydrant', 'exact', 'match', 'balloon', 'bicycle', 'cycle', 'hello', 'plant', 'stamp', 'crate', 'nothing', 'the', 'end' ] I c. Write a function named enter_a_word (word_type, num_letters) that takes as inputs word_type, and num_letters. word_type is a string that describes if the word is a 'secret' or 'player' word (generally it can be any string), and num_letters is an integer showing how many letters the word needs to have. The function needs to prompt the user to enter the word using 'Enter the xxx-letter www word' where xxx corresponds to num_letters and www gets the value of word_type. It needs to convert the user input to lowercase letters and return the user input in a string named a_word. Note: this function just prompts used to input an xxx- letter word, but does not check the length. SAMPLE RUN: To test the function enter_a_word use code: print (enter_a_word ('any', 3)) the expected output: Enter the secret 3-letter any word: THE the
d. Write a function named is_it_a_word (input_word, dictionary_list) that takes as inputs a string input_word and a list of stings dictionary_list that contains all valid English words (that is made in step b.). It returns a Boolean variable is_word that is true when the word is in dictionary_list and false otherwise. Refer to the code from Project 3 to write this function. SAMPLE RUN: To test the function is_it_a_word use code: print(is_it_a_word ('abcd', ['apple', 'banana'])) [ The expected output: False
Also use the code: print (is_it_a_word (apple', ['apple', 'banana'])) The expected output: True e. Write a function named enter_and_check (word_type, dictionary_list) that takes as inputs word_type, and dictionary_list. word_type is a string that describes if the word is a 'secret' or 'player' word (generally it can be any string), and dictionary_list is a list of strings that contains all valid English words (refer to step b. and the function that produces dictionary_list). Function enter_and_check will ask the user to enter a word and check if it is in the dictionary using the functions implemented in c and d, respectively. The word will be stored in a string in_word. The function should also check the word length. The function will request a word until a valid word was provide. After the word was checked for both lengths and if it belongs to the dictionary if one of those is False the function will print 'you entered a pop-letter word that is qgg in the dictionary. Please try again!'. Where ppp is the number of letters in the in_word, and qqq is either empty or contains the word 'not'. The function returns in_word that is in the dictionary and has a desired number of letters. SAMPLE RUN: To test the function enter_and_check use code: = words_list read_dictionary ('project4_dictionary.txt') print (enter_and_check('secret', words_list))
The expected output: Enter the secret 5-letter secret word: mochi You entered a 5-letter word that is not in the dictionary. Please try again! Enter the secret 5-letter secret word: mama You entered a 4-letter word that is not in the dictionary. Please try again! Enter the secret 5-letter secret word: fire You entered a 4-letter word that is in the dictionary. Please try again! Enter the secret 5-letter secret word: you entered a 0-letter word that is in the dictionary. Please try again! Enter the secret 5-letter secret word: cycle cycle Note, that the same function will be called for the 'player' word.
f. Write a function named compare_words (player, secret) that takes as inputs two strings player, secret that store player and secret word, respectively. The function also uses the following global variables. global remaining_alphabet global in_secret_word_correct_spot global in secret word somewhere global not_in_secret_word The function will perform a comparison of the player and secret word using string methods. It will return a string final that shows all letters of the player word that are in the secret word, and an integer in correct_spot indicating the number of letters in the correct spot. String final will have: - _'for each letter in the player word that is not in the secret word, - the letter itself if it is in the player and secret word in the correct spot, and - the letter in a () if the letter of a player word is in a secret word but not in the correct spot. For example, for a player word 'abcd' and a secret word 'axde', final would be a string containing a _(d)' and in correct spot will have a value of 1. Additionally, the function will update global variables remaining_alphabet, in_secret_word_correct_spot, in_secret_word_somewhere, not_in_secret_word. All of them are lists of strings (that is, lists of letters of the alphabet). remaining_alphabet keeps track of all letters that have not been attempted so far in the game. This list is created from the list of all 26 letters of English alphabet by removing any letter that has been attempted (appears in the player word during the game). Lists in_secret_word_correct_spot, in_secret_word_somewhere, not_in_secret_word keep track of all letters that have been previously attempted in the game that are in a correct spot, the letters that are in the word but not in the correct spot, and not in the secret word, respectively. Hint: remember, using the word global allows a function to see and update” a global variable. Make sure to add a letter to the corresponding list only if it has not already been added to the list.
g. Now is the time to tie it all together and use the functions to implement the code for "new and improved” Wordle. Use the following code and required variable names: alphabet_string = string.ascii_lowercase #create a string of all lowercase letters remaining_alphabet list (alphabet_string) #Create a list of all lowercase letters = in_secret_word_correct_spot [] # a list of all characters that have been previously attempted that are in the secret word in the correct spot = in_secret_word_somewhere = [] # a list of all characters that have been previously attempted that are in the secret word but not in the correct spot = not_in_secret_word [] # a list of all characters that have been previously attempted but not in the secret words_list = [] # a list of strings that contains all words in "our dictionary" read from the file secret word =' player_word # a sting containing the secret word a sting containing the player word 1 1 T = = N = 0 # the total allowed number of attempts (guesses) attempts = 0 # the number of attempts the user has made so far letter_in_the_right_spot 0 # the number of letters that are the same, and in the same position in the player_word and the secret word =
h. From now on, the rules are the same as for Project 3. The first player is promised to input a secret word. The word is checked until it is a valid word. The first player also inputs the value for N. Your program should allow the second player to try up to N times to guess the secret word. player_word also needs to be a valid word. Count as an attempt only when a player inputs a valid word. i. The printout is slightly different, to allow the reuse of the function enter_and_check for entering both secret and player words. Please refer to the SAMPLE RUNS below. j. During each attempt, the code should print letter_in_the_right_spot for the current attempt and contents of each list(remaining_alphabet, in_secret_word_correct_spot, in_secret_word_somewhere, not_in_secret_word) reflecting all the attempts made so far. Please refer to SAMPLE RUNS below. 7. The game is over if a player 2: a. Exhausts all N attempts, please print 'You already used 3 attempts. Better luck tomorrow!' b. Guesses the word in fewer than or equal to N attempts, please print 'Congrats you won using 2 attempt(s)', where 2 is the exact number of attempts it took player 2 to guess the secret word.
5. Design and implement a program to implement the 'CECS 174-style new and improved Wordle' game without using any GUI.
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
5. Design and implement a program to implement the 'CECS 174-style new and improved Wordle' game without using any GUI.
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!