I am supposed to make a Boggle word detector thru backtracking. I based my Python solution out of this: https://www.youtube.com/watch?v=T0z1VFXegQk&t=1737s. The program is supposed to print out words from the board that match the contents of this list:
word_list = ["O","OE","C","CW","CP"]
Now, the only valid answers are the result, and the row and column index of its last letter (where the index starts at 0). The output should look like this:
Result: O; row: 3; col: 0
Result: C; row: 2; col: 2
Result: CP; row: 1; col: 2
However, it outputs:
Result: C; row: 0; col: 0
Result: CW; row: 0; col: 1
Result: O; row: 0; col: 0
Result: OE; row: 1; col: 0
This is obviously wrong. Problems include:
For example "OE" cannot possibly be a valid result because the only letters adjacent to "O" is "G","S","N". Same case with "CW" because the only letters adjacent to "C" is two "N"s, two "S"s, "P","H","R","E".
The coordinates for "O" and "C" are wrong.
Here is the full code. I would appreciate a correction.
def find(board,visited,row,col,word,word_list):
#path_row and path_col contain the x and y movements of each of the 8 directions that can be taken
path_row = [-1,-1,0,0,1,1,-1,1]
path_col = [-1,1,-1,1,-1,1,0,0]
if word in word_list:
print(f"Result: {word}; row: {row}; col: {col}") #outputs word.
if len(word) > 5: #Limited to 5 characters because the program takes too long to execute otherwise.
return
for i in range(0,8):
row_new = row + path_row[i] #movement in the x direction
col_new = col + path_col[i] #movement in the y direction
if(valid(row_new,col_new,visited)): #check if it is a valid movement
visited[row_new][col_new] = True #leaves a mark for visited cells
find(board,visited,row_new,col_new,word+board[row_new][col_new],word_list) #recursion! This makes the program go on to the next cell if a the current cell is valid.
visited[row_new][col_new] = False #in case a word "fails", we backtrack.
def valid(row_new,col_new,visited): #checks if a movement is valid,=.
#Below are just all the conditions wherein a cell must satisfy to be a valid cell.
#The first set of conditions are to make sure the cell is actually in range, i.e. prevents wacky movements like going to cell (-1,-1), which does not exist and out of range.
#The second set, i.e. (visited[row_new][col_new] == False), makes sure that the cell has not been visited before.
if(((row_new>=0) and (col_new>=0) and (row_new<4) and (col_new<4)) and (visited[row_new][col_new] == False)):
return True
else:
return False
board = [
["T","W","Y","R"],
["E","N","P","H"],
["G","S","C","R"],
["O","N","S","E"]
]
visited = [
[False,False,False,False],
[False,False,False,False],
[False,False,False,False],
[False,False,False,False]
]
word = ""
word_list = ["O","OE","C","CW","N","NN","CP"] #Note "CP" is not included. The problem is that the program sets any of the starting letter as at (0,0).
word_list = ["O","OE","C","CW","CP"]
print("Start.")
for row in range(0,4):
for col in range(0,4):
visited[row][col] = True
find(board,visited,0,0,word+board[row][col],word_list)
visited[row][col] = False
print("End.")
The wrong outputs basically.