forked from CidWB/COMP3607_Jeopardy_Project
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGame.java
More file actions
112 lines (87 loc) · 3.14 KB
/
Game.java
File metadata and controls
112 lines (87 loc) · 3.14 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
import java.util.Scanner;
public class Game {
private static Game instance;
private int caseId;
private GameConfig config;
private QuestionList questions;
private PlayerList players;
private GameEventLogPublisher publisher;
private Scanner scanner;
private boolean isRunning;
private Game() {
this.caseId = 1;
this.scanner = new Scanner(System.in);
this.isRunning = false;
this.publisher = new GameEventLogPublisher();
}
public static Game getInstance() {
if (instance == null) {
instance = new Game();
}
return instance;
}
public void setupGame(GameConfig config) {
this.config = config;
this.questions = config.getQuestions();
this.players = config.getPlayers();
// Setup observers
OutputEventLogger outputLogger = new OutputEventLogger();
CsvEventLogger csvLogger = new CsvEventLogger("game_log.csv");
publisher.addObserver(outputLogger);
//publisher.addObserver(csvLogger);
System.out.println("\nGame setup complete!");
}
public void playGame() {
if (questions == null || players == null) {
System.out.println("Game not properly configured!");
return;
}
isRunning = true;
System.out.println("\n" + "=".repeat(60));
System.out.println("GAME START!");
System.out.println("Type 'QUIT' at any category prompt to end the game");
System.out.println("=".repeat(60));
while (isRunning && questions.checkHasQuestions()) {
questions.display();
// Check if there's a question to answer
if (!questions.checkHasQuestions()) {
System.out.println("\nNo more questions available!");
break;
}
// Player's turn
System.out.print("\nPress Enter to continue or type QUIT to end game: ");
String input = scanner.nextLine();
if (input.equalsIgnoreCase("QUIT")) {
quitGame();
break;
}
players.startTurn(questions, scanner);
players.endTurn();
}
if (isRunning) {
quitGame();
}
}
public void quitGame() {
isRunning = false;
System.out.println("\n" + "=".repeat(60));
System.out.println("GAME OVER - FINAL SCORES");
System.out.println("=".repeat(60));
// Display final scores
for (Player player : players.getPlayers()) {
System.out.println(player.getPlayerId() + ": " + player.getScore() + " points");
}
if (players.getCurrentPlayer() != null) {
Player current = players.getCurrentPlayer();
System.out.println(current.getPlayerId() + ": " + current.getScore() + " points");
}
System.out.println("=".repeat(60));
System.out.println("Game log saved to game_log.csv");
}
public int getCaseId() {
return caseId;
}
public void incrementCaseId() {
this.caseId++;
}
}