Online-TicTacToe

Log | Files | Refs | README | LICENSE

score.java (5357B)


      1 package com.groupproject.tictactoeclient.ContentPanes;
      2 
      3 import com.groupproject.tictactoeclient.TicTacToeClient;
      4 import javax.swing.*;
      5 import java.awt.*;
      6 import java.awt.event.ActionEvent;
      7 import java.awt.event.ActionListener;
      8 
      9 
     10 /**
     11  *
     12  * Author: Luke
     13  */
     14 public class score extends CustomPanel {
     15 
     16     public score(TicTacToeClient client) {
     17         SpringLayout layout = new SpringLayout();
     18         setLayout(layout);
     19 
     20         // Use JTextArea for multiline output
     21         JTextArea statsArea = new JTextArea();
     22         statsArea.setEditable(false);
     23         statsArea.setFont(new Font("Monospaced", Font.PLAIN, 14));
     24         statsArea.setBackground(getBackground());
     25         statsArea.setForeground(Color.WHITE);
     26 
     27         JScrollPane scrollPane = new JScrollPane(statsArea);
     28         add(scrollPane);
     29 
     30         // Back Button
     31         JButton backButton = new JButton("Back");
     32         add(backButton);
     33 
     34         // Layout constraints for JTextArea
     35         layout.putConstraint(SpringLayout.NORTH, scrollPane, 20, SpringLayout.NORTH, this);
     36         layout.putConstraint(SpringLayout.WEST, scrollPane, 10, SpringLayout.WEST, this);
     37         layout.putConstraint(SpringLayout.EAST, scrollPane, -10, SpringLayout.EAST, this);
     38         layout.putConstraint(SpringLayout.SOUTH, scrollPane, -50, SpringLayout.SOUTH, this);
     39 
     40         // Layout constraints for Back Button
     41         layout.putConstraint(SpringLayout.SOUTH, backButton, -10, SpringLayout.SOUTH, this);
     42         layout.putConstraint(SpringLayout.WEST, backButton, 10, SpringLayout.WEST, this);
     43 
     44         // Back Button Action
     45         backButton.addActionListener(new ActionListener() {
     46             @Override
     47             public void actionPerformed(ActionEvent e) {
     48                 client.showPanel(new MainContentPanel(client));
     49             }
     50         });
     51 
     52         try {
     53             String leagueData = client.proxy.leagueTable(); //stores our leagueTable data into leagueData variable
     54             System.out.println("League Data: " + leagueData);
     55 
     56             // Check if the response indicates no games or a database error
     57             if (leagueData.equals("ERROR-NOGAMES")) {
     58                 statsArea.setText("No games found.");
     59                 return;
     60             } else if (leagueData.equals("ERROR-DB")) {
     61                 statsArea.setText("Database error occurred.");
     62                 return;
     63             }
     64 
     65             // Prepare header for stats
     66             StringBuilder statsBuilder = new StringBuilder();
     67             statsBuilder.append(String.format("%-20s %-10s %-10s %-10s\n", "Username", "Wins", "Losses", "Draws")); //headers for the user, wins, losses and draws
     68             statsBuilder.append("-".repeat(50)).append("\n");
     69 
     70             // Process league data
     71             String[] games = leagueData.split("\n"); // splits the league data into its diffrent games
     72             //counters for the wins, losses and draws
     73             int wins = 0;
     74             int losses = 0;
     75             int draws = 0;
     76 
     77             for (String game : games) {
     78                 try {
     79                     String[] columns = game.split(","); //splits each game into player1s ID, Player2s ID and the game state
     80                     if (columns.length < 4) {
     81                         throw new IllegalArgumentException("Malformed row: " + game);
     82                     }
     83                     //split game stores the users ID and the game state
     84                     String player1UID = columns[1].trim();
     85                     String player2UID = columns[2].trim();
     86                     String gameState = columns[3].trim();
     87                     int gameStateInt = Integer.parseInt(gameState); //turns game state into an int for if statments
     88                     //checks the game state and assigns a win, loss or draw for a user
     89                     if (player1UID.equals(client.username)) {
     90                         if (gameStateInt == 1) { // Player 1 wins
     91                             wins++;
     92                         } else if (gameStateInt == 2) { // Player 2 wins
     93                             losses++;
     94                         } else if (gameStateInt == 3) { // Draw
     95                             draws++;
     96                         }
     97                     } else if (player2UID.equals(client.username)) {
     98                         if (gameStateInt == 2) { // Player 2 wins
     99                             wins++;
    100                         } else if (gameStateInt == 1) { // Player 1 wins
    101                             losses++;
    102                         } else if (gameStateInt == 3) { // Draw
    103                             draws++;
    104                         }
    105                     }
    106 
    107                 } catch (Exception rowException) {
    108                     System.err.println("Error processing row: " + game);
    109                     rowException.printStackTrace();
    110                 }
    111             }
    112 
    113             // Add the user's stats to the output
    114             statsBuilder.append(String.format("%-20s %-10d %-10d %-10d\n", client.username, wins, losses, draws)); //prints out all the wins, losses and draws for a user
    115 
    116             // Set the stats to the JTextArea
    117             statsArea.setText(statsBuilder.toString());
    118 
    119         } catch (Exception e) {
    120             statsArea.setText("An error occurred while calculating player stats.");
    121             e.printStackTrace();
    122         }
    123     }
    124 
    125     @Override
    126     public void refresh() {
    127         // Not needed
    128     }
    129 }