import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
public class Main implements ActionListener{
static JButton[][] buttons = new JButton[6][7];
static char[][] grid = new char[6][7];
static boolean gameOver = false;
static int turn = 0;
static JLabel player;
public Main() {
JFrame frame = new JFrame();
JPanel panel = new JPanel();
panel.setBorder(BorderFactory.createEmptyBorder(3,3,1,3));
panel.setLayout(new GridLayout(6, 7));
for(int gridRow = 0; gridRow < grid.length ; gridRow++) {
for(int gridCol = 0; gridCol < grid[0].length; gridCol++){
buttons[gridRow][gridCol] = new JButton("");
buttons[gridRow][gridCol].setPreferredSize(new Dimension(90,90));
buttons[gridRow][gridCol].addActionListener(this);
buttons[gridRow][gridCol].setFont(new Font("Dialog", Font.BOLD, 50));
panel.add(buttons[gridRow][gridCol]);
}
}
frame.add(panel);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setSize(300,300);
frame.setTitle("Connect Four");
frame.pack();
frame.setResizable(false);
frame.setVisible(true);
player = new JLabel("Player ");
player.setFont(new Font("Dialog", Font.BOLD, 15));
player.setBounds(50,50,50,50);
panel.add(player);
}
public static void main(String[] args) {
new Main();
}
@Override
public void actionPerformed(ActionEvent e) {
if(gameOver == false) {
for (int row = 0; row <= grid.length - 1; row++) {
for (int col = 0; col <= grid[0].length - 1; col++) {
if (buttons[row][col] == e.getSource()) {
if (((JButton) e.getSource()).getText().isEmpty()) {
if (turn % 2 == 0 ) {
for (int row2 = grid.length - 1; row2>= 0; row2--){
if (buttons[row2][col].getText().isEmpty()) {
buttons[row2][col].setText("X");
grid[row2][col] = 'X';
if(Win(grid)) {
System.out.println("Player 1 Won");
gameOver = true;
}
break;
}
}
turn++;
} else {
for (int row2 = grid.length - 1; row2 >= 0; row2--) {
if(buttons[row2][col].getText().isEmpty()) {
buttons[row2][col].setText("O");
grid[row2][col] = 'O';
//turn++;
if(Win(grid)) {
System.out.println("Player 2 Won");
gameOver = true;
}
break;
}
}
turn++;
}
}
}
} break;
}
}
}
public static boolean Win(char[][] board) {
if(Horizontal(board) || Vertical(board) || Diagonals(board)) {
return true;
}
return false;
}
public static boolean Horizontal(char[][] grid) {
for(int row = 0; row < grid.length; row++){
for(int columns = 0; columns <= 3; columns++ ) {
if((grid[row][columns] == 'X') && ((grid[row][columns + 1] == 'X')) && (grid[row][columns + 2] == 'X') && (grid[row][columns + 3] == 'X')) {
return true;
}
if((grid[row][columns] == 'O') && (grid[row][columns + 1] == 'O') && (grid[row][columns + 2] == 'O') && (grid[row][columns + 3] == 'O')) {
return true;
}
}
}
return false;
}
public static boolean Vertical(char[][] grid) {
for (int columns = 0; columns < grid[0].length; columns++) {
for(int row = 0; row <= 2; row++){
if((grid[row][columns] == 'X') && (grid[row + 1][columns] == 'X') && (grid[row + 2][columns] == 'X') && (grid[row + 3][columns] == 'X')) {
return true;
} if((grid[row][columns] == 'O') && (grid[row + 1][columns] == 'O') && (grid[row + 2][columns] == 'O') && (grid[row + 3][columns] == 'O')) {
return true;
}
}
} return false;
}
public static boolean Diagonals(char[][] grid) {
for(int row = 0; row <= 2; row++) {
for(int columns = 0; columns <= 3; columns++) {
if((grid[row][columns] == 'X') && (grid[row + 1][columns + 1] == 'X') && (grid[row + 2][columns + 2] == 'X') && (grid[row + 3][columns + 3] == 'X')) {
return true;
} if((grid[row][columns] == 'O') && (grid[row + 1][columns + 1] == 'O') && (grid[row + 2][columns + 2] == 'O') && (grid[row + 3][columns + 3] == 'O')) {
return true;
}
}
}
for(int row = grid.length - 1; row >= 3; row--) {
for(int columns = 0; columns <= 3; columns++){
if((grid[row][columns] == 'X') && (grid[row - 1][columns + 1] == 'X') && (grid[row - 2][columns + 2] == 'X') && (grid[row - 3][columns + 3] == 'X')) {
return true;
} if((grid[row][columns] == 'O') && (grid[row - 1][columns + 1] == 'O') && (grid[row - 2][columns + 2] == 'O') && (grid[row - 3][columns + 3] == 'O')) {
return true;
}
}
} return false;
}
}
I resolved it sorry for wasting your time, it was just a problem with my breaks and for loops.