Class: WordleGame::Game

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

Constant Summary collapse

WORD_LIST =
%w[apple banjo cider delta eagle other words]

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(word_length = 5, max_attempts = 6) ⇒ Game

Returns a new instance of Game.



7
8
9
10
11
12
# File 'lib/wordle_game/game.rb', line 7

def initialize(word_length = 5, max_attempts = 6)
  @word_length = word_length
  @max_attempts = max_attempts
  @attempts = []
  @target_word = WORD_LIST.select { |word| word.length == word_length }.sample
end

Instance Attribute Details

#attemptsObject (readonly)

Returns the value of attribute attempts.



3
4
5
# File 'lib/wordle_game/game.rb', line 3

def attempts
  @attempts
end

#max_attemptsObject (readonly)

Returns the value of attribute max_attempts.



3
4
5
# File 'lib/wordle_game/game.rb', line 3

def max_attempts
  @max_attempts
end

#target_wordObject (readonly)

Returns the value of attribute target_word.



3
4
5
# File 'lib/wordle_game/game.rb', line 3

def target_word
  @target_word
end

#word_lengthObject (readonly)

Returns the value of attribute word_length.



3
4
5
# File 'lib/wordle_game/game.rb', line 3

def word_length
  @word_length
end

Instance Method Details

#attempt(guess) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/wordle_game/game.rb', line 14

def attempt(guess)
  return { status: :game_over, message: "No more attempts allowed" } if game_over?
  
  if guess.length != word_length
    return { status: :invalid, message: "Guess length must be #{word_length}" }
  end
  
  result = check_guess(guess)
  attempts << { guess: guess, result: result }
  
  if guess == target_word
    { status: :correct, result: result, attempts_left: max_attempts - attempts.size }
  elsif attempts.size >= max_attempts
    { status: :game_over, result: result, attempts_left: 0 }
  else
    { status: :incorrect, result: result, attempts_left: max_attempts - attempts.size }
  end
end