added lab10 ex1
This commit is contained in:
		
							
								
								
									
										17
									
								
								src/lab10_memento/ex1/CheckpointManager.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										17
									
								
								src/lab10_memento/ex1/CheckpointManager.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,17 @@ | ||||
| package lab10_memento.ex1; | ||||
|  | ||||
| public class CheckpointManager { | ||||
|     private Player.Checkpoint lastCheckpoint = null; | ||||
|  | ||||
|     public void save(Player player) { | ||||
|         lastCheckpoint = player.makeCheckpoint(); | ||||
|     } | ||||
|  | ||||
|     public void restore(Player player) { | ||||
|         if (lastCheckpoint != null) { | ||||
|             lastCheckpoint.restore(player); | ||||
|         } else { | ||||
|             System.out.println("No checkpoint to restore"); | ||||
|         } | ||||
|     } | ||||
| } | ||||
							
								
								
									
										60
									
								
								src/lab10_memento/ex1/Game.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										60
									
								
								src/lab10_memento/ex1/Game.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,60 @@ | ||||
| package lab10_memento.ex1; | ||||
|  | ||||
| public class Game { | ||||
|     private final Player player; | ||||
|     private final CheckpointManager checkpointManager; | ||||
|  | ||||
|     public Game() { | ||||
|         player = new Player(0, 0, 0); | ||||
|         checkpointManager = new CheckpointManager(); | ||||
|     } | ||||
|  | ||||
|     public void printInfo() { | ||||
|         System.out.println("Player position: (" + player.getX() + "," + player.getY() + ")"); | ||||
|         System.out.println("Player score: " + player.getScore()); | ||||
|         System.out.println(); | ||||
|     } | ||||
|  | ||||
|     public void doTurn(int dx, int dy, int score, boolean isCheckpoint) { | ||||
|         player.moveTo(player.getX() + dx, player.getY() + dy); | ||||
|         System.out.print("Player moves to (" + player.getX() + "," + player.getY() + ")"); | ||||
|         if (score != 0) { | ||||
|             player.increaseScore(score); | ||||
|             System.out.print(" and gains +" + score + " score"); | ||||
|         } | ||||
|         if (isCheckpoint) { | ||||
|             checkpointManager.save(player); | ||||
|             System.out.print(". It's a checkpoint and their position and score are saved"); | ||||
|         } | ||||
|         System.out.println(); | ||||
|         printInfo(); | ||||
|     } | ||||
|  | ||||
|     public void doTurn(int dx, int dy) { | ||||
|         doTurn(dx, dy, 0, false); | ||||
|     } | ||||
|  | ||||
|     public void doTurn(int dx, int dy, int score) { | ||||
|         doTurn(dx, dy, score, false); | ||||
|     } | ||||
|  | ||||
|     public void doTurn(int dx, int dy, boolean isCheckpoint) { | ||||
|         doTurn(dx, dy, 0, isCheckpoint); | ||||
|     } | ||||
|  | ||||
|     public void die() { | ||||
|         checkpointManager.restore(player); | ||||
|         System.out.println("Player died ! Returning to previous checkpoint"); | ||||
|         printInfo(); | ||||
|     } | ||||
|  | ||||
|     public static void main(String[] args) { | ||||
|         Game game = new Game(); | ||||
|  | ||||
|         game.printInfo(); | ||||
|         game.doTurn(1, 1, 100); | ||||
|         game.doTurn(1, 1, true); | ||||
|         game.doTurn(4, 4, 100); | ||||
|         game.die(); | ||||
|     } | ||||
| } | ||||
							
								
								
									
										68
									
								
								src/lab10_memento/ex1/Player.java
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										68
									
								
								src/lab10_memento/ex1/Player.java
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,68 @@ | ||||
| package lab10_memento.ex1; | ||||
|  | ||||
| public class Player { | ||||
|     private int x; | ||||
|     private int y; | ||||
|     private int score; | ||||
|  | ||||
|     public Player(int x, int y, int score) { | ||||
|         this.x = x; | ||||
|         this.y = y; | ||||
|         this.score = score; | ||||
|     } | ||||
|  | ||||
|     public void moveTo(int x, int y) { | ||||
|         this.x = x; | ||||
|         this.y = y; | ||||
|     } | ||||
|  | ||||
|     public void increaseScore(int score) { | ||||
|         this.score += score; | ||||
|     } | ||||
|  | ||||
|     public int getX() { | ||||
|         return x; | ||||
|     } | ||||
|  | ||||
|     public int getY() { | ||||
|         return y; | ||||
|     } | ||||
|  | ||||
|     public int getScore() { | ||||
|         return score; | ||||
|     } | ||||
|  | ||||
|     public Checkpoint makeCheckpoint() { | ||||
|         return new Checkpoint(this); | ||||
|     } | ||||
|  | ||||
|     public class Checkpoint { | ||||
|         private final int x; | ||||
|         private final int y; | ||||
|         private final int score; | ||||
|  | ||||
|         public Checkpoint(Player player) { | ||||
|             x = player.getX(); | ||||
|             y = player.getY(); | ||||
|             score = player.getScore(); | ||||
|         } | ||||
|  | ||||
|         public int getX() { | ||||
|             return x; | ||||
|         } | ||||
|  | ||||
|         public int getY() { | ||||
|             return y; | ||||
|         } | ||||
|  | ||||
|         public int getScore() { | ||||
|             return score; | ||||
|         } | ||||
|  | ||||
|         public void restore(Player player) { | ||||
|             player.x = this.getX(); | ||||
|             player.y = this.getY(); | ||||
|             player.score = this.getScore(); | ||||
|         } | ||||
|     } | ||||
| } | ||||
		Reference in New Issue
	
	Block a user