-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPlayer.java
More file actions
164 lines (134 loc) · 4.18 KB
/
Player.java
File metadata and controls
164 lines (134 loc) · 4.18 KB
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
package APCSA.APCSA_Code_Your_Own;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.*;
public class Player implements Serializable {
private String direction;
private HashMap<String, Item> inventory;
private int currentRow = 0;
private int currentColumn = 0;
private ArrayList<Room> visited;
private int currentHealth = 50;
private int maxHealth = 50;
private boolean isDead = false;
private static final String fileName = "PlayerState.ser";
public Player(String direction, HashMap<String, Item> initialInventory) {
this.direction = direction;
this.inventory = initialInventory;
this.visited = new ArrayList<Room>();
}
public void setDirection(String direction) {
this.direction = direction;
}
public void setRow(int row) {
this.currentRow = row;
}
public void setColumn(int column) {
this.currentColumn = column;
}
public void addVisited(Room room) {
if (!visited.contains(room)) {
visited.add(room);
}
}
public ArrayList<Room> getVisited() {
return visited;
}
public String getDirection() {
return direction;
}
public void addItem(String name, Item item) {
inventory.put(name, item);
}
public Item getItem(String name) {
return inventory.get(name);
}
public HashMap<String, Item> getInventory() {
return inventory;
}
public void removeItem(String name) {
inventory.remove(name);
}
public int getRow() {
return currentRow;
}
public int getColumn() {
return currentColumn;
}
public String getLocation() {
return "You are facing direction " + direction + ", and are in the room at coordinates (" + currentRow + ", "
+ currentColumn + "). ";
}
public boolean inventoryContains(String name) {
for (String itemName : inventory.keySet()) {
if (itemName.equals(name)) {
return true;
}
}
return false;
}
public String getInventoryString() {
String returnString = "Your inventory contains:\n";
for (String name : inventory.keySet()) {
returnString += "\t" + name + " --> " + inventory.get(name) + "\n";
}
return returnString;
}
public void setMaxHealth() {
this.maxHealth = maxHealth;
}
public int getCurrentHealth() {
return currentHealth;
}
public int getMaxHealth() {
return maxHealth;
}
public boolean takeDamage(int damage) {
currentHealth -= damage;
if (currentHealth > 0) {
isDead = true;
}
return isDead;
}
public void renewHealth() {
currentHealth = maxHealth;
}
public void renewHealth(int health) {
currentHealth += health;
if (currentHealth > maxHealth) {
currentHealth = maxHealth;
}
}
public String toString() {
return getLocation() + getInventoryString();
}
public boolean save() {
try {
FileOutputStream fileOutputStream = new FileOutputStream(fileName);
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream);
objectOutputStream.writeObject(this);
objectOutputStream.close();
fileOutputStream.close();
return true;
} catch (IOException exception) {
System.err.println(exception);
return false;
}
}
public static Player restore() {
try {
FileInputStream fileInputStream = new FileInputStream(fileName);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
Player player = (Player) objectInputStream.readObject();
objectInputStream.close();
fileInputStream.close();
return player;
} catch (Exception exception) { // IOException, ClassNotFoundException
return null;
}
}
}