-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathboggle.rb
More file actions
52 lines (42 loc) · 1.33 KB
/
boggle.rb
File metadata and controls
52 lines (42 loc) · 1.33 KB
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
require 'byebug'
class Solver
def initialize
@path = []
@target= ["K", "A", "B"]
@directions = [-5, -4, -3, -1, 1, 3, 4, 5]
@board = [ "F", "Z", "K", "L",
"T", "A", "K", "N",
"N", "A", "Z", "A",
"B", "Q", "R", "K" ]
end
def solve!
@board.each_with_index do |letter, position|
find_word(position) if letter == @target.first
break if finished?
end
@path.length == 0 ? "no word" : @path
end
def find_word(position, target_letter_index=0)
# p @path
return false if @path.include?(position) #so the same position can't be in the path twice
@path << position #pushes position in to path
return true if finished? #stops if the path is complete
#if path isn't complete and position is valid...
@directions.each do |direction| #look at each of the adjacent cells
next if position_invalid?(position + direction) #make sure the position is on the board
if @board[position + direction] == @target[target_letter_index + 1] #look ahead to see if there is a match
return true if find_word(position + direction, target_letter_index + 1)
end
end
@path.pop
return false
end
def position_invalid?(position)
!(0..15).to_a.include?(position)
end
def finished?
@path.length == @target.length
end
end
boggle = Solver.new
p boggle.solve!