Uninitialized constant Game::World when I try and initialize the World class in Ruby -
so i'm trying create basic text adventure in ruby, , i'm following tutorial (https://jsrn.gitbooks.io/make-your-first-text-adventure-in-ruby/content/creating_the_framework.html)
but when try , run code error:
gamebase.rb:10:in `initialize': uninitialized constant game::world (nameerror) gamebase.rb:55:in `new' gamebase.rb:55:in `<main>'
this code:
from gamebase.rb
dir["gamedir/**.*"].each { |file| require_relative file } class game actions = [ :forward, :backward, :look, :attack, :loot, :inventory, :use, :cast ] def initialize @world = world.new @player = player.new start_game end private def start_game while @player.alive? @current_room = @world.get_room_of(@player) print_player_status action = take_player_input next unless actions.include? action take_action(action) end end def take_player_input print "what do?" gets.chomp.to_sym end def take_action(action) case action when :forward @world when :backward @world when :look @world when :attack @current_room.interact(@player) when :loot @current_room.interact(@player) when :inventory when :use when :cast end end end game.new
and world.rb
class world def initialize @past_rooms = 0 @current_room = 0 end def move_entity_forward(entity) @current_room += 1 end def move_entity_backward(entity) @current_room -= 1 end def get_room_of(entity) if @current_room <= @past_rooms @current_room else room.new @past_rooms += 1 end end end class room attr_accessor :size, :content def initialize @content = get_content @size = get_size @adjetive = get_adjetive end def interact(player) if @content @content.interact(player) @content = nil else puts "there isn't here..." end end private def get_content [monster, item.sample.new] end def get_size dimensions = [5, 10, 15, 20, 25, 30, 40, 50, 75, 100, 200] "#{dimensions.sample}'x#{dimensions.sample}'" end def get_adjetive ["well-lit", "dim", "filthy", "suprisingly clean", "round", "muddy", "oppressive"] end end
try adding line top of gamebase.rb
:
require_relative 'world'
this tells ruby interpreter file named world.rb
in same folder gamebase.rb
, run code in file, define world
class. world
class available when try use it.
Comments
Post a Comment