Hi,
I am currently writing a java pool game in eclipse. I am currently trying to assign a ball colour(red or yellow) to a player who pots the first of these balls.
At the moment I have an array of balls declared in my main applet (PoolHustler) class from the Ball class.
Code:
Ball b[] = new Ball[16];
Within the start() method of the same class has each balls x/y positions, the deltaX/Y(ball speed) values and the balls colour as follows:
Code:
b[0] = new Ball(200,425,0,0, "White");
b[1] = new Ball(200, 140, 0, 0, "Red");
b[2] = new Ball(190, 120, 0, 0, "Yellow");
b[3] = new Ball(210, 120, 0, 0, "Red");
b[4] = new Ball(180, 100, 0, 0, "Red");
b[5] = new Ball(200, 100, 0, 0, "Black");
b[6] = new Ball(220, 100, 0, 0, "Yellow");
b[7] = new Ball(170, 80, 0, 0, "Yellow");
b[8] = new Ball(190, 80, 0, 0, "Red");
b[9] = new Ball(210, 80, 0, 0, "Yellow");
b[10] = new Ball(230, 80, 0, 0, "Red");
b[11] = new Ball(160, 60, 0, 0, "Yellow");
b[12] = new Ball(180, 60, 0, 0, "Red");
b[13] = new Ball(200, 60, 0, 0, "Yellow");
b[14] = new Ball(220, 60, 0, 0, "Red");
b[15] = new Ball(240, 60, 0, 0, "Yellow");
In my Ball class I have 2 constructors. The first is for setting details of the cue ball(white). The second is for setting the details of all other balls. The constructors are as follows in the Ball class:
First constructor:
second constructor:
Code:
public Ball(double i, double j, double k, double l, String m)
{
x = i;
y = j;
deltaX = k;
deltaY = l;
ballColour = m;
}
I also have get and set methods for the colour of the balls:
Code:
public static String getBallColour(){
return ballColour;
}
public static void setBallColour(String ballColour){
Ball.ballColour = ballColour;
}
In my applet class I have a method called playerTurn(). The following method determines which player is the current player:
Code:
public String playerTurn()
{
if(previousBallsPotted == BALLS_POTTED)
currentPlayer = (currentPlayer.equals(player1) ? player2 : player1);
else
currentPlayer = (currentPlayer.equals(player1) ? player1 : player2);
return currentPlayer;
}
To paint the balls colours I have an array of colours within my paint() method. The order of these colours are determined by the number of each ball. For example white is b[0] and the black ball is b[5]. The array is as follows:
Code:
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:
Code:
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)
{
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.
Code:
private void blackPotted()
{
String winner = PoolHustler.player1;
if(PoolHustler.BALLS_POTTED <= 14)
{
if(PoolHustler.currentPlayer.equals(PoolHustler.player1))
winner = PoolHustler.player2;
JOptionPane.showMessageDialog(null, "GAME OVER \nPOTTED BLACK BALL OUT OF SEQUENCE!!\n" + winner + " WINS!!");
System.exit(0);
}
else
JOptionPane.showMessageDialog(null, PoolHustler.currentPlayer +" WINS!!! \nWELL DONE \nGAME OVER");
System.exit(0);
}
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.
What I have to do here is assign the first colour to be potted to the current player. Initially I have "Red/Yellow" appearing on the screen before a ball is potted. I am wondering how to go about giving red or yellow to the player. As you can see I have get and set methods for the ball colours. What I'm not sure about is whether or not I should create another 2 arrays(one for red and one for yellow) in order to group the 7 of each coloured balls so that the if statement in the potting logic doesn't have to contain each ball or am I way off track? I also need to alter the blackPotted() method so that instead of 14 balls being potted that once the 7 red/yellow balls are potted then the player who is assigned those balls can pot the black.
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 project then please pm me and I will post it.
Thanks in advance.
coding7