From: James Gray Date: 2008-02-10T04:09:25+09:00 Subject: Re: Beginner help - txt dungeon --Apple-Mail-2-938670715 Content-Type: text/plain; charset=WINDOWS-1252; format=flowed; delsp=yes Content-Transfer-Encoding: quoted-printable On Feb 8, 2008, at 11:05 PM, Jonathon Hartoon wrote: > I have tried to learn other programming languages before and ruby > was the first one that really stuck. Welcome to Ruby. > Anyways, I own both Prgramming Ruby & Beginning Ruby. The later has =20= > a tutorial for making a text dungeon. I expanded the example in the =20= > book to accept commands from a user and load new maps from a txt file. Excellent work. Modifying code like this is a terrific first step. > I am now trying to build in an inventory system, and smashing my =20 > face into the rocks. I can't even come up with an acceptable way to =20= > do it. Actually, you are already very close=85 > Attachments: > http://www.ruby-forum.com/attachment/1409/Dungeon.rb I've added some comments to try and give you new ideas. Read through =20= and see if I've helped. If not, come back with more questions. We will get you there. Don't give up! ;) James Edward Gray II --Apple-Mail-2-938670715 Content-Disposition: attachment; filename=Dungeon.rb Content-Type: text/x-ruby-script; x-unix-mode=0644; name="Dungeon.rb" Content-Transfer-Encoding: quoted-printable class Dungeon=0D attr_accessor :player=0D =0D def run=0D @player.location =3D @rooms[0].reference=0D current_room.describe=0D while true # loop do=0D @input_words =3D gets.chomp.downcase.split=0D command @input_words[0]=0D end=0D end=0D =0D def create=0D File.open("Rooms.txt").each do |line|=0D rooms =3D line.split('~')=0D # =0D # Lines like the following get a little messy. The code is = fragile and if=0D # we want to change the file format, we will break a lot of = things. It's=0D # usually better to refer to things by name, instead of position. = For=0D # example, can you imagine a roome file like this:=0D # =0D # id: cave_cell=0D # name: Cave Cell=0D # description: A small cell in the side of a cave wall.=0D # The gate on the north side is open.=0D # exits: north (cell_block_a1)=0D # items: mkey, ...=0D # =0D # id: cell_block_a1=0D # ...=0D # =0D add_room(rooms[0].to_sym, rooms[1], rooms[2], { rooms[3].to_sym =3D>= rooms[4].to_sym, rooms[5].to_sym =3D> rooms[6].to_sym, rooms[7].to_sym = =3D> rooms[8].to_sym, rooms[9].to_sym =3D> rooms[10].to_sym })=0D end=0D end=0D =0D def move(direction)=0D puts "\nYou go " + direction.to_s # puts "\nYou go #{direction}"=0D @player.location =3D next_room(direction)=0D current_room.describe=0D end=0D =0D def next_room(direction)=0D find_room(@player.location).connections[direction]=0D end=0D =0D =0D =0D def current_room=0D find_room(@player.location)=0D end=0D =0D def find_room(reference)=0D # =0D # You are using an Array as a Hash. The following line could just = be:=0D # =0D # @rooms[reference] # using a Hash=0D # =0D @rooms.detect { |room| room.reference =3D=3D reference }=0D end=0D =0D def initialize(player_name)=0D @player =3D Player.new(player_name)=0D @rooms =3D Array.new=0D @input_words =3D Array.new=0D create=0D run=0D end=0D =0D def add_room(reference, name, description, connections)=0D @rooms << Room.new(reference, name, description, connections)=0D end=0D =0D def command input=0D # =0D # Some ideas for the following. First, a case statement can be nice = here:=0D # =0D # case input=0D # when "west"=0D # ...=0D # when "east"=0D # ...=0D # ...=0D # end=0D # =0D # Another thought is to imagine you had code like this:=0D # =0D # @player.move_to( current_room.exits[input] )=0D # =0D # Can you make something like that work? exits() could just return = a Hash.=0D # =0D if input =3D=3D "west"=0D if next_room(:west) !=3D :n=0D move(:west)=0D else=0D puts "That direction is impassable"=0D end=0D end=0D =0D if input =3D=3D "east"=0D if next_room(:east) !=3D :n=0D move(:east)=0D else=0D puts "That direction is impassable"=0D end=0D end=0D =0D if input =3D=3D "north"=0D if next_room(:north) !=3D :n=0D move(:north)=0D else=0D puts "That direction is impassable"=0D end=0D end=0D =0D if input =3D=3D "south"=0D if next_room(:south) !=3D :n=0D move(:south)=0D else=0D puts "That direction is impassable"=0D end=0D end=0D =0D if input =3D=3D "look"=0D current_room.describe=0D end=0D if input =3D=3D "inventory"=0D @player.inventory=0D end=0D =0D end=0D =0D class Player #.inventory=0D attr_accessor :name, :location, :items=0D =0D def initialize(name)=0D @name =3D name=0D @items =3D Array.new=0D =0D #Temporary: Give player the key.=0D @items << Item.new(:mkey)=0D end=0D =0D # =0D # Adding to inventory isn't much harder than what you already have:=0D= # =0D # def pickup(item)=0D # @items << item=0D # end=0D # =0D # And later:=0D # =0D # @player.pickup(whatever)=0D # =0D =0D def inventory=0D puts "\n Inventory:"=0D @items.each {|x| puts "\n", x.name}=0D end=0D =0D end=0D =0D class Room #.describe=0D attr_accessor :reference, :name, :description, :connections=0D =0D def initialize(reference, name, description, connections)=0D @reference =3D reference=0D @name =3D name=0D @description =3D description=0D @connections =3D connections=0D end=0D =0D def describe=0D puts "\n\n" + @name + "\nYou are in " + @description=0D end=0D =0D end=0D class Item=0D attr_accessor :type, :name=0D =0D def initialize(type)=0D @type =3D type=0D =0D if @type =3D=3D :mkey=0D @name =3D "Main Dungeon Key"=0D end=0D end=0D end=0D end=0D =0D my_d =3D Dungeon.new("Jon")= --Apple-Mail-2-938670715 Content-Type: text/plain; charset=US-ASCII; format=flowed Content-Transfer-Encoding: 7bit --Apple-Mail-2-938670715--