Last active
August 29, 2015 14:18
-
-
Save CafeFerguson/e6bf94837224a15a68d5 to your computer and use it in GitHub Desktop.
Ruby - Viking Maker (Class Example)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Weapon | |
attr_accessor :armedWith | |
# Give random weapon | |
def Weapon.random_weapon() | |
armedWith = ["sword","axe","spear","crossbow"].sample | |
return armedWith # returned value | |
end | |
end | |
class Viking | |
#attr_accessor is both getter and setter (attr_reader and attr_writer methods can be also used) | |
#@@starting_health = 100 | |
attr_accessor :name, :age, :strength, :health | |
#creating a class method alternately we could type Viking.class_method (Viking.create_warrior(name)) | |
def self.create_warrior(name) | |
age = 20 + rand(10) # remember, rand gives a random 0 to 1 | |
health = 100 | |
strength = [3 + rand(6) + rand(6) + rand(6) + 3, 10].max | |
Viking.new(name, age, strength, health) # returned | |
weapon1 = createWeapon() | |
print "Armed with: ", weapon1, "\n" | |
end | |
def createWeapon() | |
#weapon1 = Weapon.create_weapon(Weapon.random_weapon) | |
weapon = Weapon.random_weapon() | |
print "Armed with: ", weapon, "\n" | |
end | |
def self.random_name # useful for making new warriors! | |
["Erik","Red","Leif","Ed"].sample | |
end | |
def initialize(name, age, strength, health) | |
@name = name | |
@age = age | |
@strength = strength | |
@health = health | |
end | |
def attack(enemy) | |
# code to fight | |
print "#{self.name} is attacking #{enemy.name}", "\n" | |
#enemy.health = enemy.health - 1 | |
take_damage(enemy, 1) | |
end | |
def take_damage(who, damage) | |
#lars.attack(oleg)... calls lars.take_damage(oleg)... which subtracts 1 from oleg's health | |
#(who.health -= damage) and subtracts 1 from lars' health too (self.health -= damage) | |
who.health -= damage | |
#self.health -= damage | |
# OR we could have said @health -= damage | |
who.shout("OUCH #{who.name} took damage!") #use who not self because lar.attach(oleg) lars is self, but Oleg is enemy | |
end | |
def shout(str) | |
puts str | |
end | |
def sleep | |
self.health += 1 unless health >= 100 | |
self.shout("#{self.name} feels better after a nap!") | |
end | |
#defining a getter method to allow access outside of here | |
#def health | |
# @health | |
#end | |
#defining a getter method to allow access outside of here | |
#def name | |
# @name | |
#end | |
#defining a setter method | |
#def health=(new_health) | |
# @health = new_health | |
#end | |
end | |
warrior1 = Viking.create_warrior(Viking.random_name) | |
#weapon1 = Weapon.create_weapon(Weapon.random_weapon) | |
#weapon1 = createWeapon() | |
#print "Viking 1: ", warrior1, "\n" | |
print "Name: ", warrior1.name, " | Health: ", warrior1.health, " | Strength: ", warrior1.strength, " | Age: ", warrior1.age, "\n" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment