Skip to content

Instantly share code, notes, and snippets.

@dengsauve
Last active April 19, 2017 16:45
Show Gist options
  • Save dengsauve/29fa7e2207f0a358cefeec8d2e93e294 to your computer and use it in GitHub Desktop.
Save dengsauve/29fa7e2207f0a358cefeec8d2e93e294 to your computer and use it in GitHub Desktop.
CIS283 Ruby Various Classes Assignment
############################################################
#
# Name: Dennis Sauve
# Date: 04/16/17
# Assignment: Ruby Classes
# Class: CIS283
# Description: A Program that implements ruby classes to
# store object information. This is an
# exercise in learning about the nature of
# Classes, and therefore the attr_accessor
# cannot be used. Lots of typing...
#
############################################################
class Person
def initialize(first_name, last_name, age, hair_color, eye_color)
@first_name = first_name
@last_name = last_name
@age = age
@hair_color = hair_color
@eye_color = eye_color
end
def first_name() @first_name end
def last_name() @last_name end
def age() @age end
def hair_color() @hair_color end
def eye_color() @eye_color end
def first_name=(first_name) @first_name = first_name end
def last_name=(last_name) @last_name = last_name end
def age=(age) @age = age end
def hair_color=(hair_color) @hair_color = hair_color end
def eye_color=(eye_color) @eye_color = eye_color end
def to_s
"Name: #{@first_name} #{@last_name}, Age: #{@age.to_s}, Hair Color: #{@hair_color}, Eye Color: #{@eye_color}"
end
end
class Address
def initialize(line1, line2=nil, city, state, zip)
@line1 = line1
@line2 = line2
@city = city
@state = state
@zip = zip
end
def line1() @line1 end
def line2() @line2 end
def city() @city end
def state() @state end
def zip() @zip end
def line1=(line1) @line1 = line1 end
def line2=(line2) @line2 = line2 end
def city=(city) @city = city end
def state=(state) @state = state end
def zip=(zip) @zip = zip end
def to_s
"#{@line1}#{"\n#{@line2}" if @line2}\n#{@city}, #{@state} #{@zip.to_s}"
end
end
class Character
def initialize(name, race, hit_points, gold)
@name = name
@race = race
@hit_points = hit_points
@weapons = []
@gold = gold
@clothing = []
end
def name() @name end
def race() @race end
def hit_points() @hit_points end
def weapons() @weapons end
def gold() @gold end
def clothing() @clothing end
def name=(name) @name = name end
def race=(race) @race = race end
def hit_points=(hit_points) @hit_points = hit_points end
def gold=(gold) @gold = gold end
def add_weapon(weapon) @weapons << weapon end
def add_clothing(clothing) @clothing << clothing end
def drop_weapon(weapon)
@weapons.delete_at(@weapons.index(weapon)) if @weapons.include?(weapon)
end
def drop_clothing(clothing)
@clothing.delete_at(@clothing.index(clothing)) if @clothing.include?(clothing)
end
def to_s
"Name: #{@name}\nRace: #{@race}\nHit Points: #{@hit_points}\nWeapons: #{@weapons.join(', ')}\nGold: #{@gold}\nClothing: #{@clothing.join(', ')}"
end
end
#Person tests
adam = Person.new('Adam', 'Crowly', 24, 'brown', 'blue')
adam.first_name= 'AJ'
puts adam.first_name
adam.last_name = 'Crowley'
puts adam.last_name
adam.age = 25
puts adam.age
adam.hair_color = 'brown'
puts adam.hair_color
adam.eye_color = 'hazel'
puts adam.eye_color
puts '', adam.to_s, ''
#Address tests
home = Address.new('2312 S Davis Ct.', 'Spokane', 'WA', 99216)
home.line1 = '2313 S Davis Ct.'
puts home.line1
home.line2 = 'Upstairs Room'
puts home.line2
home.city = 'Spokane Valley'
puts home.city
home.state = 'Washington'
puts home.state
home.zip = 99226
puts home.zip
puts '',home.to_s, ''
#Character tests
player_one = Character.new('Raistlin Majere: The Greatest Mage Ever', 'human', 5, 500)
player_one.name = 'Raistlin Majere'
puts player_one.name
player_one.race = 'civilized human'
puts player_one.race
player_one.hit_points = 13
puts player_one.hit_points
player_one.add_weapon('Spell Tome')
player_one.add_weapon('Dust')
player_one.add_weapon('Staff')
player_one.drop_weapon('Dust')
puts player_one.weapons
player_one.gold = 1100
puts player_one.gold
player_one.add_clothing('Robes')
player_one.add_clothing('Sandals')
player_one.add_clothing('Ring')
player_one.drop_clothing('Sandals')
puts player_one.clothing
puts '', player_one.to_s
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment