我正在学习Ruby,并且每天都在进步。我有关于如何玩多个单词的游戏的问题
以下RSPEC测试通过,因为游戏是用一个单词玩的
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
下面是一个失败的测试,因为我必须玩多个单词。为了清楚起见;expect(game_output_lines[7](;你输了"我希望能够重播比赛;期望(game_output_lines[8](。匹配(/^2/2。从变位词[OMGA]{5}$/(中猜一个单词";以下将是可能的
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
以下是主要代码。该代码在第一次测试中是好的,但在第二次测试中失败了。
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
我怎样才能通过第二次考试?
感谢
answers
是一个可以包含多个元素的数组,但只考虑answers[-1]
,即最后一个元素:
if guess == answers[-1] # <- compares guess to last element
output.puts "Correct! The answer is #{answers[-1]}."
相反,您必须将guess
与answers
中的所有元素进行比较,例如通过include?
:
if answers.include?(guess)
output.puts "Correct! The answer is #{guess}."
此外,你可能想从answers
中得到delete
的正确猜测,这样玩家就不能";猜测;一遍又一遍地用同一个词:
if answers.include?(guess)
output.puts "Correct! The answer is #{guess}."
answers.delete(guess)
您仍然需要调整代码的其他部分,但这将为您指明正确的方向。