Ranking Poker Hands Java

Some players will claim hands like 3 5 are worse, because when you input 2 7 and 3 5 into a poker hand calculator, the 2 7 actually rates higher. But that’s a heads up battle involving only two hands, so naturally the seven high has a better chance to win based on the many occasions when both hands remained unpaired. . Can construct a hand from standard input, format a hand for output,. determine if a hand is a flush or a full house. Test main sets up 5-card poker hands from standard input,. prints flushes and full houses (one per line). Poker Hand Evaluator Java. Here are the possible poker hands, in increasing order of value: pair: two cards with the same rank. Poker Hand Rankings Explained If two players have a Straight or Straight Flush, the higher Straight or Straight Flush wins. If two players have a quads, the player with the highest quad wins. If they are identical, the highest kicker wins. Poker hand rankings is the first thing you need to learn when starting with poker, and this page will teach you everything you need to know. We will cover poker hands list in order, best poker hands in Texas Holdem and detailed examples of how to analyze your hands when playing.

  1. Ranking Poker Hands Javascript
  2. Ranking Poker Hands Java Tutorial
  3. Ranking Poker Hands Java Games

This program creates a deck of 52 cards, similar to a authentic deck of poker cards. Two hands of 5 cards are then distributed to two players. The hands are evaluated and if your hand is better than the opponent, you win.

***NOTE : There are 4 seperate java files, I will post each below ***

//hand.java
public class Hand //Creating class Hand which will contain one persons cards and evaluate.

{
private Card[] cards; //cards represent one player’s cards.
private int[] value; //value is used later to determine hand strength.

Hand(Deck d)
{
value = new int[6]; //value is used to determine what kind of hand a player has.
cards = new Card[5];

Hands

for (int x=0; x<5; x++)//Draw five cards from deck and place them in cards.
{
cards[x] = d.drawFromDeck();
}

Poker

int[] ranks = new int[14];
int[] orderedRanks = new int[5];
boolean flush = true, straight = false; //Variables to be used to determine pairs, flush, etc.
int sameCards = 1, sameCards2 = 1;
int largeGroupRank = 0,smallGroupRank = 0;
int index = 0;
for (int x = 0; x <= 13; x++) //Clean out ranks array.
{
ranks[x] = 0;
}
for (int x = 0; x <= 4; x++) //Assign rank to cards in hand.
{
ranks[ cards[x].getRank() ]++;
}

for (int x = 0; x < 4; x++) { //Test for similar suit, for possible flush.
if ( cards[x].getSuit() != cards[x+1].getSuit() )
flush = false;
}

for (int x = 13; x >= 1; x–) //Determine the amount of multiple cards
{ //and determine the highest face value for possible full house.
if (ranks[x] > sameCards)
{
if (sameCards != 1)

{
sameCards2 = sameCards;
smallGroupRank = largeGroupRank;
}

sameCards = ranks[x];
largeGroupRank = x;

}

else if (ranks[x] > sameCards2)
{
sameCards2 = ranks[x];
smallGroupRank = x;
}
}
if (ranks[1]1) //Run first if ace since it is the highest value.
{
orderedRanks[index]=14;
index++;
}

for (int x=13; x>=2; x–)
{
if (ranks[x]1)
{
orderedRanks[index]=x;
index++;
}
}

for (int x=1; x<=9; x++) //We set constraints to 9 because a straight can’t begin at 10.
{
if (ranks[x]1 && ranks[x+1]1 && ranks[x+2]1 && ranks[x+3]1 && ranks[x+4]1) //If ranks ascend = straight.
{
straight=true;
break;
}
}

if (ranks[10]1 && ranks[11]1 && ranks[12]1 && ranks[13]1 && ranks[1]1) //If straight.
{
straight=true;
}

for (int x=0; x<=5; x++)
{
value[x]=0; //Clean array value[]
}

if ( sameCards1 ) //Evaluation of hands.
{
value[0]=1;
value[1]=orderedRanks[0];
value[2]=orderedRanks[1];
value[3]=orderedRanks[2];
value[4]=orderedRanks[3];
value[5]=orderedRanks[4];
}

if (sameCards2 && sameCards21)
{
value[0]=2;
value[1]=largeGroupRank; //Rank of pair
value[2]=orderedRanks[0];
value[3]=orderedRanks[1];
value[4]=orderedRanks[2];
}

Ranking Poker Hands Javascript

if (sameCards2 && sameCards22) //Two pair
{
value[0]=3;
value[1]= largeGroupRank>smallGroupRank ? largeGroupRank : smallGroupRank;
value[2]= largeGroupRank<smallGroupRank ? largeGroupRank : smallGroupRank;
value[3]=orderedRanks[0];
}

if (sameCards3 && sameCards2!=2)
{
value[0]=4;
value[1]= largeGroupRank;
value[2]=orderedRanks[0];
value[3]=orderedRanks[1];
}

Ranking Poker Hands Java

if (straight)
{
value[0]=5;
value[1]=orderedRanks[0];
}

if (flush)
{
value[0]=6;
value[1]=orderedRanks[0]; //Tie determined by ranks of cards
value[2]=orderedRanks[1];
value[3]=orderedRanks[2];
value[4]=orderedRanks[3];
value[5]=orderedRanks[4];
}

if (sameCards3 && sameCards22)
{
value[0]=7;
value[1]=largeGroupRank;
value[2]=smallGroupRank;
}

if (sameCards4)
{
value[0]=8;
value[1]=largeGroupRank;
value[2]=orderedRanks[0];
}

}

void strength() //Display method is used to determine and print the strength of a hand.
{
String result;
switch( value[0] ) //The higher the value[] , the stronger the hand.
{

case 1:
result=”high card”;
break;
case 2:
result=”pair of ” + Card.rankAsString(value[1]) + “’s”;
break;
case 3:
result=”two pair ” + Card.rankAsString(value[1]) + ” ” + Card.rankAsString(value[2]);
break;

case 4:
result=”three of a kind ” + Card.rankAsString(value[1]) + “’s”;
break;

case 5:
result=Card.rankAsString(value[1]) + ” high straight”;
break;

case 6:
result=”flush”;
break;

case 7:
result=”full house ” + Card.rankAsString(value[1]) + ” over ” +
Card.rankAsString(value[2]);
break;

case 8:
result=”four of a kind ” + Card.rankAsString(value[1]);
break;

default:
result=”Error, check coding”;
}

System.out.print(result); //Strength of hand, “pair, three of a kind etc” is printed.
}

void displayCards() //This method prints the hands that a player has in their hand.
{
for (int x=0; x<5; x++) //Print all 5 cards in hand.
System.out.printf(“%-16stn”, cards[x]);

System.out.printf(“n”);
}

int compareTo(Hand that) //Comparing hands. The top hand relates to “this”, bottom hand “that”.
{
for (int x=0; x<6; x++)
{
if (this.value[x]>that.value[x])
return 1;
else if (this.value[x]<that.value[x])
return -1;
}
return 0; //Return value translates to top/bottom winner.
}
}

//Deck.java
import java.util.Random; //import Random and ArrayList which will be used in our deck.

import java.util.ArrayList;

public class Deck { //Creating class Deck which will hold cards from Card.
private ArrayList<Card> cards;

Deck()
{
cards = new ArrayList<Card>(); //cards represent the cards in the Deck.
int index_1, index_2; //index_1 & index_2 will be used in
Random generator = new Random(); //generator is created to generate random numbers.
Card temp;
for (short a=0; a<=3; a++) //For loop fills deck with cards
{
for (short b=0; b<=12; b++)
{
cards.add( new Card(a,b) ); //New card is added to cards
}
}

int size = cards.size() -1; //size is created to store total amount of cards in deck.

for (short i=0; i<52; i++)
{
index_1 = generator.nextInt( size ); //Random card is generated.
index_2 = generator.nextInt( size );

temp = (Card) cards.get( index_2 );
cards.set( index_2 , cards.get( index_1 ) );
cards.set( index_1, temp );
}
}

Ranking poker hands javascript

public Card drawFromDeck() //This method is used when drawing card from deck to place in hand.
{
return cards.remove( cards.size()-1 ); //Deck size is adjusted each time.
}
}

//card.java
public class Card //Card class represents the cards that will be contained in Deck.
{
private short rank, suit;

private static String[] suits = { “hearts”, “spades”, “diamonds”, “clubs” }; //suits and ranks are arrays
private static String[] ranks = { “Ace”, “2”, “3”, “4”, “5”, “6”, “7”, //that will be placed in cards[]
“8”, “9”, “10”, “Jack”, “Queen”, “King” };

public static String rankAsString( int __rank ) //Convert rank and return
{
return ranks[__rank];
}

Card(short suit, short rank)
{
this.rank=rank;
this.suit=suit;
}

public @Override String toString()
{
return ranks[rank] + ” of ” + suits[suit];
}

public short getRank() { //Assign rank.
return rank;
}

public short getSuit() { //Assign suit.
return suit;
}
}

//main.java

public class Main
{
public static void main(String[] args)
{
Deck deck= new Deck(); //Creating constructors to call
Hand hand= new Hand(deck); //classes Deck and Hand which create
Hand hand2= new Hand(deck); //a deck of cards and deals & evaluates two hands.

System.out.printf(“%-16sn”,”Top hand:”);
//Top and bottom’s cards are printed to the screen, as well as the strength of each hand.
hand.displayCards();
System.out.printf(“%-16sn”,”Bottom hand:”);
hand2.displayCards();
System.out.printf(“Hand Values:nTop: “); hand.strength();
System.out.printf(“nBottom: “); hand2.strength();
System.out.printf(“nn”);

Ranking Poker Hands Java Tutorial

//The winner of the game is printed to the screen after a comparison.
if(hand.compareTo(hand2) 1)
System.out.println(“Result: Top hand wins!”);

Ranking Poker Hands Java Games

else if(hand.compareTo(hand2) -1)
System.out.println(“Result: Bottom hand wins!”);

else
System.out.println(“Result: Draw!”);
}
}