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.
class MoneyTree
def initialize
end
def stats
end
def countMoney
end
def itemOptions
end
def harvest harvestAmount
end
def timePasses
end
def options
end
def choice selection
end
end
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.
def initialize
#HEIGHT AND AGE
@height = 120
@age = 5
#SEASONS
@seasons = ["Spring", "Summer", "Fall", "Winter"]
@currentSeason = 3
#LEAVES
@springLeaves = 0
@summerLeaves = 0
@moneyCount = 0
#MONEY
@money
#ITEMS
@irrigation = false
@bugSpray = false
@fertilizer = false
#NATURAL DISASTERS
@droughtCount = 0
#CALL THE "OPTIONS" METHOD
options
end
Now, I'll do the next easiest part, which will be the options
and choice
methods.
def options
puts "What would you like to do?"
puts "[1] Current Status"
puts "[2] Count Money"
puts "[3] Harvest"
puts "[4] Buy Items"
puts "[5] Pass Time"
puts "[6] Leave"
#get the typed selection
selection = gets.chomp.to_i #convert the String to an Integer
#call the choice method with the selection variable as its parameter
choice selection
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.
def choice selection
#start a switch statement with the parameter as the looked-at number
case selection
when 1
stats #call the "stats" method
when 2
countMoney #call the "countMoney" method
when 3
puts "How much would you like to harvest?"
puts "[1] Some of it"
puts "[2] All of it"
if gets.chomp == "1"
puts How much would you like to harvest?"
harvestAmount = gets.chomp.to_i
#call the "harvest" method with the variable harvestAmount as the parameter
harvest harvestAmount
elsif gets.chomp == "2"
#call the "harvest" method with @moneyCount as the parameter
harvets @moneyCount
else
puts "That is not a valid option."
options #call the "options" method
end
when 4
itemOptions #call the "itemOptions" method
when 5
timePasses #call the "timePasses" method
when 6
exit() #leave
else
puts "That is not a valid option."
options call the "options" method
end
end
There's is still a lot of coding left to do, but that will have to wait until next time.
Nov 12, 2009