TicTacToeGrid.java (3558B)
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.ContentPanes; 6 7 import com.groupproject.tictactoeclient.HelperMethods; 8 import com.groupproject.tictactoeclient.TicTacToeClient; 9 10 import java.awt.Color; 11 import java.awt.Font; 12 import java.awt.GridLayout; 13 import java.util.ArrayList; 14 import javax.swing.JButton; 15 import javax.swing.JPanel; 16 import javax.swing.*; 17 18 /** 19 * 20 * @author William 21 */ 22 public class TicTacToeGrid extends JPanel { 23 24 TicTacToeClient client; 25 ArrayList<JButton> gridButtons; 26 HelperMethods helperMethods; 27 28 public TicTacToeGrid(TicTacToeClient client, int rows, int cols) { 29 this.client = client; 30 helperMethods = new HelperMethods(client); 31 setLayout(new GridLayout(rows, cols, 5, 5)); 32 gridButtons = new ArrayList<JButton>(); 33 34 for (int i = 0; i < rows * cols; i++) { 35 final int index = i; 36 JButton button = new JButton(); 37 gridButtons.add(button); 38 button.setFont(new Font("Arial", Font.BOLD, 32)); 39 add(button); 40 41 button.addActionListener(e -> { 42 43 if (client.OPPONENTS_TURN) { 44 System.out.println("Not my turn!"); 45 return; 46 } 47 System.out.println("It is my turn!"); 48 49 //need to add an if statment that checks the GID 50 if (client.UID.equals(client.HOST_UID)) { 51 if (helperMethods.TakeSqure(index)) { 52 button.setText("X"); 53 } 54 } else { 55 if (helperMethods.TakeSqure(index)) { 56 button.setText("O"); 57 } 58 } 59 button.setEnabled(false); // prevents the player from clicking the same spot again 60 client.OPPONENTS_TURN = true; // prevent user from taking square again 61 }); 62 } 63 } 64 65 public void refresh() { 66 //go through the grid and clear the buttons 67 for (var button : gridButtons) { 68 button.setText(""); //set grid to empty 69 button.setEnabled(true); 70 } 71 72 if (client.BOARD == null) { 73 return; 74 } 75 76 final int[][] board = client.BOARD; 77 client.NUM_OF_MOVES = client.BOARD.length; 78 79 // decide whose turn it is 80 if (client.NUM_OF_MOVES % 2 == 0 && helperMethods.IsHost()) { 81 client.OPPONENTS_TURN = false; 82 } 83 84 if (client.NUM_OF_MOVES % 2 == 1 && !helperMethods.IsHost()) { 85 client.OPPONENTS_TURN = false; 86 } 87 88 for (int[] row : board) { 89 // Convert grid coordinates to proper index 90 int index = (row[2] * 3) + row[1]; 91 92 // if there is no host player id saved save it from detected move 93 if (!String.valueOf(row[0]).equals(client.UID) && client.HOST_UID.isBlank()) { 94 client.HOST_UID = String.valueOf(row[0]); 95 } 96 97 if (String.valueOf(row[0]).equals(client.HOST_UID)) { 98 gridButtons.get(index).setText("X"); 99 gridButtons.get(index).setEnabled(false); 100 } else { 101 gridButtons.get(index).setText("O"); 102 gridButtons.get(index).setEnabled(false); 103 } 104 } 105 106 this.revalidate(); 107 this.repaint(); 108 } 109 110 }