在Ruby中实例化类?

时间:2015-05-23 23:00:32

标签: ruby

所以这是关于我的秘密号码游戏的问题的跟进。

Ruby - loop not breaking

现在我试图将其分解为多个类/文件:

主 播放机 secret_number 游戏

现在我试图将游戏分成几段,而不是在一个文件中执行所有操作。

以下是每个班级的代码。我不能为我的生活弄清楚如何将课程联系起来(而且它让我疯狂......)

非常感谢你们。

main.rb的

class Main
# Ask player for their name, and instantiate the game class

    $:.unshift (File.dirname(__FILE__))
    require 'lib/game'

# put code here print a welcome message for your user
    puts "Welcome to the Secret Number Game!"
# put code here ask the user for their name, and save it
    puts "What is your name?"
    name = gets.strip

    newPlayer = Player.new
    puts newPlayer.name
# put code here to create a new game, and start it
end

player.rb

require "main.rb"
    class Player
# this class will likely be simple, and just remember its name
    attr_accessor :name    
    def initialize( name )
    @name = name
    end
end

secret_number.rb

class SecretNumber
# this class will generate our secret number for us
# This class should initiate an array of numbers that range between 1 - 10.
# Use a method from the array class to select a random element from that array. This random number will be the secret number.
# This way the secret number is also a secret to you.

    attr_accessor :secret_number

    def initialize( secret_number )
        @secret_number = 1 + rand(10)
    end 
end

game.rb

#   This class holds most of the game logic and should:
#   Welcome players and inform them of the game rules.
#   Initialize the Player class.
#   Initialize the Secret Number class.
#   Prompt the user to choose a number, verify if the user guessed correctly.
#   If the user guesses incorrectly let them know if they were too high or too low.
#   Monitor how many guesses the player has before the game is over.
#   this class will be complex
#   we need to write logic to initialize a new game, and run the game loop
#   we'll want to write a few separate methods for unique tasks so that our
#   code is structured properly


require "lib/player"
require "lib/secret_number"

class Game

    attr_accessor :name, :secret_number

    def intialize ( name )
        @name = Player.new
        @secret_number = SecretNumber.new
    end

    restart = true

    def guess_check( playerInput, secret_number, attempts )
        if playerInput == @secret_number
            puts "Congratulations, you guessed the secret number! [#{@secret_number}]"
        elsif 
            attempts == 0 
            puts "Sorry, you're out of guesses, the secret number is [#{@secret_number}]"
        elsif playerInput > @secret_number
            puts "Too high! try again!"
        elsif playerInput < @secret_number
            puts "Too low! try again!"
        else
            puts @secret_number
        end
    end

    while restart

        guesses = []
        attempts = 3

        while attempts

            attempts = attempts - 1
            puts "Guess the secret number, you have #{attempts} tries left"
            playerInput = gets.to_i
            guesses.push( playerInput )
            guess_check( playerInput, secret_number, attempts )
            puts "You've guessed #{guesses}"
            break if playerInput == @secret_number || attempts == 0

        end

        puts "Do you want to play again? (y/n)"
        answer = gets.strip
        restart = false if answer == "n"

    end

end

0 个答案:

没有答案
相关问题