Online-TicTacToe

Log | Files | Refs | README | LICENSE

commit 364a876e907095e05dd22cfc0e79f9dcdcde6a82
parent c0b1dcc45a9ad28297172fdacac8b0a8035de713
Author: lukeeeeeee334 <lukeosullivan123@gmail.com>
Date:   Tue, 19 Nov 2024 11:02:33 +0000

changes to main

Diffstat:
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/MainContentPanel.java | 18+++++++-----------
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/allscore.java | 28+++++++++++++---------------
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/score.java | 28+++++++++++++---------------
3 files changed, 33 insertions(+), 41 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 @@ -6,9 +6,7 @@ import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; -/** - * MainContentPanel contains the content visible in the main program. - */ + public class MainContentPanel extends CustomPanel { TicTacToeClient client; @@ -50,7 +48,7 @@ public class MainContentPanel extends CustomPanel { add(gameTimerLabel); add(scoreButton); - // SpringLayout constraints + layout.putConstraint(SpringLayout.WEST, createGameButton, 20, SpringLayout.WEST, this); layout.putConstraint(SpringLayout.NORTH, createGameButton, 10, SpringLayout.NORTH, this); @@ -135,24 +133,22 @@ public class MainContentPanel extends CustomPanel { timer.start(); } - /** - * A custom class for the TicTacToe grid. - */ + class TicTacToeGrid extends JPanel { public TicTacToeGrid(int rows, int cols) { - setLayout(new GridLayout(rows, cols, 5, 5)); // Grid with 5px gaps + 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)); // Large text for "X" + button.setFont(new Font("Arial", Font.BOLD, 32)); add(button); - // Add ActionListener for button clicks + button.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { button.setText("X"); - button.setEnabled(false); // Disable the button after it's clicked + button.setEnabled(false); // so player cant click again } }); } diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/allscore.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/allscore.java @@ -8,12 +8,10 @@ import java.awt.event.ActionListener; import java.util.HashMap; import java.util.Map; -/** - * Class to display the scores of all users. - */ + public class allscore extends CustomPanel { public allscore(TicTacToeClient client) { - // Set SpringLayout + SpringLayout layout = new SpringLayout(); setLayout(layout); @@ -37,7 +35,7 @@ public class allscore extends CustomPanel { layout.putConstraint(SpringLayout.SOUTH, scrollPane, -50, SpringLayout.SOUTH, this); // Set constraints for Back Button - layout.putConstraint(SpringLayout.SOUTH, backButton, -10, SpringLayout.SOUTH, this); // 10px above bottom + layout.putConstraint(SpringLayout.SOUTH, backButton, -10, SpringLayout.SOUTH, this); layout.putConstraint(SpringLayout.WEST, backButton, 10, SpringLayout.WEST, this); // Populate stats area @@ -55,10 +53,10 @@ public class allscore extends CustomPanel { String[] games = leagueData.split("\n"); Map<String, int[]> playerStats = new HashMap<>(); - // Parse each game's data + for (String game : games) { try { - String[] columns = game.split(","); // Assuming data is comma-separated + String[] columns = game.split(","); //mabye change later depending if it shows up if (columns.length < 4) { throw new IllegalArgumentException("Malformed row: " + game); } @@ -69,16 +67,16 @@ public class allscore extends CustomPanel { int gameStateInt = Integer.parseInt(gameState); - // Update stats for Player 1 + // update the stats for Player one playerStats.putIfAbsent(player1UID, new int[2]); playerStats.putIfAbsent(player2UID, new int[2]); - if (gameStateInt == 1) { // Player 1 wins - playerStats.get(player1UID)[0]++; // Increment wins for Player 1 - playerStats.get(player2UID)[1]++; // Increment losses for Player 2 - } else if (gameStateInt == 2) { // Player 2 wins - playerStats.get(player2UID)[0]++; // Increment wins for Player 2 - playerStats.get(player1UID)[1]++; // Increment losses for Player 1 + if (gameStateInt == 1) { // player 1 wins + playerStats.get(player1UID)[0]++; // increment wins for Player 1 + playerStats.get(player2UID)[1]++; // increment losses for Player 2 + } else if (gameStateInt == 2) { // player 2 wins + playerStats.get(player2UID)[0]++; // increment wins for Player 2 + playerStats.get(player1UID)[1]++; // increment losses for Player 1 } } catch (Exception rowException) { @@ -106,7 +104,7 @@ public class allscore extends CustomPanel { e.printStackTrace(); } - // Back Button Listener + backButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/score.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/score.java @@ -6,15 +6,13 @@ import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; -/** - * Class to display the score panel with player stats. - */ + public class score extends CustomPanel { public score(TicTacToeClient client) { SpringLayout layout = new SpringLayout(); setLayout(layout); - // Label to display stats + JLabel statsLabel = new JLabel(); statsLabel.setHorizontalAlignment(SwingConstants.CENTER); statsLabel.setFont(new Font("Arial", Font.BOLD, 16)); @@ -25,16 +23,16 @@ public class score extends CustomPanel { JButton backButton = new JButton("Back"); add(backButton); - // Position the statsLabel (centered horizontally, at the top) + layout.putConstraint(SpringLayout.HORIZONTAL_CENTER, statsLabel, 0, SpringLayout.HORIZONTAL_CENTER, this); layout.putConstraint(SpringLayout.NORTH, statsLabel, 20, SpringLayout.NORTH, this); - // Position the backButton (bottom-left corner) + layout.putConstraint(SpringLayout.SOUTH, backButton, -10, SpringLayout.SOUTH, this); layout.putConstraint(SpringLayout.WEST, backButton, 10, SpringLayout.WEST, this); try { - // Call the leagueTable method to get all games + String leagueData = client.proxy.leagueTable(); System.out.println("League Data: " + leagueData); @@ -47,35 +45,35 @@ public class score extends CustomPanel { return; } - // Split the result into rows + String[] games = leagueData.split("\n"); int wins = 0; int losses = 0; - // Parse each game's data + for (String game : games) { try { - String[] columns = game.split(","); // Assuming data is comma-separated + String[] columns = game.split(","); if (columns.length < 4) { throw new IllegalArgumentException("Malformed row: " + game); } String player1UID = columns[1].trim(); // Player 1 UID String player2UID = columns[2].trim(); // Player 2 UID - String gameState = columns[3].trim(); // Game state as a string + String gameState = columns[3].trim(); // Game state (change to a int later) int gameStateInt = Integer.parseInt(gameState); if (player1UID.equals(client.username)) { - if (gameStateInt == 1) { // Player 1 wins + if (gameStateInt == 1) { // player 1 wins and adds to the score wins++; - } else if (gameStateInt == 2) { // Player 2 wins + } else if (gameStateInt == 2) { // player 2 wins and adds to the score losses++; } } else if (player2UID.equals(client.username)) { - if (gameStateInt == 2) { // Player 2 wins + if (gameStateInt == 2) { // P2 wins wins++; - } else if (gameStateInt == 1) { // Player 1 wins + } else if (gameStateInt == 1) { // P1 wins losses++; } }