-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathGame.java
135 lines (119 loc) · 3.82 KB
/
Game.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
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
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
/**Class: Game
* @authors Janna Timmer, Matthew Nelson, Matthew Xiong
* @version 1.0
* Course: ITEC 3860 Fall 2016
* Written: Nov 20, 2016
*
*
* This class provides the blueprint for Game files
*
* Purpose: Handle saving, loading, and starting a new game.
*/
public class Game
{
private HashMap<String, Room> updatedRooms = new HashMap<String, Room>(), rooms = RoomFactory.getRooms();
private Player newPlayer = new Player("00", 20, 10, 0, "L1001", rooms);
private Player player;
private String zhtext = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n" +
"| |'''''/ '|| '|| ||` || '||` |\n" +
"| // || '' || || '' || || |\n" +
"| // .|''|, '||),,(|, ||''|, || .|''|, ||''|| .|''|, ('''' '||''|, || ''||'' '''|. || |\n" +
"| // || || || || || || || || ||..|| || || || || `'') || || || || .|''|| || |\n" +
"| /.....| `|..|' .|| ||. .||..|' .||. `|... .|| ||. `|..|' `...' ||..|' .||. `|..' `|..||. .||. |\n" +
"| || |\n" +
"| .|| |\n" +
"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~";
public void makeNewGame()
{
newPlayer = new Player("00", 20, 10, 0, "L1001", rooms);
rooms = RoomFactory.getRooms();
saveGame(ZHTester.stockSave, newPlayer, rooms);
}
public void newGame()
{
System.out.print("New Game ");
loadGame(ZHTester.stockSave);
}
public void saveGame(File file, Player player, HashMap<String, Room> rooms)
{
try
{
FileOutputStream fileOut = new FileOutputStream(file);
ObjectOutputStream objectOut = new ObjectOutputStream(fileOut);
objectOut.writeObject(player);
objectOut.writeObject(rooms);
objectOut.close();
}
catch(Exception e)
{
e.printStackTrace();
}
}
@SuppressWarnings("unchecked")
public void loadGame(File file)
{
try
{
FileInputStream fileIn = new FileInputStream(file);
@SuppressWarnings("resource")
ObjectInputStream objectIn = new ObjectInputStream(fileIn);
player = (Player) objectIn.readObject();
updatedRooms = (HashMap<String, Room>) objectIn.readObject();
ZHTester.setPlayer(player);
ZHTester.setRooms(updatedRooms);
}
catch(Exception e)
{
System.out.println("That file doesn't seem to exist.. Create a file\n"
+ "and then load it!");
}
if(player != null)
{
System.out.println("Loading... ");
System.out.println(zhtext);
System.out.println(updatedRooms.get(player.getCurrentRoomID()).getDescription());
}
}
public HashMap<String, Room> getRooms()
{
return rooms;
}
public Player getNewPlayer()
{
return newPlayer;
}
public HashMap<String, Room> getUpdatedRooms()
{
return updatedRooms;
}
public Player getPlayer()
{
return player;
}
public String getZHText()
{
return zhtext;
}
public void setRooms(HashMap<String, Room> rooms)
{
this.rooms = rooms;
}
public void setNewPlayer(Player newPlayer)
{
this.newPlayer = newPlayer;
}
public void setUpdatedRooms(HashMap<String, Room> updatedRooms)
{
this.updatedRooms = updatedRooms;
}
public void setPlayer(Player player)
{
this.player = player;
}
}