根据Ruby文件中的答案创建并启动游戏



我目前正在努力提高我的ruby技能。

我正在做一个猜谜游戏,游戏在没有写在文件上的情况下玩得很好。

现在我介绍了一个文件,游戏的答案写在一个文件上。

我现在的问题是,我正在寻找一种";从写在文件中的答案开始游戏";。

from_file方法是我想要实现问题解决方案的地方。虽然我已经开始了,但卡住了,我需要有人来帮助我。

以下是主代码,而RSPEC测试低于代码

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
if answers.size == 1
word_anagram = "anagram"
else
word_anagram = "anagrams"
end
output.puts "Let's play a game. You will have to guess #{answers.size} # 
{word_anagram}."
answers.each.with_index(1) do |answer, index|
output.puts "#{index}/#{answers.size}. Guess a word from an anagram # 
{Anagram.create("#{answer.upcase}")}"
evaluate_user_guesses(answer)
end
output.puts "Your final score: #{correct_answers}/#{answers.size}."
end
def evaluate_user_guesses(answer)
guess = nil
current_attempt = 1
while guess != answer.upcase && current_attempt <= max_attempts
guess = input.gets.to_s.strip
if guess.upcase == answer.upcase
output.puts "Correct! The answer is #{answer.upcase}."
@correct_answers += 1
return output.puts "So far you have correctly guessed #{correct_answers} out of 
#{answers.size}."
else
output.puts "The answer is not #{guess.upcase}."
if current_attempt < max_attempts
output.puts "Try again. Attempts left: #{max_attempts - current_attempt}" 
else
output.puts "You lost."
end
end
current_attempt += 1
end
end
def self.from_file(path:, input: $stdin, output: $stdout, max_attempts: 3)
self.new(answers: [], input: input, output: output, max_attempts: max_attempts)
end
private
attr_reader :answers, :correct_answers, :input, :max_attempts, :output
end

这是一个RSPEC测试,测试如何根据文件中的答案创建和启动游戏。我将感谢你的意见。

describe "#from_file" do
let(:output) { StringIO.new }
it "creates and starts a game from answers written in a file" do
file = Tempfile.new
answers = %w(alpha beta)
file_contents = answers.join("n")
begin
file.write(file_contents)
file.rewind
guesses = %w(alpha beta)
input = StringIO.new(guesses.join("n"))
game = Game.from_file(
path: file.path,
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 8
expect(game_output_lines[0]).to eq "Let's play a game. You will have to guess 
2 anagrams."
expect(game_output_lines[1]).to match(/^1/2. Guess a word from an anagram 
[ALPH]{5}$/)
expect(game_output_lines[2]).to eq "Correct! The answer is ALPHA."
expect(game_output_lines[3]).to eq "So far you have correctly guessed 1 out of 
2."
expect(game_output_lines[4]).to match(/^2/2. Guess a word from an anagram 
[BETA]{4}$/)
expect(game_output_lines[5]).to eq "Correct! The answer is BETA."
expect(game_output_lines[6]).to eq "So far you have correctly guessed 2 out of 
2."
expect(game_output_lines[7]).to eq "Your final score: 2/2."
end
ensure
file.close
file.unlink
end
end
end

根据您的评论,您需要从文件中读取answers: [],因此对于每行包含一个答案的文本文件,

#answers.txt
one
two
three
> answers = File.read("answers.txt").split
=> ["one", "two", "three"]
def self.from_file(path:, input: $stdin, output: $stdout, max_attempts: 3)
answers = File.read("answers.txt").split
self.new(answers: answers, input: input, output: output, max_attempts: max_attempts)
end

相关内容

  • 没有找到相关文章

最新更新