Online-TicTacToe

Log | Files | Refs | README | LICENSE

commit 6df8d41f340f417db2657e679b8b7c4d9bf72cc2
parent 4d2077ea76ebab9b299e2bd68da6b458d0c9fd38
Author: adglas21 <adamglasheen99@gmail.com>
Date:   Tue, 26 Nov 2024 19:24:48 +0000

Implemented, check who wins, reset the game  after winner

Diffstat:
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/TicTacToeGrid.java | 19+++++++++----------
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/HelperMethods.java | 47+++++++++++++++++++++++++++++++++++++++++++++++
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/PollingThread.java | 40++++++++++++++++++++++++++++++++++++++++
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/TicTacToeClient.java | 15++++++++++++++-
4 files changed, 110 insertions(+), 11 deletions(-)

diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/TicTacToeGrid.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/TicTacToeGrid.java @@ -13,13 +13,13 @@ import java.awt.GridLayout; import java.util.ArrayList; import javax.swing.JButton; import javax.swing.JPanel; -import javax.swing.border.LineBorder; +import javax.swing.*; /** * * @author William */ -class TicTacToeGrid extends JPanel { +public class TicTacToeGrid extends JPanel { TicTacToeClient client; ArrayList<JButton> gridButtons; @@ -50,9 +50,9 @@ class TicTacToeGrid extends JPanel { button.setText("O"); } } - button.setEnabled(false); // prevents the player from clicking the same spot again - }); - } + button.setEnabled(false); // prevents the player from clicking the same spot again + }); + } } public void refresh() { @@ -80,11 +80,11 @@ class TicTacToeGrid extends JPanel { } else { gridButtons.get(index).setText("O"); gridButtons.get(index).setEnabled(false); - } + } } - + this.revalidate(); this.repaint(); } - -} -\ No newline at end of file + +} diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/HelperMethods.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/HelperMethods.java @@ -4,8 +4,10 @@ */ package com.groupproject.tictactoeclient; +import com.groupproject.tictactoeclient.ContentPanes.MainContentPanel; import java.util.ArrayList; import java.util.Arrays; +import javax.swing.JOptionPane; /** * @@ -63,4 +65,49 @@ public class HelperMethods { return false; } } + + //https://www.geeksforgeeks.org/java-joptionpane/ + public void showOptions() { + + String[] options = {"Main Menu", "Quit"}; + + // Display an option dialog with custom options + // The user's choice is stored in the 'choice' + // variable + int choice = JOptionPane.showOptionDialog( + null, // Parent component (null means center on screen) + "Options", // Message to display + "Custom Options", // Dialog title + JOptionPane.YES_NO_CANCEL_OPTION, // Option type (Yes, No, Cancel) + JOptionPane.QUESTION_MESSAGE, // Message type (question icon) + null, // Custom icon (null means no custom icon) + options, // Custom options array + options[0] // Initial selection (default is "Cancel") + ); + + // Check the user's choice and display a + // corresponding message + if (choice == JOptionPane.YES_OPTION) { + // If the user chose 'Yes' + // show a message indicating that they are + // proceeding + client.resetGame(); + client.showPanel(new MainContentPanel(client)); + } + else if (choice == JOptionPane.NO_OPTION) { + // If the user chose 'No' + // show a message indicating that they are not + // proceeding + + System.exit(0); + } + else { + // If the user chose 'Cancel' or closed the + // dialog + // show a message indicating the operation is + // canceled + JOptionPane.showMessageDialog(null, "Operation canceled."); + } + + } } diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/PollingThread.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/PollingThread.java @@ -4,8 +4,10 @@ */ package com.groupproject.tictactoeclient; +import com.groupproject.tictactoeclient.ContentPanes.MainContentPanel; import java.util.logging.Level; import java.util.logging.Logger; +import javax.swing.JOptionPane; /** * @@ -15,6 +17,9 @@ public class PollingThread implements Runnable { private TicTacToeClient client; private HelperMethods helperMethods; + //flag used to keep track if game is over so the dialog box doesnt keep appearing + private boolean gameOver = false; + public PollingThread(TicTacToeClient client) { this.client = client; helperMethods = new HelperMethods(client); @@ -36,6 +41,41 @@ public class PollingThread implements Runnable { client.openGames = client.proxy.showOpenGames(); System.out.println("Result: " + client.openGames); client.refreshCurrentPanel(); + + + //call the web server to check if the game has been won or not yet + String result = client.proxy.checkWin(Integer.parseInt(client.GID)); + + //if the game is not over check the result of the game + if(!gameOver) { + switch (result) { + case "1": + // Player 1 wins + JOptionPane.showMessageDialog(client.frame, "Player 1 wins!"); + helperMethods.showOptions(); + gameOver = true; + break; + case "2": + // Player 2 wins + JOptionPane.showMessageDialog(client.frame, "Player 2 wins!"); + helperMethods.showOptions(); + gameOver = true; + break; + case "3": + // It's a draw + JOptionPane.showMessageDialog(client.frame, "It's a draw!"); + helperMethods.showOptions(); + gameOver = true; + break; + case "0": + // Game continues + break; + default: + JOptionPane.showMessageDialog(client.frame, "Error checking the game status."); + + } + } + } catch (Exception e) { e.printStackTrace(); } diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/TicTacToeClient.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/TicTacToeClient.java @@ -25,7 +25,7 @@ public class TicTacToeClient { private static TicTacToeClient client; private static TicTacToeWebService service; public static TicTacToeWS proxy; - private JFrame frame; + public JFrame frame; private CustomPanel CurrentPanel; public String UID; public String GID; @@ -81,4 +81,17 @@ public class TicTacToeClient { public void refreshCurrentPanel() { CurrentPanel.refresh(); } + + //reset the game data when user goes back to the main menu + //might be more to do but at the moment it works fine + public void resetGame() { + + this.GID = null; + this.UID2 = null; + this.HOST_UID = " "; + refreshCurrentPanel(); +// } + + +} } \ No newline at end of file