|
|
|||||||||
|
|||||||||
| |||||||||
|
|
|
| ||||||||||||||||||||||||||
![]() |
|
|
«
Previous Thread
|
Next Thread
»
|
Thread Tools | Search this Thread | Rate Thread | Display Modes |
|
#1
|
|||
|
|||
|
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* |
|
#2
|
|||
|
|||
|
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 |
|
#3
|
|||
|
|||
|
*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. (: |
|
#4
|
||||
|
||||
|
Quote:
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:
Ruby runs on most platforms. Make sure you do not do platform-specific code (such as some shell commands) and you should be alright. |
|
#5
|
|||
|
|||
|
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. |
![]() |
| Viewing: Dev Shed Forums > Programming Languages > Ruby Programming > An easier method than tons of elseifs? |
| Thread Tools | Search this Thread |
| Display Modes | Rate This Thread |
|
|
|
|
|