# As a user, you can initialize the guessing game with a number, which is
the correct guess
# so the initialize method takes in one parameter, and sets game_complete?
to false
#
# As a user, I can guess the number, which will
# return :too_high if its > answer
# return :too_low if its < answer
# return :correct if its = answer
# correct changes the game_complete? to true
# if a user guesses the incorrect number after guessing the correct number,
it should
# change the game_complete? to false
# return :too_high or :too_low
require_relative 'guess'
describe GuessingGame do
let(:game) { GuessingGame.new(50) }
describe "#initialize" do
it "expects a single parameter" do
expect(GuessingGame.instance_method(:initialize).arity).to eq 1
end
end
describe "#guess" do
it "expects a single parameter" do
expect(GuessingGame.instance_method(:guess).arity).to eq 1
end
it "returns :too_low when the guess is lower than the answer" do
expect(game.guess(1)).to eq :too_low
end
it "returns :too_high when the guess is higher than the answer" do
expect(game.guess(100)).to eq :too_high
end
it "returns :correct when the guess matches answer" do
expect(game.guess(50)).to eq :correct
end
it "changes game_complete? when the correct guess is made" do
expect {
game.guess(50)
}.to change(game, :game_complete?).from(false).to(true)
end
it "doesn't change game_complete? when an incorrect guess is made" do
expect {
game.guess(10)
}.to_not change(game, :game_complete?).from(false)
end
it "returns :game_solved once you try to guess in a completed game" do
game.guess(50)
expect(game.guess(100)).to eq :game_solved
end
end
describe "#game_complete?" do
it "returns false in a new game" do
expect(game.game_complete?).to eq false
end
end
end
现在,当我运行此代码时,我得到错误GuessingGame#guess返回:game_solved一旦你尝试在完成的游戏中猜测
这是我猜的课程
class GuessingGame
def initialize(num)
@num=num
def game_complete?
return false
end
end
def guess(num1)
if num1<@num
return :too_low
elsif num1>@num
return :too_high
else
def game_complete?
return true
end
return :correct
end
end
端
我尝试用false初始化一个bool变量,一旦做出正确的猜测,我将其设为true,如果该变量为true,则返回:game_completed但是对我没有用
答案 0 :(得分:1)
看起来您没有初始化主题,除非您没有显示更多代码。这是一个有效的版本:
class GuessingGame
def initialize(num)
@num = num
end
def guess(num)
if num < @num
return :too_low
elsif num > @num
return :too_high
else
return :correct
end
end
end
require 'spec_helper'
require 'foo'
describe GuessingGame do
let(:foo) { GuessingGame.new(50) }
describe "when guess is too low" do
it "returns :too_low" do
expect(foo.guess(25)).to eq :too_low
end
end
describe "when guess is too high" do
it "returns :too_high" do
expect(foo.guess(75)).to eq :too_high
end
end
describe "when guess is correct" do
it "returns :correct" do
expect(foo.guess(50)).to eq :correct
end
end
end
现在进行一些重构。从方法中间返回通常不是一个好主意。 Ruby总是返回最后一个表达式的值,所以我们可以利用它。
def guess(num)
case num <=> @num
when -1
:too_low
when 1
:too_high
else
:correct
end
end
<=>
运算符比较两个值并返回-1,0或1.稍微有些偷偷摸摸,我们可以进一步将guess
方法重构为一行:
def guess(num)
[:correct, :too_high, :too_low][num <=> @num]
end
修改
似乎您还想要定义另一种方法来指示游戏是否完整。这是一种方法:
class GuessingGame
def initialize(num)
@num = num
end
def compare(num)
[:correct, :too_high, :too_low][num <=> @num]
end
def guess(num)
@comparison = compare(num)
end
def game_complete?
@comparison == :correct
end
end
答案 1 :(得分:0)
也许你可以尝试这样的事情,而不是定义方法只是将它们变成变量。
class GuessingGame
attr_reader :game_complete
def initialize(num)
@num=num
end
def guess(num1)
if num1<@num
return :too_low
elsif num1>@num
return :too_high
else
@game_complete = true
end
return :correct
end
end
现在要检查游戏是否完成,你可以使用game.game_complete
如果它没有完成将返回nil
(评估为false),或者如果强制它将返回{{1} }}。
希望这会对你有所帮助。