Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8578313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:21:34+00:00 2026-06-11T20:21:34+00:00

I’ve coded an entire program to play Connect Four, but the algorithm for checking

  • 0

I’ve coded an entire program to play Connect Four, but the algorithm for checking who has won (after each turn) isn’t working and I don’t know why. I keep getting a weird message when I compile it (exception ArrayIndexOutOfBoundsException). Any help is appreciated.

Here is the code:

import java.applet.*;
import java.awt.*;
import java.awt.event.*;

public class connectFourDemo extends Applet
    implements MouseListener, MouseMotionListener {
    private static final long serialVersionUID = 1L;

    int[][] myGrid = new int[7][6];
    // If piece is 0, white. If 1, red. If 2, black.

int xCoord, yCoord; // X and Y co-ordinates for mouse navigation.
int width, height;
int playerTurn = 1; // Player's turn. Default is 1 since red goes first.
int mx, my;  // The mouse coordinates.
boolean isButtonPressed = false;

public void init() {
    width = getSize().width;
    height = getSize().height;
    setBackground(Color.yellow);

    mx = width / 2;
    my = height / 2;     

    addMouseListener(this);
    addMouseMotionListener(this);   
}

private int getXValue(int xValue) {
    if (xValue < width / 7) return 0;

    else if (xValue < width / 7 * 2) return 1;

    else if (xValue < width / 7 * 3) return 2;

    else if (xValue < width / 7 * 4) return 3;

    else if (xValue < width / 7 * 5) return 4;  

    else if (xValue < width / 7 * 6) return 5;

    else return 6;
}

private int getYValue(int yValue) {
    if (yValue < width / 6) return 0;

    else if (yValue < width / 6 * 2) return 1;

    else if (yValue < width / 6 * 3) return 2;

    else if (yValue < width / 6 * 4) return 3;

    else if (yValue < width / 6 * 5) return 4;  

    else return 5;
}

public void verticalCheck(int x, int y) {
    if (myGrid[x][y] == 1) {
        int counter = 1;

        for (int i = 5; i >= 0; i--) {
            if (myGrid[xCoord][i] == 1) {
                System.out.println("Counter one in vertical check is " + counter + ".");
                if (myGrid[xCoord][i - 1] == 1 && (i - 1 >= 0)) counter++;
            }
        }

        if (counter == 4) {
            System.out.println("Player 1 has won Connect Four vertically!");
        }
    }

    else if (myGrid[x][y] == 2) {
        int counter = 1;

        for (int i = 5; i >= 0; i--) {
            if (myGrid[xCoord][i] == 2) {
                System.out.println("Counter two in vertical check is " + counter + ".");
                if (myGrid[xCoord][i - 1] == 2 && (i - 1 >= 0)) counter++;
            }
        }

        if (counter == 4) {
            System.out.println("Player 2 has won Connect Four vertically!");
        }
    }
}

public void horizontalCheck(int x, int y) {
    if (myGrid[x][y] == 1) {
        int counter = 1;

        for (int i = 0; i <= 6; i++) {
            if (myGrid[i][y] == 1) {
                System.out.println("Counter one in horizontal check is " + counter + ".");
                if (myGrid[i + 1][y] == 1 && (i + 1 <= 6)) counter++;
            }
        }

        if (counter == 4) {
            System.out.println("Player 1 has won Connect Four horizontally!");
        }           
    }

    else if (myGrid[x][y] == 2) {
        int counter = 1;

        for (int i = 0; i <= 6; i++) {
            if (myGrid[i][y] == 2) {
                System.out.println("Counter two in horizontal check is " + counter + ".");
                if (myGrid[i + 1][y] == 2 && (i + 1 <= 6)) counter++;
            }
        }

        if (counter == 4) {
            System.out.println("Player 2 has won Connect Four horizontally!");
        }
    }
}

public void diagonalCheckRight(int x, int y) {
    if (myGrid[x][y] == 1) {
        int counter = 1;

        for (int i = 0; i <= 6; i++) {
            for (int j = 5; j >= 0; j--) {
                if (myGrid[i][j] == 1) {
                    System.out.println("Counter one in diagonal check right is " + counter + ".");
                    if (myGrid[i + 1][j + 1] == 1 && (i + 1 <= 6) && (j + 1 <= 5)) counter++;
                    else if (myGrid[i - 1][j + 1] == 1 && (i - 1 >= 0) && (j + 1 <= 5)) counter++;
                }
            }
        }

        if (counter == 4) {
            System.out.println("Player 1 has won Connect Four diagonally!");
        }
    }

    else if (myGrid[x][y] == 2) {
        int counter = 1;

        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 5; j++) {
                if (myGrid[i][j] == 2) {
                    System.out.println("Counter two in diagonal check right is " + counter + ".");
                    if (myGrid[i + 1][j + 1] == 1 && (i + 1 <= 6) && (j + 1 <= 5)) counter++;
                    else if (myGrid[i - 1][j + 1] == 1 && (i - 1 >= 0) && (j + 1 <= 5)) counter++;
                }
            }
        }

        if (counter == 4) {
            System.out.println("Player 2 has won Connect Four diagonally!");
        }
    }
}

public void diagonalCheckLeft(int x, int y) {
    if (myGrid[x][y] == 1) {
        int counter = 1;

        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 5; j++) {
                if (myGrid[i][j] == 1) {
                    System.out.println("Counter one in diagonal check left is " + counter + ".");
                    if (myGrid[i - 1][j - 1] == 1 && (i + 1 <= 6) && (j - 1 >= 0)) counter++;
                }
            }
        }

        if (counter == 4) {
            System.out.println("Player 1 has won Connect Four diagonally!");
        }
    }

    else if (myGrid[x][y] == 2) {
        int counter = 1;

        for (int i = 0; i < 6; i++) {
            for (int j = 0; j < 5; j++) {
                if (myGrid[i][j] == 2) {
                    System.out.println("Counter one in diagonal check left is " + counter + ".");
                    if (myGrid[i - 1][j - 1] == 2 && (i <= 6) && (j >= 0)) counter++;
                }
            }
        }

        if (counter == 4) {
            System.out.println("Player 2 has won Connect Four diagonally!");
        }
    }
}

public void mouseEntered( MouseEvent e ) {
    // Called when the pointer enters the applet's rectangular area.
}

public void mouseExited( MouseEvent e ) {
    // Called when the pointer leaves the applet's rectangular area.
}

public void mouseClicked( MouseEvent e ) {
    // Called after a press and release of a mouse button with no motion in between.

    mx = e.getX();
    my = e.getY();

    xCoord = getXValue(mx);
    yCoord = getYValue(my);

    if (myGrid[xCoord][yCoord] == 0 && playerTurn == 1) { // Drop from top, fall to bottom and vice versa.
        for (int y = 5; y >= yCoord; y--) {
            if (myGrid[xCoord][y] == 0) {
                myGrid[xCoord][y] = 1;
                y = yCoord - 1;
            }
        }

        verticalCheck(xCoord, yCoord);
        horizontalCheck(xCoord, yCoord);
        diagonalCheckRight(xCoord, yCoord);
        diagonalCheckLeft(xCoord, yCoord);

        playerTurn = 2;
    }

    else if (myGrid[xCoord][yCoord] == 0 && playerTurn == 2) {          
        for (int y = 5; y >= yCoord; y--) {
            if (myGrid[xCoord][y] == 0) {
                myGrid[xCoord][y] = 2;
                y = yCoord - 1;
            }
        }  

        verticalCheck(xCoord, yCoord);
        horizontalCheck(xCoord, yCoord);
        diagonalCheckRight(xCoord, yCoord);
        diagonalCheckLeft(xCoord, yCoord);

        playerTurn = 1;
    }
}

public void mousePressed(MouseEvent e) {  // Called after a button is pressed down.
    repaint();
    // "Consume" the event so it won't be processed in the
    // default manner by the source which generated it.
    e.consume();
}

public void mouseReleased(MouseEvent e) {  // Called after a button is released.
    repaint();
    e.consume();
}

public void mouseMoved(MouseEvent e) {  // Called during motion when no buttons are down.
    mx = e.getX();
    my = e.getY();

    mx = mx / 50; // Divides applet width by the width of each oval (50).
    my = my / 50; // Divides applet height by the height of each oval (50).

    showStatus("Mouse in column " + (mx + 1) + ", row " + (my + 1) + ".");
}

public void mouseDragged(MouseEvent e) {  // Called during motion with buttons down.
}

public void paint(Graphics g) {
   for (int y = 0; y < 6; y++) {
       for (int x = 0; x < 7; x++) {
            if (myGrid[x][y] == 0) {
                g.setColor(Color.white);
            }

            if (myGrid[x][y] == 1) {
                g.setColor(Color.red);
            }

            if (myGrid[x][y] == 2) {
                g.setColor(Color.black);
            }

            g.fillOval((width / 7) * x + 2, (height / 6) * y + 1, (width / 7) - 4, (height / 6) - 4);
        }
    }
}
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:21:35+00:00Added an answer on June 11, 2026 at 8:21 pm

    Ran the code, your problem is in “diagonalCheckRight”, namely this section:

    for (int i = 0; i <= 6; i++) {
                for (int j = 5; j >= 0; j--) {
                    if (myGrid[i][j] == 1) {
                        System.out.println("Counter one in diagonal check right is " + counter + ".");
                        if (myGrid[i + 1][j + 1] == 1 && (i + 1 <= 6) && (j + 1 <= 5)) counter++;
                        else if (myGrid[i - 1][j + 1] == 1 && (i - 1 >= 0) && (j + 1 <= 5)) counter++;
                    }
                }
            }
    

    Your j index starts at 5, in the if you do myGrid[i+1][j+1] so that means on the first iteration you’re accessing myGrid[1][6], however you defined myGrid to be of size [7][6] and so you’re out of bounds because the valid indices are: [0..6][0..5].

    Also next time look at the error message, my console was showing:
    java.lang.ArrayIndexOutOfBoundsException: 6
    at Main.diagonalCheckRight(Main.java:134)

    I renamed the class to Main, and that 134 is exactly the line number where I found the error.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I'm trying to create is a page of div tags, each has
I want to count how many characters a certain string has in PHP, but
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I've got a string that has curly quotes in it. I'd like to replace
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.