【求助】有人能解出來這題題目嗎(跪求

提供如下兩個 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
留言
比較偏向梭哈的遊戲程式 隨機發五張牌然後比兩者的大小 不過還是有些缺陷啦,當時沒時間修改(牌型一樣要再判定大小但是沒寫這部份) 雖然說是C程式,希望會對你有幫助0..0 #include <stdio.h> #include <stdlib.h> #include <time.h> #define cls system("cls"); #define INT_MIN 0 int main(int argc, char *argv[]) { srand(time(NULL)); int money1 = 1,money2 = 1; int max=0,max2=0,max3=0,max4=0; max = INT_MIN, max2 = INT_MIN, max3 = INT_MIN, max4 = INT_MIN ; int player[10][5]={{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0},{0,0}}; int x=0,y=0,plate1=0,plate2=0,plate3=0,plate4=0,plate5=0;//玩家陣列[第幾張牌][點數,花色],亂數點數,亂數花色,計算牌重複1~5 int plate6=0,plate7=0,plate8=0,plate9=0,plate10=0; int i,j; for( i=0;i<10;i++) { x=rand()%13+1;//點數 y=rand()%4+1; //花色 player[i][0]=x; //第i張牌 點數 player[i][1]=y; //第i張牌 花色 for(j=0;j<i;j++) { if(player[j][0]==x&player[j][1]==y) //從第一張牌檢查到前一張牌,如果與現在的牌相同,i-1重新取亂數 i--; } } int playermax=player[0][0]; int playermin=player[0][0]; int playermax1=player[5][0]; int playermin1=player[5][0]; for(i=0;i<5;i++)//取五張牌最大與最小 { if(player[i][0]>=playermax)playermax=player[i][0]; if(player[i][0]<=playermin)playermin=player[i][0]; } printf("玩家1\n"); for(i=0;i<5;i++)//將所有牌列出 { printf("第 %d 張牌 ",i+1); switch(player[i][1])//花色 { case 1:printf("梅花"); break; case 2:printf("方塊"); break; case 3:printf("愛心"); break; default:printf("黑桃"); break; } printf("%d\n\n",player[i][0]);//點數 if(player[i][0]==player[0][0])plate1++; //計算單支,一對,兩對,三條,葫蘆,鐵支 else if(player[i][0]==player[1][0])plate2++; else if(player[i][0]==player[2][0])plate3++; else if(player[i][0]==player[3][0])plate4++; else if(player[i][0]==player[4][0])plate5++; else ; } int bs[5]={plate1,plate2,plate3,plate4,plate5}; for(i=0;i<5;i++) //取最大數&次大數 { if (bs[i]>= max) { max2 = max; max = bs[i]; } else if (bs[i] > max2) max2 = bs[i]; } if(max==2)//雙對&一對 { if(max2==2) { printf("牌面:雙對\n"); money1=money1*3; } else { printf("牌面:一對\n"); money1=money1*2; } } else if(max==3)//葫蘆&&三條 { if(max2==2) { printf("牌面:葫蘆\n"); money1=money1*20; } else { printf("牌面:三條\n"); money1=money1*4; } } else if(max==4)//鐵支 { printf("牌面:鐵支\n"); money1=money1*50; } else//同花順]&順子&無賴 { if((playermax-playermin)==4) { if(player[0][1]==player[1][1]&&player[2][1]==player[0][1]&&player[3][1]==player[0][1]&&player[4][1]==player[0][1]) { printf("牌面:同花順\n"); money1=money1*100; } else { printf("牌面:順子\n"); money1=money1*5; } } else printf("牌面:無賴\n"); money1=money1; } printf("\n"); printf("玩家2\n"); for(i=5;i<10;i++)//取五張牌最大與最小 { if(player[i][0]>=playermax1)playermax1=player[i][0]; if(player[i][0]<=playermin1)playermin1=player[i][0]; } for(i=5;i<10;i++)//將所有牌列出 { printf("第 %d 張牌 ",i-4); switch(player[i][1])//花色 { case 1:printf("梅花"); break; case 2:printf("方塊"); break; case 3:printf("愛心"); break; default:printf("黑桃"); break; } printf("%d\n\n",player[i][0]);//點數 if(player[i][0]==player[5][0])plate6++; //計算單支,一對,兩對,三條,葫蘆,鐵支 else if(player[i][0]==player[6][0])plate7++; else if(player[i][0]==player[7][0])plate8++; else if(player[i][0]==player[8][0])plate9++; else if(player[i][0]==player[9][0])plate10++; else ; } int cs[5]={plate6,plate7,plate8,plate9,plate10}; for(i=0;i<5;i++) //取最大數&次大數 { if (cs[i]>= max3) { max4= max3; max3 = cs[i]; } else if (cs[i] > max2) max4 = cs[i]; } if(max3==2)//雙對&一對 { if(max4==2) { printf("牌面:雙對\n"); money2=money2*3; } else { printf("牌面:一對\n"); money2=money2*2; } } else if(max3==3)//葫蘆&&三條 { if(max4==2) { printf("牌面:葫蘆\n"); money2=money2*20; } else { printf("牌面:三條\n"); money2=money2*4; } } else if(max3==4)//鐵支 { printf("牌面:鐵支\n"); money2=money2*50; } else//同花順]&順子&無賴 { if((playermax1-playermin1)==4) { if(player[5][1]==player[6][1]&&player[7][1]==player[5][1]&&player[8][1]==player[5][1]&&player[9][1]==player[5][1]) { printf("牌面:同花順\n"); money2=money2*100; } else { printf("牌面:順子\n"); money2=money2*5; } } else printf("牌面:無賴\n"); money2=money2; } if(money1>money2) { printf("\n玩家1勝利\n\n"); } else if(money1<money2) { printf("\n玩家2勝利\n\n"); } else if(money1=money2) { printf("\n平手\n\n"); } system("PAUSE"); return 0; }