-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathKey.java
110 lines (104 loc) · 3.44 KB
/
Key.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/**
* A key piece that can be picked up by players to unlock the chest.
*
* @author Mohamed Haryz Izzudin bin Mohamed Rafy (1141127874)
* @author Ramanan R Muralitharan (1141128291)
*/
public class Key extends SpecialPiece {
/**
* The movement strategy tied to the key.
*/
private MoveStrategy strategy;
/**
* Constructor for the Key class.
* Sets the movement strategy related to the key.
* Also sets the key's initial square and icon path.
*
* @author Haryz
* @param square The square that the key is initially on.
* @param iconPath The path to the icon for the piece.
* @param strategy The movement strategy related to the key.
*/
public Key(Square square, String iconPath, MoveStrategy strategy) {
super(square, iconPath);
this.strategy = strategy;
}
/**
* Constructor for the Key class.
* Constructs the class based on a string with the same format as the string returned
* by the toString method.
*
* @author Haryz
* @param str A string containing the key data.
* @throws Error if the string format is not recognised.
*/
public Key(String str) {
super(new Square(9, 9), str.split("\\|")[2]);
String[] data = str.split("\\|");
if (!data[0].equals("key")) {
throw new Error("Bad key string format");
}
else {
String[] squareCoordinates = data[1].split(",");
setSquare(Board.getInstance().getSquare(Integer.parseInt(squareCoordinates[0]), Integer.parseInt(squareCoordinates[1])));
switch (data[3]) {
case "MonkeyStrategy":
strategy = new MonkeyStrategy();
break;
case "PinkeyStrategy":
strategy = new PinkeyStrategy();
break;
case "KeyNoteStrategy":
strategy = new KeyNoteStrategy();
break;
case "KeyDiskStrategy":
strategy = new KeyDiskStrategy();
break;
case "DonkeyStrategy":
strategy = new DonkeyStrategy();
break;
case "DefaultStrategy":
default:
strategy = new DefaultStrategy();
}
}
}
/**
* Gets the strategy attached to the key.
*
* @author Haryz
* @return The strategy.
*/
public MoveStrategy getStrategy() {
return strategy;
}
/**
* Method for a player to interact with the special piece.
*
* @author Haryz
* @param player The player that is interacting with the special piece.
* @return A boolean - true if the player successfully picks up the key, else false
*/
@Override
public boolean interact(Player player) {
return player.pickupKey(this);
}
/**
* Gets the key's data in the form of a string.
* Example: "key|3,4|/icons/monkey.gif|MonkeyStrategy"
*
* @author Haryz
* @author Ramanan
* @return A string containing the key's data.
*/
@Override
public String toString() {
String str = "key|";
str = str + getSquare().getPosition().x + "," + getSquare().getPosition().y;
str = str + "|";
str = str + getIconPath();
str = str + "|";
str = str + strategy.toString();
return str;
}
}