Hi all,

I'm am very close to finishing my 2 player pool game with only a couple of rules left to add. I am currently trying to assign a ball colour (red or yellow) to the player who pots the first ball. I am at a loss at the minute as to how to go about coding this.

in my applet class I have a method called playerTurn(). This method determines which player is the current player. This method is as follows:

public String playerTurn()
            if(previousBallsPotted == BALLS_POTTED)
                currentPlayer = (currentPlayer.equals(player1) ? player2 : player1);
                currentPlayer = (currentPlayer.equals(player1) ? player1 : player2);
            return currentPlayer;

To set the balls colours I have an array of colours within my paint() method. The order of these colours is determined by the number of each ball. For example white is ball 0 and black is ball 5. The array is as follows:

Color[] colours = {Color.WHITE, Color.RED, Color.YELLOW, Color.RED, Color.RED,
                           Color.BLACK, Color.YELLOW, Color.YELLOW, Color.RED, Color.YELLOW,
                           Color.RED, Color.YELLOW, Color.RED, Color.YELLOW, Color.RED,

Finally in my Vector2 class which deals with collisions between the balls and balls and pockets I have a method called ballPotted(). This method is as follows:

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

                        if(centres <= pot)
                            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() + ")");

                            if(i != 0)
                                PoolHustler.BALLS_POTTED ++;
                                b[i] = null;

                            if(i == 0)

                            if(i == 5)

You'll notice that the first if() statement deals with any ball which is potted except the cue ball. While the last if() statement deals with if the black is potted. If the black ball is potted then the game ends. This is dealt with in the blackPotted() method.

I am wondering if anyone can point me in the right direction as to how I should go about assigning a colour to a player when the first red or yellow ball is potted and the other colour to the second player. If there is anything else you would like to know or if you would like a copy of my entire classes then please let me know and I will post it.

Thanks in advance.


Is there a Player class? Does it have get/setColor methods? Is the color initially null?
Why not just have a test in the potting logic that says if ball is yellow or red AND player1.getColor()==null, call setColor as appropriate?

I don't have a Player class. I have Ball, PoolHustler(main applet) and Vector2 classes. When the applet starts the balls are positioned using this code in the start() method of the PoolHustler class:

        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);

The balls are then painted on the screen using the following code within the paint() method again in the PoolHustler class:

        for(int i = 0; i < b.length; i++)
            if(b[i] != null)
                b[i].display(g, colours[i]);

This for loop goes throught the array of balls and assigns the corresponding ball colour with the numbered ball. For example the cue ball b[0] will be white and b[5] will be black in accordance with the order of the colours. I don't have get/setColour and the colour will not be null initially but will be null once a ball is potted.

You want to assign a color to a player. In Object Oriented terms that immediately implies that Player is a class and color is one of its attribues (member variables). Maybe you should look at your design again and see what other info is associated with a player and how that could fit into an OO solution?

Yeah I'll have a look over it for now so and see. Thanks a lot. I have variables called player1 and player2 in the again in the start() method of the PoolHustler class:

The first line are the variables:

        public static String player1, player2, currentPlayer;

This code is held within the start() method:

        player1 = JOptionPane.showInputDialog("Please enter the name of Player 1: ");
        player2 = JOptionPane.showInputDialog("Please enter the name of Player 2: ");
        currentPlayer = player1;

I'll have a look over it for now anyway and see where I should go.

Thanks again.

This question has already been answered. Start a new discussion instead.