DEV Community

Discussion on: Daily Challenge #192 - Can you Survive the Zombies?

Collapse
 
nijeesh4all profile image
Nijeesh Joshy • Edited
MISS_PROBABILITY = 5


def hit?
  rand(1..100) > MISS_PROBABILITY
end

def all_zombies_dead_message(killed)
  "You shot all #{killed} zombies."
end

def got_eaten_message(killed)
  "You shot #{killed} zombies before being eaten: overwhelmed."
end

def ran_out_of_ammo_message(killed)
  "You shot #{killed} zombies before being eaten: ran out of ammo."
end

def zombie_shootout(n, r, a)
  ran_out_of_ammo_message(0) if a < n
  zombies_killed = 0
  (r * 2).times do
    return all_zombies_dead_message(zombies_killed) if n == zombies_killed
    return ran_out_of_ammo_message(zombies_killed) if a == 0
    a -= 1
    if hit?
      zombies_killed += 1
    end
  end
  return got_eaten_message(zombies_killed) if zombies_killed < n
end


puts zombie_shootout(3, 10, 10)
puts zombie_shootout(100, 8, 200)
puts zombie_shootout(50, 10, 8)