Help! Please, I'm lost!

D

Dark Haukka

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.
 
M

Martin DeMello

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
 
B

Bernard Lambeau

[Note: parts of this message were removed to make it a legal post.]

Hello,

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

Have fun!

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.


--
PhD Student, Computer Science Department, EPL/INGI, UCLouvain, Belgium
Mail: (e-mail address removed)
Mobile: +32 477 24 58 61
Blog: http://revision-zero.org/
Code: http://github.com/blambeau/
Follow: http://twitter.com/blambeau/
 
M

Martin DeMello

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
attr_reader :value

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

# play the game

# 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}"
 
D

Dark Haukka

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?
 
M

Martin DeMello

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
 
D

Dark Haukka

Martin DeMello wrote in post #976948:
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.
 
D

Dark Haukka

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!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Could someone please help me with this javascript exercise? 5
Need help again please 19
Help With Homework Code 10
Neel Help 3
Ruby/tk Help Please 19
Please help with this 8
help me please 2
[SUMMARY] Lost Cities (#51) 0

Members online

Forum statistics

Threads
473,997
Messages
2,570,239
Members
46,827
Latest member
DMUK_Beginner

Latest Threads

Top