-- Homework 27, problem 2 -- solution.

local Character = {}
Character.__index = Character

function Character.new(name, hp)
    local self = setmetatable({}, Character)
    self.name = name
    self.hp = hp
    self.max_hp = hp
    return self
end

function Character:takeDamage(amount)
    self.hp = self.hp - amount
    if self.hp < 0 then self.hp = 0 end
end

function Character:heal(amount)
    self.hp = self.hp + amount
    if self.hp > self.max_hp then self.hp = self.max_hp end
end

function Character:isAlive()
    return self.hp > 0
end

function Character:report()
    print(string.format("%s: %d / %d HP (alive: %s)",
        self.name, self.hp, self.max_hp, tostring(self:isAlive())))
end

local c = Character.new("Keiko", 100)
c:report()
c:takeDamage(30)
c:report()
c:takeDamage(80)
c:report()
c:heal(20)
c:report()
