DEV Community

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

Collapse
 
candidateplanet profile image
lusen / they / them 🏳️‍🌈🥑

Python, where of course the expected number of zombies killed is a maximum and sometimes the function says less were killed.

import random

# n: number of zombies
# r: range in meters
# a: number of bullets
def zombie_shootout(n, r, a):
  zombie_speed = 0.5 # m/s
  bullets_per_second = 1 # bullet/s
  miss_rate = .05 # %

  zombie_distance = r
  number_zombies = n
  number_bullets = a

  while True:
    #print(zombie_distance, number_zombies, number_bullets)
    # if shoot all zombies:
    if number_zombies <= 0:
      return "You shot all %s zombies." % n

    # if get eaten:
    if zombie_distance <= 0.0:
      return "You shot %s zombies before being eaten: overwhelmed." % (n - number_zombies)

    # if run out of ammo:
    if number_bullets <=  0:
      return "You shot %s zombies before being eaten: ran out of ammo." % (n - number_zombies)

    zombie_distance -= zombie_speed
    number_bullets -= bullets_per_second

    zombie_killed = 1
    if random.randint(1,100) > 95:
      zombie_killed = 0
    number_zombies -= zombie_killed

print(zombie_shootout(3, 10, 10), "\nYou shot all 3 zombies.\n")
print(zombie_shootout(3, 3, 3), "\nYou shot all 3 zombies.\n")
print(zombie_shootout(3, 4, 4), "\nYou shot all 3 zombies.\n")
print(zombie_shootout(10, 2, 30), "\nYou shot 4 zombies before being eaten: overwhelmed.\n")
print(zombie_shootout(10, 30, 4), "\nYou shot 4 zombies before being eaten: ran out of ammo.\n")