2

I am a newbie in Ruby. I am trying to write a simple interactive game in ruby but am stuck. Need help to proceed. Below is the concept:

money         = [100, 200, 300, 400]
beneficiaries = ["John", "Sam", "Mary"]
cost1         = [50, 25, 30, 75, 18]
cost2         = [120, 150, 200, 250]
gift1         = [" crayon ", " Pencil ", " Biro "]
gift2         = [" bag ", " shoe ", " Radio "]
cashowned     = [50]
a             = money.sample
b             = cost1.sample
c             = cost2.sample
d             = gift1.sample
e             = gift2.sample
f             = cashowned
puts " Hi, what is your name? "
puts
name = gets.chomp
puts
puts " #{name} is a nice name."
puts
puts "#{name} you have #{f} in your bank account."
puts
puts "Roll the dice and let's see how much you just earned."
puts
gets.chomp.to_i
dice1 = a
puts "#{name} you just earned: #{dice1}"
puts
gets.chomp.to_i
Cashowned = dice1 + f
puts "Your account balance is now : #{cashowned}"
puts
gets.chomp.to_i

I am stuck here. I want to repeat/loop the sequence so that I can give out gift at a cost and deduct from cash owned. But cash owned is not updating. A simpler way to go about this will be appreciated. Thanks.

3 Answers 3

1

Ruby is a case sensitive language.

Fist you declared your variable as:

cashowned = [50]

and later you are trying to update using another variable name:

Cashowned = dice1 + f

note that "cashowned" and "Cashowned" are different variables

UPDATE:

Line 7, you have:

cashowned = [50] 

my question is, why not use only:

cashowned = 50

?

As you are doing will cause the error: Array can't be coerced into Fixnum (TypeError)

You are trying to sum a Fixnum with an Array.

See the code:

money = [100, 200, 300, 400]
beneficiaries = ["John", "Sam", "Mary"]
cost1 = [50, 25, 30, 75, 18]
cost2 = [120, 150, 200, 250]
gift1 = ["crayon", "Pencil", "Biro"]
gift2 = ["bag", "shoe", "Radio"]
cashowned = 50
a = money.sample
b = cost1.sample
c = cost2.sample
d = gift1.sample
e = gift2.sample
f = cashowned
puts "Hi, what is your name?"
name = gets.chomp
puts "#{name} is a nice name."
puts "#{name} you have #{f} in your bank account."
puts "Roll the dice and let's see how much you just earned."
gets.chomp.to_i
dice1 = a
puts "#{name} you just earned: #{dice1}"
gets.chomp.to_i
cashowned = dice1 + f
puts "Your account balance is now : #{cashowned}"
gets.chomp.to_i

And See the test result:

enter image description here

2
  • Thanks for that. My main project is ok. I had typos only in the posted question. Still I cant update that variable. It is keeping its first value.
    – ZamZamZam
    Aug 16, 2015 at 20:50
  • Ok I give up. Anyone with better idea of how to code this simple game? You roll a dice and you get rewarded from an array that selects the reward at random. Reward will be added to cash owned. Then at intervals, instead of getting a reward, you get an expense to be deducted from your earnings. The circle keeps going till you reach a predefined maximum figure or bankruptcy. Any help Pls.
    – ZamZamZam
    Aug 16, 2015 at 21:23
0

If you want to update/access value in an array, you need to use index operator

cashowned[0] = dice1 + f[0]
0

Here is a simple game I've made. It's not perfect as I've rushed it, but it works and shows you what type of code I think you are asking for. Pay attention to the while loop and if statements within the while loops.

#set your variables

b = 200 #initial balance
br = -500 #account limit
dice = [1, 2, 3, 4, 5, 6]

puts "State name?"
n = gets.chomp
puts ""
puts "Greetings #{n}"
puts ""
puts "============================"
puts "Your initial balance is #{b}"
puts "============================"
puts ""
puts "If it reaches or exceeds #{br}"
puts "you'll be declared bankrupt!"
puts ""


while b > br do

#rolling the positive di   
   puts "Roll good di? (y/n)"
   puts ""
        a = gets.chomp.downcase

        if a == "y"
            pd = dice.sample*100
            b += pd 
            puts ""
            puts "****you scored #{pd}****"
            puts "============================"
            puts" new balance: #{b}"
            puts "============================"
            puts ""
        else
            puts "============================"            
            puts "your balance remains at #{b}"
            puts "============================"            
            puts ""
        end


#rolling the negative di  
   puts "Roll evil di? (y/n)"
   puts ""
   ans = gets.chomp.downcase

        if ans == "y"
            nd = dice.sample*-100
            b += nd
            puts ""
            puts "****you scored ****#{nd}****"
            puts "============================"
            puts "new balance: #{b}" 
            puts "============================"
            puts ""
        else
            puts "============================"            
            puts "your balance remains at #{b}"
            puts "============================"            
            puts ""
        end


#option to repeat
   puts ""
   puts "Continue playing (y/n)?"
   puts ""
   ans = gets.chomp.downcase
   puts ""

        if ans == "n" 
            puts ""
            puts "============================"            
            puts "final balance is #{b}"
            puts "============================"            
            puts ""        
            exit

        else
            b
        end

end

puts "You have now reached/exceeded your limit"
puts "of #{br}, so your account has been frozen"
puts ""

Run this code and have a look and modify it to suit your needs.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.