提供如下兩個 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

共 31 則回應

中原資工?
期中考腦殘廢中ˊˋ
我幫你問問我有沒有無聊的神人學長....
曾經用c寫過XD
不過還是交給後面大神了

另外功課還是要自己寫阿~~
我走錯版了
我只看到一堆亂碼...
原PO加油(拍拍
嗚嗚~
試過很多方法了QQ

真的不行才PO上來的(跪
不是故意打擾各位的阿~~~
中原資工?
我也只看到一堆亂碼....
不.....並不是.....(悲劇
C++作業寫過撲克牌遊戲
淚推一個...
神之難阿
看題意是要寫一個撲克牌程式碼的作業
他給你葫蘆、順子之類的範例要你照樣照句寫三條和Full house
那一手牌較好就是判斷兩手牌有沒有同花、鐵支、葫蘆...然後照順序比大小

不過這種補齊程式碼的功課又不是數學題目 根本沒什麼好解的阿-.-
也至少貼個自己怎麼想、程式怎麼寫卻遇到Bug的才叫問問題吧
只能說....原PO加油(拍
程式很難別人幫你吧XD
B5似乎不打自招是原PO了XDD
Jave的話就沒辦法幫忙了...(沒學過QQ)
如果是C語言之前期末報告有寫過類似的東西可以讓你參考
有需要的話我在把程式碼貼上來
好窩~~~

謝謝你~!!!(握

麻煩了~
比較偏向梭哈的遊戲程式
隨機發五張牌然後比兩者的大小
不過還是有些缺陷啦,當時沒時間修改(牌型一樣要再判定大小但是沒寫這部份)
雖然說是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;
}
等等,B17沒有封裝
兩個邏輯是完全不一樣的
建議不要去研究
否則會越來越亂
每個class的功能都已經在註解有很好的解釋了
善用註解的提示吧!

PS.最近奇摩知識家的問題該不會也是你提問的@@?
恩阿,C語言跟JAVA畢竟還是有不同
所以只能讓原PO參考一下而已~_~
B18 不是我喔~

B19 恩恩~我知道
其實可以把遇到的問題或bug拿去問老師和助教。

像這樣直接貼問題的方式,助教應該很難回答,也難怪要上網匿名發問。

加油吧..
我以前貌似寫過阿@@

但那已經是大二的事了~(遠目
我最討厭寫程式惹
學妹...
這是JAVA..?
恕學姐幫不上忙
這是四年前的事了...
我問問有沒有人會 ((期望不要太大
空缺得就照附近的class複製貼上&修改.....
至於B的話....
第2個class好像需要改掉部份他給的東西才能用.....
(對java不熟...不太確定...

// determine if hand contains a three of a kind
public int threeOfAKind( Card hand[] )
{
int triples = 0;
int numbers[] = totalHand( hand );

// count triples
for ( int k = 0; k < numbers.length; k++ )
{
if ( numbers[ k ] == 3 )
{
System.out.printf( "triples of %ss\n", faces[ k ] );
++triples;
} // end if
} // end for

return triples;
} // end method threeOfAKind


// determine if hand contains a full house
public void fullHouse( int couples, int triples )
{

if((couples > 0)&&(triples == 1) || (triples == 2)){ //2個3張的可以拆吧= =?
return true;
}else{
return false;
}

} // end method fullHouse
呃未看先猜 是筆試?
孩子阿
作業要自己寫喔
不然永遠不會寫程式XDD
樂推B11最後一句
=口=?(目前表情)
我剛剛問了一下
據說課本有範例
改一下就好~