Online-TicTacToe

Log | Files | Refs | README | LICENSE

commit e21e04de8ee520ef15a5b3f70dcbcfab981c176f
parent 6f85ed92342b741fec56413b737fc2bb1e34ca91
Author: William Lindholm <william_lindholm@outlook.com>
Date:   Wed, 27 Nov 2024 15:59:08 +0000

Now checking turn before allowing player to make a move

Diffstat:
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/TicTacToeGrid.java | 16+++++++++-------
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/PollingThread.java | 6++++++
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/TicTacToeClient.java | 46++++++++++++++++++++++------------------------
3 files changed, 37 insertions(+), 31 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 @@ -57,22 +57,24 @@ public 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 + client.OPPONENTS_TURN = true; // prevent user from taking square again }); } } public void refresh() { - System.out.println("UID: " + client.UID + ", HOST_UID: " + client.HOST_UID + ", GAME_ID: " + client.GID); + System.out.println("UID: " + client.UID + ", HOST_UID: " + client.HOST_UID + ", GAME_ID: " + client.GID + ", MOVES: " + client.NUM_OF_MOVES); if (client.BOARD == null) { return; } - - System.out.println("Updating TicTacToeGrid..."); - + final int[][] board = client.BOARD; - final int totalNumOfMoves = client.BOARD.length; - if (totalNumOfMoves % 2 == 0 && helperMethods.IsHost()) { + client.NUM_OF_MOVES = client.BOARD.length; + if (client.NUM_OF_MOVES % 2 == 0 && helperMethods.IsHost()) { + client.OPPONENTS_TURN = false; + } + if (client.NUM_OF_MOVES % 2 == 1 && !helperMethods.IsHost()) { client.OPPONENTS_TURN = false; } diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/PollingThread.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/PollingThread.java @@ -41,10 +41,16 @@ public class PollingThread implements Runnable { client.openGames = client.proxy.showOpenGames(); client.refreshCurrentPanel(); + // if there is no game id, dont run code below if (client.GID.isBlank() || client.GID.isEmpty()) { continue; } + // if there are no moves, dont run code below + if (client.NUM_OF_MOVES < 2) { + continue; + } + //call the web server to check if the game has been won or not yet String result = client.proxy.checkWin(Integer.parseInt(client.GID)); diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/TicTacToeClient.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/TicTacToeClient.java @@ -1,9 +1,8 @@ /* * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license */ - - package com.groupproject.tictactoeclient; + import com.formdev.flatlaf.FlatDarkLaf; import com.groupproject.tictactoeclient.ContentPanes.CustomPanel; import com.groupproject.tictactoeclient.ContentPanes.MainContentPanel; @@ -22,6 +21,7 @@ import javax.swing.UnsupportedLookAndFeelException; * @author William */ public class TicTacToeClient { + private static TicTacToeClient client; private static TicTacToeWebService service; public static TicTacToeWS proxy; @@ -30,13 +30,13 @@ public class TicTacToeClient { public String UID = ""; public String GID = ""; public boolean OPPONENTS_TURN = false; + public int NUM_OF_MOVES = 0; public String UID2; public String GID_Temp; public String HOST_UID = ""; public String username; public String openGames; public int[][] BOARD; - public static void main(String[] args) { // Enable flatlaf dark theme @@ -44,34 +44,34 @@ public class TicTacToeClient { UIManager.setLookAndFeel(new FlatDarkLaf()); } catch (UnsupportedLookAndFeelException e) { JOptionPane.showMessageDialog( - null, - "Could not initialize FlatLaf. Default swing look and feel will be used.", - "Error", - JOptionPane.ERROR_MESSAGE + null, + "Could not initialize FlatLaf. Default swing look and feel will be used.", + "Error", + JOptionPane.ERROR_MESSAGE ); } - + // Initialize SOAP interface service = new TicTacToeWebService(); proxy = service.getTicTacToeWSPort(); - + // Initalize UI client = new TicTacToeClient(); } - + public TicTacToeClient() { frame = new JFrame("TicTacToe"); // Create new Swing window frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.setSize(600, 400); - //showPanel(new register(this)); + //showPanel(new register(this)); showPanel(new StartPanel(this)); - + frame.setVisible(true); - + Thread t1 = new Thread(new PollingThread(this)); t1.start(); } - + public void showPanel(CustomPanel panel) { frame.getContentPane().removeAll(); // Clear current content CurrentPanel = panel; @@ -79,21 +79,20 @@ public class TicTacToeClient { frame.revalidate(); // Refresh the frame frame.repaint(); } - + 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.GID = ""; this.UID2 = null; - this.HOST_UID = " "; + this.HOST_UID = ""; + this.NUM_OF_MOVES = 0; refreshCurrentPanel(); -// } - - + } + } -} -\ No newline at end of file