这是以上纸牌游戏的规则:
Two players, playing poker game, five cards in each hand, comparing the five cards, decide who is going to win.
1. If nothing, highest card win. If tie, compare second card, highest win, If tie again, compare the third card, and so on, if five cards all tie, then it's a tie.
2. If there's a pair, pair wins, if both have pair, higher pair win, if pairs are the same, compare the third card, higher one win, if tie, compare the fourth card, higher one win. if tie again, compare the fifth card. if tie, then it's a tie.
3. If there's two pairs in one hand, two pairs wins. If both have two pairs, higher one pair wins. If the first pair is the same, then compare the second pair, higher one wins, if the same, then compare the fifth card, higher one wins, if still the same, then it's a tie.
4. If there's Three of a kind ( Three Queen, for example ), three of a kind wins. If both have three of a kind, higher one wins.
5. If there's straight ( eg. cards in consecutive number, like 2,3,4,5,6, doesn't mater if they are the same suit), staight wins. If both have staight, higher one wins, if the first high card ties, then compare the second one, and so on, just like in number 1.
6. If there's flush ( all same suit, eg. five cards are all hearts, doesn't have to be the consecutive number ), flush wins. If both have flush, higher card wins, and so on just like in number 1.
7. If there's full house ( three of a kind, one pair ), full house wins, if both have full house, higher one wins.
8. If there's four of a kind ( eg. 4,4,4,4, 7 ), four of a kind wins, if both have four of a kind, higher one wins.
9. If there's straight flush ( same suit, consecutive number, eg. 2,3,4,5,6, and they are all hearts ), straight flush wins. If both have straight flush, higher one wins. ( eg. 10, Jack, Queen, King, Ace , this flush is always the highest ). If same, tie.
From 1 to 9, it's from low to high, so number 2 always wins over number 1, number 3 always win over number 1 and 2, number 4 always win over 3, 2, 1, and so on.
the cout statement should be able to show the two players playing like this:
player 1 player 2
__________ _________
card 1 card 1
card 2 card 2
card 3 card 3
card 4 card 4
card 5 card 5
(should show what card they are, like, seven of heart )
下面是一个纸牌游戏的代码,你可以作为参考!
#include <iostream>
using std::cout;
#include <iomanip>
using std::ios;
using std::setw;
using std::setiosflags;
#include <cstdlib>
#include <ctime>
struct CARD {
char *FACE;
char *SUIT;
}_CARD;
struct PLAYER {
char *NAME;
CARD cards[5];
}_PLAYER;
void shuffle( int [][13] );
void deal( const int [][13], const char *[], const char *[] );
void StringToInt( char *sFACE, unsigned int iFACE );
int JudgeFormFace(unsigned int iFACE[]);
int JudgeSuit( CARD cards[] );
int JudgeKind(PLAYER *player);
int CompareCard( PLAYER *player0, PLAYER *player1 );
PLAYER players[2];
unsigned int bFACE[5]; //经过排序后的5张牌的面值(FACE)顺序 因为题目没有要求花色的顺序,所以只有面值即可判定大小.
int main()
{
const char *suit[4] = {"Hearts", "Dismonds", "Clubs", "Spades"};
const char *face[13] = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
int deck[4][13] = {0};
int ret;
srand( time(0) );
shuffle( deck );
deal( deck, face, suit );
ret = CompareCard(&players[0], &players[1]);
if ( ret = 1 )
cout << "\n" << players[0].NAME << "Is Wins!\n";
if ( ret = -1 )
cout << "\n" << players[1].NAME << "Is Wins!\n";
if ( ret = 0 )
cout << "\n" << players[0].NAME << "And" << players[1].NAME <<"Are Tie!\n";
return 0;
}
//洗牌
void shuffle( int wDeck[][13] )
{
int row, column;
int card;
for( card =1; card <= 10; card++ ){
do {
row = rand() % 4;
column = rand() % 13;
} while ( wDeck[row][column] != 0);
wDeck[row][column] = card;
}
}
//发牌并显示
void deal(const int wDeck[][13],const char *wFace[],const char *wSuit[])
{
*/
int CompareCard( PLAYER *player0, PLAYER *player1 )
{
unsigned int i, r;
i = JudgeKind(player0);
r = JudgeKind(player1);
if ( i > r ) return 1;
if ( i < r ) return -1;
//这里是 i=r 时, 即有相同类型时的判断 {未完成}
return 0;
}
/* 函数功能: 判断玩家手中的牌属于哪种类型
1. Nothing, Highest Card Win
2. Pair
3. Two Pair
4. Three Of Kind
5. Straight
6. Flush
7. Full House (Three Of Kind And Pair)
8. Four Of Kind
9. Straight Flush
返回 -1 表示错误!
*/
int JudgeKind(PLAYER *player)
{
CARD cards[5];
unsigned int iFACE[5], bFACE[5];