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 4 of 4

Thread: connect4 problem

  1. #1
    Junior Member
    Join Date
    Jul 2013
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default connect4 problem

    [code=Java]
    public connectFour() {
    for (int i = 0; i < ROWSNUM; i++) {
    for (int j = 0; j < COLSNUM; j++) {
    button[i][j] = new JLabel();
    button[i][j].setHorizontalAlignment(SwingConstants.CENTER);
    button[i][j].setBorder(new LineBorder(Color.black));
    button[i][j].addMouseListener(this);
    contentPane.add(button[i][j]);
    }
    }
    }
    public void mouseClicked(MouseEvent event) {
    // TODO Auto-generated method stub
    String player;
    for (int row = ROWSNUM - 1; row >= 0; row--) {
    for (int col = COLSNUM - 1; col >= 0; col--) {
    if (button[row][col] == event.getSource()) {
    if (currentPlayer % 2 == 0 && grid[row][col] == 0) {
    // button[row][col].setRolloverIcon(p1);
    button[row][col].setOpaque(true);
    button[row][col].setBackground(Color.red);
    player = player1;
    currentPlayer++;
    if (CheckWhoWin()) {
    WinStatue();
    JOptionPane.showMessageDialog(null,
    "The winner is: " + player);
    for (int x = 0; row <= ROWSNUM - 1; x++) {
    for (int y = 0; y <= COLSNUM - 1; y++) {
    grid[x][y] = 0;
    }
    }
    }
    break;
    } if (currentPlayer % 2 != 0 && grid[row][col] == 0) {
    // button[row][col].setRolloverIcon(p2);
    button[row][col].setOpaque(true);
    button[row][col].setBackground(Color.yellow);
    player = player2;
    currentPlayer++;
    if (CheckWhoWin()) {
    WinStatue();
    JOptionPane.showMessageDialog(null,
    "The winner is: " + player);
    for (int x = 0; row <= ROWSNUM - 1; x++) {
    for (int y = 0; y <= COLSNUM - 1; y++) {
    grid[x][y] = 0;
    }
    }
    }
    break;
    } else
    JOptionPane.showMessageDialog(null, "ERROR");
    break;
    }

    }
    }
    }
    public boolean CheckWhoWin() {

    // Vertically
    for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 7; j++) {
    if (grid[i][j] != 0 && grid[i][j] == grid[i + 1][j]
    && grid[i][j] == grid[i + 2][j]
    && grid[i][j] == grid[i + 3][j]){
    winStatue = 1;
    win = true;
    }
    }
    }

    // horizontally
    for (int i = 0; i < 6; i++) {
    for (int j = 0; j < 4; j++) {
    if (grid[i][j] != 0 && grid[i][j] == grid[i][j + 1]
    && grid[i][j] == grid[i][j + 2]
    && grid[i][j] == grid[i][j + 3]){
    winStatue = 2;
    win = true;
    }
    }
    }

    // positive Diagonal
    for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 4; j++) {
    if (grid[i][j] != 0 && grid[i][j] == grid[i + 1][j + 1]
    && grid[i][j] == grid[i + 2][j + 2]
    && grid[i][j] == grid[i + 3][j + 3]){
    winStatue = 3;
    win = true;
    }
    }
    }

    // negative Diagonal
    for (int i = 3; i < 6; i++) {
    for (int j = 0; j < 4; j++) {
    if (grid[i][j] != 0 && grid[i][j] == grid[i - 1][j + 1]
    && grid[i][j] == grid[i - 2][j + 2]
    && grid[i][j] == grid[i - 3][j + 3]){
    winStatue = 4;
    win = true;
    }
    }
    }
    return win;
    }

    public void WinStatue(){
    for (int i = 0; i < ROWSNUM - 1; i++){
    for (int j = 0; j < COLSNUM - 1; j++){
    if (winStatue == 1){
    button[i][j].setOpaque(false);
    button[i + 1][j].setOpaque(false);
    button[i + 2][j].setOpaque(false);
    button[i + 3][j].setOpaque(false);
    }
    if (winStatue == 2){

    button[i][j].setOpaque(false);
    button[i][j + 1].setOpaque(false);
    button[i][j + 2].setOpaque(false);
    button[i][j + 3].setOpaque(false);
    }
    if (winStatue == 3){
    button[i][j].setOpaque(false);
    button[i + 1][j + 1].setOpaque(false);
    button[i + 2][j + 2].setOpaque(false);
    button[i + 3][j + 3].setOpaque(false);
    }
    if(winStatue == 4){
    button[i][j].setOpaque(false);
    button[i - 1][j + 1].setOpaque(false);
    button[i - 2][j + 2].setOpaque(false);
    button[i - 3][j + 3].setOpaque(false);
    }
    }
    }
    }
    I dont know what is the error


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: connect4 problem

    I dont know what is the error
    And neither do we. Please tell us. Does it compile? Are there exceptions? Does it run as expected?

    Please see Announcements - What's Wrong With My Code? for instructions on code tags formatting.

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

    Default Re: connect4 problem

    it's running but it is not winning when I match 4
    Attached Files Attached Files

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: connect4 problem

    I am allergic to clicking things. Please post the code to the forum in code tags.

    You will need to look at the program and see what it is doing. Use printlns or a debugger to monitor the values of variables. (Specifically those that control things related to the problem you see.)
    Verify the values are what you think they should be. If you can not find the problem, you will have to explain to us what it is doing, and what you would like for it to do instead, specifically. Generalized statements like "it is not winning" is not specific, and would force someone to try to read and understand what broken-code was supposed to do.

Similar Threads

  1. Replies: 3
    Last Post: January 5th, 2012, 01:44 AM
  2. [SOLVED] [Problem] imports javax.swing problem
    By Brollie in forum AWT / Java Swing
    Replies: 8
    Last Post: July 5th, 2009, 07:59 AM
  3. Java program for 2-D Array Maze
    By Peetah05 in forum Collections and Generics
    Replies: 11
    Last Post: May 8th, 2009, 04:30 AM