Esempio n. 1
0
def find_words(board, row, col, prefix, results):
    """Find all words starting with prefix that
    can be completed from row,col of board.
    Args:
        row:  row of position to continue from (need not be on board)
        col:  col of position to continue from (need not be on board)
        prefix: looking for words that start with this prefix
        results: list of words found so far
    Returns: nothing
        (side effect is filling results list)
    Effects:
        inserts found words (not necessarily unique) into results
    """
    if not board.available(row,col):
        return
    char = board.get_char(row,col)
    prefix += char
    if game_dict.search(prefix) == 1:
        board.mark_taken(row,col)
        results.append(prefix)
    elif game_dict.search(prefix) == 2:
        board.mark_taken(row,col)
    else:
        prefix = prefix[:len(prefix)-1]
        return
    find_words(board,row+1,col,prefix,results) #Down
    find_words(board,row-1,col,prefix,results) #Up
    find_words(board,row,col+1,prefix,results) #Right
    find_words(board,row,col-1,prefix,results) #Left
    find_words(board,row-1,col-1,prefix,results) #Up and Right
    find_words(board,row-1,col+1,prefix,results) #Up and Left
    find_words(board,row+1,col-1,prefix,results) #Down and Right
    find_words(board,row+1,col+1,prefix,results) #Down and Left
    board.unmark_taken(row,col)
    return
Esempio n. 2
0
def find_words(board, row, col, prefix):
    """Find all words starting with prefix that
    can be completed from row,col of board.
    Args:
        row:  row of position to continue from (need not be on board)
        col:  col of position to continue from (need not be on board)
        prefix: looking for words that start with this prefix
        results: list of words found so far
    Returns: nothing
        (side effect is filling results list)
    Effects:
        inserts found words (not necessarily unique) into results
    """
    board.mark_taken(row,col)
    for x in range(row - 1, row + 2): #this and following line set search range to each tile around current tile, discounts tiles out of play 
        for y in range(col - 1, col + 2):
            if board.available(x,y):
                prefix += board.get_char(x,y)
                if game_dict.search(prefix) == 1:
                    board.results.append(prefix)
                    find_words(board,x,y,prefix)
                elif game_dict.search(prefix) == 2:
                    find_words(board,x,y,prefix)
                if board.get_char(x,y) == "qu": #makes sure that if program runs aross "qu" it backs up 2 letters but only 1 tiles in search function
                    prefix = prefix[:-2]
                else:
                    prefix = prefix[:-1]
    board.unmark_taken(row,col)
    return
Esempio n. 3
0
def find_words(board, row, col, prefix, results):
    """
    Find all words starting with prefix that
    can be completed from row,col of board.
    
    Args:
        row:  row of position to continue from (need not be on board)
        col:  col of position to continue from (need not be on board)
        prefix: looking for words that start with this prefix
        results: list of words found so far
        
    Returns: nothing
        (side effect is filling results list)
        
    Effects:
        inserts found words (not necessarily unique) into results
    """
	# FIXME: one base case is that position row,col is not
	#    available (could be off the board, could be currently
	#    in use).  board.py can check that
	# FIXME:  For the remaining cases, where the tile at row,col 
	#    is available, we need to consider the new prefix that 
	#    includes the letter on this tile
	# FIXME:  Another base case is that no word can start with 
	#    the current prefix.  No use searching further on that path.
	# FIXME:  If the current position is a complete word, it is NOT 
	#    a base case, because it might also be part of a longer word. 
	#    We save the word we found into the global results list, and
	#    continue with the recursive case. 
	# FIXME: The recursive case is when the current prefix (including
	#    the tile at row,col) is a possible prefix of a word.  We 
	#    must mark it as currently in use, then search in all 8 directions
	#    around it, and finally mark it as no longer in use. See board.py
	#    for how to mark and unmark tiles, and how to get the text
	#    on the current tile
    if board.available(row,col) == True:
        current = board.get_char(row, col)
        new_prefix = prefix + current
        if (game_dict.search(new_prefix) == game_dict.WORD): # recursive case
            results.append(new_prefix)
        if (game_dict.search(new_prefix) == game_dict.PREFIX) or (game_dict.search(new_prefix) == game_dict.WORD):
            board.mark_taken(row, col)
            find_words(board, row+1, col, new_prefix, results)
            find_words(board, row-1, col, new_prefix, results)
            find_words(board, row, col+1, new_prefix, results)
            find_words(board, row, col-1, new_prefix, results)
            find_words(board, row+1, col-1, new_prefix, results)
            find_words(board, row+1, col+1, new_prefix, results)
            find_words(board, row-1, col+1, new_prefix, results)
            find_words(board, row-1, col-1, new_prefix, results)
            board.unmark_taken(row, col)    
Esempio n. 4
0
def find_words(board, row, col, prefix, results):
    """Find all words starting with prefix that
    can be completed from row,col of board.
    Args:
        row:  row of position to continue from (need not be on board)
        col:  col of position to continue from (need not be on board)
        prefix: looking for words that start with this prefix
        results: list of words found so far
    Returns: nothing
        (side effect is filling results list)
    Effects:
        inserts found words (not necessarily unique) into results
    """
    if board.available(row, col):   # Continue if square is available
        char = board.get_char(row, col)
        prefix += char
        search_result = game_dict.search(prefix)
        if search_result != game_dict.NO_MATCH:   # Stop if prefix returns no_match
            board.mark_taken(row, col)
            if search_result == game_dict.WORD:   # Add word to dictioary
                results.append(prefix)
            find_words(board, row+1, col, prefix, results)
            find_words(board, row-1, col, prefix, results)
            find_words(board, row, col+1, prefix, results)
            find_words(board, row, col-1, prefix, results)
            find_words(board, row+1, col+1, prefix, results)
            find_words(board, row+1, col-1, prefix, results)
            find_words(board, row-1, col+1, prefix, results)
            find_words(board, row-1, col-1, prefix, results)
            board.unmark_taken(row, col)
    return
Esempio n. 5
0
def find_words(board, row, col, str1):
    """Find all words starting with string that can be completed from 
        row,col of board. 
    
    Parameters
    ---------
    Input:
    row: row of position to continue from (need not be on board)
    col: col of position to continue from (need not be on board)
    str1: looking for words that start with this string
    
    
    Output:
    results: set
    A set of all unique words found on the boggle board  
    """
    
    # A list of relative positions of any cell in a square grid.
    neighbors = [
        (-1, -1), (-1,  0), (-1,  1), (0,  1), (1,  1), 
        (1,  0), (1, -1), ( 0, -1)
    ]

    board.mark_taken(row, col) # marks the currently occupied square as taken.
    for x, y, in neighbors:
        x += row
        y += col
        if board.available(x, y): 
            str1 += board.get_char(x, y) # builds the string to be searched.
            if game_dict.search(str1, word_dict) == 1:
                results.add(str1) 
                find_words(board, x, y, str1) # a word can also be a prefix.
            elif game_dict.search(str1, word_dict) == 2:
                find_words(board, x, y, str1)
            # subtracting from the string when moving away from a tile.
            # "q" char in boggle is always represented as "qu"
            if board.get_char(x, y) == 'qu': 
                str1 = str1[:-2]
            else:
                str1 = str1[:-1]
    board.unmark_taken(row, col) # unmarks a square before leaving the square.
    return results  
Esempio n. 6
0
def find_words(board, row, col, prefix, results):
	"""Find all words starting with prefix that
	can be completed from row,col of board.
	Args:
		row:  row of position to continue from (need not be on board)
		col:  col of position to continue from (need not be on board)
		prefix: looking for words that start with this prefix
		results: list of words found so far
	Returns: nothing
		(side effect is filling results list)
	Effects:
		inserts found words (not necessarily unique) into results
	"""
	#print(prefix)
	board.mark_taken(row, col)	#mark the letter we are using
	if row < 0 or row >= len(board.content) or col < 0 or col >= len(board.content[0]):	#see if out of board
		return
	#print(game_dict.search(prefix))
	if game_dict.search(prefix)==0:	#NO_MATCH
		board.unmark_taken( row, col)	#return and unmark the letter
		return
	elif game_dict.search(prefix)==1:	#find the word successfully
		if prefix not in results:	#see if we have already found it
			results.append(prefix)	#put the word into results
	if board.available( row+1, col):	#check the letter under this letter
		find_words(board, row+1, col, prefix+board.get_char(row+1,col),results)
	if board.available( row, col+1):	#check the letter on the right of this letter
		find_words(board, row, col+1, prefix+board.get_char(row,col+1),results)
	if board.available( row-1, col):	#check the letter above this letter
		find_words(board, row-1, col, prefix+board.get_char(row-1,col),results)
	if board.available( row, col-1):	#check the letter on the left of this letter
		find_words(board, row, col-1, prefix+board.get_char(row,col-1),results)
	if board.available( row+1, col+1):	#check the letter on the low right of this letter
		find_words(board, row+1, col+1, prefix+board.get_char(row+1,col+1),results)
	if board.available( row+1, col-1):	#check the letter on the low left of this letter
		find_words(board, row+1, col-1, prefix+board.get_char(row+1,col-1),results)
	if board.available( row-1, col+1):	#check the letter on the up right of this letter
		find_words(board, row-1, col+1, prefix+board.get_char(row-1,col+1),results)
	if board.available(row-1, col-1):	#check the letter on the up left of this letter
		find_words(board, row-1, col-1, prefix+board.get_char(row-1,col-1),results)
	board.unmark_taken( row, col)	#if there's nothing can do,unmark and return
	return	
Esempio n. 7
0
def find_words(board, row, col, prefix):
    """Find all words starting with prefix that can be completed from row,
       col of board.

       From any given tile on the boggle board, this function will check
       all of its neighbors to be a valid path forward (via game_dict),
       if it is viable, the function calls itself with that as the new
       center.

    Args:
        row:  row of position to continue from (need not be on board)
        col:  col of position to continue from (need not be on board)
        prefix: looking for words that start with this prefix

    Returns: nothing
        (side effect is filling results list (a state of board object))

    Effects:
        inserts found words (not necessarily unique) into results
    """

    board.mark_taken(row, col)
    for x in range(row-1, row+2):  # One above and below current,
                                   # upper bound is non-inclusive.
        for y in range(col-1, col+2):  # One above and below current,
                                       # upper bound is non-inclusive.
            if board.available(x, y):
                prefix += board.get_char(x, y)
                if game_dict.search(prefix) == 1:  # 1 is the value of WORD
                    board.results.append(prefix)
                    find_words(board, x, y, prefix)
                elif game_dict.search(prefix) == 2:  # 2 is the value of PREFIX
                    find_words(board, x, y, prefix)
                if board.get_char(x, y) == 'qu':  # Special case as 'qu' is
                                                  # treated as a single
                    prefix = prefix[:-2]
                else:
                    prefix = prefix[:-1]   # Pull last charcter off of test case
    board.unmark_taken(row, col)
    return
Esempio n. 8
0
def find_words(board, row=0, col=0, prefix=""):
    """Find all words starting with prefix that
    can be completed from row,col of board.
    Args:
        row:  row of position to continue from (need not be on board)
        col:  col of position to continue from (need not be on board)
        prefix: looking for words that start with this prefix
        results: list of words found so far
    Returns: nothing
        (side effect is filling results list)
    Effects:
        inserts found words (not necessarily unique) into results
    """
    global results
    if not board.available(row,col):
        return
    ch = board.get_char(row,col)
    prefix = prefix + ch
    if game_dict.search(prefix) == 1:
        results.append(prefix)
        board.mark_taken(row, col)
    elif game_dict.search(prefix) == 2:
        board.mark_taken(row, col)
    else:
        prefix = prefix[:len(prefix) - 1]
        return
    #checking th squares around
    find_words(board, row-1, col, prefix)
    find_words(board, row-1, col+1, prefix)
    find_words(board, row, col+1, prefix)
    find_words(board, row+1, col+1, prefix)
    find_words(board, row+1, col, prefix)
    find_words(board, row+1, col-1, prefix)
    find_words(board, row, col-1, prefix)
    find_words(board, row-1, col-1, prefix)
    board.unmark_taken(row, col)
Esempio n. 9
0
def find_words(board, row, col, prefix, results):
    """Find all words starting with prefix that
    can be completed from row,col of board.

    Args:
    ------
        row:  int - row of position to continue from (need not be on board)
        col:  int - col of position to continue from (need not be on board)
        prefix: string - looking for words that start with this prefix
        results: list of strings containing words found on board

    Returns: nothing
    -------- (side effect is filling results list)

    Effects: Inserts found words (not necessarily unique) into results
    -------
    """

    if prefix is None:
        return "You must start with a prefix!"

    if board.available(row, col) is False:
        return "Tile not available!"

    else:
        board.mark_taken(row, col)

        for i in range(row - 1, row + 2):
            for j in range(col - 1, col + 2):
                if board.available(i, j) is True:
                    prefix += board.get_char(i, j)
                    is_word = game_dict.search(prefix)
                    if is_word == 1:  # 1  - prefix is a word
                        results.append(prefix)
                        find_words(board, i, j, prefix, results)
                    elif is_word == 2:  # 2 - prefix is a prefix but not also a 
                                        # word
                        find_words(board, i, j, prefix, results)  
                    if board.get_char(i, j) == 'qu':  # If tile is qu, back up
                                                      # 2 spaces
                        prefix = prefix[:-2]
                    else:
                        prefix = prefix[:-1]
        board.unmark_taken(row, col)
Esempio n. 10
0
def find_words(board, row, col, prefix, results):
    """Find all words starting with prefix that
    can be completed from row,col of board.
    Args:
        row:  row of position to continue from (need not be on board)
        col:  col of position to continue from (need not be on board)
        prefix: looking for words that start with this prefix
        results: list of words found so far
    Returns: nothing
        (side effect is filling results list)
    Effects:
        inserts found words (not necessarily unique) into results
    """

    """Extracting content
    for item in BoggleBoard.content:
        row = BoggleBoard.content[item]
        for column in row: 
            col = row[column]
    """
    
    visited, stack = set(), [prefix] # keeps track of what we visited.
    while stack:
        vertex = stack.pop()    
        results.append(board.get_char(row, col))
        board.mark_taken(row, col)    

        if prefix == None:
            return "No prefixes!" # Do something here

        if board.available(row, col) is False:
            return "Not available!"

        else:
            if game_dict.search(prefix) == 2:
                offset = [-1, 0, 1]
                for i in offset:
                    for j in offset:
                        if board.available(row + i, col + j) is True:
                            continue
                        else:
                            find_words(board, row + i, col + j, 
                                    board.get_char(row + i, col + j), results)