Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Nov 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    assign a colour to a player

    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:
    Code:
    public Ball(){}
    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

  • #2
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,979
    Thanks
    4
    Thanked 2,659 Times in 2,628 Posts
    Unfortunately, I am not familiar with the rules of pool or snooker or anything similar.
    I'm assuming this "yellow" and "red" ball's are referring to the high and low balls? If so, that would really make more sense in the Ball object.
    Player I would also create a class out of, not a String. This lets you get away with assigning ownership of Ball to Player instead of needing to check strings. It also lets you manipulate objects within the other objects without bringing in an intermediate class such as the PoolHustle class. A grouping of ball type may also be wise.

    So here's how I perceive this without paying attention to any actual operations on a ball.
    Code:
    public class BallGroup extends Vector<Ball>
    {
        public static enum BALLGROUP {HIGHBALL, LOWBALL};
        private BALLGROUP ballGroup;
        private Player ownedBy;
    }
    
    public class Ball
    {
        private int iNum;
        private String sBallColor;
        private BallGroup ballGroup;
    }
    
    public class Player
    {
        private String sName;
    }
    So what we have here is a ballgroup representing high and low balls, containing a vector of ball, and owned by a player.
    Then we can create them.
    PHP Code:
    BallGroup bgHigh BallGroup.getBallGroup(BallGroup.BALLGROUP.HIGHBALL);
    BallGroup bgLow BallGroup.getBallGroup(BallGroup.BALLGROUP.LOWBALL);
    new 
    Ball(1"yellow"bgLow);
    // ... leave the 8 ball out, or potentially add the 8 ball to both groups
    new Ball(15"brown"bgHigh);
    // btw, all of these balls can be added to any other collect you want as well.
    // the constructor should take care of adding itself to a group

    // Finally, when a ball is first pocketed
    BallGroup bg thatBall.getBallGroup(); // either high or low
    Player p1;
    Player p2;
    // These can be simplified, but this way is cleaner to read
    if (getCurrentPlayer() == player1)
    {
        
    p1 player1;
        
    p2 player2;
    }
    else
    {
        
    p1 player2;
        
    p1 player1;
    }

    if (
    thatBall.getBallGroup() == bgHigh)
    {
        
    // this ball is from the bgHigh group
        
    bgHigh.setOwner(p1);
        
    bgLow.setOwner(p2);
    }
    else
    {
        
    bgLow.setOwner(p2);
        
    bgHigh.setOwner(p1);

    Yes, this is very different than your current approach. But there is one major thing that we both share in your question: the ball groups I'd suggest be separate (high and low in mine, red and yellow in yours?). Whether it be done at an object level to set the owner or using arrays to determine whom owns what, I think it's quite logical to assign an ownership so it can be looked up (cross referencing arrays or using collection object, it makes no difference how you get there).

    With this type of approach, you can literally check if a ball should be sunk by a player, or if you really want you can write a method of iterating it that forces a specific ball that has to go down next.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •