Ok here is my quick SpaceMerchant::Sector class. I wasn't able to
participate in last weeks quiz (and I suggested it!) so I want to be
sure and do something in this week. I need to go through and clean this
up now, but I wanted to get this out there quick because I haven't seen
any parts besided the Galaxy put up. At the very end is the code I've
been using to tests with. Ok thanks!
-----Jay Anderson
module SpaceMerchant
class Sector
attr_reader :name
def initialize(name, region)
@name, @region = name, region
@planets = []
@stations = []
@links = []
end
def link(other_sector)
@links << other_sector
end
def add_planet(planet)
@planets << planet
end
def add_station(station)
@stations << station
end
def handle_event(player)
menu = :main_menu
while menu != :done
menu = send menu, player
end
end
def main_menu player
puts "Sector #{name}"
puts "#{@region.name}"
puts
if(@stations.length > 0) then
puts "Station#{'s' if @stations.length>0}:
"+@stations.map{|s|s.name}.join(', ')
end
if(@planets.length > 0) then
puts "Planet#{'s' if @planets.length>0}:
"+@planets.map{|p|p.name}.join(', ')
end
puts
puts '(D)ock with station' if @stations.length > 0
puts '(L)and on planet' if @stations.length > 0
puts
if(@links.length > 0) then
puts 'Warp to nearby sector:
'+@links.map{|l|'['+l.name+']'}.join(', ')
end
puts
puts '(Q)uit game'
print '?'
response = gets.chomp
case response
when /^d/i
return :dock
when /^l/i
return :land
when /^q/i
puts "Are you sure you want to quit? (y/n)"
y_or_n = ''
while (y_or_n = gets) !~ /^(y|n)/i
puts "Are you sure you want to quit? (y/n)"
end
if y_or_n =~ /^y/i then puts 'goodbye!'; exit 0; end
return :main_menu
else
new_loc = @links.select{|l| l.name == response}
if new_loc.length > 0 then
puts "Travelling..."
player[:location] = new_loc.first
return :done
else
puts " *** INVALID CHOICE ***"
return :main_menu
end
end
end
def dock player
if @stations.length < 1 then
puts "There are no stations to dock with"
return :main_menu
end
puts "Choose station to dock with:"
puts
@stations.each_with_index do |s,index|
puts " #{index}: #{s.name}"
end
puts
puts "(M)ain menu"
print "?"
response = gets.chomp
choice = response.to_i
if response =~ /^m/i then
return :main_menu
elsif choice.to_s != response || choice >= @stations.length
then
puts " *** INVALID CHOICE ***"
return :dock
else
puts "Docking..."
player[:location] = @stations[choice]
return :done
end
end
def land player
if @planets.length < 1 then
puts "There are no planets to land on"
return :main_menu
end
puts "Choose planet to land on:"
puts
@planets.each_with_index do |p,index|
puts " #{index}: #{p.name}"
end
puts
puts "(M)ain menu"
print "?"
response = gets.chomp
choice = response.to_i
if response =~ /^m/i then
return :main_menu
elsif choice.to_s != response || choice >= @planets.length
then
puts " *** INVALID CHOICE ***"
return :dock
else
puts "Landing..."
player[:location] = @planets[choice]
return :done
end
end
end
end
if __FILE__ == $0
Named = Struct.new(:name)
region = Named.new('The Region')
s = SpaceMerchant::Sector.new('Test Sector', region)
5.times do |i|
s.add_planet(Named.new("planet #{i}"))
s.add_station(Named.new("station #{i}"))
s.link(SpaceMerchant::Sector.new("#{i}", region))
end
player = {}
s.handle_event(player)
p player
end