Datasets:

ArXiv:
License:
File size: 12,532 Bytes
c574d3a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
package client.src.controleurs.tictactoe;

import java.io.Serializable;
import java.net.MalformedURLException;
import java.net.URL;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.util.ResourceBundle;
import java.util.UUID;

import client.src.ClientMain;
import commun.Cellule;
import commun.ITicTacToe;
import commun.ITicTacToeListener;
import commun.NullBool;
import javafx.application.Platform;
import javafx.fxml.FXML;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.scene.Group;
import javafx.scene.control.Label;
import javafx.scene.control.ListView;
import javafx.scene.control.TextField;
import javafx.scene.control.Alert.AlertType;

public class TicTacToeControleur extends client.src.controleurs.BaseControleur {
    private ITicTacToe partie;
    private UUID id;

    private TicTacToeMonitor monitor;

    @FXML
    private Pane pane_caseConteneur;
    @FXML
    private StackPane sp_mainConteneur;
    @FXML
    private VBox vbox_lobbyConteneur;
    @FXML
    private TextField tf_entrerSalon;
    @FXML
    private ListView<String> lv_salonListe;
    @FXML
    private Label lbl_log;
    @FXML
    private Label lbl_nomSalon;

    private boolean estTonTour;
    private boolean partieTerminee;
    private Cellule numJoueur;

    @Override
    public void initialize(URL location, ResourceBundle ressources) {
        try {
            // On se connecte a userveur
            this.partie = (ITicTacToe) Naming.lookup("rmi://" + ClientMain.HOTE + ":" + ClientMain.PORT + "/tictactoe");
            this.monitor = new TicTacToeMonitor(this);
            // On initialise le lobby
            initLobby();

            // Evenement lors du clic de la croix de la fenetre
            Platform.runLater(() -> {
                _vue.setOnCloseRequest((event) -> {
                    quitter();
                });
            });

        } catch (RemoteException | NotBoundException | MalformedURLException e) {
            showErreurAlerte("TicTacToe exception: ", e.toString());
            this.fermer();
        }
    }

    public void quitter() {
        try {
            // Si l'id n'est pas null, on ferme le salon
            if (id != null) {
                this.partie.quitterSalon(this.id, monitor);
                this.id = null;
            }
        } catch (RemoteException e) {
            showErreurAlerte("TicTacToe exception: ", e.toString());
        }
        this._vue.close();
    }

    private void initLobby() throws RemoteException {
        // On affiche que ce qui est necessaire sur la fenetre
        sp_mainConteneur.setVisible(false);
        tf_entrerSalon.setText("");
        vbox_lobbyConteneur.setVisible(true);
        setLog("");
        setNomSalon("");

        // On rerempli la liste des salonss
        lv_salonListe.getItems().clear();
        var noms = this.partie.recupererNoms();
        noms.forEach((key, value) -> {
            lv_salonListe.getItems().add(key);
        });

        // Evenement de clic sur un element de la liste
        lv_salonListe.setOnMouseClicked((event) -> {
            // On recupere le nom du salon sur le quel l'utilisateur a cliqué
            var nomSalon = lv_salonListe.getSelectionModel().getSelectedItem();
            if (event.getClickCount() == 2 && nomSalon != null)
                try {
                    var salonID = noms.get(nomSalon);
                    // Verification de si le salon existe
                    if (salonID != null && this.partie.rejoindreSalon(salonID, monitor)) {
                        this.id = salonID;
                        setNomSalon(nomSalon);
                        vbox_lobbyConteneur.setVisible(false);
                        sp_mainConteneur.setVisible(false);
                    } else
                        showErreurAlerte("TicTacToe erreur: ", "Nous n'avons pas pu rejoindre le salon.");
                } catch (RemoteException e) {
                    showErreurAlerte("TicTacToe exception: ", "Nous n'avons pas pu rejoindre le salon.");
                    e.printStackTrace();
                }
        });
    }

    public void onEnterEntrerSalon() throws RemoteException {
        // Fonction lorsque l'utilisateur fait entrer dans la textfield
        var noms = this.partie.recupererNoms();
        var nomSalon = tf_entrerSalon.getText().trim();
        var salonID = noms.get(nomSalon);
        // Verification de si le salon existe
        if (salonID != null && this.partie.rejoindreSalon(salonID, monitor)) {
            this.id = salonID;
            setNomSalon(nomSalon);
            vbox_lobbyConteneur.setVisible(false);
            sp_mainConteneur.setVisible(false);
        } else
            showErreurAlerte("TicTacToe exception: ", "Nous n'avons pas pu rejoindre le salon.");

    }

    public void nouveauSalon() throws RemoteException {
        // Fonction lors du clic sur le bouton de nouveau salon
        this.id = this.partie.nouveauSalon(monitor);

        // On récupère le nom du salon pour l'afficher
        var noms = this.partie.recupererNoms();
        noms.forEach((key, value) -> {
            if (value.equals(this.id))
                setNomSalon(key);
        });

        // On attend un autre joueur
        attendAutreJoueur();
    }

    public void initPartie() throws RemoteException {
        Platform.runLater(() -> {
            // On affiche ce qui est necessaire
            vbox_lobbyConteneur.setVisible(false);
            sp_mainConteneur.setVisible(true);
            partieTerminee = false;

            // On initialise les cases du jeu
            for (int i = 0; i < 9; i++)
                caseCliquable((Group) pane_caseConteneur.getChildren().get(i), i);
        });
    }

    private void attendAutreJoueur() {
        // Affiche d'attente d'un autre joueur
        vbox_lobbyConteneur.setVisible(false);
        sp_mainConteneur.setVisible(false);
        setLog("En attente d'un autre joueur...");
    }

    private void addCroix(Group grp, boolean isMoi) {
        grp.getChildren().clear();

        // On dessine la croix
        Rectangle tige1 = new Rectangle(.1, .7);
        tige1.setFill(isMoi ? Color.rgb(0, 122, 204) : Color.rgb(204, 0, 85));
        tige1.setArcHeight(.1);
        tige1.setArcWidth(.1);
        tige1.setRotate(-45);
        Rectangle tige2 = new Rectangle(.10, .7);
        tige2.setFill(isMoi ? Color.rgb(0, 122, 204) : Color.rgb(204, 0, 85));
        tige2.setArcHeight(.1);
        tige2.setArcWidth(.1);
        tige2.setRotate(45);

        grp.getChildren().addAll(tige1, tige2);
        // On enleve l'effet de clic
        grp.setOnMouseClicked(null);
    }

    private void addRond(Group grp, boolean isMoi) {
        grp.getChildren().clear();

        // On dessine le rond
        Circle cercle = new Circle(.28, Color.TRANSPARENT);
        cercle.setStrokeWidth(.1);
        cercle.setStroke(isMoi ? Color.rgb(0, 122, 204) : Color.rgb(204, 0, 85));

        grp.getChildren().add(cercle);
        // On enleve l'effet de clic
        grp.setOnMouseClicked(null);
    }

    private void caseCliquable(Group grp, int i) {
        grp.getChildren().clear();

        // On dessine une forme de fond de case pour cliquer dessus
        Rectangle fond = new Rectangle(1, 1);
        fond.setFill(Color.TRANSPARENT);
        grp.getChildren().add(fond);

        // Evenement de clic sur une case pour la jouer
        grp.setOnMouseClicked((e) -> {
            try {
                if (estTonTour)
                    this.partie.jouer(id, i % 3, i / 3, this.monitor);
            } catch (RemoteException e1) {
            }
        });
    }

    private void setLog(String logString) {
        Platform.runLater(() -> {
            lbl_log.setText(logString);
        });
    }

    private void setNomSalon(String nomSalon) {
        Platform.runLater(() -> {
            lbl_nomSalon.setText(nomSalon);
        });
    }

    public void partieLancee(boolean estTonTour, Cellule numJoueur) throws RemoteException {
        // La partie peut etre lancée, on l'initialise
        initPartie();
        this.numJoueur = numJoueur;
        setTour(estTonTour);
    }

    public void joueurRejoindre() throws RemoteException {
        setLog("Un joueur a rejoint la partie.");
    }

    public void joueurQuitter() throws RemoteException {
        // Un joueur a quitté la partie
        // Ne s'affiche que lorsque la partie n'est pas déjà terminée
        if (!partieTerminee)
            Platform.runLater(() -> {
                showAlerte("TicTacToe", "", "Vous avez gagné car le joueur en face a quitté la partie.",
                        AlertType.INFORMATION);
                try {
                    if (id != null)
                        this.partie.quitterSalon(this.id, monitor);
                    initLobby();
                } catch (RemoteException e) {
                    e.printStackTrace();
                    quitter();
                }
            });
    }

    public void celluleMAJ(int x, int y, Cellule status, boolean estTonTour) throws RemoteException {
        setTour(estTonTour);
        // Dessine sur la case concernées
        switch (status) {
        case JOUEUR_1:
            Platform.runLater(() -> addCroix((Group) pane_caseConteneur.getChildren().get(x + 3 * y),
                    numJoueur == Cellule.JOUEUR_1));
            break;
        case JOUEUR_2:
            Platform.runLater(() -> addRond((Group) pane_caseConteneur.getChildren().get(x + 3 * y),
                    numJoueur == Cellule.JOUEUR_2));
            break;
        default:
            break;
        }
    }

    private void setTour(boolean estTonTour) {
        // Affiche si c'est le tour du joueur ou non
        this.estTonTour = estTonTour;
        setLog(this.estTonTour ? "C'est à vous de jouer." : "En attente de l'autre joueur...");
        // Effet d'opacité sur la grille pour reconnaitre si c'est au client de jouer
        sp_mainConteneur.setOpacity(this.estTonTour ? 1. : .6);
    }

    public void aGagner(NullBool estGagnant) throws RemoteException {
        Platform.runLater(() -> {
            partieTerminee = true;
            // On observe le cas de fin de partie
            if (estGagnant == NullBool.TRUE)
                showAlerte("TicTacToe", "", "Vous avez gagné la partie.", AlertType.INFORMATION);
            else if (estGagnant == NullBool.FALSE)
                showAlerte("TicTacToe", "", "Vous avez perdu la partie.", AlertType.INFORMATION);
            else
                showAlerte("TicTacToe", "", "Vous avez fait égalité.", AlertType.INFORMATION);

            try {
                // On fait quitter le salon au joueur
                if (id != null) {
                    this.partie.quitterSalon(this.id, monitor);
                    this.id = null;
                }
                // On réinitialise le lobby
                initLobby();
            } catch (RemoteException e) {
                e.printStackTrace();
                quitter();
            }
        });
    }
}

class TicTacToeMonitor extends java.rmi.server.UnicastRemoteObject implements ITicTacToeListener, Serializable {
    private transient TicTacToeControleur controller;

    public TicTacToeMonitor(TicTacToeControleur controller) throws RemoteException {
        this.controller = controller;
    }

    @Override
    public void partieLancee(boolean estTonTour, Cellule numJoueur) throws RemoteException {
        controller.partieLancee(estTonTour, numJoueur);
    }

    @Override
    public void joueurRejoindre() throws RemoteException {
        controller.joueurRejoindre();
    }

    @Override
    public void joueurQuitter() throws RemoteException {
        controller.joueurQuitter();
    }

    @Override
    public void celluleMAJ(int x, int y, Cellule status, boolean estTonTour) throws RemoteException {
        controller.celluleMAJ(x, y, status, estTonTour);
    }

    @Override
    public void aGagner(NullBool estGagnant) throws RemoteException {
        controller.aGagner(estGagnant);
    }
}

class CelluleMAJ implements Serializable {
    public int x;
    public int y;
    public Cellule status;

    public CelluleMAJ(int x, int y, Cellule status) {
        this.x = x;
        this.y = y;
        this.status = status;
    }
}