game can be played with multiple words

Viewed 38

I am learning Ruby and improving every day. I have question on how I can play a game with multiple words

Below RSPEC test passed because the game is played with one word

fit "is lost with a different set of incorrect guesses" do
  guesses = ["ALPHA", "BETA", "GAMMA"]
  input = StringIO.new(guesses.join("\n")) 
  game = Game.new(answers: ["RUBY"], input: input, output: output, 
  max_attempts: 3)

  game.start

  game_output_lines = output.string.split("\n").map(&:strip)

  aggregate_failures do
    expect(game_output_lines.size).to eq 9
    expect(game_output_lines[2]).to eq "The answer is not ALPHA."
    expect(game_output_lines[3]).to eq "Try again. Attempts left: 
    2"
    expect(game_output_lines[4]).to eq "The answer is not BETA."
    expect(game_output_lines[5]).to eq "Try again. Attempts left: 
    1"
    expect(game_output_lines[6]).to eq "The answer is not GAMMA."
    expect(game_output_lines[7]).to eq "You lost."
    expect(game_output_lines[8]).to eq "Your final score: 0/1."
  end

Below is the test that is failing because I have to play with multiple words. For clarity sake, once the "expect(game_output_lines[7]).to eq "You lost." I want to be able to replay the game so that "expect(game_output_lines[8]).to match(/^2/2. Guess a word from an anagram [OMEGA] {5}$/)" below will be possible

it "can be played with multiple words" do
  guesses = ["ALPHA", "BETA", "GAMMA", "MEGA", "OMEGA"]
  input = StringIO.new(guesses.join("\n")) 
  game = Game.new(answers: ["DELTA", "OMEGA"], input: input, 
  output: output, 
  max_attempts: 3)

  game.start

  game_output_lines = output.string.split("\n").map(&:strip)

  aggregate_failures do
    expect(game_output_lines.size).to eq 14
    expect(game_output_lines[2]).to eq "The answer is not ALPHA."
    expect(game_output_lines[3]).to eq "Try again. Attempts left: 
    2"
    expect(game_output_lines[4]).to eq "The answer is not BETA."
    expect(game_output_lines[5]).to eq "Try again. Attempts left: 
    1"
    expect(game_output_lines[6]).to eq "The answer is not GAMMA."
    expect(game_output_lines[7]).to eq "You lost."
    expect(game_output_lines[8]).to match(/^2\/2. Guess a word from 
    an anagram [OMEGA]{5}$/)
    expect(game_output_lines[9]).to eq "The answer is not MEGA."
    expect(game_output_lines[10]).to eq "Try again. Attempts left: 
    2"
    expect(game_output_lines[11]).to eq "Correct! The answer is 
    OMEGA."
    expect(game_output_lines[12]).to eq "So far you have correctly 
    guessed 1 out of 2."
    expect(game_output_lines[13]).to eq "Your final score: 1/2."
 end

Below is the main code. The code is good for the first test but fails for the second test.

class Game
  def initialize(answers:, input: $stdin, output: $stdout, 
    max_attempts: 3)
    @answers = answers
    @correct_answers = 0
    @input = input
    @max_attempts = 3
    @output = output
  end

  def start
    output.puts "Let's play a game. You will have to guess 1 
    anagram."
    output.puts "1/1. Guess a word from an anagram #{answers[-1]}"
    evaluate_user_guesses
  end

  def evaluate_user_guesses
    guess = nil
    current_attempt = 1
    max_attempts_count = 3
    guess_counts = 1
    scores = 1
    while guess != answers[-1] && current_attempt <= max_attempts
      guess = input.gets.to_s.strip
      if guess == answers[-1]
        output.puts "Correct! The answer is #{answers[-1]}."
      else
        output.puts "The answer is not #{guess}."
      end
      if guess == answers[-1]  && current_attempt <= max_attempts
        output.puts "So far you have correctly guessed # 
       {guess_counts} out of # 
        {scores}."
        return output.puts "Your final score: #{guess_counts}/# 
        {scores}."
      elsif guess != answers[-1] && current_attempt < max_attempts
        output.puts "Try again. Attempts left: #{max_attempts_count 
        -= 1}"
      else 
        output.puts "You lost."
      end
      current_attempt += 1
    end
     output.puts "Your final score: #{guess_counts -= 1}/# 
     {scores}."
  end

  private

  attr_reader :answers, :correct_answers, :input, :max_attempts, 
  :output
end

How can i make the second test pass?

Thanks

1 Answers

answers is an array which can contain multiple elements, but you only ever take answers[-1] into account, i.e. the last element:

if guess == answers[-1] # <- compares guess to last element
  output.puts "Correct! The answer is #{answers[-1]}."

Instead, you have to compare guess to all elements in answers, e.g. via include?:

if answers.include?(guess)
  output.puts "Correct! The answer is #{guess}."

In addition, you probably want to delete the correct guess from answers so the player can't "guess" the same word over and over again:

if answers.include?(guess)
  output.puts "Correct! The answer is #{guess}."
  answers.delete(guess)

You still have to adjust some other parts of your code, but this sould point you in the right direction.

Related