-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboggle.rb
executable file
·129 lines (116 loc) · 2.63 KB
/
boggle.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
#!/usr/bin/env ruby
# Homework!
# Only print out 1 of each word that it finds
# Hand this program a board and output words
require 'pp'
class PrefixFinder
def initialize(word_list)
@my_hash = make_prefix_hash(word_list)
end
def search(prefix)
return @my_hash[prefix]
end
private
def make_prefix_hash( words )
result = {}
words.each do | word |
result[word + "."] = word
prefix = word
while prefix.length > 0 do
result[prefix] = word
prefix = prefix.chop
end
end
return result
end
end
class BoggleSolver
def initialize(dict)
@prefix = PrefixFinder.new(dict)
end
def solve(board)
word_list = []
word_dict = {}
ghost_board = [
[0,0,0,0],
[0,0,0,0],
[0,0,0,0],
[0,0,0,0]
]
for row in 0..3
for col in 0..3
word_list=word_list+recurse(board,row, col, "", ghost_board )
end
end
word_list.each { |word|
word_dict[word] = word
}
return word_dict.keys # This should a list of matched words
end
# This is going to be magic.. fucking magic - MB
#private
def recurse(board, row, col, word, visited)
# assume the board is 4x4
if row < 0 or row >= 4 or col < 0 or col >= 4
return []
end
if visited[row][col] == 1
return []
end
word = word+board[row][col]
word_list = []
if @prefix.search(word+".") and @prefix.search(word+".").length > 2
word_list.push(word)
end
if [email protected](word)
return []
end
visited[row][col] = 1
for diff_row in -1..1
for diff_col in -1..1
if diff_row == 0 and diff_col == 0
next
end
word_list = word_list+recurse(board,row+diff_row, col+diff_col, word, visited)
end
end
visited[row][col] = 0
return word_list
end
end
# Testing Section yo!
test_board = [
["x","x","x","x"],
["c","a","t","x"],
["t","d","o","x"],
["x","x","g","x"]
]
#pp test_board
test_dict = [ "cat", "dog", "fish" ]
test_solver = BoggleSolver.new(test_dict)
#puts test_solver.solve(test_board)
#puts test_solver.recurse(test_board, 1, 2, "ca")
all_words = []
File.open('ospd.txt').each do | line |
all_words.push(line.strip)
end
# Generate a random 4x4 board
def random_board
def random_letter
return ('a'.ord + rand(26)).chr
end
board = []
for row in 0..3
current_row = []
for col in 0..3
current_row.push(random_letter)
end
board.push(current_row)
end
return board
end
allword_solver = BoggleSolver.new(all_words)
#puts allword_solver.solve(test_board)
r_board = random_board
pp r_board
puts allword_solver.solve(r_board)