【求助】有人能解出來這題題目嗎(跪求
匿名
提供如下兩個 class,一是Card class代表一張撲克牌的花色和點數;另一是 DeckOfCards class代表一付牌,並提供了判斷下列七手牌型的相對應方法(methods)
1. A pair
2. Two pairs
3. Three of kind (e.g., three jacks) < 待完成>
4. Four of a kind (e.g., four aces)
5. A flush (i.e., all five cards of the same suit)
6. A straight (i.e., five cards of consecutive face values)
7. A full house (i.e., two cards of one face value and three cards of another face value) < 待完成>
A. 請完成 DeckOfCards 中缺少的程式(5 pts)。
B. 使用這兩個 class,再寫另一個 Java application 來發兩手牌,並比較這兩手牌之間,那一手牌較好(5 pts)。
public class Card
{
private String face; // face of card
private String suit; // suit of card
// two-argument constructor initializes card's face and suit
public Card( String cardFace, String cardSuit )
{
face = cardFace; // initialize face of card
suit = cardSuit; // initialize suit of card
} // end two-argument Card constructor
// return card face
public String getFace()
{
return face;
} // end method getFace
// return card suit
public String getSuit()
{
return suit;
} // end method getSuit
// return String representation of Card
public String toString()
{
return face + " of " + suit;
} // end method toString
} // end class Card
import java.util.Random;
public class DeckOfCards
{
String faces[] = { "Ace", "Deuce", "Three", "Four", "Five", "Six",
"Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" };
String suits[] = { "Hearts", "Diamonds", "Clubs", "Spades" };
private Card deck[]; // array of Card objects
private int currentCard; // the index of next Card to be dealt
private final int NUMBER_OF_CARDS = 52; // constant number of cards
private Random randomNumbers; // random number generator
// constructor fills deck of cards
public DeckOfCards()
{
deck = new Card[ NUMBER_OF_CARDS ]; // create array of Card objects
currentCard = 0; // initialize currentCard
randomNumbers = new Random(); // create random number generator
// populate deck with Card objects
for ( int count = 0; count < deck.length; count++ )
deck[ count ] =
new Card( faces[ count % 13 ], suits[ count / 13 ] );
} // end DeckOfCards constructor
// shuffle deck of cards with one-pass algorithm
public void shuffle()
{
currentCard = 0; // reinitialize currentCard
// for each card, pick another random card and swap them
for ( int first = 0; first < deck.length; first++ )
{
int second = randomNumbers.nextInt( NUMBER_OF_CARDS );
Card temp = deck[ first ];
deck[ first ] = deck[ second ];
deck[ second ] = temp;
} // end for
} // end method shuffle
// deal one card
public Card dealCard()
{
// determine whether cards remain to be dealt
if ( currentCard < deck.length )
return deck[ currentCard++ ]; // return current Card in array
else
return null; // return null to indicate that all cards were dealt
} // end method dealCard
// tally the number of each face card in hand
private int[] totalHand( Card hand[] )
{
int numbers[] = new int[ faces.length ]; // store number of face
// initialize all elements of numbers[] to zero
for ( int i = 0; i < 13; i++ )
numbers[ i ] = 0;
// compare each card in the hand to each element in the faces array
for ( int h = 0; h < hand.length; h++ )
{
for ( int f = 0; f < 13; f++ )
{
if ( hand[ h ].getFace() == faces[ f ] )
++numbers[ f ];
} // end for
} // end for
return numbers;
} // end method totalHand
// determine if hand contains pairs
public int pairs( Card hand[] )
{
int couples = 0;
int numbers[] = totalHand( hand );
// count pairs
for ( int k = 0; k < numbers.length; k++ )
{
if ( numbers[ k ] == 2 )
{
System.out.printf( "Pair of %ss\n", faces[ k ] );
++couples;
} // end if
} // end for
return couples;
} // end method pairs
// determine if hand contains a three of a kind
public int threeOfAKind( Card hand[] )
{
int triples = 0;
<待完成>
return triples;
} // end method threeOfAKind
// determine if hand contains a four of a kind
public void fourOfAKind( Card hand[] )
{
int numbers[] = totalHand( hand );
for ( int k = 0; k < faces.length; k++ )
{
if ( numbers[ k ] == 4 )
System.out.printf ( "Four %ss\n", faces[ k ] );
} // end for
} // end fourOfAKind
// determine if hand contains a flush
public void flush( Card hand[] )
{
String theSuit = hand[ 0 ].getSuit();
for ( int s = 1; s < hand.length; s++ )
{
if ( hand[ s ].getSuit() != theSuit )
return; // not a flush
} // end for
System.out.printf( "Flush in %s\n", theSuit );
} // end method flush
// determine if hand contains a straight
public void straight( Card hand[] )
{
int locations[] = new int[ 5 ];
int z = 0;
int numbers[] = totalHand( hand );
for ( int y = 0; y < numbers.length; y++ )
{
if ( numbers[ y ] == 1 )
locations[ z++ ] = y;
} // end for
int faceValue = locations[ 0 ];
if ( faceValue == 0 ) // special case, faceValue is Ace
{
faceValue = 13;
for ( int m = locations.length - 1; m >= 1; m-- )
{
if ( faceValue != locations[ m ] + 1 )
return; // not a straight
else
faceValue = locations[ m ];
} // end if
} // end if
else
{
for ( int m = 1; m < locations.length; m++ )
{
if ( faceValue != locations[ m ] - 1 )
return; // not a straight
else
faceValue = locations[ m ];
} // end if
} // end else
System.out.println( "Straight" );
} // end method straight
// determine if hand contains a full house
public void fullHouse( int couples, int triples )
{
< 待完成>
} // end method fullHouse
// determine if hand contains two pairs
public void twoPairs( int couples )
{
if ( couples == 2 )
System.out.println( "\nTwo Pair!" );
} // end method twoPair
} // end class DeckOfCards