Question
HERE IS THE ASSIGNMENT HERE IS MY CODE: I AM GETTING ERROS AND I AM NOT SURE WHY. I NEED CODE IN PYTHON import random
HERE IS THE ASSIGNMENT
HERE IS MY CODE:
I AM GETTING ERROS AND I AM NOT SURE WHY. I NEED CODE IN PYTHON
import random
def main(): def list_from_file(filePath): list_of_words = [] infile1 = open(filePath, "r") for words in infile1: words = words.strip() list_of_words.append(words) infile1.close() return list_of_words
def list_from_file2(filepath): tiles = {} infile2 = open(filepath, "r") for line in infile2: line = line.strip() x, y = line.split(",") tiles[x] = y infile2.close() all_letters = [] for k in tiles.keys(): temp_WD = k * int(tiles[k]) for letter in temp_WD: all_letters.append(letter.lower()) return all_letters
def generate_hand(list): user_hand = [] random.shuffle(list) count = 0
while count != 8: user_hand.append(list[count]) count += 1 for letters in user_hand: list.remove(letters) return user_hand
def words_from_tiles(a_list, some_tiles):
words_from_tiles = [] for words in a_list: # print('', end=' ') x = some_tiles.copy() count = 0 for i in range(0, len(words)):
if words[i] in x: letter = words[i] # print(x) x.remove(letter) # print(count) count += 1
else: pass
if count == len(words): # print(words, end=" ") words_from_tiles.append(words) else: # print(count) pass return words_from_tiles
def turn(a_list, players_tiles): # print(f"Your hand {players_tiles}") listToScore = [] your_word = input("enter a word press q to end turn ")
while your_word != 'q': if your_word not in a_list: your_word = input( "Invalid word try again.. press q when your done ") else: listToScore.append(your_word) your_word = input("""Great word enter another one!!! """)
return listToScore
def sorting_tupe(a_tupe): return a_tupe[1]
def scoring(played_words): scores = {"a": 1, "c": 3, "b": 3, "e": 1, "d": 2, "g": 2, "f": 4, "i": 1, "h": 4, "k": 5, "j": 8, "m": 3, "l": 1, "o": 1, "n": 1, "q": 11, "p": 3, "s": 1, "r": 1, "u": 1, "t": 1, "w": 4, "v": 4, "y": 4, "x": 8, "z": 10} tupes_list = [] for words in played_words: score = 0 for letter in words: if letter in scores: score += scores[letter]
a_tupe = words, score tupes_list.append(a_tupe) tupes_list.sort(reverse=True, key=sorting_tupe) count_items = 0 print(" Word Scores and Words ") for items in tupes_list: count_items += 1 print(f'{count_items: >2}: {items[0]:
def gameStart(handOfTiles, num): print(f""" Player {num}'s hand: {''.join(handOfTiles).upper()}""") ################################################################################################################################### gameWords = list_from_file("TestData/CROSSWD.txt") gametiles = list_from_file2("TestData/GameTiles.txt")
round = 1 while round != 4:
string = f"ROUND {round}" print(f" {string: ^20} ")
winners = [] player_num = 1 player_scores = [] while player_num != 5: # print(len(gametiles)) player = generate_hand(gametiles) words_you_can_play = words_from_tiles(gameWords, player) # ENABLE THIS TO SEE WHAT WORDS U CAN PLAY DURING YOUR TURN ;) # print(words_you_can_play) gameStart(player, player_num) player_words = set(turn(words_you_can_play, player)) # print(player_words) scoring(player_words) score = len(player_words) player_scores.append((f'Player {player_num}', score)) # print(player_scores) player_num += 1
player_scores.sort(key=sorting_tupe) # print(player_scores[-1][1]) # print(player_scores[-2][1])
if player_scores[2][1] == player_scores[3][1] and player_scores[0][1] == player_scores[1][1]: rand_num = random.randint(1, 4) if rand_num == 1: winners.append(player_scores[0]) print( f"WINNER OF ROUND {round} IS {winners[0][0]} WITH THE SCORE OF {winners[0][1]}") elif rand_num == 2: winners.append(player_scores[1]) print( f"WINNER OF ROUND {round} IS {winners[0][0]} WITH THE SCORE OF {winners[0][1]}") elif rand_num == 3: winners.append(player_scores[2]) print( f"WINNER OF ROUND {round} IS {winners[0 ][0]} WITH THE SCORE OF {winners[0][1]}") else: if rand_num == 4: winners.append(player_scores[3]) print( f"WINNER OF ROUND {round} IS {winners[0][0]} WITH THE SCORE OF {winners[0][1]}") elif player_scores[2][1] == player_scores[3][1]: rand_num = random.randint(1, 2) if rand_num == 1: winners.append(player_scores[2]) print( f"WINNER OF ROUND {round} IS {winners[0][0]} WITH THE SCORE OF {winners[0 - 1][1]}")
else: winners.append(player_scores[3]) print( f"WINNER OF ROUND {round} IS {winners[0 - 1][0]} WITH THE SCORE OF {winners[0 - 1][1]}")
else: # print(player_scores) winners.append(player_scores[3]) print( f"WINNER OF ROUND {round} IS {winners[0][0]} WITH THE SCORE OF {winners[0][1]}")
round += 1 print(" NOT ENOUGH TILES FOR ANOTHER ROUND THANKS FOR PLAYING!!!") exit = input("press anything to quit")
main()
Homework \#4: Friendly Game with Friends Attached Files: CROSSWD.TXT (1.078 MB) G Gametiles.scort (157 B) players can construct an equal number of words, then the game ends in a tie. Write a program that simulates the game. tile in their handStep 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