Question
Below is the count_down.py A simple implementation of countdown import lzma import pickle import random import time import a2 FILE_PATH = ./dictionary.xz CHARS_NUM
Below is the count_down.py
""" A simple implementation of countdown """
import lzma import pickle import random import time import a2
FILE_PATH = "./dictionary.xz" CHARS_NUM = 9 MIN_VOWELS = 3 MAX_VOWELS = 5 VOWELS = "aeiou" CONSONANTS = "bcdfghjklmnpqrstvwxyz"
RED = "\033[91m" END = "\033[0m"
def play(): """ Implements the game's main loop. """
# Load dictionary with lzma.open(FILE_PATH, mode='rb') as fp: dictionary = pickle.load(fp)
print("****************** COUNTDOWN *******************")
end = False while not end: start_game(dictionary)
continue_playing = custom_input( "Would you like to continue playing (y)? ", check_func=a2.is_valid_response, convert_func=a2.convert_response_to_bool ) end = not continue_playing
def start_game(dictionary): """ dictionary: dict
Implements the game's logic, using the dictionary of words dictionary. """ print("****************** new game ******************")
# First, get Game settings rounds_num = custom_input( "How many rounds would you like to play? [pick a value between 1 and 10] ", check_func=is_valid_int_rounds, convert_func=int )
countdown_duration = custom_input( "How long should the countdown be? [0, 30, 60, 90] secs ", check_func=is_valid_duration, convert_func=int )
players_num = custom_input( "How many players? [1, 2] ", check_func=is_valid_int_players, convert_func=int )
players = [] players_cumulative_scores = [] for pl_idx in range(players_num): player_name = custom_input(f"Enter name for player {pl_idx + 1} ") players.append(player_name) players_cumulative_scores.append(0.0)
for r_idx in range(rounds_num): start_round(r_idx, dictionary, countdown_duration, players, players_cumulative_scores)
print_winner(players, players_cumulative_scores)
def start_round(idx, dictionary, countdown_duration, players, players_cumulative_scores): """ idx: int dictionary: dict countdown_duration: int players: list of str players_cumulative_scores: list of int
Implements the logic for the idx th round of the game. """ print(" ") print(f"================== ROUND {idx:0>2} ==================")
for pl_idx, player in enumerate(players):
print(f"It is now {player}'s turn to pick the letters ") vowels_num = custom_input( f"Enter the number of vowels [between {MIN_VOWELS} and {MAX_VOWELS} inclusive]: ", check_func=is_valid_int_vowels, convert_func=int ) consonants_num = CHARS_NUM - vowels_num
letters = [] letters.extend(random.choices(VOWELS, k=vowels_num)) letters.extend(random.choices(CONSONANTS, k=consonants_num)) random.shuffle(letters) letters = ''.join(letters)
print("The letters are: ") print("+---" * len(letters), "+", sep="") print("| ", " | ".join([letter.upper() for letter in letters]), " |", sep="") print("+---" * len(letters), "+", sep="")
if countdown_duration > 0: print(f"You have {countdown_duration} secs to make your guesses ")
for itr in range(countdown_duration): remaining_duration = countdown_duration - itr print( f" [{'#' * remaining_duration:{countdown_duration}}] {remaining_duration:0>2} secs remaining", end="" ) time.sleep(1) print(" ", end="")
print("Time to enter your guesses")
guesses = {} for turn_idx in range(len(players)):
curr_player_idx = (pl_idx + turn_idx) % len(players) curr_player = players[curr_player_idx]
print(f"It is now {curr_player}'s turn to guess")
curr_player_guess = None while curr_player_guess is None: curr_input = a2.clean_whitespace(input("Enter your guess now: ")).lower() if curr_input in guesses: print(f"Inavlid Guess! {guesses[curr_input]} has already entered {curr_input}. Try again.") else: curr_player_guess = curr_input
if curr_player_guess == "": print("Forfeiting turn. Points earned: 0") elif curr_player_guess not in dictionary: print("Word not in dictionary. Points earned: 0") elif not a2.is_subset(letters, curr_player_guess): print("Word is not a subset of the provided letters. Points earned: 0") else: points_earned = a2.calculate_score(letters, curr_player_guess) players_cumulative_scores[curr_player_idx] += points_earned print(f"Great! The guess {curr_player_guess} has earned you {points_earned} points")
print("Current Scores: ") for player, player_score in zip(players, players_cumulative_scores): print(f" {player}: {player_score}")
def print_winner(players, players_scores): """ players: list of str players_scores: list of int
Prints the winner of the game from the players list, according to the players_scores. """ if len(players) == 1: print(a2.get_single_player_message(players[0], players_scores[0])) else: print(a2.get_two_players_message(players[0], players_scores[0], players[1], players_scores[1]))
def is_valid_duration(text): """ text: str
Returns True iff text is a string representation of a valid duration. """ return text in ["0", "30", "60", "90"]
def is_valid_int_players(text): """ text: str
Returns True iff text is a string representation of a valid number of players. """ return a2.is_valid_int(text, 0, 3)
def is_valid_int_rounds(text): """ text: str
Returns True iff text is a string representation of a valid number of rounds. """ return a2.is_valid_int(text, 0, 11)
def is_valid_int_vowels(text): """ text: str
Returns True iff text is a string representation of a valid number of vowels. """ return a2.is_valid_int(text, MIN_VOWELS - 1, MAX_VOWELS + 1)
def custom_input(prompt, check_func=None, convert_func=None): """ prompt: str check_func: function convert_func: function
Continually prompts the user with prompt, until the user returns a valid input as specified by check_func. Finally the value is passed to convert_func, if provided. """ return_val = None while return_val is None: user_input = a2.clean_whitespace(input(prompt)) if check_func and not check_func(user_input): print(f"{RED}Invalid input {user_input}. Please, try again.{END}") else: return_val = user_input if convert_func: return_val = convert_func(return_val)
return return_val
if __name__ == "__main__": play()
Countdown word game Due: March 15, 2021 at 23:59 EST Late policy: 1 hour grace period, following by automatic deduction of 5% per hour Additional rules Do not add any other import statements Do not add code outside of your functions. Test your code using the interactive shell, the MarkUs tester or by placing your code under if __name_ _main__". Do not call print() or input) . You may create additional functions. Grading scheme Docstring examples - 20% Function correctness - 80% Markus Autotester Note that the MarkUs autotester for Assignment 2 only checks for your doestring examples and that your functions return the correct type. The function correctness tests are not provided for you to run before the deadline, so you are responsible for thoroughly testing your code. You are given unlimited tokens for running the checker for this assignment. Provided Files Download the starter code from MarkUs and extract the zip archive. The files are described below. a2.py: This is the only file you will hand in. There are 8 functions for you to complete in this file. count_down.py: The main program that runs the countdown game. This program will NOT run correctly until you complete a2.py. You do not need to understand all the code in this file, but you should read through it and understand what the program is doing. dictionary. xz: A file containing the dictionary entries used in this game. You shouldn't modify this file or try to extract it. Background In this assignment, we will implement a modified version of the countdown letters game, where the objective is to form valid words from a randomly generated list of letters. The game is composed of 1 to 10 rounds. During each round, players take turns to select the number of vowels (between 3 and 5 inclusive). Based on this criteria, 9 letters are randomly generated. If specified, a countdown of 0, 30, 60 or 90 secs take place, during which players try to make their guesses using the provided 9 letters. Once done, players share their guesses, starting with the player in control (the player that selected the number of vowels). Players are not allowed to repeat guesses within a single round. The words are scored based on their length. Players get additional points if they guess an anagram of the provided letters i.e., their guess uses all 9 presented letters. Furthermore, forming words that contain letters such as j, k, q, x and 2 wins them additional points. The player with the most points at the end of the game wins. Tasks All the tasks refer to functions in a2.py. For each of the 8 functions, complete the docstring (including at least 2 docstring examples and function body. Your docstring examples must run and pass all tests when you run doctest.testmod() for full docstring example marks. clean_whitespace (text) Input parameters text is a str. Expected Output Return a new version of text where all leading and trailing whitespace is removed. Do not modify any whitespace that is not leading or trailing. Whitespace includes spaces, tabs and new lines. is_valid_response (text) Input parameters text is a str, with no leading or trailing whitespace characters. Expected Output Return True if text, when disregarding case, is one of the following 4 values: "y", "yes", "no" or "n". Return False otherwise. convert_response_to_bool(text) Input parameters text is a str that, when disregarding case, is one of the following 4 values: "y", "yes", "no" or "n". Expected Output Return True if text (disregarding case) has the value "y"or "yes" and False otherwise. is_valid int(text, start_interval, end interval) Input parameters text is a str with no leading or trailing whitespace characters. Start_interval and end_interval are ints that define the start and end of an interval respectively. You may assume that: end interval - start interval >= 2 Expected Output Return True if both of the following conditions are satisfied, and False otherwise: 1. text is a valid representation of an int. "-1" and "1" are examples of valid representations of ints whereas "1.0" is not valid representation of an int. 2. the int value represented by text lies between start interval and end_interval (but is not equal to either start interval or end interval) calculate_score (puzzle, guess) Input parameters puzzle and guess are lower-cased strs with the leading and trailing whitespace removed. They represent the puzzle and the player's guess respectively. you may assume that puzzle and guess are composed of at least one character each. You can also assume that guess is a valid subset of puzzle i.e., is_subset(puzzle, guess) returns True. Expected Output Return an int which represents the points the player earns for making the guess guess for puzzle. The score is calculated as follows: 1. The player earns one point for each letter in guess 2. If puzzle and guess have the same length, the player earns an additional 10 points 3. if the player's guess include any of the following special characters", the player earns the corresponding number of points for each occurrence of the character: Special Character Points earned for each occurrence k 9 4 2 5 4 5 2 For instance, if the value of the puzzle is 'ckik' and the value of the guess is 'kick', the player will earn 4 points (one for each letter in guess) +10 points (since guess and puzzle have the same length +(2 x 2) = 4 (for including 2 occurences of the 'special character' k). In total, that guess will earn the player: 18 points. is_subset (puzzle, guess) Input parameters puzzle and guess are lower-cased strs with the leading and trailing whitespace removed. They represent the puzzle and the player's guess respectively. you may assume that puzzle and guess are composed of at least one character each. Expected Output Return True if the characters in guess are a subset of the characters in puzzle i.e., All characters in guess should occur in puzzle but not necessairly in the same order. Furthermore, a valid guess should NOT contain more instances of a letter then there are present in the puzzle. For instance, 'lab' is a valid subset of 'ahlbj' but 'labs' and 'ball' are not. get_single_player_message (player_name, player_score) Input parameters player_name is a str with the leading and trailing whitespace removed that represents the player's name and player_score is an int that represents their final score. Expected Output Return a str of the following format: "Final score of
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started