-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
242 lines (208 loc) · 9.18 KB
/
Copy pathGame.java
File metadata and controls
242 lines (208 loc) · 9.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
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
import java.io.IOException;
import java.util.Scanner;
public class Game {
public static final String CRYPTO_FILE = "TestFile.txt";
public static final String PLAYER_FILE = "Players.txt";
public static final String GAME_FILE = "Game.txt";
public static Players players = new Players();
private void clearScreen() {
for (int i = 0; i < 32; i++) {
System.out.println();
}
}
private void checkSolution(Cryptogram cg) {
System.out.println(cg.checkSolution() ? "Correct" : "Wrong");
if (cg.checkSolution()) {
players.savePlayers();
System.exit(0);
}
}
private String askToPlay(Scanner scanner) {
System.out.println("Do you want to play? (Y / N)");
return scanner.nextLine();
}
private void checkPlay(String command) {
if (command.toUpperCase().charAt(0) != 'Y') {
System.out.println("Bye then!");
}
}
private void loadPlayers(Players players, Scanner scanner) {
players.loadPlayers();
System.out.println("Who are you playing as?");
players.setCurrentPlayer(scanner.nextLine().toUpperCase());
}
private Cryptogram typeCrypto(Scanner scanner){
System.out.println("Do you want to play? (Y / N)");
String type = scanner.nextLine().toUpperCase();
switch(type){
case "L", "LETTER", "LETTER CRYPTO" -> {
return new LetterCryptogram();
}
case "N", "NUMBER", "NUMBER CRYPTO" -> {
return new NumberCryptogram();
}
}
return new LetterCryptogram();
}
private boolean askToLoadSavedGame(Scanner scanner) {
System.out.println("Do you want to load saved game ? (Y/N)");
return scanner.nextLine().equalsIgnoreCase("Y");
}
private String[] getOption(Scanner scanner) {
System.out.println("Enter a command or type 'manual' / 'm'");
String command = scanner.nextLine();
return command.toUpperCase().split("\\s+");
}
private boolean askToSave(Scanner scanner) {
System.out.println("Would you like to save your game? Y | N");
return scanner.nextLine().equalsIgnoreCase("Y");
}
private boolean askToOverwrite(Scanner scanner) {
System.out.println("You will overwrite your previously saved game, are you sure? Y | N");
return scanner.nextLine().equalsIgnoreCase("Y");
}
public void play() throws IOException {
Scanner scanner = new Scanner(System.in);
String command = askToPlay(scanner);
checkPlay(command);
loadPlayers(players, scanner);
// TODO: load cryptogram from player save data
Cryptogram cg = typeCrypto(scanner);
boolean saveGameFileExists = false, corrupted = false, loadGame;
loadGame = askToLoadSavedGame(scanner);
try {
saveGameFileExists = cg.checkIfSaveGameExists(players.getCurrentPlayer().getUsername());
} catch (IOException e1) {
corrupted = true;
}
if (saveGameFileExists && loadGame) {
try {
if (!corrupted) {
cg.loadPreviousGame();
System.out.println("Previous game loaded.");
} else {
System.out.println("Corrupted save file");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
else if (loadGame && corrupted) {
System.out.println("Corrupted save file");
return;
}
else if (!saveGameFileExists && loadGame) {
System.out.println("No save game exists, new game started.");
players.getCurrentPlayer().incrementCryptogramsPlayed();
}
else {
System.out.println("New game started.");
players.getCurrentPlayer().incrementCryptogramsPlayed();
}
clearScreen();
boolean running = true;
while (running) {
cg.render();
String[] params = getOption(scanner);
clearScreen();
switch (params[0]) {
case "GUESS", "G" -> {
if (params.length < 3) {
System.out.println("This command requires a place and a value");
break;
}
else {
System.out.println(cg.guess(params[1], params[2].charAt(0)) ? "Placed letter successfully"
: "Error placing letter");
players.getCurrentPlayer().incrementTotalGuesses();
}
if (cg.isCorrectGuess(params[1], params[2].charAt(0)))
players.getCurrentPlayer().incrementTotalCorrectGuesses();
if (cg.isGuessFull()) {
if (cg.checkSolution()) {
System.out.println("Congratulations " + players.getCurrentPlayer().getUsername() + "! " + " The correct solution was " + cg.phrase);
players.getCurrentPlayer().incrementCryptogramsCompleted();
//players.getCurrentPlayer().incrementTotalGuesses();
players.savePlayers();
Main.main(new String[] {});
} else {
System.out.println("Incorrect " + players.getCurrentPlayer().getUsername() + "! Your current solution is not correct.");
//players.getCurrentPlayer().incrementTotalGuesses();
}
//players.getCurrentPlayer().incrementCryptogramsPlayed();
}
players.savePlayers();
//cg.save(players.getCurrentPlayer().getUsername());
}
case "UNDO", "U" -> System.out.println(cg.undo(params[1]) ? "Removed letter successfully" : "Error removing letter");
case "SOLUTION", "S" -> {
players.getCurrentPlayer().incrementCryptogramsPlayed();
System.out.println(cg.getPhrase());
System.out.println("Correct solution now shown");
Main.main(new String[] { "a", "b" });
checkSolution(cg);
players.savePlayers();
cg.clearGameFile();
}
case "HINT", "H" -> System.out.println("Work in progress");
case "FREQUENCY", "F", "FREQUENCIES" -> System.out.println("Work in progress1");
// for debug only
case "PLAYER", "P" -> System.out.println(players.getCurrentPlayer().toString());
case "STATISTICS", "STATS" -> players.displayPlayer();
case "SCORES", "SCORE","TOP10" -> {
try {
players.getTopScores();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
case "MANUAL", "M" -> {
System.out.println("guess | g <place> <value> - guess a letter at the place marker");
System.out.println("undo | u <place> - remove letter at the place marker");
System.out.println("solution | s - show the completed solution");
System.out.println("frequency| f - toggle frequency on/off");
System.out.println("hint | h - correctly place a letter");
System.out.println("statistics| stats - show current player info");
System.out.println("player | p - show the current player");
System.out.println("scores | top10 - show the top 10 best players");
System.out.println("manual | m - display the manual you are looking at");
System.out.println("quit | q - save all data and quit");
}
case "QUIT", "Q" -> {
boolean saveCheck;
try {
saveCheck = cg.checkIfSaveGameExists(players.getCurrentPlayer().getUsername());
} catch (IOException e) {
saveCheck = false;
}
if(askToSave(scanner)) {
if(saveCheck) {
if(askToOverwrite(scanner)) {
System.out.println("Overwritten save game, game has been quit.");
cg.save(players.getCurrentPlayer().getUsername());
running = false;
}
else {
System.out.println("Game has been quit without saving.");
running = false;
}
}
else {
System.out.println("Your new game has been saved.");
cg.save(players.getCurrentPlayer().getUsername());
running = false;
}
}
else {
System.out.println("Game has been quit without saving.");
running = false;
}
}
default -> System.out.println("Unknown command!");
}
}
System.out.println();
players.savePlayers();
}
}