Hi I am currently writing a pool game in java and am very close to finishing it. I am having a problem with the rule of the black ball being potted out of sequence. I need all balls to be potted first before the black ball is potted if all balls are not potted then the game should end and a message displayed. At the moment I do not need to worry about the player being assigned a specific colour so therefore all 14 red/yellow balls must be potted first. The balls are in an array and are numbered from 0 to 15 with the black ball being number 5(I realise that I probably should have made the black ball number 8 in keeping with its number in the game). I'm am not sure how to go about this and would really appreciate any help in regards to checking that all red and yellow balls have been potted before the black ball.

If anyone requires the whole program (3 classes) to view please let me know and I can zip it and send it on to you. Also if you have any other questions then let me know.

Here is the start method where the balls and pockets are created in my main applet class:

public void start()
    {
        b[0] = new Ball(200,425,0,0);
        b[1] = new Ball(200, 140, 0, 0);
        b[2] = new Ball(190, 120, 0, 0);
        b[3] = new Ball(210, 120, 0, 0);
        b[4] = new Ball(180, 100, 0, 0);
        b[5] = new Ball(200, 100, 0, 0);
        b[6] = new Ball(220, 100, 0, 0);
        b[7] = new Ball(170, 80, 0, 0);
        b[8] = new Ball(190, 80, 0, 0);
        b[9] = new Ball(210, 80, 0, 0);
        b[10] = new Ball(230, 80, 0, 0);
        b[11] = new Ball(160, 60, 0, 0);
        b[12] = new Ball(180, 60, 0, 0);
        b[13] = new Ball(200, 60, 0, 0);
        b[14] = new Ball(220, 60, 0, 0);
        b[15] = new Ball(240, 60, 0, 0);

        p[0] = new Ball(2, 2, 0, 0);
        p[1] = new Ball(2, 295, 0, 0);
        p[2] = new Ball(2, 590, 0, 0);
        p[3] = new Ball(387, 2, 0, 0);
        p[4] = new Ball(387, 295, 0, 0);
        p[5] = new Ball(385, 590, 0, 0);

        collision = new Vector2(0.0, 0.0);

        mainThread = new Thread(this);
        mainThread.start();
    }

The b array is balls and there starting positions and starting deltaX and deltaY positions. The p array is creating new pockets and positioning them accordingly.

Inside my Vector2 class where I have my code for potting the balls the following code is used for checking each ball against each pocket in each of the arrays:

private void ballPotted(Ball b[], Ball p[])
    {
            for(int i = 0; i < b.length; i++) 
            {
                for(int j = 0; j < p.length; j ++)
                {
                    double yPosDifference = p[j].getY() - b[i].getY();
                    double xPosDifference = p[j].getX() - b[i].getX();
                    double pot = Ball.getRadius() + Ball.getRadius();
                    double centres = Math.sqrt((yPosDifference * yPosDifference) + (xPosDifference * xPosDifference));

                    if(centres <= pot && (b[i].getVelocity()).magnitude() > 0)
                    {
                        System.out.println("Pot has occurred for ball at position (" + b[i].getX() + ", " +
                                b[i].getY() + ") and pocket position (" + p[j].getX() + ", " + p[j].getY() + ")");

                        performRemoval(b[i], p[j]);
                    }
                }
            }
        }

I have a very basic removal method at the the moment which just removes the balls from the screen and is fine for wot I am trying to achieve at the moment. This method is as follows:

private void performRemoval(Ball b, Ball p)
        {
            b.setDeltaX(0);
            b.setDeltaY(0.0);
            b.setX(500);
            b.setY(700);
        }

Thanks in advance

lj

Recommended Answers

All 2 Replies

Maybe add a boolean to the Ball class for isPotted. (initially false, set t o true when the ball is potted) Then your paint/collision etc methods can call isPotted() to see if the ball is still in play or not (rather than performRemoval). Then when the black is potted you can loop thru and test all the other Balls to see if they are all potted.

Thank you so much for your help. That was brilliant. I got it working.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.