-
Notifications
You must be signed in to change notification settings - Fork 0
/
boggle.rb
145 lines (108 loc) · 3.04 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# 0 1 j
# 0 m n
# 1 a b
# i
#m -> n
#| \
#a b
#m <- n
# / |
# a b
# m n
# | /
# a -> b
# m n
# \ |
# a <- b
#---
# 0 1 2 j
# 0 m n k
# 1 a b c
# 2 d e f
# i
class Boggle
attr_reader :board
def initialize(board)
@board = board
@dictionary = {"a" => true, "an" => true, "ban" => true, "man" => true, "bank" => true}
end
def find_all_words
result = []
(0..(rows-1)).each do |i|
(0..(columns-1)).each do |j|
result.concat(search("", [], 0, i, j))
end
end
#result.select! { |word| @dictionary.include?(word) }
result
end
def search(word, visited, depth, i, j)
return [] unless (i >= 0 && i < rows)
return [] unless (j >= 0 && j < columns)
return [] if visited.include?([i, j])
letter = @board[i][j]
visited = [*visited, [i, j]]
new_word = "#{word}#{letter}"
result = @dictionary[new_word] ? new_word : nil
depth=depth+1
#depth_indicator = depth.times.collect { '>' }.join('')
#puts "#{depth_indicator} Visiting: (#{i}, #{j}), #{letter}, #{new_word}"
#puts "#{depth_indicator} Visited: #{visited}"
# (i-1, j-1) (i-1, j) (i-1, j+1)
# (i, j-1) (i, j) (i, j+1)
# (i+1, j-1) (i+1, j) (i+1, j+1)
[result].concat([search(new_word, visited, depth, i-1, j-1),
search(new_word, visited, depth, i-1, j),
search(new_word, visited, depth, i-1, j+1),
search(new_word, visited, depth, i, j-1),
search(new_word, visited, depth, i, j+1),
search(new_word, visited, depth, i+1, j-1),
search(new_word, visited, depth, i+1, j),
search(new_word, visited, depth, i+1, j+1)
]).flatten.compact
end
def rows
@board.first.length
end
def columns
@board.length
end
end
def assert(expectation, message)
raise message unless expectation
end
def it_finds_a_one_letter_word
boggle = Boggle.new([["m", "n"], ["a", "b"]])
words = boggle.find_all_words
assert(words.include?("a"), "One letter word not found: a")
puts "Passed: it_finds_a_one_letter_word"
end
def it_finds_a_two_letter_word
boggle = Boggle.new([["m", "n"], ["a", "b"]])
words = boggle.find_all_words
assert(words.include?("an"), "Two letter word not found: an")
puts "Passed: it_finds_a_two_letter_word"
end
def it_finds_a_three_letter_word
boggle = Boggle.new([["m", "n"], ["a", "b"]])
words = boggle.find_all_words
assert(words.include?("man"), "Three letter word not found: man")
puts "Passed: it_finds_a_three_letter_word"
end
def it_finds_a_four_letter_word
boggle = Boggle.new([["m", "n", "k"], ["a", "b", "c"], ["d", "e", "f"]])
words = boggle.find_all_words
assert(words.include?("bank"), "Four letter word not found: bank")
puts "Passed: it_finds_a_four_letter_word"
end
def it_excludes_words_not_in_the_dictionary
boggle = Boggle.new([["m", "n", "k"], ["a", "b", "c"], ["d", "e", "f"]])
words = boggle.find_all_words
assert(!words.include?("mnad"), "Incorrect word found: mnad")
puts "Passed: it_excludes_words_not_in_the_dictionary"
end
it_finds_a_one_letter_word()
it_finds_a_two_letter_word()
it_finds_a_three_letter_word()
it_finds_a_four_letter_word()
it_excludes_words_not_in_the_dictionary()