forked from CODEScs3250/ConsoleGameHub
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameLauncher.java
More file actions
167 lines (150 loc) · 5.37 KB
/
GameLauncher.java
File metadata and controls
167 lines (150 loc) · 5.37 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
import java.util.Scanner;
import java.io.IOException;
import java.util.List;
import java.util.ArrayList;
import java.util.Optional;
/**
* Console Game Hub.
* Provides an arcade-style launcher for multiple games,
* with support for scoring and history tracking.
*
* @author ChatGPT (from engineered prompts)
* @author Dr. Jody Paul
* @version 2.1 (Refactored. Includes dependency injection for testability.)
*/
public class GameLauncher {
/** Default history file name. */
private static final String HISTORY_FILENAME = "history.dat";
/** Name of the history file. */
private String historyFileName;
/** Console input. */
private final Scanner scanner;
/** Collection of known games. */
private final List<Game> games;
/** Game history tracker. */
private final GameHistoryTracker historyTracker;
/**
* Default constructor, used in production.
* Loads default games, scanner, and history tracker.
*/
public GameLauncher() {
this(new Scanner(System.in),
GameHistoryTracker.loadHistory(HISTORY_FILENAME),
registerGames(),
HISTORY_FILENAME);
}
/**
* Constructor with injected testable components.
*
* @param inputScanner the console input
* @param tracker the tracker to record and save history
* @param gameList the list of games available to play
* @param fileName the name of the history tracker file
*/
public GameLauncher(final Scanner inputScanner,
final GameHistoryTracker tracker,
final List<Game> gameList,
final String fileName) {
this.scanner = inputScanner;
this.historyTracker = tracker;
this.games = gameList;
this.historyFileName = fileName;
}
/**
* Main entry point of the application.
* @param args command-line arguments (not used)
*/
public static void main(final String[] args) {
GameLauncher launcher = new GameLauncher();
launcher.run();
launcher.saveHistory();
}
/**
* Registers all available games in the arcade.
* @return list of all available games
*/
private static List<Game> registerGames() {
List<Game> games = new ArrayList<>();
games.add(new TicTacToeGame());
games.add(new WordGuessGame());
games.add(new JottoGame());
games.add(new HangmanGame());
games.add(new SnakeGame());
games.add(new ConnectFourGame());
games.add(new MineSweeperGame());
games.add(new SudokuGame());
games.add(new NumberGuessGame());
games.add(new MemoryMatchGame());
games.add(new LightsOutGame());
games.add(new MasterMindGame());
return games;
}
/**
* Runs the game launcher loop.
* Allows user to choose and play games, and to view history.
*/
protected void run() {
boolean running = true;
while (running) {
System.out.println("\n=== Console Arcade Hub ===");
for (int i = 0; i < this.games.size(); i++) {
System.out.printf("%d. %s\n",
i + 1,
this.games.get(i).getName());
}
System.out.println("0. Exit");
System.out.println("H. View Game History");
System.out.println("C. Clear Game History");
System.out.print("Choose a game: ");
String input = this.scanner.nextLine().trim();
if (input.equalsIgnoreCase("H")) {
this.historyTracker.displayHistory();
continue;
} else if (input.equalsIgnoreCase("C")) {
confirmClearHistory();
continue;
}
try {
int choice = Integer.parseInt(input);
if (choice == 0) {
running = false;
System.out.println("Goodbye!");
} else if (choice > 0 && choice <= this.games.size()) {
Game game = this.games.get(choice - 1);
Optional<Integer> score = game.play();
this.historyTracker.recordPlay(game.getName(),
score.orElse(null));
} else {
System.out.println("Invalid choice.");
}
} catch (NumberFormatException e) {
System.out.println(
"Please enter a valid number or Letters H or C.");
}
}
}
/**
* Clears the history after user confirmation.
*
*/
private void confirmClearHistory() {
System.out.println("Are you sure you want to clear history?");
System.out.println("type CLEAR to confirm");
System.out.println("or type anything else to cancel.");
String input = this.scanner.nextLine().trim();
if (input.equalsIgnoreCase("clear")) {
System.out.println("Clearing History.\n");
historyTracker.clearHistory("history.dat");
}
}
/**
* Saves the history of games played.
*/
protected void saveHistory() {
try {
this.historyTracker.saveHistory(this.historyFileName);
} catch (IOException e) {
System.out.println("game history save failed: " + e.getMessage());
}
}
}