Re: Random number help

Wnat's the error?I suspect it is related to

if (A = 2)

That should be

if (A == 2)

And the same for 3, and 4 conditions. You are assigning the value of 2 to A in your if condition rather than comparing it to the value of A.You might also want to look at using the case statement rather than if elses

case A
when 2
puts "A is 2 between"
when 3
puts "It's 3"
else
puts "You gave me #{a} -- I have no idea what to do with that."
end

What version of ruby are you running?You might want to try

r = Random.new
A = r.rand(2...4)

What you want and what you need are too often not the same thing!When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.(Quote by me 15th July 2009)

Re: Random number help

Dynamic constant assignment means that you are trying to assign a dynamic value to a constantA constant is obviously not variable as it's constant Curious as to why you chose to use a constant "A" rather than a variable "a" ?

Try

a = rand(2..4)
case a

Actually Ruby should have just produced a warning rather than an error so there is obviously some difference between the version of ruby you are using and what I'm used to using

And yes you are correct about 2..4 - use whatever values you need

What you want and what you need are too often not the same thing!When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.(Quote by me 15th July 2009)

Re: Random number help

Yeah I just figured that out. However I'm still pretty sure I'm doing this wrong. I got a reply fromthe Ruby monk people, although they did help it really wasn't the fix I was looking for.

Just for reference purposes:

Hey!
Great to see your attempt. Can you tell me more about what you are trying to achieve? Unfortunately, it isn't readily apparent to me from the code.
But here are some recommendations to make your code follow Ruby standards better:
method names must be snakecase. Like this_is_a_snake_case. The letters are lower case and multiple words are separated by an underscore. So, def Game_Event becomes def game_events
Class names does not have underscores. Words are separated by upper case letters. Like so: class RandomMaps
only constants start with uppercase letters. Normal variable names must always be in snakecase. So, A = rand(2..4) should ideally be something like: move = rand(2..4)
008 is an invalid octal. In Ruby, if you start a number with 0, it assumes it is an octal (http://en.wikipedia.org/wiki/Octal). Octal can only have a range from 0-7. I'm assuming you haven't tried running this code - if you do, it would raise an error.
$game_player is not recommended. It is a global variable. Global variables means global state. A fundamental programming best practice is to avoid unbounded state as much as possible.
I've written an unconsidered snippet of code to try approaching your requirement. I'm trying to approximate your solution as much as possible. The details will be wrong, please bear with me!
class World
# the intention of these constants are unclear to me. Giving them some arbitrary values.
VILLAGE = "village"
MAP003 = "map3"
MAP004 = "map4"
UP = "direction-up"
DOWN = "direction-down"
attr_reader :map
def initialize(map)
@map = map
end
def new_turn
move = rand(4)
# I'm not quite sure what this signifies. We can work on this if you elaborate this part.
case move
when 2
map.reserve_transfer(VILLAGE, 36, 23, UP)
when 3
map.reserve_transfer(MAP003, 8, 2, DOWN)
when 4
map.reserve_transfer(MAP004, 14, 4, DOWN)
end
end
end
class Map
def initialize
end
def reserve_transfer(who, x, y, direction)
puts "Transferred #{who} by #{x}, #{y} in #{direction}"
end
end
world = World.new(Map.new)
world.new_turn
world.new_turn
world.new_turn
Try copy-pasting the above code to a Ruby file and run it. Or even better, go to http://rubymonk.com/learning/books/1/chapters/5-strings/lessons/31-string-basics, and paste this code in the first example. You can use RubyMonk itself to play around with this code!
Let me know what you think of this, also try to give me a detailed idea of you have in your mind. Hope this helps!

Re: Random number help

Well, it seems like you are being offered all the help you need from people who understand the specific nature of your app far better than I do.The best advice ?I can give you right now is to go ahead and reply with a detailed explanation of what you are trying to achieve within the case statement and see what they come back with

What you want and what you need are too often not the same thing!When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.(Quote by me 15th July 2009)