Enemy Generator

Enemy Generator

Build a constructor that can generate enemy "objects." Then write a function to create 100 enemy objects, store them in an array, and return the array.

Each enemy object should have the following properties:

  • type
  • hitPoints
  • defense

The type should be randomly selected among the following types:

  • Ancient Dragon
  • Prowler
  • Mighty Grunt

To accomplish this, you'll want to create a separate helper function (not a part of your Enemy constructor) whose sole purpose is to return an enemy type. Each type should have an equal chance at being selected as the others.

The hitPoints property should be generated randomly according to the following criteria:

  • If the type is Ancient Dragon then the hitPoints should be between 80-100
  • If the type is Prowler then the hitPoints should be between 50-79
  • If the type is Mighty Grunt then the hitPoints should be between 20-49

Like the function to choose an enemy type, you'll want to create another separate helper function whose sole purpose is to return the number of hit points that enemy has. This function should take a string enemy type of the already-decided enemy type from above. Check the first hint below to see how to choose a number in a random range when specifying the maximum and minimum.

The enemy's defense is generated by multiplying the hitPoints by 3

Your constructor function will call on these other functions you've created to help populate the statistics for each enemy generated.

Summary

  • Create 5 functions:
    • Enemy constructor - for building individual enemy objects
    • Enemy generator - for creating 100 enemies and returning them in an array
    • Enemy type generator - for randomly choosing one of the three enemy types
    • Hit points generator - for randomly choosing the hit points depending on the determined enemy type
    • Defense points generator - for determining the number of defense points for an enemy

Hints