Then we use a function called check_guess() to check if the user has guessed the word correctly or not. If the user has guessed a word, we put the word in the already_guessed list. In either of the cases, we print an error and prompt the user to guess again. We also continuously keep checking whether the word guessed by the user contains five letters or not as well as if the user has already guessed that word. The all_words_guessed list will contain the words with their colors.Īfter that, we run a while loop until end_of_game and prompt the user to guess the word. The full_wordle_pattern list will contain the Wordle pattern (made of colored squares). The while loop will run until end_of_game becomes True.Īlready_guessed is a list that will contain words that the user has already guessed. We have a boolean variable end_of_game that basically controls the while loop. The game() function accepts two arguments – console and chosen_word (correct answer). If len(already_guessed) = ALLOWED_GUESSES and guess != chosen_word:Ĭonsole.print(f"\nWORDLE X/\n")Ĭonsole.print(*full_wordle_pattern, sep="\n") If guess = chosen_word or len(already_guessed) = ALLOWED_GUESSES: Guessed, pattern = check_guess(guess, chosen_word)Ĭonsole.print(*all_words_guessed, sep="\n") While len(guess) != 5 or guess in already_guessed:Ĭonsole.print("You've already guessed this word!!\n")Ĭonsole.print('Please enter a 5-letter word!!\n') Guess = Prompt.ask(GUESS_STATEMENT).upper() Return ''.join(guessed), ''.join(wordle_pattern) We will run a while loop until all the guesses have been used. In the last line, we just print the Welcome message and Player Instructions using console.print. We then choose a random word from the word_list defined in words.py. In the main function, we first create an instance of the Console class from nsole. We have also added a welcome message and player instructions, too, that will be used in the main function. In the above code, we have defined three functions to print colored text using Rich. PLAYER_INSTRUCTIONS = "You may start guessing\n" WELCOME_MESSAGE = f'\n WELCOME TO WORDLE \n' It's quite easy to print colored text using it with this code: from nsole import Console Yellow = correct letter, but in the incorrect positionįor this, we'll use Rich.Green = correct letter in the correct position.We'll use similar settings to the Wordle defaults: We'll define a few utility functions to help us print colored text on the console. So, first, let's define the list of words called word_list in a separate words.py file: word_list = To do this, we will select a random word from a list of five-letter words. Let's see what our final game will look like: Let's Code Our Wordle GameĪs with the real Wordle game, a random five-letter word is chosen daily. Make sure you have installed Rich before you begin following along here. If you're new to Rich, check out this tutorial to get started with it. In this tutorial, we'll build our own terminal version of the popular game Wordle using Python and Rich, a library for rich-text formatting. It is similar to the Hangman game which I've already shown you how to build using Python. After each guess, the color of the tiles will change to show how close your guess was to the word. Wordle is a popular game where you guess a five-letter word in six tries.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |