Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: How would I drop the button to the lowest column for Connect 4.

  1. #1
    Junior Member
    Join Date
    Jul 2020
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Angry How would I drop the button to the lowest column for Connect 4.

    public void actionPerformed(ActionEvent e) {
    for (int row = grid.length - 1; row >= 0; row--) {
    for(int col = grid.length - 1; col >= 0; col--) {

    if(buttons[row][col] == e.getSource()) {
    for(row = grid.length - 1; row >= 0; row--) {
    if(buttons[row][col].getText().equals("")) {
    if (turn % 2 == 0 && grid[row][col] == 0) {
    buttons[row][col].setText("X");
    grid[row][col] = 'X';
    turn++;
    break;

    } else {
    buttons[row][col].setText("O");
    grid[row][col] = 'O';
    turn++;
    break;
    }

    }
    }

    }
    }
    }

    //just for checking
    for (int rows = 0; rows < grid.length; rows++) {
    for (int columns = 0; columns < grid[0].length; columns++) {
    System.out.print(grid[rows][columns] + " ");
    } System.out.println();
    }
    }

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,186
    Thanks
    53
    Thanked 2,465 Times in 2,416 Posts

    Default Re: How would I drop the button to the lowest column for Connect 4.

    drop the button to the lowest column
    Please explain what you are trying to do.
    I think of columns as vertical and laid out from left to right, with column 0 being the leftmost one
    rows are horizontal and go from top to bottom with row 0 being the one at the top.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Jul 2020
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How would I drop the button to the lowest column for Connect 4.

    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;
     
      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);
     
      }
     
      public static void main(String[] args) {
        new Main();
      }
     
     
      @Override
      public void actionPerformed(ActionEvent e) {
        for (int row = grid.length - 1; row >= 0; row--) {
          for(int col = grid.length - 1; col >= 0; col--) {
     
            if(buttons[row][col] == e.getSource()) {
              for(row = grid.length - 1; row >= 0; row--) {
                if(buttons[row][col].getText().equals("")) {
                  if (turn % 2 == 0 && grid[row][col] == 0) {
                    buttons[row][col].setText("X");
                    grid[row][col] = 'X';
                    turn++;
                    break;
     
                } else {
                   buttons[row][col].setText("O");
                   grid[row][col] = 'O';
                   turn++;
                   break;
              } 
                  break;
                }
              }
     
              }
            }
          }
     
          //just for checking
          for (int rows = 0; rows < grid.length; rows++) {
              for (int columns = 0; columns < grid[0].length; columns++) {
                System.out.print(grid[rows][columns] + " ");
              } System.out.println();
            }
        }
     
     
      // public static char[][] justChecking(char[][] grid, JButton[][] buttons) {
      //   for (int row = grid.length - 1; row >= 0; row--) {
      //     for(int col = grid.length - 1; col >= 0; col--) {
      //       buttons[row][col].setText("X");
      //       grid[row][col] = 'X';
      //     }
      //   } 
      // return grid;
      // }  
     
     
     
      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;
        }
    }

    im making a conenct 4 game and right now i can click to position the X's and O's on any button and anywhere in the array, but Im having difficulties making the X's and O's go the the bottom if you know how connect 4 works. ty

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    23,186
    Thanks
    53
    Thanked 2,465 Times in 2,416 Posts

    Default Re: How would I drop the button to the lowest column for Connect 4.

    having difficulties making the X's and O's go the the bottom
    Can you explain what happens? For example the player does something and then the computer does something.
    What is it the computer does? What do you want it to do differently?

    There are no comments in the code so I can not tell what it is trying to do. For example what are all the loops and if statements in the actionPerformed() method trying to do?
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Junior Member
    Join Date
    Jul 2020
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: How would I drop the button to the lowest column for Connect 4.

     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.

Similar Threads

  1. [SOLVED] Find the lowest number
    By Elyril in forum What's Wrong With My Code?
    Replies: 2
    Last Post: April 13th, 2014, 06:32 AM
  2. Ask Drop Down Button?
    By adh13 in forum What's Wrong With My Code?
    Replies: 3
    Last Post: November 13th, 2013, 05:24 AM
  3. Sum elements column by column
    By NallelyXIII in forum What's Wrong With My Code?
    Replies: 2
    Last Post: May 9th, 2013, 01:22 AM
  4. Get the lowest value from Object[][]
    By Tyluur in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 19th, 2012, 10:55 AM
  5. [SOLVED] flightpaths, finding lowest cost -- lowest amount of crossovers
    By CjStaal in forum Algorithms & Recursion
    Replies: 4
    Last Post: May 8th, 2012, 12:47 AM