Class: WordSearcher::Solver

Inherits:
Object
  • Object
show all
Defined in:
lib/word_searcher/solver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeSolver

Returns a new instance of Solver.



6
7
8
9
10
11
12
13
# File 'lib/word_searcher/solver.rb', line 6

def initialize
  @word = []
  @puzzle = []
  @word_chars = []
  @found = []
  @first_letter = nil
  @coords = []
end

Instance Attribute Details

#coordsObject

Returns the value of attribute coords.



4
5
6
# File 'lib/word_searcher/solver.rb', line 4

def coords
  @coords
end

#puzzleObject

Returns the value of attribute puzzle.



4
5
6
# File 'lib/word_searcher/solver.rb', line 4

def puzzle
  @puzzle
end

#wordObject

Returns the value of attribute word.



4
5
6
# File 'lib/word_searcher/solver.rb', line 4

def word
  @word
end

#word_charsObject

Returns the value of attribute word_chars.



4
5
6
# File 'lib/word_searcher/solver.rb', line 4

def word_chars
  @word_chars
end

Instance Method Details

#check_scoreObject



31
32
33
34
35
36
# File 'lib/word_searcher/solver.rb', line 31

def check_score
  remaining = @word_chars - @found
  found = @found & @word_chars
  remaining.any? ? win = false : win = true
  score_hsh = {word: @word, found: found, remaining: remaining, win: win, puzzle: @puzzle}
end

#find_col_index(row, ri) ⇒ Object



44
45
46
47
48
49
50
51
52
# File 'lib/word_searcher/solver.rb', line 44

def find_col_index(row, ri)
  if row&.include?(@first_letter)
    ci = row.index(@first_letter)
    col = @puzzle.map { |row| row[ci] }
    coord_hsh = { row: row, col: col, ri: ri, ci: ci }
    @coords << coord_hsh
  end
  @coords
end

#find_coordinatesObject



38
39
40
41
42
# File 'lib/word_searcher/solver.rb', line 38

def find_coordinates
  @puzzle.each_with_index do |row, ri|
    find_col_index(row, ri)
  end
end

#generate_puzzleObject



86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/word_searcher/solver.rb', line 86

def generate_puzzle
  word = @word
  word = word.chars
  while word.length < 7
    alph = ('a'..'z').to_a.sample
    word.length.odd? ? pos = 0 : pos = -1
    word.insert(pos,alph)
  end

  puzzle = (0..7).map { ('a'..'z').to_a.shuffle[0,7] }
  n = rand(0..7)
  puzzle[n] = word
  @puzzle = puzzle
end

#generate_wordObject



102
103
104
105
106
# File 'lib/word_searcher/solver.rb', line 102

def generate_word
  # words = %w(texas austin dallas houston cowboy)
  # word = words.shuffle.sample
  word = 'texas'
end

#get_neighbors(coord) ⇒ Object



67
68
69
70
71
72
73
74
75
76
77
# File 'lib/word_searcher/solver.rb', line 67

def get_neighbors(coord)
  row_range = get_range(coord[:ri]).to_a
  col_range = get_range(coord[:ci]).to_a
  start = col_range.first
  stop = col_range.last

  neighbors = { top: @puzzle[row_range.first][start..stop],
                mid: @puzzle[coord[:ri]][start..stop],
                bot: @puzzle[row_range.last][start..stop]
              }
end

#get_range(index) ⇒ Object



79
80
81
82
83
84
# File 'lib/word_searcher/solver.rb', line 79

def get_range(index)
  row_range = (0...7).to_a
  indexes = (index-1..index+1).to_a.map do |new_index|
    row_range.include?(new_index) ? new_index : index
  end
end

#iterate_coordsObject



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/word_searcher/solver.rb', line 54

def iterate_coords
  if @coords.any?
    @word_chars[0..-1].each do |letter|
      @coords.each do |coord|
        neighbors = get_neighbors(coord)
        neighbors = neighbors.values.flatten.uniq
        @found << letter if neighbors.include?(letter)
      end
    end
  end
  @found
end

#search(args = {}) ⇒ Object

AlgoService.new.search



16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/word_searcher/solver.rb', line 16

def search(args = {})
  @word = args.fetch(:word, nil)
  @word = generate_word if !@word.present?
  @puzzle = args.fetch(:puzzle, nil)
  @puzzle = generate_puzzle if !@puzzle.present?

  @word_chars = @word.chars
  @found = []
  @first_letter = @word[0]
  @coords = []
  find_coordinates
  iterate_coords
  score_hsh = check_score
end