Online-TicTacToe

Log | Files | Refs | README | LICENSE

commit a6639bde4e1e2d5cc32b20d8983e5cb8239d5e10
parent 118748460e138d24af76032f48763f80d78e3670
Author: William Lindholm <william_lindholm@outlook.com>
Date:   Thu,  5 Dec 2024 19:32:24 +0000

Fixed formatting

Diffstat:
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/allscore.java | 13++++++-------
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/PollingThread.java | 142+++++++++++++++++++++++++++++++++++++------------------------------------------
MTicTacToeClient/src/main/java/com/groupproject/tictactoeclient/TicTacToeClient.java | 11+++++------
3 files changed, 78 insertions(+), 88 deletions(-)

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,10 +8,10 @@ import java.awt.event.ActionListener; import java.util.HashMap; import java.util.Map; - public class allscore extends CustomPanel { + public allscore(TicTacToeClient client) { - + SpringLayout layout = new SpringLayout(); setLayout(layout); @@ -35,9 +35,9 @@ 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); + layout.putConstraint(SpringLayout.SOUTH, backButton, -10, SpringLayout.SOUTH, this); layout.putConstraint(SpringLayout.WEST, backButton, 10, SpringLayout.WEST, this); - + backButton.addActionListener(new ActionListener() { @Override public void actionPerformed(ActionEvent e) { @@ -61,7 +61,6 @@ public class allscore extends CustomPanel { String[] games = leagueData.split("\n"); //splits the leagueTable into its games Map<String, int[]> playerStats = new HashMap<>(); //The variable playerStats is of type Map<String, int[]> - for (String game : games) { try { String[] columns = game.split(","); //splits the games into the users ID and the game state @@ -98,12 +97,12 @@ public class allscore extends CustomPanel { // Build the display string StringBuilder statsBuilder = new StringBuilder(); -// statsBuilder.append(String.format("%-20s %-10s %-10s %-10s\n", "Username", "Wins", "Losses", "Draws"));//headers for cleaner UI + // statsBuilder.append(String.format("%-20s %-10s %-10s %-10s\n", "Username", "Wins", "Losses", "Draws"));//headers for cleaner UI statsBuilder.append("-".repeat(50)).append("\n"); for (Map.Entry<String, int[]> entry : playerStats.entrySet()) { String username = entry.getKey(); //updates the wins, losses and draws for this specific user - int wins = entry.getValue()[0]; + int wins = entry.getValue()[0]; int losses = entry.getValue()[1]; int draws = entry.getValue()[2]; statsBuilder.append(String.format("%-20s %-10d %-10d %-10d\n", username, wins, losses, draws)); //prints out all the wins losses and draws along with the username diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/PollingThread.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/PollingThread.java @@ -46,92 +46,84 @@ public class PollingThread implements Runnable { 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 using the WS checkWin function String result = client.proxy.checkWin(Integer.parseInt(client.GID)); System.out.println("checkWin: " + result); //if the game is not over check the result of the game //if (!gameOver) { - System.out.println("gameOver not working"); - switch (result) { - - - case "1": - // Player 1 wins - //Show on both player's frames player 1 has won - JOptionPane.showMessageDialog(client.frame, "Player 1 wins!"); - //Set the gamestate to 1 (player 1 has won) - client.proxy.setGameState(Integer.parseInt(client.GID), 1); - //Show opions - main menu or quit - helperMethods.showOptions(); - gameOver = true; - break; - case "2": - // Player 2 wins, show on both player's frame player 2 has won - JOptionPane.showMessageDialog(client.frame, "Player 2 wins!"); - //Set the game state to 2 indicating player 2 has won the game - client.proxy.setGameState(Integer.parseInt(client.GID), 2); - //Show options - main menu or quit - helperMethods.showOptions(); - gameOver = true; - break; - case "3": - // It's a draw, show on both players screen its a draw - JOptionPane.showMessageDialog(client.frame, "It's a draw!"); - //Set the gamestate to 3 indicating game is a draw - client.proxy.setGameState(Integer.parseInt(client.GID), 3); - //Show options - main menu or quit - helperMethods.showOptions(); - gameOver = true; - break; - case "0": - // Game continues - break; - default: - JOptionPane.showMessageDialog(client.frame, "Error checking the game status."); - - } - - - //call the web server to check if the game has been won or not yet if the player is forfeiting - String gameState = client.proxy.getGameState(Integer.parseInt(client.GID)); - switch (gameState) { - - - case "1": - // Player 1 wins - // This message appears on opponents screen indicating the other player has forfeited - JOptionPane.showMessageDialog(client.frame, "The other player has forfeited, you win"); - // Opponent is presented with option to go to main menu or quit - helperMethods.showOptions(); - gameOver = true; - break; - case "2": - // Player 2 wins - //This message appears on the opponents screen indicating the other player has forfeited - JOptionPane.showMessageDialog(client.frame, "The other player has forfeited, you win"); - //Opponent presented with option to go to main menu or quit - helperMethods.showOptions(); - gameOver = true; - break; - case "3": - // Player 2 wins - JOptionPane.showMessageDialog(client.frame, "It's a draw!"); - //Opponent presented with option to go to main menu or quit - helperMethods.showOptions(); - gameOver = true; - break; - } - - //} + System.out.println("gameOver not working"); + switch (result) { + + case "1": + // Player 1 wins + //Show on both player's frames player 1 has won + JOptionPane.showMessageDialog(client.frame, "Player 1 wins!"); + //Set the gamestate to 1 (player 1 has won) + client.proxy.setGameState(Integer.parseInt(client.GID), 1); + //Show opions - main menu or quit + helperMethods.showOptions(); + gameOver = true; + break; + case "2": + // Player 2 wins, show on both player's frame player 2 has won + JOptionPane.showMessageDialog(client.frame, "Player 2 wins!"); + //Set the game state to 2 indicating player 2 has won the game + client.proxy.setGameState(Integer.parseInt(client.GID), 2); + //Show options - main menu or quit + helperMethods.showOptions(); + gameOver = true; + break; + case "3": + // It's a draw, show on both players screen its a draw + JOptionPane.showMessageDialog(client.frame, "It's a draw!"); + //Set the gamestate to 3 indicating game is a draw + client.proxy.setGameState(Integer.parseInt(client.GID), 3); + //Show options - main menu or quit + helperMethods.showOptions(); + gameOver = true; + break; + case "0": + // Game continues + break; + default: + JOptionPane.showMessageDialog(client.frame, "Error checking the game status."); + + } + + //call the web server to check if the game has been won or not yet if the player is forfeiting + String gameState = client.proxy.getGameState(Integer.parseInt(client.GID)); + switch (gameState) { + case "1": + // Player 1 wins + // This message appears on opponents screen indicating the other player has forfeited + JOptionPane.showMessageDialog(client.frame, "The other player has forfeited, you win"); + // Opponent is presented with option to go to main menu or quit + helperMethods.showOptions(); + gameOver = true; + break; + case "2": + // Player 2 wins + //This message appears on the opponents screen indicating the other player has forfeited + JOptionPane.showMessageDialog(client.frame, "The other player has forfeited, you win"); + //Opponent presented with option to go to main menu or quit + helperMethods.showOptions(); + gameOver = true; + break; + case "3": + // Player 2 wins + JOptionPane.showMessageDialog(client.frame, "It's a draw!"); + //Opponent presented with option to go to main menu or quit + helperMethods.showOptions(); + gameOver = true; + break; + } } 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 @@ -91,20 +91,19 @@ public class TicTacToeClient { this.UID2 = null; this.HOST_UID = ""; this.NUM_OF_MOVES = 0; - + refreshCurrentPanel(); } - - + //used to log the user out and remove their user details - public void logout(){ + public void logout() { this.UID = null; this.GID = ""; this.UID2 = null; this.HOST_UID = ""; this.NUM_OF_MOVES = 0; - this.BOARD = new int [2][2]; - + this.BOARD = new int[2][2]; + } }