-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTextGame.java
More file actions
347 lines (315 loc) · 10.1 KB
/
TextGame.java
File metadata and controls
347 lines (315 loc) · 10.1 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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
/*
* Author : Jordan Harrison
* Date 11/11/2019
* Software Development 1
*/
package mockTextGameProject;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Scanner;
public class TextGame
{
private Scanner input = new Scanner(System.in);
private String currentStringInFile = "";
private Scanner sc = new Scanner(System.in);
private ArrayList <Room> listOfRooms = new ArrayList<Room>();
private ArrayList<Collectible> listOfCollectibles = new ArrayList<Collectible>();
private boolean inGame = false;
private String answer = ""; private String pickupCommand; private String dropCommand; private String consumeCommand;
Player ourPlayer;
public static void main(String[] args)
{
TextGame tester = new TextGame();
//tester.writeFile(fileName);
tester.playGame();
}
/*
* Method - createRoomObject
* @param - File file
* Return - This method reads a file formated for the game. It take the information and separates it into the key information needed for a Room class. It then creates a new room for every applicable subdivision in the given text file.
*/
public void readRoomObjectsFromFile(File file)
{
int counter = 0;
Integer roomNum = 0;
String roomName = "";
String roomDesc = "";
ArrayList <String> roomDirections = new ArrayList<String>();
try
{
input = new Scanner(file);
while(input.hasNextLine())
{
currentStringInFile = input.nextLine();
switch(counter)
{
case 0:roomNum = Integer.parseInt(currentStringInFile);
break;
case 1: roomName = currentStringInFile;
break;
case 2: roomDesc = currentStringInFile;
break;
}
if(counter >= 4)
{
// two underscores in a line signifies the end of a room inside the text file
if(currentStringInFile.contains("__"))
{
Room newRoom = new Room(roomNum, roomName, roomDesc, roomDirections); //creates new room
listOfRooms.add(newRoom); //adds new room to the list
roomNum = 0; roomName = "";roomDesc = "";roomDirections.clear(); //reset all of the room values
counter = -1;
}
else
{
roomDirections.add(currentStringInFile);
}
}
counter++;
}
}
catch (FileNotFoundException fne)
{
fne.printStackTrace();
}
}
/*
* Method - writeFile
* @Param - File file
* Return - Void
*/
public void writeFile(String fileName)
{
try
{
FileWriter fw = new FileWriter(fileName);
PrintWriter pw = new PrintWriter(fw);
System.out.println("What would like to write the file? ");
String writtenToFile = input.nextLine();
pw.write(writtenToFile);
pw.close();
}
catch (IOException ioe)
{
ioe.printStackTrace();
}
}
/*
* creates a new player named The joker and returns it
*/
public Player createNewPlayer()
{
Player newPlayer = new Player(" The Joker");
return newPlayer;
}
/*
* Method - playGame
* @Param - void
* Return - void
* This method starts the game
*/
public void playGame()
{
int counter = 0;
this.inGame = true;
this.ourPlayer = createNewPlayer();
//File file = new File("C:\\Users\\callm\\Oxygen Workspace\\SoftwareDevelopment1\\src\\Room.txt");//src : textfile.txt
File file = new File("Room.txt");
readRoomObjectsFromFile(file);
//setupItems(new File("C:\\Users\\callm\\Oxygen Workspace\\SoftwareDevelopment1\\src\\Items.txt")); //sorry Dr.b I dont understand how to get my file to read from the SRC.
setupItems(new File("Items.txt"));
System.out.println("\t Welcome into the Game : Vacuum Game");
System.out.println("\t -------------------------------------------");
//commenceFight(ourPlayer,new Monster());
//Testing Currently
for(int i = 0; i < listOfRooms.size(); i++)
{
System.out.println( "\t : " + listOfRooms.get(i).getroomName() + " has connections to " + listOfRooms.get(i).getRoomNumberAndName().toString());
}
listOfRooms.get(2).addItemIntoRoom(new Collectible());
listOfRooms.get(4).addMonstersToRoom(new Monster());
System.out.println();
while(inGame)
{
System.out.println("\t Visited List : " + ourPlayer.getRoomsVisited().toString());
System.out.println("\t You are in : " + listOfRooms.get(ourPlayer.getCurrentRoom()).getroomName()); //
if(listOfRooms.get(ourPlayer.getCurrentRoom()).getHeldItemList().isEmpty())
{
}
else
{
System.out.println("\t Room Items : " + listOfRooms.get(ourPlayer.getCurrentRoom()).getHeldItemList());
}
System.out.println("\t --------------------------------------------------");
System.out.println("\t " + listOfRooms.get(ourPlayer.getCurrentRoom()).getroomDesc());
System.out.println("\t Where would you like to go? If an item is present in the room, you may pick it up using the pickup command");
answer = sc.nextLine();
//make method to determine what is contained in answer so that we can deduce what method the command should be sent to
try
{
goToRoom(answer);
}
catch (InvalidInputException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
// go to Outside building
//loops through listOfRoomNames
//if is inside the list and current room has connection to the room ---> go to room
/*
*
*/
public void goToRoom(String direction) throws InvalidInputException
{
// if the room's hashmap contains direction
int currentRoomNum = ourPlayer.getCurrentRoom();
ArrayList <String> hashDirections = new ArrayList <String>();
for(int i = 0; i < listOfRooms.get(currentRoomNum).getMapRoomConnections().size(); i++)
{
// hashDirections.add(listOfRooms.get(currentRoomNum).getMapRoomConnections().get(i)) ;
System.out.println(listOfRooms.get(currentRoomNum).getMapRoomConnections().get(i));
}
// add rooms visited
if(!ourPlayer.getRoomsVisited().contains(listOfRooms.get(currentRoomNum).getroomName()))
ourPlayer.getRoomsVisited().add(listOfRooms.get(currentRoomNum).getroomName());
if(listOfRooms.get(currentRoomNum).getMapRoomConnections().containsKey(direction))
{
//System.out.println(listOfRooms.get(currentRoomNum).getMapRoomConnections().toString());
//update and take the player to the requested room
this.ourPlayer.setCurrentRoom(listOfRooms.get(currentRoomNum).getMapRoomConnections().get(direction)-1); // Give direction -> Return the mapping S
}
else
{
throw new InvalidInputException("This room doesnt have access to that direction");
}
}
/*
*
*/
public void setupItemCommands(File file)
{
// read from file to field variables commands.
}
/*
* Method - setupItems
* @Param - File file
* Return - void
*/
public void setupItems(File file)
{
int counter = 0; int itemNum = 0; String itemName = ""; String itemDesc = "";
boolean isEdible = false; String itemPurpose = ""; int itemStat = 0; this.currentStringInFile = "";
try
{
input = new Scanner(file);
if(input.hasNextLine());
while(input.hasNextLine())
{
currentStringInFile = input.nextLine();
switch(counter)
{
case 0 : itemNum = Integer.parseInt(currentStringInFile);
break;
case 1 : itemName = currentStringInFile;
break;
case 2 : itemDesc = currentStringInFile;
break;
case 3 : isEdible = Boolean.parseBoolean(currentStringInFile);
break;
case 4 :
if(isEdible)
{
itemPurpose = "Consume";
}
else
{
itemPurpose = "Equipment";
}
itemStat = Integer.parseInt(currentStringInFile);
break;
case 5 : Collectible newCollectible = new Collectible(itemNum, itemName, itemDesc, itemPurpose, isEdible,itemStat);
listOfCollectibles.add(newCollectible);
itemNum = 0; itemName = ""; itemDesc = ""; itemPurpose = ""; isEdible = false; itemStat = 0;
counter = -1;
}
counter++;
}
//testing
//System.out.println("\t" + listOfCollectibles);
}
catch (FileNotFoundException e)
{
e.printStackTrace();
}
}
/*
*
*/
public void pickupItem(String command)
{
if(command.toLowerCase().contains(pickupCommand.toLowerCase()))
{
if(true)
{
}
}
}
public Player commenceFight(Player player, Monster monster)
{
Scanner input = new Scanner(System.in);
System.out.println("\t -------------------------------------------");
System.out.println("\t Battle Sequence has commenced between" + player.getName() + " and " + monster.getName());
while(player.isAlive() && monster.isAlive())
{
player.setAttackValue(50);
System.out.println("\t" + player.getName() + " HP : " + player.getCurrentHealth() + "/" + player.getMaxHealthPoints());
System.out.println("\t" + monster.getName() + " HP : " + monster.getCurrentHealth() + "/" + monster.getMaxHealthPoints());
if(!player.isAlive())
{
System.out.println("\t You have died.\n\t You truly are foolish.");
this.inGame = false;
}
System.out.println("\t What will you do? Attack, Converse or Run?");
String command = input.nextLine();
if(command.toLowerCase().equals("run"))
{
//Return the player to the last index of the list of roomsVisited
//Reset the display
return player;
}
else if(command.toLowerCase().equals("attack"))
{
System.out.println("\t" + player.getName() + " attacks " + monster.getName());
System.out.println("\t" + monster.getName() + " has lost " + player.getAttackValue() + " points of damage.");
monster.setCurrentHealth(monster.getCurrentHealth() - player.getAttackValue());
//calculate damage
System.out.println("\t" + monster.getName() + " has attacked you and you have lost " + monster.getAttackValue() + " health points.");
player.setCurrentHealth(player.getCurrentHealth() - monster.getAttackValue());
}
else if(command.toLowerCase().equals("converse"))
{
System.out.println("\t Don't be a Panzy");
}
System.out.println("\t -------------------------------------------");
}
return player;
}
/*
* Adds items from a monsters inventory to a players inventory
*/
public void givePlayerSpoils(Player player, Monster monster)
{
while(!monster.getItemPouch().isEmpty())
{
player.addItemsToPouch(monster.getItemPouch().get(0));
player.addItemsToPouch(monster.getItemPouch().remove(0));
}
}
}