-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathMain.java
More file actions
89 lines (56 loc) · 2.23 KB
/
Main.java
File metadata and controls
89 lines (56 loc) · 2.23 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
package co.ppg2;
import co.ppg2.controllers.GameController;
import co.ppg2.controllers.PlayerDataController;
import co.ppg2.model.Player;
import co.ppg2.services.GameTimer;
import co.ppg2.views.GameView;
import co.ppg2.views.PlayerPopup;
import javafx.application.Application;
import javafx.stage.Stage;
import java.util.ArrayList;
public class Main extends Application {
private static ArrayList<Player> players;
public static GameController gameController;
public static GameView gameView;
public static GameTimer gameTimer; // Shared GameTimer instance
public static void main(String[] args) {
players = PlayerDataController.loadPlayers(); // Load players on start
launch(args);
}
@Override
public void start(Stage primaryStage) {
// Prompt for Player X's username and Player O's username
Player playerX = PlayerPopup.showPopup("Player X");
Player playerO = PlayerPopup.showPopup("Player O");
// Find or add Player X and Player O in the players list
playerX = findOrAddPlayer(playerX.getUsername());
playerO = findOrAddPlayer(playerO.getUsername());
// Initialize shared GameTimer
gameTimer = new GameTimer();
// Initialize GameController and GameView
gameController = new GameController(playerX, playerO);
gameController.setGameTimer(gameTimer); // Pass GameTimer to GameController
gameView = new GameView(gameController, primaryStage);
gameController.setGameView(gameView);
// Launch the game view
gameView.launchGame();
// Start the timer for Player X
gameTimer.startTimer(playerX.getUsername());
// add an exit for the application
primaryStage.setOnCloseRequest(event -> {
System.out.println("Game is closing...");
System.exit(0);
});
}
private Player findOrAddPlayer(String username) {
for (Player player : players) {
if (player.getUsername().equals(username)) {
return player;
}
}
Player newPlayer = new Player(username);
players.add(newPlayer);
PlayerDataController.savePlayers(players);
return newPlayer;
}
}