Skip to content

Commit c8d0bde

Browse files
committed
Memento Design Pattern - Video Game Project
State of the game / player can be restored
1 parent 6deb6b9 commit c8d0bde

File tree

7 files changed

+219
-26
lines changed

7 files changed

+219
-26
lines changed

pattern/src/com/premaseem/Client.java

Lines changed: 0 additions & 13 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.premaseem.memento.videoGame;
2+
/*
3+
@author: Aseem Jain
4+
@title: Design Patterns with Java 9
5+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
6+
*/
7+
8+
import java.util.HashMap;
9+
import java.util.Map;
10+
11+
/*
12+
* Store Room
13+
*/
14+
public class CareTaker {
15+
Map<String, Memento> restorePoints = new HashMap<String, Memento>();
16+
Originator Originator = new Originator();
17+
18+
public void saveGame(String restorePointName, GamePlayer player){
19+
Memento memento = Originator.saveMemento(player);
20+
restorePoints.put(restorePointName, memento);
21+
}
22+
23+
public void restoreGame(String restorePointName, GamePlayer player){
24+
GamePlayer playerSnapShot = null;
25+
Memento memento = restorePoints.get(restorePointName);
26+
if(memento==null){
27+
System.out.printf(" %s Restore point name is no correct \n",restorePointName);
28+
}else{
29+
playerSnapShot = memento.getPlayerSnapShot();
30+
Originator.restorePlayerSnapshot(player, playerSnapShot);
31+
System.out.printf(" %s Restore point is applied successfully \n ",restorePointName);
32+
}
33+
}
34+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package com.premaseem.memento.videoGame;
2+
3+
/*
4+
@author: Aseem Jain
5+
@title: Design Patterns with Java 9
6+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+
*/
8+
9+
import java.util.Scanner;
10+
11+
public class ClientFile {
12+
13+
public static void main(String[] args) {
14+
15+
System.out.println("Welcome to Memento Pattern Video Game ");
16+
Scanner scan = new Scanner(System.in);
17+
18+
CareTaker careTaker = new CareTaker();
19+
GamePlayer player = new GamePlayer();
20+
21+
System.out.println("Please enter your player name ... ");
22+
player.setPlayerName(scan.next());
23+
24+
String repeatRunFlag = "yes";
25+
while (!repeatRunFlag.equalsIgnoreCase("no")) {
26+
27+
System.out.println("Please enter your players Mask name eg. Mario, Batman, SuperMan ");
28+
player.setMaskPicked(scan.next());
29+
30+
System.out.println("Game started - points gained and level changed ");
31+
System.out.println("current state of player " + player);
32+
33+
player.increase100Points();
34+
System.out.println("Current state of player " + player);
35+
36+
System.out.println("Do you want to save the state of your game? Enter restore point name ");
37+
careTaker.saveGame(scan.next(), player);
38+
39+
player.increase100Points();
40+
System.out.println("Game Over :-( ");
41+
System.out.println(player);
42+
43+
System.out.println("Do you want to restore your game? Enter restore point ");
44+
careTaker.restoreGame(scan.next(), player);
45+
46+
System.out.println("Restored state of player to previous state " + player);
47+
48+
System.out.println("=============================");
49+
50+
System.out.println("Press No to Exit and any other character to repeat .... ");
51+
try {
52+
repeatRunFlag = scan.next();
53+
} catch (Exception e) {
54+
repeatRunFlag = "no";
55+
}
56+
}
57+
58+
System.out.println("\n $$$$$$$$$$$$$$$$$$$$ Thanks by Prem Aseem $$$$$$$$$$$$$$$$$$$$$$ \n ");
59+
System.out.println("\n $$$$$$$$$$$$$$$$$$$$$ www.premaseem.me $$$$$$$$$$$$$$$$$$$$$$ \n ");
60+
}
61+
}
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package com.premaseem.memento.videoGame;
2+
/*
3+
@author: Aseem Jain
4+
@title: Design Patterns with Java 9
5+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
6+
*/
7+
8+
public class GamePlayer implements Cloneable {
9+
10+
String playerName;
11+
String maskPicked="premAseem";
12+
Integer level=1;
13+
Integer totalPoints=100;
14+
15+
String getPlayerName() {
16+
return playerName;
17+
}
18+
19+
void setPlayerName(String playerName) {
20+
this.playerName = playerName;
21+
}
22+
23+
String getMaskPicked() {
24+
return maskPicked;
25+
}
26+
27+
void setMaskPicked(String maskPicked) {
28+
this.maskPicked = maskPicked;
29+
}
30+
31+
Integer getLevel() {
32+
return level;
33+
}
34+
35+
void setLevel(Integer level) {
36+
this.level = level;
37+
}
38+
39+
Integer getTotalPoints() {
40+
return totalPoints;
41+
}
42+
43+
void setTotalPoints(Integer totalPoints) {
44+
this.totalPoints = totalPoints;
45+
}
46+
47+
void increase100Points(){
48+
totalPoints+=100;
49+
level+=1;
50+
}
51+
52+
53+
54+
@Override
55+
protected Object clone() throws CloneNotSupportedException {
56+
return super.clone();
57+
}
58+
59+
@Override
60+
public String toString() {
61+
System.out.printf(" name: %s - Mask: %s - Level: %d - Points : %d ",playerName,maskPicked,level,totalPoints);
62+
return super.toString();
63+
}
64+
65+
66+
67+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.premaseem.memento.videoGame;
2+
3+
/*
4+
@author: Aseem Jain
5+
@title: Design Patterns with Java 9
6+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+
*/
8+
9+
public class Memento {
10+
private GamePlayer player;
11+
12+
public Memento(GamePlayer player) {
13+
super();
14+
this.player = player;
15+
}
16+
17+
GamePlayer getPlayerSnapShot(){
18+
return player;
19+
}
20+
21+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package com.premaseem.memento.videoGame;
2+
3+
/*
4+
@author: Aseem Jain
5+
@title: Design Patterns with Java 9
6+
@link: https://premaseem.wordpress.com/category/computers/design-patterns/
7+
*/
8+
9+
public class Originator {
10+
GamePlayer player;
11+
12+
Memento saveMemento(GamePlayer player){
13+
GamePlayer playerSnapShot = createPlayerSnapshot(player);
14+
Memento memento = new Memento(playerSnapShot);
15+
return memento;
16+
}
17+
18+
private GamePlayer createPlayerSnapshot(GamePlayer player) {
19+
GamePlayer playerSnapShot=null;
20+
try {
21+
playerSnapShot = (GamePlayer) player.clone();
22+
} catch (CloneNotSupportedException e) {
23+
24+
e.printStackTrace();
25+
}
26+
27+
return playerSnapShot;
28+
}
29+
30+
void restorePlayerSnapshot(GamePlayer playerSnapShot,GamePlayer player) {
31+
playerSnapShot.setMaskPicked(player.getMaskPicked());
32+
playerSnapShot.setLevel(player.getLevel());
33+
playerSnapShot.setTotalPoints(player.getTotalPoints());
34+
playerSnapShot.setPlayerName(player.getPlayerName());
35+
}
36+
}

patternBonus/src/com/premaseem/Client.java

Lines changed: 0 additions & 13 deletions
This file was deleted.

0 commit comments

Comments
 (0)