Online-TicTacToe

Log | Files | Refs | README | LICENSE

commit 4d2077ea76ebab9b299e2bd68da6b458d0c9fd38
parent 70038b3496b5a6ce64a98ff7250f5b32614c3121
Author: William Lindholm <william_lindholm@outlook.com>
Date:   Sun, 24 Nov 2024 16:47:55 +0000

Implemented SOAP calls for gameplay, tictactoegrid now updates server

Diffstat:
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/MainContentPanel.java | 11+++++------
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/TicTacToeGrid.java | 90+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++--------------------
DTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/FetchGamesThread.java | 40----------------------------------------
ATicTacToeClient/src/main/java/com/groupproject/tictactoeclient/HelperMethods.java | 66++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
ATicTacToeClient/src/main/java/com/groupproject/tictactoeclient/PollingThread.java | 44++++++++++++++++++++++++++++++++++++++++++++
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/TicTacToeClient.java | 5+++--
6 files changed, 185 insertions(+), 71 deletions(-)

diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/MainContentPanel.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/MainContentPanel.java @@ -14,6 +14,7 @@ public class MainContentPanel extends CustomPanel { private JLabel gameTimerLabel; private Timer timer; private int remainingTime; + private TicTacToeGrid grid; public MainContentPanel(TicTacToeClient client) { this.client = client; @@ -28,7 +29,7 @@ public class MainContentPanel extends CustomPanel { JButton joinGameButton = new JButton("Join Game"); openGamesList = new JList<>(); JScrollPane openGamesScrollPane = new JScrollPane(openGamesList); - TicTacToeGrid grid = new TicTacToeGrid(client, 3, 3); // A 3x3 TicTacToe grid + grid = new TicTacToeGrid(client, 3, 3); // A 3x3 TicTacToe grid JLabel gameStatusLabel = new JLabel(); gameTimerLabel = new JLabel(); JButton scoreButton = new JButton("View Personal Score"); @@ -100,7 +101,7 @@ public class MainContentPanel extends CustomPanel { client.GID = client.proxy.newGame(Integer.parseInt(client.UID)); if (client.GID != null && !client.GID.isEmpty()) { - client.HOST = client.UID; + client.HOST_UID = client.UID; String newGameData = "Game " + client.GID + " Host: " + client.UID; if (client.openGames == null || client.openGames.isEmpty()) { client.openGames = newGameData; @@ -139,6 +140,8 @@ public class MainContentPanel extends CustomPanel { public void refresh() { System.out.println("Refreshing MainContentPanel"); // System.out.println("Raw client.openGames data: " + client.openGames); // Debugging output + + grid.refresh(); // Check if openGames is empty/null, if there is nothin then display nothing in the box if (client.openGames == null || client.openGames.isEmpty()) { @@ -168,10 +171,6 @@ public class MainContentPanel extends CustomPanel { openGamesList.setListData(formattedGames); } - - //update the UI (not sure if this is how to do it, saw on stack overflow) - this.revalidate(); - this.repaint(); } private void startTimer() { diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/TicTacToeGrid.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/TicTacToeGrid.java @@ -3,11 +3,14 @@ * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template */ package com.groupproject.tictactoeclient.ContentPanes; + +import com.groupproject.tictactoeclient.HelperMethods; import com.groupproject.tictactoeclient.TicTacToeClient; import java.awt.Color; import java.awt.Font; import java.awt.GridLayout; +import java.util.ArrayList; import javax.swing.JButton; import javax.swing.JPanel; import javax.swing.border.LineBorder; @@ -16,31 +19,72 @@ import javax.swing.border.LineBorder; * * @author William */ - class TicTacToeGrid extends JPanel { - TicTacToeClient client; - public TicTacToeGrid(TicTacToeClient client, int rows, int cols) { - this.client = client; - setLayout(new GridLayout(rows, cols, 5, 5)); - - for (int i = 0; i < rows * cols; i++) { - JButton button = new JButton(); - button.setFont(new Font("Arial", Font.BOLD, 32)); - add(button); - - button.addActionListener(e -> { - //need to add an if statment that checks the GID - //client.UID2 = client.proxy.leagueTable(); - if (client.UID.equals(client.HOST)){ +class TicTacToeGrid extends JPanel { + + TicTacToeClient client; + ArrayList<JButton> gridButtons; + HelperMethods helperMethods; + + public TicTacToeGrid(TicTacToeClient client, int rows, int cols) { + this.client = client; + helperMethods = new HelperMethods(client); + setLayout(new GridLayout(rows, cols, 5, 5)); + gridButtons = new ArrayList<JButton>(); + + for (int i = 0; i < rows * cols; i++) { + final int index = i; + JButton button = new JButton(); + gridButtons.add(button); + button.setFont(new Font("Arial", Font.BOLD, 32)); + add(button); + + button.addActionListener(e -> { + //need to add an if statment that checks the GID + //client.UID2 = client.proxy.leagueTable(); + if (client.UID.equals(client.HOST_UID)) { + if (helperMethods.TakeSqure(index)) { button.setText("X"); - }else{button.setText("O");} - button.setEnabled(false); // prevents the player from clicking the same spot again - }); - } + } + } else { + if (helperMethods.TakeSqure(index)) { + button.setText("O"); + } + } + button.setEnabled(false); // prevents the player from clicking the same spot again + }); } - + } public void refresh() { - throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody + System.out.println("UID: " + client.UID + ", HOST_UID: " + client.HOST_UID); + if (client.BOARD == null) { + return; + } + + System.out.println("Updating TicTacToeGrid..."); + + final int[][] board = client.BOARD; + + for (int[] row : board) { + // Convert grid coordinates to proper index + int index = (row[2] * 3) + row[1]; + + // if there is no host player id saved save it from detected move + if (!String.valueOf(row[0]).equals(client.UID) && client.HOST_UID.isBlank()) { + client.HOST_UID = String.valueOf(row[0]); + } + + if (String.valueOf(row[0]).equals(client.HOST_UID)) { + gridButtons.get(index).setText("X"); + gridButtons.get(index).setEnabled(false); + } else { + gridButtons.get(index).setText("O"); + gridButtons.get(index).setEnabled(false); + } + } + + this.revalidate(); + this.repaint(); } - } -\ No newline at end of file +} +\ No newline at end of file diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/FetchGamesThread.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/FetchGamesThread.java @@ -1,40 +0,0 @@ -/* - * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license - * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template - */ -package com.groupproject.tictactoeclient; - -import java.util.logging.Level; -import java.util.logging.Logger; - -/** - * - * @author Willi - */ -public class FetchGamesThread implements Runnable { - private TicTacToeClient client; - - public FetchGamesThread(TicTacToeClient client) { - this.client = client; - } - - @Override - public void run() { - while (true) { - try { - Thread.sleep(1000); - - if (client.UID == null || Integer.parseInt(client.UID) == -1) { - continue; - } - - System.out.println("Fetch Games thread executing..."); - client.openGames = client.proxy.showOpenGames(); - System.out.println("Result: " + client.openGames); - client.refreshCurrentPanel(); - } catch (Exception e) { - e.printStackTrace(); - } - } - } -} diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/HelperMethods.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/HelperMethods.java @@ -0,0 +1,66 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com.groupproject.tictactoeclient; + +import java.util.ArrayList; +import java.util.Arrays; + +/** + * + * @author William + */ +public class HelperMethods { + private TicTacToeClient client; + + public HelperMethods(TicTacToeClient client) { + this.client = client; + } + + public int[][] GetBoard(int gid) { + System.out.println("Fetching board..."); + String response = client.proxy.getBoard(gid); + System.out.println(response); + + if ("ERROR-NOMOVES".equals(response)) { + return null; + } + + // Split by lines and map each line to an integer array + return Arrays.stream(response.split("\n")) + .map(row -> Arrays.stream(row.split(",")) + .mapToInt(Integer::parseInt) + .toArray()) + .toArray(int[][]::new); + } + + public int[][] GetBoard() { + if (client.GID == null) { + return null; + } + + return GetBoard(Integer.parseInt(client.GID)); + } + + public boolean IsHost() { + return client.UID.equals(client.HOST_UID); + } + + + /** + * Take the square with selected index + * @param index square to take + * @return return true if square was taken, false if it was already taken by opponent + */ + public boolean TakeSqure(int index) { + if ("0".equals(client.proxy.checkSquare(index % 3, (int) Math.floor(index / 3), Integer.parseInt(client.GID)))) { + System.out.println("Taking square"); + client.proxy.takeSquare(index % 3, (int) Math.floor(index / 3), Integer.parseInt(client.GID), Integer.parseInt(client.UID)); + return true; + } else { + System.out.println("Cannot take square"); + return false; + } + } +} diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/PollingThread.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/PollingThread.java @@ -0,0 +1,44 @@ +/* + * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license + * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template + */ +package com.groupproject.tictactoeclient; + +import java.util.logging.Level; +import java.util.logging.Logger; + +/** + * + * @author Willi + */ +public class PollingThread implements Runnable { + private TicTacToeClient client; + private HelperMethods helperMethods; + + public PollingThread(TicTacToeClient client) { + this.client = client; + helperMethods = new HelperMethods(client); + } + + @Override + public void run() { + while (true) { + try { + Thread.sleep(1000); + + if (client.UID == null || Integer.parseInt(client.UID) == -1) { + continue; + } + + client.BOARD = helperMethods.GetBoard(); + + System.out.println("Polling thread executing..."); + client.openGames = client.proxy.showOpenGames(); + System.out.println("Result: " + client.openGames); + client.refreshCurrentPanel(); + } 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 @@ -30,9 +30,10 @@ public class TicTacToeClient { public String UID; public String GID; public String UID2; - public String HOST = " "; + public String HOST_UID = " "; public String username; public String openGames; + public int[][] BOARD; public static void main(String[] args) { @@ -65,7 +66,7 @@ public class TicTacToeClient { frame.setVisible(true); - Thread t1 = new Thread(new FetchGamesThread(this)); + Thread t1 = new Thread(new PollingThread(this)); t1.start(); }