-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDungeonsOfDoom.java
More file actions
100 lines (97 loc) · 3.53 KB
/
DungeonsOfDoom.java
File metadata and controls
100 lines (97 loc) · 3.53 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
import java.io.File; // file manipulation
import java.util.ArrayList; // using array list
import java.util.InputMismatchException; // file manipulation exception handling
import java.util.Scanner; // user input
/**
* Game class which initializes the Map, Game Logic and plays the game
*/
public class DungeonsOfDoom {
//Declaring fields
private static ArrayList<String> filePathList; // array list of all the map files' names
private static Map map; // game map
// directory which contains all the maps
private static final File directory = new File(DungeonsOfDoom.class.getProtectionDomain().
getCodeSource().getLocation().getPath());
private static final File[] files = directory.listFiles();
private int mapNumber;
/**
* Constructor which initializes the array list of map files' names and starts the game
* @param player ChatClient object representing the user who plays the game
*/
public DungeonsOfDoom(ChatClient player) {
filePathList = new ArrayList<>();
initializeFilePath();
printWelcomeMessage();
loadMap();
GameLogic game = new GameLogic(map, player);
game.play();
}
/**
* Create the array list of map files's names
*/
private void initializeFilePath(){
if(files != null){
for(File file: files){
// be sure to get just the .txt files
if(file.getName().contains(".txt")){
filePathList.add(file.getName());
}
}
}
}
/**
* Print the name of each map file, without the extension
*/
private void printMaps(){
int index = 1;
for(File file: files){
if(file.getName().contains(".txt")){
System.out.println(index + " --- " +
file.getName().substring(0, file.getName().length() - 4));
index++;
}
}
}
/**
* Welcomes the player to the game, and shows all the existing maps.
* Asks the player to select one.
*/
private void printWelcomeMessage(){
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to Dungeons Of Doom");
System.out.println("Please choose a map from the following:");
printMaps();
System.out.println("Please enter the number corresponding to the map you want to play.");
// Check that an integer from 1 to the maximum number is entered
try{
mapNumber = scanner.nextInt() - 1;
if(mapNumber < 0 || mapNumber > filePathList.size() - 1){
invalidDifficultyMessage();
}
}
catch (InputMismatchException e) {
invalidDifficultyMessage();
}
}
/**
* Let the player know what happened because of an invalid map number
*/
private void invalidDifficultyMessage(){
mapNumber = -1;
System.out.println("Input not valid. The default map is easyMap.\n" +
"If you want to play the game with another mapNumber exit the " +
"current session and restart the game.");
}
/**
* Instantiate the correspondent Map object
*/
private void loadMap(){
if(mapNumber == -1){
// default map
map = new Map("easyMap.txt");
}
else{
map = new Map(filePathList.get(mapNumber));
}
}
}