Class: Wordle::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/wordle/game.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options_reader = Options.new) ⇒ Game

Returns a new instance of Game.



9
10
11
12
13
14
# File 'lib/wordle/game.rb', line 9

def initialize(options_reader = Options.new)
  @list = List.new
  @options = options_reader.read
  @target_word = generate_word
  @result_builder = ResultBuilder.new(@options[:contrast])
end

Class Method Details

.play(*args) ⇒ Object



5
6
7
# File 'lib/wordle/game.rb', line 5

def self.play(*args)
  new(*args).play
end

Instance Method Details

#playObject



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
# File 'lib/wordle/game.rb', line 16

def play
  winner = false

  Legend.print(@result_builder)
  puts "Guess a 5 letter word: "
  guesses = []
  must_include = []
  must_match = Array.new(5)

  while guesses.length < 6 && !winner
    guess = gets.chomp

    validator = GuessValidator.new(
      guess,
      @list,
      @options[:difficult],
      must_include,
      must_match
    )
    if validator.invalid?
      puts validator.error
      next
    end

    analyzer = GuessAnalyzer.new(
      @target_word,
      guess,
      @result_builder
    )
    puts analyzer.colors
    guesses << analyzer.squares
    if @options[:difficult]
      must_include = analyzer.must_include(must_include)
      must_match = analyzer.must_match(must_match)
    end

    if analyzer.match?
      winner = true
      break
    end
  end

  hash = Digest::SHA2.hexdigest(@target_word)[..5]
  if winner
    puts "\nWordle Gem #{hash} #{guesses.length}/6\n\n"
  else
    puts "\nWord was: #{@target_word}\n"
    puts "\nWordle Gem #{hash} X/6*\n\n"
  end

  puts guesses
end