-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathMain.java
More file actions
284 lines (270 loc) · 11.6 KB
/
Main.java
File metadata and controls
284 lines (270 loc) · 11.6 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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package RoomGame;
import java.util.Scanner;
import java.io.*;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.FileNotFoundException;
import javax.xml.parsers.SAXParser;
import java.io.File;
import javax.xml.parsers.SAXParserFactory;
import java.io.Reader;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.xml.sax.helpers.DefaultHandler;
/**
*
* @author kleesans
*/
public class Main extends DefaultHandler
{
static Room[] Rooms;
static int roomCounter;
static int CreatureNumber;
static Creature[] tempCreatures;
static PC player;
public static void main(String[] args) throws SAXException, IOException, ParserConfigurationException {
BufferedReader Input = new BufferedReader(new InputStreamReader(System.in));
System.out.println("type the name of your input file");
String Inputline = Input.readLine();
SAXParserFactory fpety = SAXParserFactory.newInstance();
DefaultHandler defaultone = new Main();
SAXParser parser = fpety.newSAXParser();
parser.parse(new File(Inputline), defaultone);
printHelp();
System.out.println("Type command: ");
for (Inputline = Input.readLine(); !Inputline.equalsIgnoreCase("exit"); Inputline = Input.readLine()) {
{
if (!Inputline.equalsIgnoreCase("help")) {
String[] lines = Inputline.split(":");
if (lines.length == 1) {
String command = lines[0];
executeCommand(Main.player, command);
}
else if (lines.length == 2) {
String command = lines[1];
try {
Creature commandCrit = commandCreature(lines[0]);
executeCommand(commandCrit, command);
}
catch (NullPointerException e) {
System.out.println("There is no creature of name " + lines[0] + " in the " + Main.player.getRoom().getName() + " room!");
}
}
else {
System.out.println("This is not a valid command! Commands can contain only one or two words separated by a colon (:).");
}
System.out.println("After the execution of this command, the PC's respect is " + Main.player.getRespect());
if (Main.player.getRespect() >= 80) {
System.out.println("Congratulations! You win the game in praise!");
}
else {
if (Main.player.getRespect() > 0) {
}
System.out.println("You end the game in disgrace. Try to play better.");
}
return;
}
printHelp();
}
System.out.println("Type new command: ");
}
System.out.println("Goodbye!");
}
private static Creature commandCreature( String pet) {
for (int i = 0; i < Main.player.getRoom().getOccupants().length; ++i) {
if (Main.player.getRoom().getOccupants()[i].getName().equals(pet)) {
return Main.player.getRoom().getOccupants()[i];
}
}
return null;
}
private static void executeCommand( Creature thisone, String command) {
if (command.equalsIgnoreCase("n") || command.equalsIgnoreCase("e") || command.equalsIgnoreCase("s") || command.equalsIgnoreCase("w")) {
boolean move = thisone.tryToMove(command, true, Main.player);
}
else if (command.equalsIgnoreCase("PeekRoom")) {
thisone.PeekRoom();
}
else if (command.equalsIgnoreCase("clean")) {
if (thisone.clean(Main.player, true)) {
Main.player.getRoom().reactCreaturesChangedRoom(thisone, command, Main.player);
}
}
else if (command.equalsIgnoreCase("dirty")) {
if (thisone.dirty(Main.player, true)) {
Main.player.getRoom().reactCreaturesChangedRoom(thisone, command, Main.player);
}
}
else {
System.out.println(command + " is not a valid action that creatures can perform!");
}
}
private static void printHelp() {
System.out.println("Type clean or dirty to clean or dirty the room");
System.out.println("To PeekRoom, simply type in the word PeekRoom");
System.out.println("To make a creature clean or dirty the current room, type creature clean or creature dirty");
System.out.println("To make a creature that is in the current room PeekRoom around the room, type PeekRoom");
System.out.println("To see this menu, type help");
System.out.println("To exit the game, type exit ");
}
public void endElement( String namespaceURI, String placeholder, String Query) throws SAXException {
if (Query.equalsIgnoreCase("room")) {
Main.Rooms[Main.roomCounter].setOccupants(Main.tempCreatures);
Main.Rooms[Main.roomCounter].setLastCreatureIndex(Main.CreatureNumber);
}
}
public void startElement( String namespaceURI, String placeholder, String Query, Attributes attrs) throws SAXException {
if (Query.equalsIgnoreCase("room")) {
Main.tempCreatures = new Creature[10];
String name = null;
String description = null;
String state = null;
String north = null;
String east = null;
String south = null;
String west = null;
Main.roomCounter++;
Main.CreatureNumber = -1;
if (attrs != null) {
for (int i = 0; i < attrs.getLength(); ++i) {
if (attrs.getQName(i).equals("name")) {
name = attrs.getValue(i);
}
if (attrs.getQName(i).equals("description")) {
description = attrs.getValue(i);
}
if (attrs.getQName(i).equals("state")) {
state = attrs.getValue(i);
}
if (attrs.getQName(i).equals("north")) {
north = attrs.getValue(i);
}
if (attrs.getQName(i).equals("east")) {
east = attrs.getValue(i);
}
if (attrs.getQName(i).equals("south")) {
south = attrs.getValue(i);
}
if (attrs.getQName(i).equals("west")) {
west = attrs.getValue(i);
}
}
}
if (name == null || state == null || description == null) {
throw new SAXException("Room in input file not formatted properly");
}
Main.Rooms[Main.roomCounter] = new Room(name, description, state);
Room neighbor = null;
if (north != null) {
neighbor = this.searchNeighbor(north);
if (neighbor != null) {
Main.Rooms[Main.roomCounter].setNorthRoom(neighbor);
neighbor.setSouthRoom(Main.Rooms[Main.roomCounter]);
neighbor = null;
}
}
if (east != null) {
neighbor = this.searchNeighbor(east);
if (neighbor != null) {
Main.Rooms[Main.roomCounter].setEastRoom(neighbor);
neighbor.setWestRoom(Main.Rooms[Main.roomCounter]);
neighbor = null;
}
}
if (south != null) {
neighbor = this.searchNeighbor(south);
if (neighbor != null) {
Main.Rooms[Main.roomCounter].setSouthRoom(neighbor);
neighbor.setNorthRoom(Main.Rooms[Main.roomCounter]);
neighbor = null;
}
}
if (west != null) {
neighbor = this.searchNeighbor(west);
if (neighbor != null) {
Main.Rooms[Main.roomCounter].setWestRoom(neighbor);
neighbor.setEastRoom(Main.Rooms[Main.roomCounter]);
neighbor = null;
}
}
}
if (Query.equalsIgnoreCase("animal")) {
String name = null;
String description = null;
++Main.CreatureNumber;
if (attrs != null) {
for (int j = 0; j < attrs.getLength(); ++j) {
if (attrs.getQName(j).equals("name")) {
name = attrs.getValue(j);
}
if (attrs.getQName(j).equals("description")) {
description = attrs.getValue(j);
}
}
}
if (name == null || description == null) {
throw new SAXException("Animal in input file not formatted properly");
}
(Main.tempCreatures[Main.CreatureNumber] = new animal(name, description)).setCurrentRoom(Main.Rooms[Main.roomCounter]);
}
if (Query.equalsIgnoreCase("NPC")) {
String name = null;
String description = null;
++Main.CreatureNumber;
if (attrs != null) {
for (int j = 0; j < attrs.getLength(); ++j) {
if (attrs.getQName(j).equals("name")) {
name = attrs.getValue(j);
}
if (attrs.getQName(j).equals("description")) {
description = attrs.getValue(j);
}
}
}
if (name == null || description == null) {
throw new SAXException("NPC in input file not formatted properly");
}
(Main.tempCreatures[Main.CreatureNumber] = new NPC(name, description)).setCurrentRoom(Main.Rooms[Main.roomCounter]);
}
if (Query.equalsIgnoreCase("PC")) {
String name = null;
String description = null;
Main.CreatureNumber++;
if (attrs != null) {
for (int j = 0; j < attrs.getLength(); ++j) {
if (attrs.getQName(j).equals("name")) {
name = attrs.getValue(j);
}
if (attrs.getQName(j).equals("description")) {
description = attrs.getValue(j);
}
}
}
if (name == null || description == null) {
throw new SAXException("PC in input file not formatted properly");
}
Main.tempCreatures[Main.CreatureNumber] = new PC(name, description);
(Main.player = (PC)Main.tempCreatures[Main.CreatureNumber]).setCurrentRoom(Main.Rooms[Main.roomCounter]);
}
}
private Room searchNeighbor(String neighbor) {
for (int i = 0; i < Main.roomCounter; i++) {
if (Main.Rooms[i].getName().equalsIgnoreCase(neighbor)) {
return Main.Rooms[i];
}
}
return null;
}
static {
Main.Rooms = new Room[50];
Main.roomCounter = -1;
Main.CreatureNumber = -1;
}
}