Amazing-Python-Scripts
121 строка · 3.6 Кб
1from PyDictionary import PyDictionary2
3
4def main():5"""Main entry point of the Scrabble game."""6print('*' * 10 + "Welcome to the Scrabble game" + '*' * 10)7print("Let's start playing!!\n")8score_board = {}9for i in range(player_count()):10player = input(f"Player {i+1}: ")11score_board[player] = 012print('*' * 40)13print(winner(get_input(score_board)).center(40, " "))14print('*' * 40)15print("Thank you for your time. Have a Nice day!")16
17
18def valid(word):19"""20Checks if a word is valid by checking its meaning using PyDictionary.
21Args:
22word (str): The word to be validated.
23Returns:
24bool: True if the word is valid, False otherwise.
25"""
26dictionary = PyDictionary()27return bool(dictionary.meaning(word))28
29
30def compute_score(word):31"""32Computes the score for a given word based on a score list.
33Args:
34word (str): The word for which the score needs to be computed.
35Returns:
36int: The computed score for the word.
37Raises:
38ValueError: If the word is invalid or contains non-alphabetic characters.
39"""
40score_list = {41'a': 1, 'b': 3, 'c': 3, 'd': 2, 'e': 1, 'f': 4, 'g': 2,42'h': 4, 'i': 1, 'j': 8, 'k': 5, 'l': 1, 'm': 3, 'n': 1,43'o': 1, 'p': 3, 'q': 10, 'r': 1, 's': 1, 't': 1, 'u': 1,44'v': 4, 'w': 4, 'x': 8, 'y': 4, 'z': 10}45score = 046if word.isalpha():47if valid(word):48for char in word:49score += score_list[char.lower()]50return score51raise ValueError("Invalid word")52raise ValueError("Word should only contain alphabetic characters")53
54
55def player_count():56"""57Prompts the user to input the number of players for the game.
58Returns:
59int: The number of players.
60Raises:
61ValueError: If the user inputs a non-positive number.
62"""
63while True:64try:65count = int(input("How many players? "))66if count < 1:67raise ValueError("Please enter a positive number")68return count69except ValueError as e:70print(str(e))71
72
73def get_input(score_board):74"""75Retrieves the word input from each player and updates their scores.
76Args:
77score_board (dict): The dictionary storing the scores of each player.
78Returns:
79dict: The updated score board.
80"""
81while True:82for player in score_board:83while True:84try:85word = input(f"{player} | Type a word: ")86score_board[player] += compute_score(word)87except ValueError as e:88print(str(e))89else:90break91
92if input("If exit, type Y: ").lower() == "y":93print('*' * 40)94break95continue96return score_board97
98
99def winner(score_board):100"""101Args:
102score_board (dict): The dictionary storing the scores of each player.
103Returns:
104str: The winner(s) message.
105Raises:
106IndexError: If there are no players in the score board.
107"""
108sorted_scores = sorted(score_board.items(),109key=lambda x: x[1], reverse=True)110if len(sorted_scores) > 0:111max_score = sorted_scores[0][1]112winners = [player for player,113score in sorted_scores if score == max_score]114if len(winners) > 1:115return f"It's a tie. The winners are {', '.join(winners)}!!"116return f"The winner is {winners[0]}!"117return "No players found. Game over!"118
119
120if __name__ == "__main__":121main()122