【发布时间】:2011-03-26 20:29:52
【问题描述】:
我知道它是如何工作的,但我仍然对排序和配对有问题,以便我可以确定获胜者。
将它们配对(对是具有相同价值的牌。)例如,红桃 A 和黑桃 A 组成一对。 然后我数那些对。拥有最高对子的手获胜。
这是我为配对而尝试的,但.. 我仍然坚持我如何开始比较以进行配对。
这是我期望每手牌的结果的方式:
第一手: 黑桃六,是黑色的 钻石七,是红色的 黑桃八,是黑色 红心十,是红色的 黑桃皇后是黑色的 对数:0
第 2 手: 黑桃三是黑色 钻石五,是红色的 梅花五,是黑色的 钻石九,是红色的 钻石皇后,是红色的 对数:1 最高的一对是:五
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct card {
const char *face;
const char *suit;
const char *color;
};
typedef struct card Card;
typedef unsigned char pairs;
void fillDeck( Card * const, const char *[], const char *[] ,const char *[]);
void shuffle( Card * const );
void print( const Card * const );
pairs findpairs(card *hand); /* finds any pairs in a hand */
int main()
{
int hand,cd,winner;
card hands[5][5],handssorted[5][5];
pairs numpairs[5],highest;
Card deck[52];
const char *face[] = { "Ace", "Two", "Three", "Four", "Five","Six", "Seven",
"Eight", "Nine", "Ten","Jack", "Queen", "King"};
const char *suit[] = { "Hearts", "Diamonds", "Clubs", "Spades"};
const char *color[]= {"Black","Red"};
srand( time( NULL ) );
fillDeck( deck, face, suit, color );
print( deck );
printf("\n ---------------------------------------------------------- \n");
shuffle( deck );
print( deck );
for(cd=0;cd<5;cd++)
{
}
for(hand=0;hand<5;hand++)
{
/* sort the hands here */
numpairs[hand]=findpairs(handssorted[hand]);
printf("Hand %i:\n",hand+1);
/* print the hands here */
/* print the number and value of any pairs here */
}
/* determine the winner and print it */
system("pause");
return 0;
}
//----------------------------------------------------------------------------- void fillDeck( Card * const wDeck, const char * wFace[], const char * wSuit[],
const char * wColor[])
{
int i;
for ( i = 0; i <= 51; i++ ) {
wDeck[i].face = wFace[ i % 13 ];
wDeck[i].suit = wSuit[ i / 13 ];
wDeck[i].color = wColor[i%2];
// if ()
// wDeck[i].suit = wSuit[ i / 13 ];
}
}
//------------------------------------------------------------------
void shuffle( Card * const wDeck )
{
int i, j;
Card temp;
for ( i = 0; i <= 51; i++ ) {
j = rand() % 52;
temp = wDeck[ i ];
wDeck[ i ] = wDeck[ j ];
wDeck[ j ] = temp;
}
}
//---------------------------------
void print( const Card * const wDeck )
{
int i;
for ( i = 0; i <= 51; i++ ){
printf( "\t%s\t of \t%-8s is \t%s \n \t", wDeck[i].face,
wDeck[i].suit,wDeck[i].color,
( i + 1 ) % 2 ? '\t' : '\n' );}
}
//--------------------------------------------------------------------------
pairs findpairs(card *hand)
{
pairs numpairs=0;
for ( int i = 0; i <= 5; i++ ){
if (hand[i].face == )
}
return numpairs;
}
【问题讨论】:
-
西装和颜色不是独立的。
-
请了解如何正确格式化您的帖子。 stackoverflow.com/editing-help
-
您告诉了我们预期的结果,但没有告诉我们实际结果。
-
这是您的完整代码吗?
handssorted是什么?而“方块黑七”、“黑桃红八”等就没有意义了。 -
我建议你再抽象一个层次,即 CardDeck 然后在 CardDeck 上有方法来初始化和洗牌等。最好使用 std::string 而不是指向带有卡名等的字符串的指针.
标签: c