Module: RubyWordlePlay

Defined in:
lib/ruby-wordle/script-play.rb

Constant Summary collapse

NUM_LETTERS =
5
NUM_ATTEMPTS =
6
LPAD =
" " * 1
PROMPT =
"〉".bold.red

Class Method Summary collapse

Class Method Details

.ask_for_attempt(str) ⇒ Object



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
# File 'lib/ruby-wordle/script-play.rb', line 67

def self.ask_for_attempt(str)
  puts
  print  " #{str} ".bold.bg_cyan + " "
  attempt = Readline.readline(LPAD + PROMPT, true).strip.downcase
  if attempt == "quit" || attempt == "q"
    exit
  elsif attempt == "help" || attempt == "h" || attempt == "?"
    get_help
    attempt = ask_for_attempt(str)
  end

  unless /\A[a-z]{5}\z/ =~ attempt
    puts
    puts LPAD + "Sorry,".red + " the input format is not right"
    puts
    attempt = ask_for_attempt(str)
  end

  unless check_dict(attempt)
    puts
    puts LPAD + "Sorry,".red + " it's not listed in the dictionary"
    puts
    attempt = ask_for_attempt(str)
  end
  attempt
end

.check_dict(attempt) ⇒ Object



20
21
22
# File 'lib/ruby-wordle/script-play.rb', line 20

def self.check_dict(attempt)
  @word_list_original.include?(attempt)
end

.draw_decorated_borderObject



94
95
96
97
98
# File 'lib/ruby-wordle/script-play.rb', line 94

def self.draw_decorated_border
  puts
  puts (' '.bg_green * 5 + ' '.bg_gray * 3 + ' '.bg_brown  * 5 + ' '.bg_gray * 3) * 4
  puts
end

.evaluate(attempt) ⇒ Object



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
# File 'lib/ruby-wordle/script-play.rb', line 24

def self.evaluate(attempt)
  puts
  @letters_known = attempt.split(//)
  correct = []
  wrong   = []
  @word.split(//).each_with_index do |l, i|
    if attempt[i] == l
      correct << i
    else
      wrong << i
      @letters_known[i] = '.'
    end
  end
  attempt.split(//).each_with_index do |l, i|
    if correct.include?(i)
      print " #{@word[i]} ".upcase.bg_green
    else
      elsewhere = wrong.map{|i|@word[i]}.index(l)
      if elsewhere
        print " #{l} ".upcase.bg_brown
        @not_used_here[i + 1] << l unless @not_used_here[i + 1].include? l
      else
        print "   ".bg_gray
        @not_used << attempt[i] if /[a-z]/ =~ attempt[i]
        @not_used.uniq!
        @not_used.sort!
      end
    end
  end
  puts
  correct.size == NUM_LETTERS
end

.get_helpObject



57
58
59
60
61
62
63
64
65
# File 'lib/ruby-wordle/script-play.rb', line 57

def self.get_help
  letters_used = []
  @not_used_here.each do |k, v|
    v.each do |r|
      letters_used << [k - 1, r]
    end
  end
  RubyWordleSolver.solve_wordle(@letters_known.join, letters_used, @not_used, :basic, @word)
end

.play_wordleObject



100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ruby-wordle/script-play.rb', line 100

def play_wordle
  draw_decorated_border;
  puts "Input a word of 5 letters and press \"↵\"".cyan.bold
  puts "Type \"?↵\" for help, \"quit↵\" to exit".gray

  NUM_ATTEMPTS.times do |i|
    attempt = ask_for_attempt("#{i + 1}/#{NUM_ATTEMPTS}")
    result = evaluate(attempt)
    puts
    if result
      puts "C O R R E C T !".upcase.bg_magenta
      exit
    elsif i + 1 == NUM_ATTEMPTS
      puts " ANSWER: #{@word} ".upcase.bg_red
      exit
    else
      puts @not_used.uniq.map{|l| " #{l.upcase} "}.join.bg_black if !@not_used.empty?
      puts
    end
  end
  draw_decorated_border
end