📄 pokerhand.java
字号:
protected Card[] deck = new Card[52];
public DeckOfCards()
{
String[] suit = {"Hearts","Diamonds","Clubs","Spades"};
int j = 0;
for ( int i = 0 ; i<4 ; i++ )
{
deck[j++] = new Ace(suit[i]);
deck[j++] = new Two(suit[i]);
deck[j++] = new Three(suit[i]);
deck[j++] = new Four(suit[i]);
deck[j++] = new Five(suit[i]);
deck[j++] = new Six(suit[i]);
deck[j++] = new Seven(suit[i]);
deck[j++] = new Eight(suit[i]);
deck[j++] = new Nine(suit[i]);
deck[j++] = new Ten(suit[i]);
deck[j++] = new Jack(suit[i]);
deck[j++] = new Queen(suit[i]);
deck[j++] = new King(suit[i]);
}
}
public PokerHand deal()
{
PokerHand deal = new PokerHand(this);
for (int i = 0 ; i<5 ; i++ )
{
deal.CardsOfHand[i] = nextCard();
}
return deal;
}
public void shuffle()
{
Random dice = new Random(System.currentTimeMillis());
int num1,num2;
Card temp = new Card();
for ( int i = 0 ; i<1000 ; i++ )
{
num1 = Math.abs(dice.nextInt())%52;
num2 = Math.abs(dice.nextInt())%52;
temp = deck[num1];
deck[num1] = deck[num2];
deck[num2] = temp;
}
DeckOfCards newDeck = new DeckOfCards();
int j = 0 ;
for (int i = 0 ; i<52 ; i++ )
{
newDeck.deck[i] = null;
}
for ( int i = 0 ; i<52 ; i++ )
{
if(deck[i] != null)
{
newDeck.deck[j] = deck[i];
j++;
}
}
deck = newDeck.deck;
}
public Card nextCard()
{
Card copy = new Card();
DeckOfCards newDeck = new DeckOfCards();
for (int i = 0 ; i<52 ; i++ )
{
if (deck[i] != null)
{
copy = deck[i];
deck[i] = null;
//to rearrange the sequence of the cards(delete blanks)
int j = 0 ;
for (int k = 0 ; k<52 ; k++ )
{
newDeck.deck[k] = null;
}
for ( int k = 0 ; k<52 ; k++ )
{
if(deck[k] != null)
{
newDeck.deck[j] = deck[k];
j++;
}
}
deck = newDeck.deck;
return copy;
}
}
return null;
}
public String toString()
{
String str = "";
for ( int i = 0 ; i<52 ; i++ )
{
if (deck[i] != null)
{
str += deck[i].toString()+"\n";
}
}
return str;
}
}
public class PokerHand
{
protected Card[] CardsOfHand = new Card[5];
static DeckOfCards thisDeck ;
public PokerHand()
{}
public PokerHand(DeckOfCards deck)
{
for (int i = 0 ; i < 5 ; i++ )
{
CardsOfHand[i] = deck.nextCard();
}
thisDeck = deck;
}
public PokerHand(Card[] cards,DeckOfCards deck)
{
this.CardsOfHand = cards;
this.thisDeck = deck;
}
public PokerHand discard()
{
return this;
}
public PokerHand discard(int num)
{
Card badCard = CardsOfHand[num];
CardsOfHand[num] = thisDeck.nextCard();
for (int i = 0 ; i<52 ; i++ )
{
if(thisDeck.deck[i] == null)
{
thisDeck.deck[i] = badCard;
break;
}
}
return this;
}
public PokerHand discard(int num1,int num2)
{
discard(num1);
discard(num2);
return this;
}
public PokerHand discard(int num1,int num2,int num3)
{
discard(num1);
discard(num2);
discard(num3);
return this;
}
public String toString()
{
String str = "";
for ( int i = 0 ; i<5 ; i++ )
{
str += "Card " + i + " is : " + CardsOfHand[i].toString() + "\n";
}
return str;
}
public void Sort()
{
int max = 0;
int pocision = 0;
Card temp = new Card();
for ( int i = 0 ; i < 5 ; i++ )
{
for ( int j = i ; j < 5 ; j++)
{
if (CardsOfHand[j].getNumber() >= max)
{
max = CardsOfHand[j].getNumber();
pocision = j;
}
}
temp = CardsOfHand[i];
CardsOfHand[i] = CardsOfHand[pocision];
CardsOfHand[pocision] = temp;
max = 0;
}
}
//------------------------------------------------------------------------------------------------------------------
// methods to decide which kind of hand is
//------------------------------------------------------------------------------------------------------------------
public boolean isRoyalFlush()
{
Sort();
if ( CardsOfHand[0].getName().equals("Ace") && CardsOfHand[1].getName().equals("King")
&& CardsOfHand[3].getName().equals("Queen") && CardsOfHand[4].getName().equals("Jack")
&& CardsOfHand[3].getName().equals("Ten"))
{
if ( CardsOfHand[0].getSuit().equals("Spades") &&
CardsOfHand[1].getSuit().equals("Spades") &&
CardsOfHand[2].getSuit().equals("Spades") &&
CardsOfHand[3].getSuit().equals(CardsOfHand[4].getSuit()) )
{
System.out.println("Your hand is a RoyalFlush :\n");
return true;
}
}
return false;
}
public boolean isStraightFlush()
{
if (isFlush()&&isStraight())
{
System.out.println("Your hand is a StraightFlush :\n");
return true;
}
return false;
}
public boolean isFlush()
{
Sort();
if (CardsOfHand[0].getSuit().equals(CardsOfHand[1].getSuit()) &&
CardsOfHand[1].getSuit().equals(CardsOfHand[2].getSuit()) &&
CardsOfHand[2].getSuit().equals(CardsOfHand[3].getSuit()) &&
CardsOfHand[3].getSuit().equals(CardsOfHand[4].getSuit()) )
{
System.out.println("Your hand is a Flush :\n");
return true;
}
return false;
}
public boolean isStraight()
{
Sort();
if ( ( (CardsOfHand[0].getNumber() - CardsOfHand[1].getNumber() == 1) || (CardsOfHand[0].getNumber() == 14) )
&& (CardsOfHand[1].getNumber() - CardsOfHand[2].getNumber() == 1)
&& (CardsOfHand[2].getNumber() - CardsOfHand[3].getNumber() == 1)
&& (CardsOfHand[3].getNumber() - CardsOfHand[4].getNumber() == 1) )
{
System.out.println("Your hand is a Straight :\n");
return true;
}
return false;
}
public boolean isFourOfAKind()
{
Sort();
if( CardsOfHand[0].getNumber() == CardsOfHand[3].getNumber() || CardsOfHand[1].getNumber() == CardsOfHand[4].getNumber())
{
System.out.println("Your hand is a Four Of A Kind :\n");
return true;
}
return false;
}
public boolean isFullHouse()
{
Sort();
if( (CardsOfHand[0].getNumber() == CardsOfHand[2].getNumber() && CardsOfHand[3].getNumber() == CardsOfHand[4].getNumber())
|| (CardsOfHand[2].getNumber() == CardsOfHand[4].getNumber() && CardsOfHand[0].getNumber() == CardsOfHand[1].getNumber() ))
{
System.out.println("Your hand is a FullHouse :\n");
return true;
}
return false;
}
public boolean isTwoPairs()
{
Sort();
if ((CardsOfHand[0].getNumber() == CardsOfHand[1].getNumber() && CardsOfHand[2].getNumber() == CardsOfHand[3].getNumber())
|| (CardsOfHand[1].getNumber() == CardsOfHand[2].getNumber() && CardsOfHand[3].getNumber() == CardsOfHand[4].getNumber())
|| (CardsOfHand[0].getNumber() == CardsOfHand[1].getNumber() && CardsOfHand[3].getNumber() == CardsOfHand[4].getNumber()) )
{
System.out.println("Your hand is a Two Pairs :\n");
return true;
}
return false;
}
public boolean isThreeOfAKind()
{
Sort();
if ( (CardsOfHand[0].getNumber() == CardsOfHand[2].getNumber())
||(CardsOfHand[1].getNumber() == CardsOfHand[3].getNumber())
||(CardsOfHand[2].getNumber() == CardsOfHand[4].getNumber()) )
{
System.out.println("Your hand is Three Of A Kind\n");
return true;
}
return false;
}
public boolean isAPair()
{
Sort();
if ( CardsOfHand[0].getNumber() == CardsOfHand[1].getNumber()
|| CardsOfHand[1].getNumber() == CardsOfHand[2].getNumber()
|| CardsOfHand[2].getNumber() == CardsOfHand[3].getNumber()
|| CardsOfHand[3].getNumber() == CardsOfHand[4].getNumber() )
{
System.out.println("Your hand is a Pair :\n ");
return true;
}
return false;
}
public boolean isCardHigh()
{
Sort();
if (!isRoyalFlush() && !isFlush() && !isStraightFlush() && !isStraight()
&& !isFourOfAKind() && !isFullHouse() && !isTwoPairs() && !isThreeOfAKind() && !isAPair())
{
System.out.println("Your hand is "+CardsOfHand[0].getName()+"'s high :\n");
return true;
}
System.out.println("error");
return false;
}
public void sorted()
{
if (isRoyalFlush());
else if (isStraightFlush());
else if (isStraight());
else if (isFlush());
else if (isFourOfAKind());
else if (isFullHouse());
else if (isThreeOfAKind());
else if (isTwoPairs());
else if (isAPair());
else if (isCardHigh());
}
public PokerHand categorize()
{
PokerHand newHand = new PokerHand();
if (isRoyalFlush())
{
newHand = new RoyalFlush(CardsOfHand,thisDeck);
}
else if (isFlush())
{
newHand = new Flush(CardsOfHand,thisDeck);
}
else if (isStraight())
{
newHand = new Straight(CardsOfHand,thisDeck);
}
else if (isStraightFlush())
{
newHand = new StraightFlush(CardsOfHand,thisDeck);
}
else if (isFourOfAKind())
{
newHand = new FourOfAKind(CardsOfHand,thisDeck);
}
else if (isFullHouse())
{
newHand = new FullHouse(CardsOfHand,thisDeck);
}
else if (isTwoPairs())
{
newHand = new TwoPairs(CardsOfHand,thisDeck);
}
else if (isThreeOfAKind())
{
newHand = new ThreeOfAKind(CardsOfHand,thisDeck);
}
else if (isAPair())
{
newHand = new APair(CardsOfHand,thisDeck);
}
else if (isCardHigh())
{
newHand = new CardHigh(CardsOfHand,thisDeck);
}
return newHand;
}
public static void main(String[] args)
{
DeckOfCards deck = new DeckOfCards();
deck.shuffle();
PokerHand testHand = new PokerHand(deck);
testHand.sorted();
System.out.println(testHand.toString());
PokerHand test2 = testHand.categorize();
test2.discard();
//test2.sorted();
System.out.println("the hand after discard is :\n\n" +test2.toString()+"\n");
}
}
//------------------------------------------------------------------------------------------------------------------
// classes of all kinds of hands
//------------------------------------------------------------------------------------------------------------------
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -