Complete the functions in the game of boggle. See the description of the game below. You need to complete the functions in the following code that has 'TODO' at the end. These functions are ________.
1. def find_word_in_board(self, word: str) → Optional[List[Tuple[int, int]]]:
2. def dictionary_driven_search(self) → Set[str]:
3. def board_driven_search(self) → Set[str]:
import copy
import random
from typing import List, Optional, Set, Tuple
from py_boggle.boggle_dictionary import BoggleDictionary
from py_boggle.boggle_game import BoggleGame
class MyGameManager(BoggleGame):
"""Your implementation of `BoggleGame`"""
def __init__(self):
"""Constructs an empty Boggle Game. A newly-constructed game is unplayable. The `new_game` method will be called to initialize a playable game. Do not call `new_game` here. This method is provided for you, but feel free to change it. """
self.board: List[List[str]] # current game board
self.size: int # board size
self.words: List[str] # player's current words
self.dictionary: BoggleDictionary # the dictionary to use
self.last_added_word: Optional[List[Tuple[int, int]]] # the position of the last added word, or None
def new_game(self, size: int, cubefile: str, dictionary: BoggleDictionary) → None:
"""This method is provided for you, but feel free to change it. """
with open(cubefile, 'r') as infile:
faces = [line.strip().split() for line in infile]
cubes = [tuple(face) for face in faces if len(face) == CUBE_SIDES]
if size < 2 or len(cubes) < size*size:
raise ValueError('ERROR: Invalid Dimensions (size, cubes)')
random.shuffle(cubes)
# Set all of the game parameters
self.board = [[cubes[r*size + c] for r in range(size)] for c in range(size)]
self.size = size
self.words = []
self.dictionary = dictionary
self.last_added_word = None
def get_board(self) → List[List[str]]:
"""This method is provided for you, but feel free to change it. """
return self.board
def find_word_in_board(self, word: str) → Optional[List[Tuple[int, int]]]:
"""Helper method called by add_word() Expected behavior: Returns an ordered list of coordinates of a word on the board in the same format as get_last_added_word() (see documentation in boggle_game.py). If `word` is not present on the board, return None. """
word = word.upper()
raise NotImplementedError("method find_word_in_board") # TODO: implement your code here
def add_word(self, word: str) → int:
"""This method is provided for you, but feel free to change it. """
word = word.upper()
if (len(word) > SHORT and word not in self.words and self.dictionary.contains(word)):
location = self.find_word_in_board(word)
if location is not None:
self.last_added_word = location
self.words.append(word)
return len(word) - SHORT
return 0
def get_last_added_word(self) → Optional[List[Tuple[int, int]]]:
"""This method is provided for you, but feel free to change it. """
return self.last_added_word
def set_game(self, board: List[List[str]]) → None:
"""This method is provided for you, but feel free to change it. """
self.board = [[cube for cube in row] for row in board]
def get_score(self) → int:
"""This method is provided for you, but feel free to change it. """
return sum([len(word) - SHORT for word in self.words])
def dictionary_driven_search(self) → Set[str]:
"""Find all words using a dictionary-driven search. The dictionary-driven search attempts to find every word in the dictionary on the board. Returns: A set containing all words found on the board. """
raise NotImplementedError("method dictionary_driven_search") # TODO: implement your code here
def board_driven_search(self) → Set[str]:
"""Find all words using a board-driven search. The board-driven search constructs a string using every path on the board and checks whether each string is a valid word in the dictionary. Returns: A set containing all words found on the board. """
raise NotImplementedError("method board_driven_search") # TODO: implement your code here