AlgoMaster Logo
AlgoMasterDesign Enemy Spawnereasy

Design Enemy Spawner

easy

A game spawns enemies by copying named prototypes. A prototype goblin carries the health, speed and loot every goblin starts with, and each spawned goblin is a copy that then takes its own damage and picks up its own loot.

Enemy and EnemySpawner are provided in the starter code. Implement only the prototype contract and the clone method: declare Prototype with clone() (IPrototype<T> in C#, EnemyPrototype in C++ and Go) and complete Enemy.clone().

The provided Enemy(type, health, speed, loot) keeps the loot list it is given. It exposes getters, addLoot(item), takeDamage(amount), which never drops health below 0, and describe(), which returns "Goblin (hp 30, speed 5) loot [dagger, coin]". Its clone() must return a new Enemy with the same type, health and speed and a new loot list holding the same items.

The provided EnemySpawner behaves as follows:

  • EnemySpawner() starts with no prototypes and no instances.
  • boolean registerPrototype(String name, String type, int health, int speed) stores a prototype with no loot and returns true. An empty name, a health below 1 or a name already registered returns false.
  • boolean addLoot(String name, String item) adds loot to the prototype, or returns false for an unknown name.
  • boolean spawn(String name, String id) clones the prototype and stores the copy under id. An unknown name, an empty id or an id already in use returns false.
  • boolean damage(String id, int amount) damages that instance, or returns false for an unknown id or an amount below 1.
  • boolean addInstanceLoot(String id, String item) adds loot to that instance, or returns false for an unknown id.
  • String describe(String id) and String describePrototype(String name) return the description, or "MISSING".
  • int instanceCount() returns how many instances have been spawned.

Every spawned enemy is independent of the prototype and of every other spawned enemy.

The tests call the provided EnemySpawner; your work should be confined to the contract and the clone method.

Example 1:

Input:

Output:

Explanation: A spawned goblin starts as an exact copy of the prototype. Damage to the spawned one leaves the prototype at full health.

Example 2:

Input:

Output:

Explanation: Loot added to the spawned orc stays on it, and loot added to the prototype afterwards reaches only enemies spawned later.

Constraints

  • 0 <= name.length, id.length, item.length <= 20
  • -5 <= health <= 500, 1 <= speed <= 20 and -5 <= amount <= 500
  • At most 100 calls in total are made across all methods.

Starter Code

Declare the prototype contract and complete Enemy.clone(). Everything else in Enemy and all of EnemySpawner is provided and must not be modified.

How the design is graded

needs 7/10 to pass
  • The contract is the copy operation

    Full marks when `Prototype` declares `clone` and `Enemy.clone` returns a new `Enemy` built from the current field values. Lose points heavily when clone returns the same object, or when the spawner copies fields itself instead of calling clone.

  • Collections are copied, not shared

    Full marks when the clone owns a new loot list holding the same items, so loot added to a spawned enemy never appears on the prototype and loot added to the prototype later never appears on enemies spawned earlier. Lose points heavily when the two lists are the same object.

  • Scalars copy by value

    Full marks when health and speed are copied as values, so damage to one instance leaves the prototype and every other instance untouched. Lose points for printing to stdout.

Passing every test is not enough on its own. A submission is accepted only when the design also clears the bar.

Hints

Loading...
CallReturns
new EnemySpawner()null
registerPrototype("goblin", "Goblin", 30, 5)true
addLoot("goblin", "dagger")true
spawn("goblin", "g1")true
damage("g1", 10)true
describe("g1")"Goblin (hp 20, speed 5) loot [dagger]"
describePrototype("goblin")"Goblin (hp 30, speed 5) loot [dagger]"

A spawned goblin starts as an exact copy of the prototype. Damage to the spawned one leaves the prototype at full health.

Run checks these cases. Submit also runs a larger hidden set.