Ruby Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsProgramming LanguagesRuby Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old March 17th, 2009, 11:09 PM
KStew308 KStew308 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Posts: 4 KStew308 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 8 m 31 sec
Reputation Power: 0
An easier method than tons of elseifs?

Hello DevShed Forumers:

I've only just started on Ruby today, just to get that out of the way. (: I like to play around with various languages, including a jungle gym of C/C++, a Python slide, and a Lua teeter-totter. There is also a PHP sandbox, if that counts as a language, over there that I started in and grew out of when I got bored with web design/scripts. *lol*
I'm a stubborn kind of learner, liking to start with glancing over someone else's work then sit down with a cup of coffee and just fiddle and tweak, with small pauses to Google a curiosity or roadblock. I learn best by fiddling with examples.
Well, about two or so hours ago, I decided to wade out into the waters to start on an idea I've had floating in my head. Things are going all right, but my problem is...
I am an ugly coder. (: I hesitate to label myself as a coder. I'm more like a nano headbanger.
If any of you experienced, *real* coders out there know how to help me make this look prettier, I'd appreciate this. (Wow, I'm as ugly of a coder as I am a rambler. Sorry!)

The basic gist is that I have an array with 16 values (0 - 15) that make up a 4 x 4 grid. It then asks for a row and a column. After checking to make sure the input is valid, it then moves on to making sure the move is valid. Sort of like chess or checkers, I guess.
For example, you start at a[0], which is 1 x 1. You can only move to 2 x 1 (a[4]) or 1 x 2 (a[1]). You input 1 for the row and 2 for the column. The script translates that into the appropriate array number and changes your position to a[1].
However, if you input 2 and 2, from your current position, that is an invalid move. I tried to make an if, elseif, elseif, elseif... etc. method to prevent invalid moves.

Since I'm still learning, there's a lot that I don't know about yet. I'm hoping one of you here might know some better system or method to use that'd make this if/elseif big mac into one of those cute little thickburgers. (:

Thanks for any and all advice! Sorry for all the rambling. *lol* It's probably the reason why my code always takes up so many lines... (;

[EDIT] Okay, the code I posted was invalid anyway, so I sat down (slightly miffed) and instead came up with this route. I shortened it so I don't take up a lot of space, this is just the general idea:

Code:
 case currpos
  when 0:
	pos[0] = 1
	pos[1] = 4
  when 1,2:
	pos[0] = currpos - 1
	pos[1] = currpos + 1
	pos[2] = currpos + 4
  when 5,6,9,10:
	pos[0] = currpos - 1
	pos[1] = currpos + 1
	pos[2] = currpos - 4
	pos[3] = currpos + 4
 end

 if currpos == newpos
	puts "But you're already standing here!"
 elseif newpos == pos[0] || newpos == pos[1] || newpos == pos[2] || newpos == pos[3]
	puts "You move to your new position."
 else
	puts "Are you sure you can move there?"
 end


Any ways to make this shorter, or is this the best I can do? If the whole code is needed, I'll post it, but just this garble is probably cringe-worthy as it is... *lol*

Reply With Quote
  #2  
Old March 18th, 2009, 08:07 AM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 795 L7Sqr User rank is Captain (20000 - 30000 Reputation Level)L7Sqr User rank is Captain (20000 - 30000 Reputation Level)L7Sqr User rank is Captain (20000 - 30000 Reputation Level)L7Sqr User rank is Captain (20000 - 30000 Reputation Level)L7Sqr User rank is Captain (20000 - 30000 Reputation Level)L7Sqr User rank is Captain (20000 - 30000 Reputation Level)L7Sqr User rank is Captain (20000 - 30000 Reputation Level)L7Sqr User rank is Captain (20000 - 30000 Reputation Level)L7Sqr User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 15 h 33 m 41 sec
Reputation Power: 233
Send a message via AIM to L7Sqr
First, kudos to you for exploring on your own; too many people expect to just have knowledge dropped on them. I wish more of my co-workers were like you (I'd take the rambling any day).

The way you have it is pretty decent. It is a standard way to do this; check edge cases, allow general rules for everything else. You may want to allow things to be more general though - what happens if you extend your grid to larger than 4x4? Then that case statement is no longer going to work.

It might be overkill for your particular case, but I would likely wrap the currpos into a class that could better represent position. Something like
Code:
class Position
   def initialize(size)
      @x, @y = 0,0
      @grid_height = @grid_width = size
   end

   def move_up
      unless @y < 1
         @y -= 1
      end
   end

   def move_down
      unless @y >= (@grid_height - 1)
         @y += 1
      end
   end

   # ... and so on ...
end


That way, in your main logic you can do something like this:
Code:
position = Position.new(4)
while move = gets
   move.chomp!
   case move
      when '^'
         position.move_up
      when 'v'
         position.move_down
      # ...
   end

   canvas.paint(position)
end

Now, obviously the above is not complete code. gets will accept any number of characters and I make no checks against what is returned. The canvas object is not defined; it exists only as an example of how wrapping the position in a class would allow you to print the grid easier than in your current implementation. And so on...

It is usually a good idea to wrap functionality in a class when appropriate. It avoids duplicating logic all over your code (which leads to subtle bugs), it helps separate the behavior of the parts of your program into distinct elements, and it tends to more maintainable code.
__________________
Some people have 20 years of experience. Some have 1 year of experience 20 times.

My personal site: Basic geek randomness

Reply With Quote
  #3  
Old March 18th, 2009, 08:38 AM
KStew308 KStew308 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Posts: 4 KStew308 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 8 m 31 sec
Reputation Power: 0
*lol* Thank you for the kudos; I do my best. (:

Interesting! So that's how classes worked... I tried to look at some examples, but none of them made much sense to me. Question, though - the initialize, does it run every time the position class is called on? Or does that just acknowledge that you start at 0,0? (x and y coordinates... duh! I should have remembered those classic terms! I'm an English major, not a Math major, so that's my excuse.)

Thank you so much. After the coffee's ready, I'll sit down and start playing with that. It'd be nice to get these class things mastered anyway, so thanks for inadvertently forcing me to finally sit down with them. (; I appreciate all of this!

[EDIT] Oh - I haven't found a definite answer one way or the other, but if my operating system is Windows and I use Cygwin to code with, would the code be portable to a Linux system eventually? I'm rather fond of Cygwin since it lets me do everything on one computer, but I read somewhere that using it handicaps you in the end. I don't see how/why it would, but... any ideas/knowledge? Thanks in advance. (:

Reply With Quote
  #4  
Old March 18th, 2009, 09:42 PM
L7Sqr L7Sqr is offline
Contributing User
Dev Shed Novice (500 - 999 posts)
 
Join Date: Jan 2004
Location: Constant Limbo
Posts: 795 L7Sqr User rank is Captain (20000 - 30000 Reputation Level)L7Sqr User rank is Captain (20000 - 30000 Reputation Level)L7Sqr User rank is Captain (20000 - 30000 Reputation Level)L7Sqr User rank is Captain (20000 - 30000 Reputation Level)L7Sqr User rank is Captain (20000 - 30000 Reputation Level)L7Sqr User rank is Captain (20000 - 30000 Reputation Level)L7Sqr User rank is Captain (20000 - 30000 Reputation Level)L7Sqr User rank is Captain (20000 - 30000 Reputation Level)L7Sqr User rank is Captain (20000 - 30000 Reputation Level) 
Time spent in forums: 1 Week 6 Days 15 h 33 m 41 sec
Reputation Power: 233
Send a message via AIM to L7Sqr
Quote:
Originally Posted by KStew308
Question, though - the initialize, does it run every time the position class is called on? Or does that just acknowledge that you start at 0,0?

initialize is how you define what happens when an object is initialized (via the new method).
For example:
Code:
#!/usr/bin/env ruby

class Foo
        def initialize(i)
                puts "I am in initialize with input: #{i}"
                @i = i
        end
        def bar
                puts "I am in bar. My variable: #@i"
        end
end

foo = Foo.new(42)
foo.bar

baz = Foo.new(100)
baz.bar

And the output
Quote:
Originally Posted by output
I am in initialize with input: 42
I am in bar. My variable: 42
I am in initialize with input: 100
I am in bar. My variable: 100


Ruby runs on most platforms. Make sure you do not do platform-specific code (such as some shell commands) and you should be alright.

Reply With Quote
  #5  
Old March 18th, 2009, 11:16 PM
KStew308 KStew308 is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2009
Posts: 4 KStew308 User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 2 h 8 m 31 sec
Reputation Power: 0
Okay, I got you! Thanks a lot, on both the initialize clarification and the platform query. (: I think I finally understand classes now. *lol* They're handy!

Thank you for your help and patience. (: I'll be sure to pop back in if I hit another roadblock Google can't help me with! So far, I'm really enjoying Ruby.

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesRuby Programming > An easier method than tons of elseifs?


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump




 Free IT White Papers!
 
How to Present Effectively Online
This white paper offers practical and actionable advice on the key steps that any presenter should consider as they plan and execute a Webinar or online meeting.

 
Open Source Security Myths
Open Source Software (OSS) is computer software whose source code is available to the general public with relaxed or non-existent intellectual property restrictions (or arrangement such as the public domain), and is usually developed with the input of many contributors.

 
Power and Cooling Capacity Management for Data Centers
This paper describes the principles for achieving power and cooling capacity management.

 
Scalable, Fault-Tolerant NAS for Oracle - The Next Generation
For several years NAS has been evolving as a storage alternative for Oracle databases, and for good reason: NAS is quite often the simplest, most cost-effective storage approach for Oracle. Learn about the benefits that HP's approach to scalable NAS brings to Oracle environments in this comprehensive white paper.

 
Understanding Web Application Security Challenges
This white paper discusses many common threats and preventive measures for Web application security, and explains what you can do to help protect your organization.

 

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 




© 2003-2009 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
Stay green...Green IT