A Simple Program in Ruby

Day Two

Last time I exlained what the tree would do, now I'm going to how you how it does it.

The first thing I'm going to do is figure out exactly how I want the program to work. For that, I'll write out the skeleton of the program before I do any real programming.

  1. class MoneyTree

  2. def initialize

  3. end

  4. def stats

  5. end

  6. def countMoney

  7. end

  8. def itemOptions

  9. end

  10. def harvest harvestAmount

  11. end

  12. def timePasses

  13. end

  14. def options

  15. end

  16. def choice selection

  17. end

  18. end

  19. tree = MoneyTree.new

I've decided to make the tree in a class. The initialize method will initialize my variables; stats will print out the current status; countMoney will tell how much the leaves on the tree are worth; itemOptions will be the "store"; harvest will take the parameter, harvestAmount and will remove leaves and turn them into money; timePasses will control what happens whenever seasons change; options will display the options and get user input; choice will take the parameter, selection, from the previous method and will work as a switch to call the appropriate method. I also created an instance of the class.

I'm going to start with the initialize method. Since I've already worked out what and how I want to keep track of things, I can start writing my instance variables. For readibility's sake, I'm going to write this in the final form of the program, instead of the jumbled mess it originally was.

  1. def initialize

  2. #HEIGHT AND AGE

  3. @height = 120

  4. @age = 5

  5. #SEASONS

  6. @seasons = ["Spring", "Summer", "Fall", "Winter"]

  7. @currentSeason = 3

  8. #LEAVES

  9. @springLeaves = 0

  10. @summerLeaves = 0

  11. @moneyCount = 0

  12. #MONEY

  13. @money

  14. #ITEMS

  15. @irrigation = false

  16. @bugSpray = false

  17. @fertilizer = false

  18. #NATURAL DISASTERS

  19. @droughtCount = 0

  20. #CALL THE "OPTIONS" METHOD

  21. options

  22. end

Now, I'll do the next easiest part, which will be the options and choice methods.

  1. def options

  2. puts "What would you like to do?"

  3. puts "[1] Current Status"

  4. puts "[2] Count Money"

  5. puts "[3] Harvest"

  6. puts "[4] Buy Items"

  7. puts "[5] Pass Time"

  8. puts "[6] Leave"

  9. #get the typed selection

  10. selection = gets.chomp.to_i #convert the String to an Integer

  11. #call the choice method with the selection variable as its parameter

  12. choice selection

  13. end

The options method is very simple, all it does is print options and take a value. The inputted value is converted into a number because of the switch statement used in the choice method.

  1. def choice selection

  2. #start a switch statement with the parameter as the looked-at number

  3. case selection

  4. when 1

  5. stats #call the "stats" method

  6. when 2

  7. countMoney #call the "countMoney" method

  8. when 3

  9. puts "How much would you like to harvest?"

  10. puts "[1] Some of it"

  11. puts "[2] All of it"

  12. if gets.chomp == "1"

  13. puts How much would you like to harvest?"

  14. harvestAmount = gets.chomp.to_i

  15. #call the "harvest" method with the variable harvestAmount as the parameter

  16. harvest harvestAmount

  17. elsif gets.chomp == "2"

  18. #call the "harvest" method with @moneyCount as the parameter

  19. harvets @moneyCount

  20. else

  21. puts "That is not a valid option."

  22. options #call the "options" method

  23. end

  24. when 4

  25. itemOptions #call the "itemOptions" method

  26. when 5

  27. timePasses #call the "timePasses" method

  28. when 6

  29. exit() #leave

  30. else

  31. puts "That is not a valid option."

  32. options call the "options" method

  33. end

  34. end

There's is still a lot of coding left to do, but that will have to wait until next time.


Nov 12, 2009 Ruby, Programming, Code Examples