HelperMethods.java (3698B)
1 /* 2 * Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license 3 * Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template 4 */ 5 package com.groupproject.tictactoeclient; 6 7 import com.groupproject.tictactoeclient.ContentPanes.MainContentPanel; 8 import java.util.ArrayList; 9 import java.util.Arrays; 10 import javax.swing.JOptionPane; 11 12 /** 13 * 14 * @author William 15 */ 16 public class HelperMethods { 17 18 private TicTacToeClient client; 19 20 public HelperMethods(TicTacToeClient client) { 21 this.client = client; 22 } 23 24 public int[][] GetBoard(int gid) { 25 System.out.println("Fetching board..."); 26 String response = client.proxy.getBoard(gid); 27 System.out.println(response); 28 29 if ("ERROR-NOMOVES".equals(response)) { 30 return null; 31 } 32 33 // Split by lines and map each line to an integer array 34 return Arrays.stream(response.split("\n")) 35 .map(row -> Arrays.stream(row.split(",")) 36 .mapToInt(Integer::parseInt) 37 .toArray()) 38 .toArray(int[][]::new); 39 } 40 41 public int[][] GetBoard() { 42 if (client.GID.isBlank()) { 43 return null; 44 } 45 46 return GetBoard(Integer.parseInt(client.GID)); 47 } 48 49 //Checks if the client is the Host or not 50 public boolean IsHost() { 51 return client.UID.equals(client.HOST_UID); 52 } 53 54 /** 55 * Take the square with selected index 56 * 57 * @param index square to take 58 * @return return true if square was taken, false if it was already taken by 59 * opponent 60 */ 61 public boolean TakeSqure(int index) { 62 if ("0".equals(client.proxy.checkSquare(index % 3, (int) Math.floor(index / 3), Integer.parseInt(client.GID)))) { 63 System.out.println("Taking square"); 64 client.proxy.takeSquare(index % 3, (int) Math.floor(index / 3), Integer.parseInt(client.GID), Integer.parseInt(client.UID)); 65 66 return true; 67 } else { 68 System.out.println("Cannot take square"); 69 return false; 70 } 71 } 72 73 //https://www.geeksforgeeks.org/java-joptionpane/ 74 public void showOptions() { 75 76 String[] options = {"Main Menu", "Quit"}; 77 // Display an option dialog 78 // The user's choice is stored in the 'choice' variable 79 int choice = JOptionPane.showOptionDialog( 80 null, // Parent component (null means center on screen) 81 "Options", // Message to display 82 "Custom Options", // Dialog title 83 JOptionPane.YES_NO_CANCEL_OPTION, // Option type (Yes, No, Cancel) 84 JOptionPane.QUESTION_MESSAGE, // Message type (question icon) 85 null, // Custom icon (null means no custom icon) 86 options, // Custom options array 87 options[0] // Initial selection (default is "Cancel") 88 ); 89 90 // Check the user's choice and display a corresponding message 91 if (choice == JOptionPane.YES_OPTION) { 92 // If the user chose 'Yes' 93 // reset their game and put them back to the main menu 94 client.resetGame(); 95 client.showPanel(new MainContentPanel(client)); 96 } else if (choice == JOptionPane.NO_OPTION) { 97 // If the user chose 'No' 98 // quit the application 99 System.exit(0); 100 } else { 101 // If the user chose 'Cancel' or closed the 102 // dialog 103 // show a message indicating the operation is 104 // canceled 105 JOptionPane.showMessageDialog(null, "Operation canceled."); 106 } 107 108 } 109 }