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);
else
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,
Color.YELLOW};
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)
{
b[i].setPotted(true);
PoolHustler.BALLS_POTTED ++;
b[i] = null;
}
if(i == 0)
{
b[0].setDeltaX(0);
b[0].setDeltaY(0);
b[0].setX(200);
b[0].setY(425);
}
if(i == 5)
blackPotted();
}
}
}
}
}
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.
Lj.