# Help! Please, I'm lost!

Well, I’m a spanish user, so I’m sorry if my English isn’t good
enough.Here`s my problem:
I’m a total newbie in Ruby (just finished Hackety Hacks) and I want to
develop a program like this:
Let’s have a dice. Written on the dice, there are six values: - - 0 0 +
+.
The “minus” value substracts one point, the “zero” doesn’t do anything
at all, and the “plus” value adds one point.
I want to create a program that asks the user how many dices wants to
roll and roll them, showing the values (not math values, but printed
values instead) and the result of adding every value.
Optionally, I want the program to ask the user if he/she wants only the
positive rolls to count, if possible.
Can anybody help me, please? I don’t have any idea…and I need this
program.

On Sun, Jan 23, 2011 at 8:56 PM, Dark H.
[email protected] wrote:

Let’s have a dice. Written on the dice, there are six values: - - 0 0 +
+.
The “minus” value substracts one point, the “zero” doesn’t do anything
at all, and the “plus” value adds one point.
I want to create a program that asks the user how many dices wants to
roll and roll them, showing the values (not math values, but printed
values instead) and the result of adding every value.

Do you want hints to solve it yourself, or the answer? Here are some
hints:

1. Use an array to represent each die

2. The way to get a random number out of an array:

r = rand(array.length) - 1
random_value = array[r]

martin

Martin DeMello wrote in post #976930:

Do you want hints to solve it yourself, or the answer? Here are some

martin

The answer well explained for a newbie, plz. Thanks.

Hello,

just a hint for the interactive part: have a look at the
highline gem (http://lmgtfy.com/?q=Ruby+HighLine)

Have fun!

On Sun, Jan 23, 2011 at 4:26 PM, Dark H.

Well I tried that code but, when I try to run it, Shoes freezes and RDE
doesn’t do anything at all… What am I doing wrong?

On Sun, Jan 23, 2011 at 9:15 PM, Dark H. >

The answer well explained for a newbie, plz. Thanks.

Here you go. We make a class to represent a die, with methods to roll
it and display the top face.

# represent a die

class Die
FACES = [-1, -1, 0, 0, 1, 1]

# we will let the value be read, but not written to

def initialize
# to begin with the die has not been rolled, and
# hence has no value
@value = nil
end

# set the value to a random face

def roll
r = rand(6)
@value = FACES[r]
end

# return the face value of the die as a string

def display_value
case @value
when -1; “-”
when 0; “0”
when 1; “+”
else “NOT ROLLED!”
end
end
end

# loop until a valid number is entered

n = 0
while true do
puts “How many dice do you want to roll?”
number = gets
n = number.to_i # convert string to integer
if n > 0
break
end
end

# this creates an array of n Die objects

dice = Array.new(n) { Die.new }

# roll the dice

dice.each {|die| die.roll}

# display and total the dice

total = 0
dice.each_with_index do |die, i|
total += die.value
puts “Die number #{i} shows: #{die.display_value}”
end

puts
puts “Your total score is #{total}”

On Sun, Jan 23, 2011 at 10:00 PM, Dark H.
[email protected] wrote:

Well I tried that code but, when I try to run it, Shoes freezes and RDE
doesn’t do anything at all… What am I doing wrong?

it’s pure ruby code, not shoes. not sure why rde doesn’t do anything,
it’s probably the call to gets. try running it from the command line,
it should work fine there.

martin

Martin DeMello wrote in post #976948:

On Sun, Jan 23, 2011 at 10:00 PM, Dark H.
[email protected] wrote:

Well I tried that code but, when I try to run it, Shoes freezes and RDE
doesn’t do anything at all… What am I doing wrong?

it’s pure ruby code, not shoes. not sure why rde doesn’t do anything,
it’s probably the call to gets. try running it from the command line,
it should work fine there.

martin

It isn’t. When I try to run it from console, it asks for the number of
dices, and no matter the number I put, it returns “nil”. Thank you for
all your help, and please excuse me, I’m a total newbie.
BTW: Which IDE do you recommend for Ruby?

KO, sorry for the double posting, but I can’t edit my last message. I
finally made it work in Netbeans, so REALLY thank you for your help. Now
all that remains is change the puts command for a command that show
dialog boxes and compile the app into an exe. THANKS!

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.