AlgoMaster Logo
AlgoMasterDesign Game Save Systemeasy

Design Game Save System

easy

A game tracks the player's level, health, score and a bag of items. The whole state can be saved into one of three slots and loaded back later.

The starter code provides GameSaveSystem, the coordinator used by the tests. Implement only its three Memento participants:

  • GameMemento, the memento that owns a saved copy of the complete game state.
  • Game, the originator that creates and restores mementos.
  • SaveManager, the caretaker that stores slot mementos without reading them.

The provided coordinator behaves as follows:

  • GameSaveSystem() starts at level 1 with 100 health, 0 score, an empty bag and three empty slots.
  • int play() raises the level by 1, adds 50 to the score, and returns the new level.
  • boolean takeDamage(int amount) subtracts the amount from health and returns true. Health never falls below 0. A negative amount changes nothing and returns false.
  • boolean pickUp(String item) adds an item to the bag and returns true, or returns false once the bag holds 8 items.
  • boolean save(int slot) stores the current state in slot 0, 1 or 2 and returns true. Any other slot number changes nothing and returns false.
  • boolean load(int slot) restores the state held in that slot and returns true. A slot outside the range, or one that was never written, changes nothing and returns false.
  • String status() returns "Level: <level>, Health: <health>, Score: <score>".
  • String items() returns the bag contents joined by commas in pickup order, or "EMPTY" when the bag holds nothing.
  • int saveCount() returns how many saves succeeded.

Saving to a slot that already holds a state replaces it. Loading leaves the slot as it was, so the same slot can be loaded any number of times and returns the same state every time.

Do not rewrite GameSaveSystem. Make the three participants satisfy the API it already uses, and keep saved states opaque to SaveManager.

Example 1:

Input:

Output:

Explanation: Two rounds of play raise the level to 3 and add 50 points each time. The save into slot 0 succeeds, and the bag is still empty.

Example 2:

Input:

Output:

Explanation: The potion goes into the bag after the save, so loading slot 0 brings back the two-item bag and the level, health and score that went with it. A snapshot sharing the game's list would report all three items.

Constraints

  • -1000 <= amount <= 1000
  • -5 <= slot <= 5
  • 1 <= item.length <= 20
  • The bag holds at most 8 items.
  • At most 100 calls in total are made across all methods.

Starter Code

GameSaveSystem is complete in every language below. Implement only GameMemento, Game, and SaveManager.

How the design is graded

needs 7/10 to pass
  • Snapshots are independent

    Full marks when the snapshot copies the bag as it is built and copies it again as it is applied, so playing on after a save never changes what the slot holds and playing on after a load never changes it either. Lose points heavily when the snapshot keeps the game's own list, or when restoring hands that list straight back to the game.

  • The caretaker cannot read a snapshot

    Full marks when the type holding the three slots stores snapshot objects and returns them untouched, leaving the game as the only type that reads or writes what is inside one. Lose points when the slots hold unpacked level, health, score and bag values, or when the caretaker writes state onto the game itself.

  • Structure and naming

    Full marks when `SaveManager` owns exactly three slots, rejects indexes outside `0` to `2`, distinguishes empty and filled slots, and replaces a slot without reading its memento, while `Game` floors health at zero, rejects negative damage, and limits the bag to eight items. Lose points for exposing snapshot contents through the caretaker or 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 GameSaveSystem()null
play()2
play()3
status()"Level: 3, Health: 100, Score: 100"
save(0)true
items()"EMPTY"

Two rounds of play raise the level to 3 and add 50 points each time. The save into slot 0 succeeds, and the bag is still empty.

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