wordl-solver 0.1.4 → 0.2.2

Sign up to get free protection for your applications and to get access to all the features.
data/lib/wordl-solver.rb CHANGED
@@ -5,65 +5,85 @@ FILE_PATH = File.join( File.dirname(__FILE__), '5_words.csv')
5
5
 
6
6
  LETTER_WORDS = []
7
7
  LETTER_FRENQUENCY = %w(e t a o i n s h r d l c u m f w y g p b v k j x q z)
8
- INITIAL_POSITIONED_LETTER_MESSAGE = "enter in order the letters for which you know the position \n if you don't know the position press enter"
9
- INITIAL_EXISTING_LETTER_MESSAGE = "enter all the letters you know exist in the word but for which you don't know the position"
10
- INITIAL_NOT_EXISTING_MESSAGE = "enter all the letters you know DO NOT exist in the word"
11
- POSITIONED_LETTER_MESSAGE = "enter all the letters in their position \n if you don't know the position press enter"
12
- EXISTING_LETTER_MESSAGE = "add the letters you know exist in the word but for which you don't know the position"
13
- NOT_EXISTING_MESSAGE = "enter all the letters you know DO NOT exist in the word"
14
8
 
15
9
  CSV.foreach(FILE_PATH) do |row|
16
10
  LETTER_WORDS << row[0]
17
11
  end
12
+
18
13
  class WordlSolver
19
14
 
15
+ def initialize
16
+ @yellows = {
17
+ 0 => [],
18
+ 1 => [],
19
+ 2 => [],
20
+ 3 => [],
21
+ 4 => []
22
+ }
23
+ @greys = []
24
+ @greens = {
25
+ 0 => nil,
26
+ 1 => nil,
27
+ 2 => nil,
28
+ 3 => nil,
29
+ 4 => nil
30
+ }
31
+ @possible_words = []
32
+ end
33
+
20
34
  def self.run
21
35
  WordlSolver.new.run
22
36
  end
23
37
 
24
38
  def run
25
- existing_letters, not_existing_letters, posish = initial_run
26
- iterating_run(existing_letters, not_existing_letters, posish)
39
+ answer = ""
40
+ while answer != "y"
41
+ ask_for_letters_and_colours
42
+ find_possible_words
43
+ present_possible_words
44
+ puts "have you found it ? (y/n)"
45
+ answer = gets.chomp
46
+ end
27
47
  end
28
48
 
29
- private
49
+ private
30
50
 
31
- def ask_and_list_words(positioned_letter_message, existing_letter_message, not_existing_message, existing_letters, not_existing_letters, posish)
32
- puts positioned_letter_message
33
-
34
- posish.each do |position, value|
35
- if value.nil? || value == ""
36
- puts "for position #{position + 1} enter a letter"
37
- posish[position] = gets.chomp
51
+ def ask_for_letters_and_colours
52
+ puts "Type in order color initial then the letter"
53
+ puts "(s for silver, y for yellow and g for green)"
54
+ 5.times do |t|
55
+ puts @greens[t]
56
+ if @greens[t].nil?
57
+ puts "for position #{t + 1}"
58
+ letters = gets.chomp
59
+ while check_word_correct(letters)
60
+ puts "sorry, didn't catch that, try again:"
61
+ letters = gets.chomp
62
+ end
63
+ if letters[0] == 's'
64
+ @greys << letters[1]
65
+ elsif letters[0] == 'y'
66
+ @yellows[t] << letters[1]
67
+ else
68
+ @greens[t] = letters[1]
69
+ end
38
70
  else
39
- puts "for position #{position + 1} you've found: #{value}"
71
+ puts "for position #{t + 1} this #{@greens[t]}"
40
72
  end
41
73
  end
42
- puts existing_letters
43
- puts existing_letter_message
44
- exist = existing_letters | gets.chomp.split('')
45
- puts not_existing_message
46
- puts not_existing_letters
47
- not_exist = not_existing_letters | gets.chomp.split('')
48
- possible_words = []
49
- LETTER_WORDS.each do |word|
50
- put_in = true
51
- posish.each do |key, value|
52
- next if value == ""
53
- put_in = false unless word[key] == value
54
- end
55
- possible_words << word if put_in
56
- end
57
- possible_words = possible_words.select do |word|
58
- exist.all? { |letter| word.include?(letter) }
59
- end
74
+ return [@greens, @yellows, @greys]
75
+ end
60
76
 
61
- possible_words = possible_words.select do |word|
62
- !not_exist.any? { |letter| word.include?(letter) }
63
- end
64
- "here are the existing words"
77
+ def find_possible_words
78
+ filter_greens
79
+ filter_yellows
80
+ filter_greys
81
+ end
82
+
83
+ def present_possible_words
84
+ puts "here are the existing words"
65
85
  hash_word_frequency = Hash.new()
66
- possible_words.each do |word|
86
+ @possible_words.each do |word|
67
87
  count = 0
68
88
  word.chars.each do |char|
69
89
  count += LETTER_FRENQUENCY.index(char) + 2 * word.chars.count(char)
@@ -72,41 +92,39 @@ class WordlSolver
72
92
  end
73
93
  hash_word_frequency = hash_word_frequency.sort_by { |k, v| v }
74
94
  hash_word_frequency.first(30).each do |word, count|
75
- puts "#{word} : #{count}"
95
+ p "#{word} : #{count}"
76
96
  end
77
- return [exist, not_exist, posish]
78
97
  end
79
98
 
80
- def initial_run
81
- existing_letters = []
82
- not_existing_letters = []
83
- posish = {
84
- 0 => nil,
85
- 1 => nil,
86
- 2 => nil,
87
- 3 => nil,
88
- 4 => nil,
89
- }
90
- history = ask_and_list_words(INITIAL_POSITIONED_LETTER_MESSAGE, INITIAL_EXISTING_LETTER_MESSAGE, INITIAL_NOT_EXISTING_MESSAGE, existing_letters, not_existing_letters, posish)
91
- puts "have you found it ? (y/n)"
92
- answer = gets.chomp
93
- existing_letters = history[0]
94
- not_existing_letters = history[1]
95
- puts "existing letters"
96
- puts existing_letters.join(" - ")
97
- puts "not existing letters"
98
- puts not_existing_letters.join(" - ")
99
- return [existing_letters, not_existing_letters, posish]
99
+ def filter_greens
100
+ @possible_words = (@possible_words.empty? ? LETTER_WORDS : @possible_words).select do |word|
101
+ put_in = true
102
+ @greens.each do |key, value|
103
+ next if value.nil?
104
+
105
+ put_in = false unless word[key] == value
106
+ end
107
+ put_in
108
+ end
100
109
  end
101
110
 
111
+ def filter_yellows
112
+ @possible_words.select! do |word|
113
+ possible = true
114
+ word.chars.each_with_index do |letter, index|
115
+ possible = false if @yellows[index].include?(letter)
116
+ end
117
+ possible
118
+ end
119
+ end
102
120
 
103
- def iterating_run(existing_letters, not_existing_letters, posish)
104
- answer = nil
105
- until answer == 'y'
106
- existing_letters, not_existing_letters, posish = ask_and_list_words(POSITIONED_LETTER_MESSAGE, EXISTING_LETTER_MESSAGE, NOT_EXISTING_MESSAGE, existing_letters, not_existing_letters, posish)
107
- puts "have you found it ? (y/n)"
108
- answer = gets.chomp
121
+ def filter_greys
122
+ @possible_words.select! do |word|
123
+ !@greys.any? { |letter| word.include?(letter) }
109
124
  end
110
- puts 'you found it!'
125
+ end
126
+
127
+ def check_word_correct(letters)
128
+ letters.length != 2 || !%w[s y g].include?(letters[0])
111
129
  end
112
130
  end
data/wordl-solver.gemspec CHANGED
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Juwelier::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: wordl-solver 0.1.4 ruby lib
5
+ # stub: wordl-solver 0.2.2 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "wordl-solver".freeze
9
- s.version = "0.1.4"
9
+ s.version = "0.2.2"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0".freeze) if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib".freeze]
13
13
  s.authors = ["george kosmopoulos".freeze]
14
- s.date = "2022-03-24"
14
+ s.date = "2022-04-06"
15
15
  s.description = "This gem helps you solve wordl puzzles. It does so by asking you for the letters position you know, the letters you know exists and the ones you know don't exists. It then proposes words depending on how good they are by weighting the letters by their frequency in the english language.".freeze
16
16
  s.email = "gkosmo1@hotmail.com".freeze
17
17
  s.executables = ["wordl-solver".freeze]
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: wordl-solver
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.4
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - george kosmopoulos
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-03-24 00:00:00.000000000 Z
11
+ date: 2022-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: shoulda