ruby 练习:构建一个简单的猜谜游戏创建一个GuessingGame类,用一个名为answer的整数初始化。定义一个实例方法

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了ruby 练习:构建一个简单的猜谜游戏创建一个GuessingGame类,用一个名为answer的整数初始化。定义一个实例方法相关的知识,希望对你有一定的参考价值。


####################################### PSEUDOCODE #####################################

# INPUT: initialize a game passing the correct integer answer to instantiate;
#        pass a guess value to #guess
# OUPUT: #guess outputs symbol representing
#        if the most recent guess is too high, too low, or correct
#        #solved? outputs true if the most recent guess is correct and flase otherwise.
# STEPS: #initialize -> set answer to instance variable, answer
         #guess -> return corresponding symbol for too high, too low, correct
         #solved? -> return true if the guess is equal the answer and false oherwise.

###################################### INITIAL CODE ####################################

class GuessingGame

  def initialize(answer)
    @answer = answer
  end

  def guess(guess)
    @guess = guess
    if guess > @answer
      :high
    elsif guess == @answer
      :correct
    else
      :low
    end
  end

  def solved?
    @guess == @answer
  end

end

#################################### REFACTORED CODE ###################################
class GuessingGame
 def initialize(answer)
   @answer = answer
 end

 def guess(guess)
   @guess = guess
   if @guess == @answer
     :correct
   else @guess > @answer ? :high : :low
   end
 end

 def solved?
   @guess == @answer
 end
end

###################################### DRIVER CODE #####################################
game = GuessingGame.new(6)
p game.guess(1) == :low
p game.guess(7) == :high
p game.solved? == false
p game.guess(6) == :correct
p game.solved? == true
p game.guess(7) == :high
p game.solved? == false


####################################### REFLECTION #####################################

#I thought this challenge was pretty easy...during refactoring I tried to play
#around with some things, but it was mostly around with returning true for
#solved? when the instance of the game has been solved, regardless of subsequent
#incorrect guesses, while still returning :high, :low, :correct for those
#guesses. If we defined the game instance as solved once guessed correctly even
#with future incorrect guesses and solved? didn't have a question mark in the
#method, we could initialize the game with an instance variable called solved
#set to false and include an assignment of solved = true before returning
#:correct within the guess. I also played with instantiating an array and
#pushing to it any incorrect guesses, then returning false but also printing the
#incorrect guess history when solved? is called before a correct guess.  I know
#this isn't in line with the instructions of the challenge, but it made more
#sense to me so I played around and solved variations just for shits. Other than
#that, I couldn't find too much to refactor...need to check out others' code to
#see if there is any very different approach.

以上是关于ruby 练习:构建一个简单的猜谜游戏创建一个GuessingGame类,用一个名为answer的整数初始化。定义一个实例方法的主要内容,如果未能解决你的问题,请参考以下文章

在终端上用 C++ 简单编写一个的猜谜游戏!

在 python 3. 数字猜谜游戏中将许多参数传递给输入

创建一个猜谜游戏直到我想停下来

CPP猜谜游戏不工作,我输入名字后它就停止了

AHOI 2015 猜谜游戏

猜谜游戏,discord.py 机器人