Question
this is where i'm stucked .. i need to populate the letters and score the user (in Python) code: import operator import sys from random
this is where i'm stucked .. i need to populate the letters and score the user (in Python)
code:
import operator import sys from random import choice
class Boggle(object): def __init__(self, file, size=4, points=None): self.size = size self.board = [[' '] * self.size for i in range(self.size)] self.adj = self.build_adj() #self.words, self.prefixes = self.load_dic(file)
points = points if points is not None else {3: 1, 4: 1, 5: 2, 6: 3, 7: 5, 8: 11} self.points = [0 for i in range(self.size**2)] for i in range(len(self.points)): if i in points: self.points[i] = points[i] else: if i > 0: self.points[i] = self.points[i-1]
def adjacent(self, pos): row, col = pos adj = [] for i in [-1, 0, 1]: for j in [-1, 0, 1]: new_row = row + i new_col = col + j if 0
def load_dict(self, file): """ load dictionary from file """ words = set() prefixes = set() with open(file, 'r') as f: next(f) for line in f: word = line.rstrip() if len(word) >= 3: words.add(word) for i in range(len(word)): prefixes.add(word[:i]) return words, prefixes
def build_adj(self): adj = dict() for row in range(0, self.size): for col in range(0, self.size): adj[(row, col)] = self.adjacent((row, col)) return adj
def draw(self): """ Prints the Boggle board """ print ("My Boogle Board!! ") for row in range(0,self.size): for col in range(0,self.size): print("[",self.board[row][col],"]"," ",end="", flush=True) print(" ") print ("Start typing your words! (press enter after each word and enter 'X' when done:)")
bog = Boggle("words.txt",4,None) bog.draw()
The game has the following phases: 1. The 16 dice with letters on their sides (described later) are "rolled" and randomly positioned on a 4x4 grid. The grid is then printed out and shown to the user. 2. The player(user) begins to type the words found on the grid. The player must search for words that can be constructed from the letters of sequentially adjacent cubes, where "adjacent" cubes are those horizontally, vertically, and diagonally neighboring. For a word to be scored, it must meet the following requirements: - The word must not have already been scored. - It must be at least three letters long. - It must be a word in the English language. We check this against the given dictionary. - It must be present in the 4x4 grid of dice. It may not use the same letter cube more than once per word. These conditions should be checked in this order. For example, the word "OG" should be rejected for being too short, not for being absent from the dictionary. The user entries should not be case-sensitive. The player must be prompted to end this phase by entering the letter X 3. When the player elects to quit (by entering X), the player will be presented with their final score. The player may only receive the points for the word if it meets the qualifications above The program should print out how many points the player received for each word and if the word was not included, the program must print the reason. Use the exact wordings as in the examples below. Do not print out extra sentences or lines. Finally, the program should print the total score. Note that we do not enforce a timer as is done in traditional Boggle. Words are awarded points according to the table below (as long as they meet the conditions listed above): The game has the following phases: 1. The 16 dice with letters on their sides (described later) are "rolled" and randomly positioned on a 4x4 grid. The grid is then printed out and shown to the user. 2. The player(user) begins to type the words found on the grid. The player must search for words that can be constructed from the letters of sequentially adjacent cubes, where "adjacent" cubes are those horizontally, vertically, and diagonally neighboring. For a word to be scored, it must meet the following requirements: - The word must not have already been scored. - It must be at least three letters long. - It must be a word in the English language. We check this against the given dictionary. - It must be present in the 4x4 grid of dice. It may not use the same letter cube more than once per word. These conditions should be checked in this order. For example, the word "OG" should be rejected for being too short, not for being absent from the dictionary. The user entries should not be case-sensitive. The player must be prompted to end this phase by entering the letter X 3. When the player elects to quit (by entering X), the player will be presented with their final score. The player may only receive the points for the word if it meets the qualifications above The program should print out how many points the player received for each word and if the word was not included, the program must print the reason. Use the exact wordings as in the examples below. Do not print out extra sentences or lines. Finally, the program should print the total score. Note that we do not enforce a timer as is done in traditional Boggle. Words are awarded points according to the table below (as long as they meet the conditions listed above)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