2010-08-08 90 views
1

我有些如何得到它的工作,但我仍然有排序和制作对的问题,以便我可以确定赢家。如何填充,洗牌,处理纸牌游戏

将它们配对(对具有相同价值的卡片)。例如心形王牌&黑桃王牌成对。 然后我计算这些对。最高双手胜。

这就是我正在尝试配对,但..我仍然rellyy坚持我如何开始比较,使配对。

这是我所期望的结果是,每手的方式:

手1: 黑桃六,是黑钻的 七,红 黑桃八,是黑 十红心,红 女王黑桃,是黑 数对: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; 

     } 
+1

西装和颜色不是独立的。 – 2010-08-08 01:32:46

+0

请了解如何正确设置您的文章的格式。 http://stackoverflow.com/editing-help – spender 2010-08-08 01:33:31

+0

你告诉我们预期的结果,但不是什么实际结果。 – zneak 2010-08-08 01:41:47

回答

1
  • 你必须print(deck)来电有srand()它使用未初始化deck阵列之后。
  • numpairsfindpairshandssorted是不确定的
  • 要打印每手整个deck。那真的是你想要的吗?
  • onst是无效的(假设你的意思const
  • fillDeck()每个Card
  • 您的洗牌算法的.color成员未完全填满的嫌疑。见Fisher-Yates shuffle
  • print()
  • ,您使用的是%c格式说明与const char *
1

这不是一个回答您的问题(我没有实际知道你是问什么的),但我会指出你的洗牌算法不是随机的。

首先,使用%rand限制为一个范围的值通常是一个坏主意。 (见Q13.16 from the comp.lang.c FAQ。)

其次,它看起来像你可能试图实现Fisher-Yates shuffling algorithm,但你的实现是错误的。很容易看出,您以前交换过的元素进行交换的方法存在问题;考虑洗牌三个元素{0,1,2}的甲板:

First iteration  Second iteration 
------------------------------------ 
{ 0, 1, 2 }   { 1, 0, 2 } 
        { 0, 1, 2 } 
        { 0, 2, 1 } 
------------------------------------ 
{ 1, 0, 2 }   { 0, 1, 2 } 
        { 1, 0, 2 } 
        { 1, 2, 0 } 
------------------------------------ 
{ 2, 1, 0 }   { 1, 2, 0 } 
        { 2, 1, 0 } 
        { 2, 0, 1 } 

的第一列的第一次迭代之后表示甲板的可能状态(其中,你交换deck[0]deck[rand() % 3])。

第二列代表第二次迭代后(您将deck[1]deck[rand() % 3]对换)的可能状态。

有9个同样可能的状态 - 但这显然是错误的,因为应该只有3个! = 6个排列。事实上,有几个国家是重复的,这意味着它们发生的可能性更高。 (请注意,即使我进行了第三次迭代,最终只有6个排列组成了27个状态,并且由于27不能被6整除,所以最终会出现比其他状态更多的状态。)

0

这是我所期望的结果是,每手

然后静态分配到手上所需卡的方式。在你的模型中,每张卡片都有从0到51的唯一数字。如果卡片和手都是数字组,那么操作就像从一组中删除数字并将其添加到另一组一样简单。

你可能想实现的功能与这样int套,占甲板,手等工作

J =兰特()%52;

如果您认真制作游戏,您可能需要检查how to generate random numbers from a range too