New to Classes

S

Scott Andrechek

Hello. I'm new to making and defining your own classes (well really to
Ruby in general)and need some of your help with areas of my code. Here
is the code for the game:
class Die

def roll
@numberShowing = 1 + rand(6)
end

def showing
@numberShowing
end

end

dice = [Die.new]
play=true
money= 100
while play==true
puts 'You have $' + money.to_s + '. What is your bid?'
bid= gets.chomp
puts '$' + money.to_s + '-' + '$' + bid.to_s
moneytest= money.to_i - bid.to_i
while moneytest<=0
puts 'Please enter a valid bid'
bid=gets.chomp
moneytest= money.to_i - bid.to_i
end
money=(money.to_i - bid.to_i)
puts '$' + money.to_s + ' remaining'
puts 'What number would you like to place your bet of ' + '$' + bid + '
on?'
number= gets.chomp
puts 'Dice roll was....'
die = Die.new
die.roll
puts die.showing
if @numberShowing.to_i==number.to_i
puts 'Congrats! You made ' + '$' + bid.to_s
else
puts 'Sorry. You lost ' + '$' + bid.to_s
end
if (money==0 or money<=0)
puts 'Sorry you lose'
play==false
end
end

The problems I'm having with it (as you will see if you test it) is that
you cannont win money, which I assume is because of my handling of
@numberShowing, and that you can't go 'all in'. Any help would be
greatly appreciated.

-Scott
 
M

Michael W. Ryder

Scott said:
Hello. I'm new to making and defining your own classes (well really to
Ruby in general)and need some of your help with areas of my code. Here
is the code for the game:
class Die

def roll
@numberShowing = 1 + rand(6)
end

def showing
@numberShowing
end

end

dice = [Die.new]
play=true
money= 100
while play==true
puts 'You have $' + money.to_s + '. What is your bid?'
bid= gets.chomp
puts '$' + money.to_s + '-' + '$' + bid.to_s
moneytest= money.to_i - bid.to_i
while moneytest<=0
puts 'Please enter a valid bid'
bid=gets.chomp
moneytest= money.to_i - bid.to_i
end
money=(money.to_i - bid.to_i)
puts '$' + money.to_s + ' remaining'
puts 'What number would you like to place your bet of ' + '$' + bid + '
on?'
number= gets.chomp
puts 'Dice roll was....'
die = Die.new
die.roll
puts die.showing
if @numberShowing.to_i==number.to_i

Why are you using @numberShowing instead of die.showing? If you print
@numberShowing here I think you will see your problem.
 
S

Scott Andrechek

Michael said:
Why are you using @numberShowing instead of die.showing? If you print
@numberShowing here I think you will see your problem.

Thanks. You can now win money. Unfortanatley you still can't go 'all
in'. Hopefully a solution will come up...

-Scott
 

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

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,189
Members
46,735
Latest member
HikmatRamazanov

Latest Threads

Top