Results 1 to 8 of 8
  1. #1
    javaman2 is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default determine the type of card hand

    im trying to write a program that determines the type of hand with three cards
    (one pair, three of a kind, straight, flush, straight flush, high or low card, or royal straight flush) This is my code so far. I got the cards to come up to be what they would be called, like the ace of spades, but i am having trouble to make my program determine the type. I tried to make the program to determine a flush, one pair, and three of a kind, but it doesnt work. I also cant allow the cards to be the same, like the two of hearts can't come up two or three times, but i also failed at doing that. I really need some feedback and help on this. Thank you in advance.

    import java.util.Random;

    public class java
    {
    public static void main(String[] args)
    {
    Random generating = new Random();
    int x, cardSuit, cardRank, card;
    String suit, rank;

    for(x=1;x<=3;x++)//deal cards
    {
    card = generating.nextInt(52);
    cardRank = card%13;
    cardSuit = card/13;

    if(cardSuit==1)
    suit = "Hearts";
    else if(cardSuit==2)
    suit = "Diamonds";
    else if(cardSuit==3)
    suit = "Clubs";
    else
    suit = "Spades";

    if(cardRank==0 || cardRank==13 || cardRank==26 || cardRank==39)
    rank = "Ace";
    else if(cardRank==1 || cardRank==14 || cardRank==27 || cardRank==40)
    rank = "Two";
    else if(cardRank==2 || cardRank==15 || cardRank==28 || cardRank==41)
    rank = "Three";
    else if(cardRank==3 || cardRank==16 || cardRank==29 || cardRank==42)
    rank = "Four";
    else if(cardRank==4 || cardRank==17 || cardRank==30 || cardRank==43)
    rank = "Five";
    else if(cardRank==5 || cardRank==18 || cardRank==31 || cardRank==44)
    rank = "Six";
    else if(cardRank==6 || cardRank==19 || cardRank==32 || cardRank==45)
    rank = "Seven";
    else if(cardRank==7 || cardRank==20 || cardRank==33 || cardRank==46)
    rank = "Eight";
    else if(cardRank==8 || cardRank==21 || cardRank==34 || cardRank==47)
    rank = "Nine";
    else if(cardRank==9 || cardRank==22 || cardRank==35 || cardRank==48)
    rank = "Ten";
    else if(cardRank==10 || cardRank==23 || cardRank==36 || cardRank==49)
    rank = "Jack";
    else if(cardRank==11 || cardRank==24 || cardRank==37 || cardRank==50)
    rank = "Queen";
    else
    rank = "King";

    if(((x==1)&&(cardSuit==0))&&((x==2)&&(cardSuit==0) )&&((x==3)&&(cardSuit==0)))//test for flush
    System.out.println("Flush");
    else if(((x==1)&&(cardSuit==1))&&((x==2)&&(cardSuit==1) )&&((x==3)&&(cardSuit==1)))
    System.out.println("Flush");
    else if(((x==1)&&(cardSuit==1))&&((x==2)&&(cardSuit==1) )&&((x==3)&&(cardSuit==1)))
    System.out.println("Flush");
    else if(((x==1)&&(cardSuit==1))&&((x==2)&&(cardSuit==1) )&&((x==3)&&(cardSuit==1)))
    System.out.println("Flush");

    if(((x==1)&&(x==2))&&((rank==rank)&&(suit!=suit)))//test for one pair
    System.out.println("One pair");

    if(((x==1)&&(x==2)&&(x==3))&&((rank==rank)&&(suit! =suit)))//test for three of a kind
    System.out.println("Three pair");

    if((x==1 && x==2)&&(rank==rank)&&(suit==suit))//cards won't be the same
    card=generating.nextInt(52);
    System.out.println("Card " + x + " is a " + rank + " of " + suit);
    }
    }
    }

  2. #2
    javaman2 is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default

    my tests for flush, one pair, three of a kind, and so the cards wont have the same rank or suit didn't work. if i could get any suggestions on fixing these problems and help with the other determinations i need to do would be greatly appreciated.

  3. #3
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default

    Suggestions:
    • Use the code button (#) when posting code
    • The "for" loop should be for only creating the hand. After the loop is over, then you can do your comparasions.
    • You're going to need some way to retain the suits and rank of the cards, so you're going to have to save them in an Array or ArrayList. Then you can do all the neccesary comparrations with the elements in the Array.
    • Might be a good idea, before going on any further, to write your design on paper. Once you're satisfied with, when you should start to code.


    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  4. #4
    Join Date
    Dec 2008
    Location
    Italy
    Posts
    79
    Rep Power
    0

    Default

    Quote Originally Posted by Cjlsman
    Might be a good idea, before going on any further, to write your design on paper. Once you're satisfied with, when you should start to code
    Your code is very confused and not well designed. There are lots of Java's features you could use to solve your problem in a more elegant way

  5. #5
    javaman2 is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default

    thank you very much but im a beginner and didnt learn about arrays. i only learned about loops, branch statements, and all the other beginner stuff

  6. #6
    javaman2 is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

    Default

    i dont understand how to use the for loop to create the hand, then to do the comparisons and do the rank and suit for each card.
    By the way, im trying to output three cards.

  7. #7
    CJSLMAN's Avatar
    CJSLMAN is offline Moderator
    Join Date
    Oct 2008
    Location
    Mexico
    Posts
    1,159
    Rep Power
    8

    Default OK... Arrays are out of the game...

    It's easier with Arrays, but if you haven't studied them then you'll have to do without them. I think this is what you want to do:
    • create the hand of three cards (that's three loops). Each card is made up of a suit and a rank (ex: spade, king).
    • You have to somehow save each card's suit and rank to later determine what kind of hand you have. If you can't use Arrays, then you can use variables. You will need 6 variables: card1Suit, card1Rank, card2Suit... etc.
    • In each loop you will assign the corresponding card suit and rank. This can be done by the following pseudo code:

    Java Code:
    if (loop #1) then
    card1Suit = cardsuit;
    card1Rank = cardRank
    if (loop #2) then
    card2Suit = cardsuit;
    card2Rank = cardRank
    etc..
    • Once the loop finishes you have your three card's suits and rank and can compare them to figure what kind of hand you have.


    Luck,
    CJSL
    Chris S.
    Difficult? This is Mission Impossible, not Mission Difficult. Difficult should be easy.

  8. #8
    javaman2 is offline Member
    Join Date
    Jan 2009
    Posts
    5
    Rep Power
    0

Similar Threads

  1. can you give-me a little hand about java threads?
    By masdrobeda in forum New To Java
    Replies: 3
    Last Post: 11-10-2008, 06:15 PM
  2. Can someone give me a helping hand...
    By kwesiaryee in forum Advanced Java
    Replies: 1
    Last Post: 09-17-2008, 08:11 PM
  3. Really Would use a hand
    By mollentze in forum New To Java
    Replies: 7
    Last Post: 07-03-2008, 03:30 PM
  4. Replies: 9
    Last Post: 03-25-2008, 04:46 PM
  5. BlackJack help please,, adding hand values
    By javakid9000 in forum New To Java
    Replies: 1
    Last Post: 11-18-2007, 05:26 AM

Posting Permissions

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