Online-TicTacToe

Log | Files | Refs | README | LICENSE

commit 57d2d55a4f371f0d12452963245f3f77d53cc0ae
parent 364a876e907095e05dd22cfc0e79f9dcdcde6a82
Author: lukeeeeeee334 <lukeosullivan123@gmail.com>
Date:   Tue, 19 Nov 2024 13:01:43 +0000

join game button functionality and changed output of the opengames

Diffstat:
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/MainContentPanel.java | 140++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----------------
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/TicTacToeClient.java | 1+
2 files changed, 112 insertions(+), 29 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 @@ -5,17 +5,18 @@ import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; - +import javax.swing.event.ListSelectionListener; public class MainContentPanel extends CustomPanel { TicTacToeClient client; JList<String> openGamesList; - private JLabel gameTimerLabel; private Timer timer; private int remainingTime; + + public MainContentPanel(TicTacToeClient client) { this.client = client; @@ -48,7 +49,6 @@ public class MainContentPanel extends CustomPanel { add(gameTimerLabel); add(scoreButton); - layout.putConstraint(SpringLayout.WEST, createGameButton, 20, SpringLayout.WEST, this); layout.putConstraint(SpringLayout.NORTH, createGameButton, 10, SpringLayout.NORTH, this); @@ -77,16 +77,62 @@ public class MainContentPanel extends CustomPanel { layout.putConstraint(SpringLayout.SOUTH, scoreButton, -20, SpringLayout.SOUTH, this); layout.putConstraint(SpringLayout.EAST, scoreButton, -20, SpringLayout.EAST, this); - // Button action listeners - createGameButton.addActionListener(e -> { - client.proxy.newGame(Integer.parseInt(client.UID)); - startTimer(); - }); + openGamesList.addListSelectionListener(e -> { + if (!e.getValueIsAdjusting() && openGamesList.getSelectedValue() != null) { + String selectedGame = openGamesList.getSelectedValue(); + + System.out.println("selected Game: " + selectedGame); + + String[] parts = selectedGame.split(" "); + if (parts.length >= 2) { + String gameID = parts[1]; // The GID is the second part + // Debug: Print the extracted GID + System.out.println("Extracted GID: " + gameID); + + // Store the game ID to use in the joinGame action + client.GID = gameID; + } else { + // Handle cases where the format doesn't match + System.out.println("Invalid format for game entry: " + selectedGame); + } +} +}); + + + + createGameButton.addActionListener(e -> { + client.proxy.newGame(Integer.parseInt(client.UID)); + + if (client.GID != null && !client.GID.isEmpty()) { + String newGameData = "Game " + client.GID + " Host: " + client.UID; + if (client.openGames == null || client.openGames.isEmpty()) { + client.openGames = newGameData; + } else { + client.openGames += "\n" + newGameData; //seperates each new game + } + } + + System.out.println("New game added: " + client.openGames); + + refresh(); // refresh the list to show if there is a new game added +}); + + + + + joinGameButton.addActionListener(e -> { - //client.proxy.joinGame(Integer.parseInt(client.UID), Integer.parseInt(client.GID)); - //startTimer(); - }); + // join the game using GID + if (client.GID != null) { + client.proxy.joinGame(Integer.parseInt(client.UID), Integer.parseInt(client.GID)); + startTimer(); + } else { + System.out.println("no game selected."); + } +}); + + scoreButton.addActionListener(e -> { client.showPanel(new score(client)); @@ -99,16 +145,57 @@ public class MainContentPanel extends CustomPanel { }); } - @Override - public void refresh() { - System.out.println("Refreshing MainContentPanel"); - var games = client.openGames.split(","); - System.out.println("Current Games: " + client.openGames); - openGamesList.setListData(games); - this.revalidate(); - this.repaint(); +@Override +public void refresh() { + System.out.println("Refreshing MainContentPanel"); + // System.out.println("Raw client.openGames data: " + client.openGames); // Debugging output + + // Check if openGames is empty/null, if there is nothin then display nothing in the box + if (client.openGames == null || client.openGames.isEmpty()) { + System.out.println("No games available."); + openGamesList.setListData(new String[0]); + } else { + String[] rawGames = client.openGames.split("\n"); + + String[] formattedGames = new String[rawGames.length]; + + //extract the GID, Host, and time (mabye add time later or just data or just time) + for (int i = 0; i < rawGames.length; i++) { + String gameData = rawGames[i].trim(); + String[] parts = gameData.split(","); + + if (parts.length == 3) { + String gameID = parts[0].trim(); + String hostID = parts[1].trim(); + String timestamp = parts[2].trim(); //use later mabye? + + // formating the output in the box + formattedGames[i] = "Game " + gameID + " Host: " + hostID; + } else { + formattedGames[i] = " "; //set the box to nothing the format isnt right i.e if there is no host name + } + } + + 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() { // Remaining time is 15 minutes remainingTime = 15 * 60; @@ -133,23 +220,18 @@ public class MainContentPanel extends CustomPanel { timer.start(); } - class TicTacToeGrid extends JPanel { public TicTacToeGrid(int rows, int cols) { - setLayout(new GridLayout(rows, cols, 5, 5)); // + 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)); + button.setFont(new Font("Arial", Font.BOLD, 32)); add(button); - - button.addActionListener(new ActionListener() { - @Override - public void actionPerformed(ActionEvent e) { - button.setText("X"); - button.setEnabled(false); // so player cant click again - } + button.addActionListener(e -> { + button.setText("X"); + button.setEnabled(false); // prevents the player from clicking the same spot again }); } } diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/TicTacToeClient.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/TicTacToeClient.java @@ -28,6 +28,7 @@ public class TicTacToeClient { private JFrame frame; private CustomPanel CurrentPanel; public String UID; + public String GID; public String username; public String openGames;