Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: 2 Player Poker Game Troubles

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question 2 Player Poker Game Troubles

    Howdy! I am new to java and am currently trying to complete a java assignment given to me to create a poker game with two players each given five cards in the form of two arrays of buttons. For the most part it works (I think) but I am having a difficult time trying to assign values to each hand strength. I have been attempting to add values to the hand strengths inside the handType() method. My question is: Can I assign an integer value to a boolean? I assume the answer is no. Any thoughts on how to assign my hand strengths (ie highCard = 0, pair = 1, twoPair = 2 and so on)?
    My Code is as follows (I tried to clean it up a bit, sorry in advance for the mess):

    import javax.swing.*; 
    import java.awt.*;   
    import java.awt.event.*; 
     
    public class Player
    {
        private String    name;			
        private Card[]    playerHand  = new Card[5];
        private ImageIcon playerPicture;
        private int       numberDealt = 0;
        private boolean   highCard;
        private boolean   pair;
        private boolean   twoPair;
        private boolean   threeOfAKind;
        private boolean   fullHouse;
        private boolean   straight;
        private boolean   flush;
        private boolean   fourOfAKind;
        private boolean   straightFlush;
        private boolean   royalFlush;
        private String    handType;
     
        public Player()
        {
            name           = "Card Shark";	
            playerPicture  = new ImageIcon("owned.gif");
            numberDealt    = 0;
        }
     
        public String getName()
        {
            return name;   
        }
     
        public void setName(String n)
        {
            name = n;
        }
     
        public ImageIcon getplayerPicutre()
        {
            return playerPicture;	
        }
     
        public void setplayerPicture(ImageIcon p)
        {
            playerPicture = p;
        }
     
        public Card getplayerHand()
        {
            if(numberDealt < 0)
            {
     
            }
            else
            {
                numberDealt --;			
            }
        }
     
        public void setplayerHand(Card c)
        {
            playerHand[numberDealt] = c;
     
            if(numberDealt < 5)
            {
                numberDealt ++;	
            }
        }
     
        public String handType()
        {
              if(highCard == true)
              {
                  return "Player has High Card";
              }
    if(pair == true)
    {
         return "Player has a pair";
    }
     
    if(twoPair == true)
    {
         return "Player has a pair";
    }
     
    if(threeOfAKind == true)
    {
    return "Player has three of a kind";
    }
     
    if(fullHouse == true)
    {
    return "Player has a full house";
    }
     
    if(straight == true)
    {
    return "Player has a straight";
    }
     
    if(flush == true)
    {
    return "Player has a flush";
    }
     
    if(fourOfAKind == true)
    {
    return "Player has four of a kind";
    }
     
    if(straightFlush == true)
    {
    return "Player has a straight flush";
    }
    if(royalFlush == true)
    {
    return "Player has a royal flush";
    }
    return handType;
    }
     
        public void sortByRank()
        {
     
            for(int x=0; x<4; x++)
            {
     
                for(int s=0; s<4; s++)
                {
     
                    if(playerHand[s].getRank() > playerHand[s+1].getRank())
                    {
    				playerHand[s]
                        Card tempCard   = playerHand[s];
                        playerHand[s]   = playerHand[s+1];
                        playerHand[s+1] = tempCard;
                    }
                }
            }
        }
     
        public void sortByValue()
        {
     
            for(int x=0; x<4; x++)
            {
     
                for(int s=0; s<4; s++)
                {
     
                    if(playerHand[s].getValue() > playerHand[s+1].getValue())
                    {
                        Card tempCard   = playerHand[s];
                        playerHand[s]   = playerHand[s+1];
                        playerHand[s+1] = tempCard;
                    }
                }
            }
        }
     
        public void sortBySuit()
        {
            for(int x=0; x<4; x++)
            {
                for(int s=0; s<4; s++)
                {
                    if(playerHand[s].getSuit() > playerHand[s+1].getSuit())
                    {
                        Card tempCard   = playerHand[s];
                        playerHand[s]   = playerHand[s+1];
                        playerHand[s+1] = tempCard;
                    }
                }
            }
        }
     
    public boolean highCard()
    {
    if(pair == false);
    {
       if(twoPair == false);
       {
          if(threeOfAKind == false)
          {
             if(fullHouse == false)
             {
                if(straight == false)
                {
                   if(flush == false)
                   {
                      if(straightFlush == false)
                      {
                         if(royalFlush == false)
                        {
                             highCard = true;
                         }
                      }
                  }
               }
            }
          }
        }
      }
    return highCard;
    }
     
        public boolean pair()
        {
    		for(int slot=0; slot<4; slot++)
    		{
    			if(playerHand[slot].getValue() == playerHand[slot+1].getValue())
    			{
    				pair = true;
    			}
    			else
    			{
    				pair = false;
    			}
    		}
     
    		return pair;
    	}
     
    public boolean twoPair()
    {
    	for(int slot=0; slot<2; slot++)
    	{
    		if(playerHand[slot].getValue() == playerHand[slot+1].getValue())
    		{
    		if(playerHand[slot+2].getValue() == playerHand[slot+3].getValue())
    		{
    			twoPair = true;
    		}
    		else
    		{
    			twoPair = false;
    		}
    	}
    }
    return twoPair;
    }
     
    public boolean threeOfAKind()
    {
    for(int slot=0; slot<3; slot++)
    {
    if(playerHand[slot].getValue() == playerHand[slot+1].getValue())
    {
    	if(playerHand[slot+1].getValue() == playerHand[slot+2].getValue())
    	{
    	threeOfAKind = true;
    	}
    }
    }
    return threeOfAKind;
    }
     
    public boolean fullHouse()
    {
    for(int slot=0; slot<1; slot++)
    {
         if(playerHand[slot].getValue() == playerHand[slot+1].getValue())
         {
            if(playerHand[slot+1].getValue() == playerHand[slot+2].getValue())
            {
               if(playerHand[slot+3].getValue() == playerHand[slot+4].getValue())
               {
                  fullHouse = true;
                }
            }
         }
       }
       return fullHouse;
    }
     
    public boolean straight()
    {
        for(int slot=0; slot<3; slot++)
       {
         if(playerHand[slot].getValue() == playerHand[slot+1].getValue()+1)
         {
             straight = true;
          }
        }
        return straight;
    }
     
    public boolean flush()
    {
         for(int slot=0; slot<1; slot++)
         { 
             if(playerHand[slot].getSuit() == playerHand[slot+1].getSuit())
             {
                if(playerHand[slot+1].getSuit() == playerHand[slot+2].getSuit())
               {
                   if(playerHand[slot+2].getSuit() == playerHand[slot+3].getSuit())
                   {
    	if(playerHand[slot+3].getSuit() == playerHand[slot+4].getSuit())
    	{
    	    flush = true;
    	}
                   }
               }
           }
        }
       return flush;
    }
     
    public boolean fourOfAKind()
    {
         for(int slot=0; slot<2; slot++)
         {
             if(playerHand[slot].getValue() == playerHand[slot+1].getValue())
             {
                 if(playerHand[slot+1].getValue() == playerHand[slot+2].getValue())
                 {
                    if(playerHand[slot+2].getValue() == playerHand[slot+3].getValue())
                    {
                       if(playerHand[slot+3].getValue() == playerHand[slot+4].getValue())
                       {
                          fourOfAKind = true
                       }
                    }
                 }
              }
           }
           return fourOfAKind;
    }
     
    public boolean straightFlush()
    {
        if(straight == true && flush == true)
        {
            straightFlush = true;
         }
         else
         {
            straightFlush = false;
         }
         return straightFlush;
    }
     
    public boolean royalFlush()
    {
        if(flush == true)
        {
            for(int slot=0; slot<1; slot++)
            {
                if(playerHand[slot].getValue() == 10)
                {
                   if(playerHand[slot+1].getValue() == 11)
                   {
                      if(playerHand[slot+2].getValue() == 12)
                     {
                        if(playerHand[slot+3].getValue() == 13)
                        {
                           if(playerHand[slot+4].getValue() == 14)
                           {
                               royalFlush = true;
                           }
                        }
                     }
                 }
              }
            }
         }
         return royalFlush;
       }
     
       public String gethandType()
       {
         return handType;
       }
    } // end player
    Last edited by Kaltonse; December 19th, 2010 at 07:57 PM.


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,320
    Thanks
    181
    Thanked 833 Times in 772 Posts
    Blog Entries
    5

    Default Re: 2 Player Poker Game Troubles

    You cannot assign an integer to a boolean, however you could create a class which groups them together. Alternatively, you could squeeze the boolean's into an integer using bitwise operators, which could then serve as the hand strength value
    public static int HIGH_CARD = 1;
    public static int PAIR = 2;
    public static int TWO_PAIR = 4;
    public static int FLUSH = 8;
    .....
    int strength = 0;
    if ( there is a pair ){
        strength |= PAIR;//assigns the 3rd bit to 1.
    }
    ...
    if ( (strength & PAIR) != 0 ){
     ///pair
    }

  3. The Following User Says Thank You to copeg For This Useful Post:

    Kaltonse (December 21st, 2010)

  4. #3
    Junior Member
    Join Date
    Dec 2010
    Posts
    5
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: 2 Player Poker Game Troubles

    Quote Originally Posted by copeg View Post
    You cannot assign an integer to a boolean, however you could create a class which groups them together. Alternatively, you could squeeze the boolean's into an integer using bitwise operators, which could then serve as the hand strength value
    Thank you for your help. After reading your reply I thought it over a bit and decided to declare a new variable to be given strengths and will compare the new variable in my poker class while using the handType variable to simply have the game print out which hand a player has.

    Thanks! ( / +rep )

Similar Threads

  1. [SOLVED] Platform game player doesn't respond to keyboard
    By jesamjasam in forum What's Wrong With My Code?
    Replies: 4
    Last Post: December 12th, 2010, 09:44 AM
  2. ArrayList troubles
    By javapenguin in forum What's Wrong With My Code?
    Replies: 20
    Last Post: November 18th, 2010, 06:03 PM
  3. help building up GUI for poker game
    By Pencil in forum AWT / Java Swing
    Replies: 5
    Last Post: October 26th, 2010, 02:53 PM
  4. JSP Troubles
    By sdkeslar in forum What's Wrong With My Code?
    Replies: 0
    Last Post: February 12th, 2010, 02:26 PM
  5. Please help Me out with this poker Problem
    By ckrisasa in forum Java Networking
    Replies: 1
    Last Post: November 29th, 2009, 02:31 AM