Imagine that your team is making a board game engine. Your teammates already created the Board class and successfully implemented theplace, move and reDraw methods of the GameEngine interface in the BoardGameEngine class, but the work is not finished yet: the undo, save and load methods are waiting to be implemented too.
Since it's a good idea to achieve their functionality using the Memento pattern, your teammates added the Snapshot nested class inside the Board class that will be used for saving and restoring the state of Board. Your task is to finish the work! However, the implementation must satisfy the following requirements:
Do not make breaking changes to the code because
BoardGameEngineis already used as an implementation of theGameEngineinterface somewhere else. You CAN make changes to the existing code but make sure they won't break anything.The state of
Boardmust be saved automatically every time it changes.undomust roll back the changes one by one. If there are no changes left to undo,undomust do nothing.saveandloadmust work together:savemust create a savepoint in memory (not on a hard drive!) andloadmust restore the board to that state. There must be only one savepoint, and subsequent calls of thesavemethod must overwrite it.If no savepoint is available, the
loadmethod must do nothing. Also, when theloadmethod successfully loads a savepoint, the history ofBoardstate changes must be cleared so thatundodoes not roll back anything until new changes are made.
Good luck and have fun!