Having a difficult time with Case statements.

This is a discussion on Having a difficult time with Case statements. within the RUBY forums in Programming Languages category; I am trying to implement a simple frontend for a text adventure that I'm putting together for ruby practice. My idea was to use case - when to validate player input and to progress through a menu system, character creation and the loading of character files. The code runs cleanly for me but effectively does absolutely nothing. I've exhausted about every idea I've had for a nice clean format for handling this and I've decided to ask someone that knows what they are doing. I've attached the source file in question and I would greatly appreciate it if someone could ...

Go Back   Application Development Forum > Programming Languages > RUBY

Object Mix

Register FAQ Calendar Search Today's Posts Mark Forums Read
  #1  
Old 08-27-2008, 07:45 PM
Chris Bailey
Guest
 
Default Having a difficult time with Case statements.

I am trying to implement a simple frontend for a text adventure that
I'm putting together for ruby practice. My idea was to use case - when
to validate player input and to progress through a menu system,
character creation and the loading of character files. The code runs
cleanly for me but effectively does absolutely nothing. I've exhausted
about every idea I've had for a nice clean format for handling this and
I've decided to ask someone that knows what they are doing. I've
attached the source file in question and I would greatly appreciate it
if someone could have a look and offer me some advice on what I'm doing
wrong or how to better design said system. All feedback is welcome,
Thank you.

Attachments:
http://www.ruby-forum.com/attachment/2623/startup.rb

--
Posted via http://www.ruby-forum.com/.

Reply With Quote
  #2  
Old 08-27-2008, 08:08 PM
Mark T
Guest
 
Default Re: Having a difficult time with Case statements.

Add the method you created to the end of the program 'definition' you have.
ie: You have defined the program method startUp without invoking it.

tesThis.rb:12:in `startUp': undefined method `blue' for
"Nephlen":String (NoMethodError)
from tesThis.rb:165

paradisaeidae.

Reply With Quote
  #3  
Old 08-27-2008, 08:15 PM
Chris Bailey
Guest
 
Default Re: Having a difficult time with Case statements.

Mark T wrote:
> Add the method you created to the end of the program 'definition' you
> have.
> ie: You have defined the program method startUp without invoking it.
>
> tesThis.rb:12:in `startUp': undefined method `blue' for
> "Nephlen":String (NoMethodError)
> from tesThis.rb:165
>
> paradisaeidae.


Thank you for the feedback but I should have mentioned that it is one of
several source files and the startUp method is called from a main
function elsewhere. The source also makes reference to a few things here
and there that are not included in that particular file. I thought it
would be a bit much to just dump all the source code up here so I opted
for the part causing me the biggest headache. When I said it "didn't
work at all", what I really meant was that it doesn't work as I intended
it to. I will post some sample output from me running the game using
that menu/startup system.


bsud.rb combat.rb items.rb magic.rb maps person.rb saved shop.rb
startup.rb world.rb
chris@Dell-D510:~/bsud$ ruby bsud.rb
Nephlen
What would you like to do?

1 - Load a Saved Game.
2 - Create a New Character.
3 - Exit from Nephlen.
4 - View the Readme file.
2
1
3
4
2
1

I go through and try any of the inputs that "should", in my opinion, be
recognized by the input tracking case statements but nothing happens at
all. I am completely dumbfounded =)


--
Posted via http://www.ruby-forum.com/.

Reply With Quote
  #4  
Old 08-27-2008, 08:51 PM
Michael W. Ryder
Guest
 
Default Re: Having a difficult time with Case statements.

Chris Bailey wrote:
> I am trying to implement a simple frontend for a text adventure that
> I'm putting together for ruby practice. My idea was to use case - when
> to validate player input and to progress through a menu system,
> character creation and the loading of character files. The code runs
> cleanly for me but effectively does absolutely nothing. I've exhausted
> about every idea I've had for a nice clean format for handling this and
> I've decided to ask someone that knows what they are doing. I've
> attached the source file in question and I would greatly appreciate it
> if someone could have a look and offer me some advice on what I'm doing
> wrong or how to better design said system. All feedback is welcome,
> Thank you.
>
> Attachments:
> http://www.ruby-forum.com/attachment/2623/startup.rb
>


Looking at your code it looks like you need to remove the first case
statement and use something like a while loop. From what I can see it
looks like when you select '2' it changes 'startstate' to 1 and then
exits the case statement. An alternative is to set startstate in the
main program and then call startUp with the current startstate -- i.e. x
= startUp(startstate). Then test x to see if you want to call startUp
again or exit the program.
Reply With Quote
  #5  
Old 08-27-2008, 09:04 PM
Chris Bailey
Guest
 
Default Re: Having a difficult time with Case statements.

Michael W. Ryder wrote:
> Chris Bailey wrote:
>> Thank you.
>>
>> Attachments:
>> http://www.ruby-forum.com/attachment/2623/startup.rb
>>

>
> Looking at your code it looks like you need to remove the first case
> statement and use something like a while loop. From what I can see it
> looks like when you select '2' it changes 'startstate' to 1 and then
> exits the case statement. An alternative is to set startstate in the
> main program and then call startUp with the current startstate -- i.e. x
> = startUp(startstate). Then test x to see if you want to call startUp
> again or exit the program.


Wrapped a call to startUp() into the main game loop and made startstate
global....it's working like a charm now! =) Thanks a billion!

--
Posted via http://www.ruby-forum.com/.

Reply With Quote
  #6  
Old 08-27-2008, 10:32 PM
Michael W. Ryder
Guest
 
Default Re: Having a difficult time with Case statements.

Chris Bailey wrote:
> Michael W. Ryder wrote:
>> Chris Bailey wrote:
>>> Thank you.
>>>
>>> Attachments:
>>> http://www.ruby-forum.com/attachment/2623/startup.rb
>>>

>> Looking at your code it looks like you need to remove the first case
>> statement and use something like a while loop. From what I can see it
>> looks like when you select '2' it changes 'startstate' to 1 and then
>> exits the case statement. An alternative is to set startstate in the
>> main program and then call startUp with the current startstate -- i.e. x
>> = startUp(startstate). Then test x to see if you want to call startUp
>> again or exit the program.

>
> Wrapped a call to startUp() into the main game loop and made startstate
> global....it's working like a charm now! =) Thanks a billion!
>


Glad to see you got it working. I think what you were trying to do
would have worked in C as the case statements keep executing until they
hit a break.
Reply With Quote
  #7  
Old 08-27-2008, 10:39 PM
Chris Bailey
Guest
 
Default Re: Having a difficult time with Case statements.

Michael W. Ryder wrote:
> Chris Bailey wrote:
>>> exits the case statement. An alternative is to set startstate in the
>>> main program and then call startUp with the current startstate -- i.e. x
>>> = startUp(startstate). Then test x to see if you want to call startUp
>>> again or exit the program.

>>
>> Wrapped a call to startUp() into the main game loop and made startstate
>> global....it's working like a charm now! =) Thanks a billion!
>>

>
> Glad to see you got it working. I think what you were trying to do
> would have worked in C as the case statements keep executing until they
> hit a break.


Yeah I was expecting them to continue until told to quit! Is there a
way to repeat cases in ruby without dancing around it?

--
Posted via http://www.ruby-forum.com/.

Reply With Quote
  #8  
Old 08-27-2008, 10:55 PM
Marc Heiler
Guest
 
Default Re: Having a difficult time with Case statements.

> Yeah I was expecting them to continue until told to quit! Is there a
> way to repeat cases in ruby without dancing around it?


There probably are but my personal habit these days is to use two
methods

one that handles the whole loop like so

loop {
result = get_user_input
break if result == :exit
}

and in that other method call it again and again until it returns
:exit (or evaluate other results if needed).
In my experience everything can be rearranged to be in some methods
which can be controlled and called in any way that is needed to
get the desired functionality. Simply make a method for the case
menu and invoke it again if you need it, testing your input
against it choices.
--
Posted via http://www.ruby-forum.com/.

Reply With Quote
  #9  
Old 08-28-2008, 06:23 AM
Sebastian Hungerecker
Guest
 
Default Re: Having a difficult time with Case statements.

Michael W. Ryder wrote:
> I think what you were trying to do
> would have worked in C as the case statements keep executing until they
> hit a break.


In C if there is no break it will fall through, yes, but that would hardly
result in the behaviour the OP wanted. Take this for example:

#include <stdio.h>
int main(int argc, char** argv) {
int i = 0;
switch(i) {
case 0:
i = 2;
case 1:
printf("i is one (except it's not)\n");
case 2:
break;
}
return 0;
}

I assume the OP would want that to execute the 0 case and then the 2 case, but
it executes all three. I also assume that if the 2 case would set i to 0
again, the OP would want it to start over again, which it also won't.

HTH,
Sebastian
--
Jabber: sepp2k@jabber.org
ICQ: 205544826

Reply With Quote
  #10  
Old 08-28-2008, 02:58 PM
Michael W. Ryder
Guest
 
Default Re: Having a difficult time with Case statements.

Sebastian Hungerecker wrote:
> Michael W. Ryder wrote:
>> I think what you were trying to do
>> would have worked in C as the case statements keep executing until they
>> hit a break.

>
> In C if there is no break it will fall through, yes, but that would hardly
> result in the behaviour the OP wanted. Take this for example:
>
> #include <stdio.h>
> int main(int argc, char** argv) {
> int i = 0;
> switch(i) {
> case 0:
> i = 2;
> case 1:
> printf("i is one (except it's not)\n");
> case 2:
> break;
> }
> return 0;
> }
>
> I assume the OP would want that to execute the 0 case and then the 2 case, but
> it executes all three. I also assume that if the 2 case would set i to 0
> again, the OP would want it to start over again, which it also won't.
>


From the looks of the code that I was tracing selecting '2' would set
the state to '1' which was the next step in the case statement.
Obviously this would not have worked for every selection but it looked
like he was trying to use C's case statement behavior.


> HTH,
> Sebastian

Reply With Quote
Reply


Thread Tools
Display Modes


All times are GMT -5. The time now is 08:47 PM.


Powered by vBulletin® Version 3.7.2
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0
vB Ad Management by =RedTyger=

In an effort to better serve ads to our visitors, cookies are used on objectmix.com. For more information, check out our Privacy Policy.