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. And if anyone can help me get the straights or high or low cards to work too, i would greatly appreciate it. Im trying to work on this code this very minute and i need help

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);
}
}
}

Hello, javaman2. You have mistake in logic here:

if(((x==1)&&(cardSuit==0))&&((x==2)&&(cardSuit==0) )&&((x==3)&&(cardSuit==0)))

All that expression contains only '&&' condition. So if a bit transform it, you are telling: do something, if that condition turns true (x==1) and (x==2) and (x==3) .... Also there are same mistakes in similar expressions. I suppose, that it must be 'or' between pairs of expressions: ((x==1)&&(cardSuit==0))||((x==2)&&(cardSuit==0) P.S. I'm also have some advices about organization of your prog. If interested - ask :)

Hello,

I was playing with your code and found few errors in it.
I added this code of three lines:

System.out.println("Card Is : "+card);
System.out.println("CardRank Is : "+cardRank);
System.out.println("CardSuit Is : "+cardSuit);

Outupt:

Card Is : 2
CardRank Is : 2
CardSuit Is : 0
Card 1 is a Three of Spades // should be Two of spades
Card Is : 5
CardRank Is : 5
CardSuit Is : 0
Card 2 is a Six of Spades // // should be Five of spades
Card Is : 11
CardRank Is : 11
CardSuit Is : 0
Card 3 is a Queen of Spades // // should be Jack of spades

Generating wrong output (according to me).

so i have modified your code.

Random generating = new Random();
int x, cardSuit, cardRank, card;
String suit, rank;

String suits[] = {"Spades","Hearts","Diamonds","Clubs"};
String ranks[] = {"","Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};

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

System.out.println("Card Is : "+card);
System.out.println("CardRank Is : "+cardRank);
System.out.println("CardSuit Is : "+cardSuit);

suit = suits[cardSuit];
rank = ranks[cardRank];

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);
}

and output is:

Card Is : 23
CardRank Is : 10
CardSuit Is : 1
Card 1 is a Ten of Hearts
Card Is : 4
CardRank Is : 4
CardSuit Is : 0
Card 2 is a Four of Spades
Card Is : 32
CardRank Is : 6
CardSuit Is : 2
Card 3 is a Six of Diamonds

I dont know what flush, one pair and three pair is all about? so i can say anything about it.

But if you can tell me, then i will be glad to help you.

Regards,

thank you very much for the help. a flush is when the suit are the same, one pair is when two card have the same rank and may have the same suit, like the two of hearts and the two of spades is one pair, and three of a kind is when the three cards have the same rank regardless of suit

antenka, if you could help me organize my work a little. i know its messed up and i would greatly appreciate your help

Hello, javaman2. You have mistake in logic here:

if(((x==1)&&(cardSuit==0))&&((x==2)&&(cardSuit==0) )&&((x==3)&&(cardSuit==0)))

All that expression contains only '&&' condition. So if a bit transform it, you are telling: do something, if that condition turns true (x==1) and (x==2) and (x==3) .... Also there are same mistakes in similar expressions. I suppose, that it must be 'or' between pairs of expressions: ((x==1)&&(cardSuit==0))||((x==2)&&(cardSuit==0) P.S. I'm also have some advices about organization of your prog. If interested - ask :)

but i need all three of them to be true to have Flush output when all three cards have the same suit

If I were you, I would probably do that:
1. Create a class, that will represent a single card (it will contain rank, suit). I suppose it will be better to use enum to list all possible suits and ranks.
That class can contain constructor, that will generate some card. And this class can contain equals method to compare 2 cards. With equals method you'll check if the card was already generated.
2. Next you'll need check appearances of some card combinations. For that you can create methods in some else class (that will contain methods, to check a list of cards for all possible combinations that you need - flush, pair ....). Also in this methods you shouldn't 'think' about position of the card (here you do that: (x==1) ) ... for alternative, you could use, for example some 'flags' for each card in hand.

Im still confused on One Pair so i havent included it.

Code :

package com.puneetk.testwork;
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;

		String suits[] = {"Spades","Hearts","Diamonds","Clubs"};
		String ranks[] = {"","Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};
		int storeSuit[] = new int[4];
		int storeRank[] = new int[4];
		for(x=1;x<=3;x++)//deal cards
		{
			card = generating.nextInt(52);
			//card = 5;
			cardRank = card%13;
			cardSuit = card/13;

			//System.out.println("Card Is : "+card);
			//System.out.println("CardRank Is : "+cardRank);
			//System.out.println("CardSuit Is : "+cardSuit);

			suit = suits[cardSuit];
			rank = ranks[cardRank];
			
			storeSuit[x] = cardSuit;
			storeRank[x] = cardRank;

			System.out.println("Card " + x + " is a " + rank + " of " + suit);
			
			if((x==3)){
				if((storeSuit[1] == storeSuit[2]) && (storeSuit[3] == storeSuit[2])){
					System.out.println("Flush");
				}
				if((storeRank[1] == storeRank[2]) && (storeRank[3] == storeRank[2])){
					System.out.println("Three Pair");
				}
			}
		}
	}
}

Output:

Card 1 is a Five of Spades
Card 2 is a Five of Spades
Card 3 is a Five of Spades
Flush
Three Pair

best of luck bro! :D

im sorry to be a bother but i havent learned how to do the following
String suits[] = {"Spades","Hearts","Diamonds","Clubs"};
String ranks[] = {"","Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};
int storeSuit[] = new int[4];
int storeRank[] = new int[4];
suit = suits[cardSuit];
rank = ranks[cardRank];
storeSuit[x] = cardSuit;
storeRank[x] = cardRank;

if there is any other way with what i showed im still trying to figure it out.
thank you for all your help i really appreciate it

im sorry to be a bother but i havent learned how to do the following
String suits[] = {"Spades","Hearts","Diamonds","Clubs"};
String ranks[] = {"","Ace","Two","Three","Four","Five","Six","Seven","Eight","Nine","Ten","Jack","Queen","King"};
int storeSuit[] = new int[4];
int storeRank[] = new int[4];
suit = suits[cardSuit];
rank = ranks[cardRank];
storeSuit[x] = cardSuit;
storeRank[x] = cardRank;

if there is any other way with what i showed im still trying to figure it out.
thank you for all your help i really appreciate it

Then you better use Switch case or if else ( to get Suit and Rank ).

To check Flush, One Pair and Three Pair, you need to store old values in variables or arrays. I dont think theres any other way to do it.

Regards, :-/

This question has already been answered. Start a new discussion instead.