Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Comparing changes

Choose two branches to see what's changed or to start a new pull request. If you need to, you can also compare across forks.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also compare across forks.
base fork: ledewitt/simple_game
base: master
...
head fork: ledewitt/simple_game
compare: jeg2s_notes
Checking mergeability… Don't worry, you can still create the pull request.
  • 1 commit
  • 2 files changed
  • 0 commit comments
  • 1 contributor
Showing with 81 additions and 72 deletions.
  1. +14 −0 ruby_guidelines.md
  2. +67 −72 simple_game.rb
View
14 ruby_guidelines.md
@@ -0,0 +1,14 @@
+# Ruby Guidelines
+
+1. It's better to fix the code to not need a comment than to write a comment
+2. p() is a temporary debugging tool that shouldn't stay in your code
+3. Most output should use puts()
+4. Use print() if you need fine-grain non-line-oriented output
+5. Prefer objects to represent problem domain data (NOT YET!)
+6. An Array is for data that has a logical ordering only
+7. Otherwise, name keys in a Hash
+8. If you have separate variable names for each object, you want a Hash
+9. Translate if var ==/=~ SOMETHING… elsif var ==/=~ ELSE… end to case
+10. A method should do one thing
+11. Methods should be as short as possible
+12. Name methods with an intention revealing verb
View
139 simple_game.rb
@@ -1,85 +1,80 @@
-#### MY NOTES ####
-
-# Objects -
-# WHISKEY_BOTTLE, BUCKET, FROG, CHAIN
-
-# Locations -
-# House with a LIVING_ROOM and an ATTIC
-# GARDEN
-
-#### METHODS ####
-
-# Need some type of way to look at a locaiton and see if an object is at this location.
-
-def look(location)
- #if location then print description
- print location[0], "\n"
- location[1].each { |x| print "From here there is a #{x}\n" }
- if location[2]
- location[2].each { |x| print "There is a #{x} here.\n" }
+#################
+### Constants ###
+#################
+
+MAP = { living_room: { description: 'You are in the LIVING_ROOM of a wizards house. There is a wizard snoring loudly on the couch.',
+ exits: {upstairs: :attic, west: :garden} },
+ garden: { description: 'You are in a beautiful GARDEN. There is a WELL with water in it.',
+ exits: {east: :living_room} },
+ attic: { description: 'You are in the ATTIC of the WIZARDS house. There is a giant WELDING_TORCH in the corner.',
+ exits: {downstairs: :living_room} } }
+INITIAL_OBJECT_LOCATIONS = { living_room: %w[WHISKEY_BOTTLE BUCKET],
+ garden: %w[FROG CHAIN],
+ inventory: [ ] }
+
+################
+### Commands ###
+################
+
+def look(location, object_locations)
+ room = MAP[location]
+ puts room[:description]
+ room[:exits].each do |direction, room|
+ puts "You can go #{direction.upcase} to the #{room.upcase}."
+ end
+ object_locations[location].each do |object|
+ puts "A #{object} is here."
end
-
+ nil
end
-def interpret(command,location)
- if command == 'look'
- look(location)
+def walk(direction, location, object_locations)
+ if destination = MAP[location][:exits][direction.to_sym]
+ look(destination, object_locations)
+ destination
+ else
+ puts "You cannot go that way."
+ nil
end
end
-def start(location)
- puts "Welcome to a very simple game!"
-
- while (command = gets.strip) != 'quit'
- interpret(command,location)
+############
+### Game ###
+############
+
+def dispatch(command, location, object_locations)
+ words = command.split(" ")
+ case words.first
+ when "quit"
+ exit
+ when "look"
+ look(location, object_locations)
+ when "pickup"
+
+ when "walk"
+ walk(words.last, location, object_locations)
+ else
+
end
end
-# def describe_location(location)
-# p location[1]
-# end
-
-# def describe_path(path)
-# p "There is a #{path[1]} going, #{path[0]} from here"
-# end
-
-#### MAIN BODY OF CODE ####
-
-# Define the games Objects, and Locations
-
-objects = ['WISKEY_BOTTLE', 'BUCKET', 'FROG', 'CHAIN']
-
-living_room_objects = ['WHISKEY_BOTTLE', 'BUCKET']
-
-living_room_directions = ['WEST door to the garden', 'UPSTAIRS stairway to the ATTIC']
-
-living_room = ['You are in the LIVING_ROOM of a wizards house. There is a wizard snoring loudly on the couch.', living_room_directions, living_room_objects]
-
-garden_objects = ['WELL', 'FROG', 'CHAIN']
-
-garden_directions = ['EAST door LIVING_ROOM']
-
-garden = ['You are in a beautiful GARDEN.', garden_directions, garden_objects ]
-
-attic_objects = ['giant welding TORCH in the corner']
-
-attic_directions = ['DOWNSTAIRS stairway to LIVING_ROOM']
-
-attic = ['You are in the ATTIC of the WIZARDS house.', attic_directions, attic_objects]
-
-map = [living_room, garden, attic]
-
-object_locations = ['WHISKEY_BOTTLE LIVING_ROOM', 'BUCKET LIVING_ROOM', 'CHAIN GARDEN', 'FROG GARDEN']
+def introduce
+ puts "Welcome to a very simple game!"
+end
-location = map[0]
+def run(location, object_locations)
+ look(location, object_locations)
+ while command = gets.strip
+ location = dispatch(command, location, object_locations) || location
+ end
+end
-#describe_location(location)
+############
+### Main ###
+############
-start(location)
+current_location = :living_room
+object_locations = INITIAL_OBJECT_LOCATIONS.dup
-# p location[0]
-#
-# p objects[0]
-#
-# p map[0]
-#
+introduce
+run(current_location, object_locations)

No commit comments for this range

Something went wrong with that request. Please try again.