Skip to content

Instantly share code, notes, and snippets.

@shinobcrc
Created July 1, 2016 03:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinobcrc/2d9df52521a94890cf9e85d197aa1196 to your computer and use it in GitHub Desktop.
Save shinobcrc/2d9df52521a94890cf9e85d197aa1196 to your computer and use it in GitHub Desktop.
Robot Mock Test
require_relative 'robot'
class BoxOfBolts < Item
def initialize
super("Box of bolts", 25)
end
def feed(robot_to_feed)
robot_to_feed.heal(20)
end
end
class Item
attr_reader :name , :weight
def initialize(name, weight)
@name = name
@weight = weight
end
end
class Laser < Weapon
def initialize
super("Laser", 125, 25)
end
end
class PlasmaCannon < Weapon
def initialize
super('Plasma Cannon', 200, 55)
end
end
class Robot
MAX_CAPACITY = 250
attr_reader :position, :items, :health
attr_accessor :equipped_weapon
def initialize
@position = [0,0]
@items = []
@health = 100
@equipped_weapon = nil
end
def move_left
@position[0] -= + 1
end
def move_right
@position[0] += 1
end
def move_up
@position[1] += 1
end
def move_down
@position[1] -= +1
end
def items
@items
end
def pick_up(item)
@items << item if (items_weight + item.weight) <= MAX_CAPACITY
@equipped_weapon = item if (item.is_a? Weapon)
#if the equipped weapon is a insance of Class Weapon,
#it will pick up the item, if not it'll stay the same
end
def items_weight
@items.inject(0) {|sum, item|
sum += item.weight
}
end
def wound(damage)
@health -= damage
@health = 0 if @health < 0
#set health back to 0 if @health goes below 0
end
def heal(amount)
@health += amount
#reset health to 100 if healed more than 100
@health = 100 if @health > 100
end
def heal!
begin
raise RobotAlreadyDeadError, "INVALID - ROBOT ALREADY DEAD ERROR: your health is at 0. You are dead. You cannot revive robot" if health == 0
end
end
def attack(enemy)
@equipped_weapon.hit(enemy)
end
def attack!(target)
begin
raise UnattackableEnemy, "INVALID - NOT ATTACKABLE: you can only attack robots" unless target.is_a? Robot
end
end
end
class Weapon < Item
attr_reader :damage
def initialize(name, weight, damage)
super("power_shock", 10)
@damage = damage
end
def hit(robot_to_be_hit)
robot_to_be_hit.wound(@damage)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment