Please check out The Two Towers !

Member Discussions

terms



[Previous] [Next] [Post] [Reply] [Topics] [Summary] [Search]


1. Looking for a good OO solution for menus Mon Sep 26, 2005 [11:00 PM]
the_grip
Email not supplied
member since: Aug 29, 2005
Reply
Hey all,

i've been banging my head against my desk in fruitless attempts to come up with a good object-oriented menu system in the C++ MUD engine i'm writing.

After going through several C-based MUDs that use an enormous switch statement to handle menus, i'm trying to opt for a cleaner and simpler menu structure. i'm thinking of having a single base class type that can be a menu or an action (i.e. Create New Character or Enter Game, respectively), but i can't seem to get a good design paradigm. Anyone know of any tips or examples for such a process? i keep getting about 90% of the way there, but either a. it gets too complicated or b. it runs into a snag that breaks the whole thing.

Much appreciated!


2. RE: Looking for a good OO solution for menus Tue Sep 27, 2005 [4:00 AM]
shasarak
Email not supplied
member since: Dec 10, 2004
In Reply To
Reply
Each possible action should probably be a class in its own right. Or was that what you meant?
Please do not feed the troll.


3. RE: Looking for a good OO solution for menus Tue Sep 27, 2005 [10:22 AM]
Tyche
Email not supplied
member since: Apr 4, 2000
In Reply To
Reply
Does this give you any ideas?


class Menu
def initialize(title, options={})
@title,@options = title,options
@selected = nil
end

def display
@options.each do |key,val|
puts '#{key} - #{val}'
end
print '\n>'
end

def choose(item)
if @options[item]
@selected = item
else
raise 'Invalid choice'
end
end

def invoke
if @selected
@options[@selected].execute
else
raise 'No choice'
end
end
end

class Command
def initialize(name)
@name = name
end

def to_s
@name
end

def execute
puts 'Command '' + @name + '' executed'
end
end

menu = Menu.new('My Menu',
{ 1 => Command.new('Foo'),
2 => Command.new('Bar'),
3 => Command.new('Baz')})

while true
begin
menu.display
c = $stdin.gets
menu.choose(c.to_i)
menu.invoke

rescue
puts $!.message
end

end


(Comment added by Tyche on Tue Sep 27 11:26:33 2005)

By way of translation help....

@variables are instance variables
initialize is a constructor
@options = {} creates an associative array, hash, or map

(Comment added by Tyche on Tue Sep 27 11:55:20 2005)

Whoops! Here's what it looks like executing.

$ ruby menu.rb 
1 - Foo
2 - Bar
3 - Baz

>1
Command 'Foo' executed
1 - Foo
2 - Bar
3 - Baz

>2
Command 'Bar' executed
1 - Foo
2 - Bar
3 - Baz

>5
Invalid choice
1 - Foo
2 - Bar
3 - Baz

>
The Sourcery - http://sourcery.dyndns.org
TeensyMud - http://teensymud.kicks-ass.org
"A man can receive nothing, except it be given him from heaven."


4. RE: Looking for a good OO solution for menus Sat Oct 1, 2005 [6:57 AM]
Gromble
Email not supplied
member since: Oct 1, 2005
In Reply To
Reply
Consider creating a "parser" class. Have your "session" class support a parser stack so you can "push" and "pop" parser subclasses. Your "session" will simply direct user input to the top parser in the stack.

Now, you can implement each menu in its own subclass of the "parser" class. This minimizes the number of states each "parser" subclass needs to handle. As a user navigates the menu, it may push another "parser" subclass that implements a submenu or "pop" itself to return to the previous menu.

I'm developing something like this now and have a parser subclass identified for each of the following...

account login
character management
in-play
bulletin board
shop
quest conversation

...you get the idea.

-Gromble




[Previous] [Next] [Post] [Reply] [Topics] [Summary] [Search]