Loading...
Loading...
Loading...
A game keeps one save point. SAVE stores the current state; LOAD brings it back, and loading again later must bring back the same saved state. A state has a level, coins and a position (an object with x and y). Build Position and GameState with copy constructors and run the commands.
MOVE dx dy, COIN c, LEVEL, SAVE, LOAD or PRINT.SAVE prints Saved. LOAD prints Loaded, or No save point before the first save. PRINT prints Level <l>, coins <c>, at (<x>, <y>).Example
Input
6 MOVE 2 3 COIN 10 SAVE MOVE 1 1 LOAD PRINT
Output
Saved Loaded Level 1, coins 10, at (2, 3)
The move after the save is undone by the load.
Try the problem first. Open one hint at a time.
save = game; and game = save; share one object: the next move changes the save point itself.
Copy on both sides: save = new GameState(game) and game = new GameState(save).
GameState's copy constructor must also copy the Position, or the save shares it.
Write your Java code, run it on the examples and submit it to hidden tests.
Solve in the editor