commit 2d5764719402b8d8d8f06279366c30ca9c7fd6f5
parent 27d328c36396c82d552a1a92de741c1951ca0295
Author: William Lindholm <william_lindholm@outlook.com>
Date: Wed, 27 Nov 2024 15:35:01 +0000
Implemented some bugfixes
Diffstat:
6 files changed, 67 insertions(+), 63 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
@@ -93,21 +93,16 @@ public class MainContentPanel extends CustomPanel {
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_Temp = gameID;
- System.out.println("Temp GID = " + client.GID_Temp);
} else {
// Handle cases where the format doesn't match
- System.out.println("Invalid format for game entry: " + selectedGame);
}
}
});
@@ -125,8 +120,6 @@ public class MainContentPanel extends CustomPanel {
}
}
- System.out.println("New game added: " + client.openGames);
-
refresh(); // refresh the list to show if there is a new game added
});
@@ -135,7 +128,7 @@ public class MainContentPanel extends CustomPanel {
if (client.GID_Temp != null) {
client.UID2 = client.proxy.joinGame(Integer.parseInt(client.UID), Integer.parseInt(client.GID_Temp));
client.GID = client.GID_Temp;
- client.HOST_UID = null;
+ client.HOST_UID = "";
startTimer();
} else {
System.out.println("no game selected.");
@@ -155,14 +148,12 @@ public class MainContentPanel extends CustomPanel {
@Override
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()) {
- System.out.println("No games available.");
openGamesList.setListData(new String[0]);
} else {
String[] rawGames = client.openGames.split("\n");
@@ -213,5 +204,4 @@ public class MainContentPanel extends CustomPanel {
});
timer.start();
}
-
}
diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/TicTacToeGrid.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/TicTacToeGrid.java
@@ -39,6 +39,13 @@ public class TicTacToeGrid extends JPanel {
add(button);
button.addActionListener(e -> {
+
+ if (client.OPPONENTS_TURN) {
+ System.out.println("Not my turn!");
+ return;
+ }
+ System.out.println("It is my turn!");
+
//need to add an if statment that checks the GID
//client.UID2 = client.proxy.leagueTable();
if (client.UID.equals(client.HOST_UID)) {
@@ -56,7 +63,7 @@ public class TicTacToeGrid extends JPanel {
}
public void refresh() {
- System.out.println("UID: " + client.UID + ", HOST_UID: " + client.HOST_UID);
+ System.out.println("UID: " + client.UID + ", HOST_UID: " + client.HOST_UID + ", GAME_ID: " + client.GID);
if (client.BOARD == null) {
return;
}
@@ -64,6 +71,10 @@ public class TicTacToeGrid extends JPanel {
System.out.println("Updating TicTacToeGrid...");
final int[][] board = client.BOARD;
+ final int totalNumOfMoves = client.BOARD.length;
+ if (totalNumOfMoves % 2 == 0 && helperMethods.IsHost()) {
+ client.OPPONENTS_TURN = false;
+ }
for (int[] row : board) {
// Convert grid coordinates to proper index
@@ -72,7 +83,7 @@ public class TicTacToeGrid extends JPanel {
// 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");
diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/login.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/ContentPanes/login.java
@@ -129,6 +129,6 @@ public class login extends CustomPanel {
@Override
public void refresh() {
- throw new UnsupportedOperationException("Not supported yet."); // Generated from nbfs://nbhost/SystemFileSystem/Templates/Classes/Code/GeneratedMethodBody
+
}
}
diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/HelperMethods.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/HelperMethods.java
@@ -39,7 +39,7 @@ public class HelperMethods {
}
public int[][] GetBoard() {
- if (client.GID == null) {
+ if (client.GID.isBlank()) {
return null;
}
diff --git a/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/PollingThread.java b/TicTacToeClient/src/main/java/com/groupproject/tictactoeclient/PollingThread.java
@@ -14,71 +14,73 @@ import javax.swing.JOptionPane;
* @author Willi
*/
public class PollingThread implements Runnable {
+
private TicTacToeClient client;
private HelperMethods helperMethods;
-
+
//flag used to keep track if game is over so the dialog box doesnt keep appearing
private boolean gameOver = false;
-
+
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) {
+
+ if (client.UID == null || client.UID.isEmpty() || 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();
-
-
+
+ if (client.GID.isBlank() || client.GID.isEmpty()) {
+ continue;
+ }
+
//call the web server to check if the game has been won or not yet
- String result = client.proxy.checkWin(Integer.parseInt(client.GID));
-
- //if the game is not over check the result of the game
- if(!gameOver) {
- switch (result) {
- case "1":
- // Player 1 wins
- JOptionPane.showMessageDialog(client.frame, "Player 1 wins!");
- helperMethods.showOptions();
- gameOver = true;
- break;
- case "2":
- // Player 2 wins
- JOptionPane.showMessageDialog(client.frame, "Player 2 wins!");
- helperMethods.showOptions();
- gameOver = true;
- break;
- case "3":
- // It's a draw
- JOptionPane.showMessageDialog(client.frame, "It's a draw!");
- helperMethods.showOptions();
- gameOver = true;
- break;
- case "0":
- // Game continues
- break;
- default:
- JOptionPane.showMessageDialog(client.frame, "Error checking the game status.");
-
- }
- }
-
+ String result = client.proxy.checkWin(Integer.parseInt(client.GID));
+
+ //if the game is not over check the result of the game
+ if (!gameOver) {
+ switch (result) {
+ case "1":
+ // Player 1 wins
+ JOptionPane.showMessageDialog(client.frame, "Player 1 wins!");
+ helperMethods.showOptions();
+ gameOver = true;
+ break;
+ case "2":
+ // Player 2 wins
+ JOptionPane.showMessageDialog(client.frame, "Player 2 wins!");
+ helperMethods.showOptions();
+ gameOver = true;
+ break;
+ case "3":
+ // It's a draw
+ JOptionPane.showMessageDialog(client.frame, "It's a draw!");
+ helperMethods.showOptions();
+ gameOver = true;
+ break;
+ case "0":
+ // Game continues
+ break;
+ default:
+ JOptionPane.showMessageDialog(client.frame, "Error checking the game status.");
+
+ }
+ }
+
} 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
@@ -27,11 +27,12 @@ public class TicTacToeClient {
public static TicTacToeWS proxy;
public JFrame frame;
private CustomPanel CurrentPanel;
- public String UID;
- public String GID;
+ public String UID = "";
+ public String GID = "";
+ public boolean OPPONENTS_TURN = false;
public String UID2;
public String GID_Temp;
- public String HOST_UID = " ";
+ public String HOST_UID = "";
public String username;
public String openGames;
public int[][] BOARD;