首页 热点专区 义务教育 高等教育 出国留学 考研考公

200 分悬赏 改进一下c语言代码 \\关于发牌的

发布网友 发布时间:2022-04-25 13:15

我来回答

5个回答

热心网友 时间:1天前

晕,改了好几次,楼上几位的代码确实不错,我也学到了点,没想到一下有那么多人回,但既然写了就帖出来算了,否则我的辛苦就白费啦:

// compiler: gcc 4.2.0
// os: xpsp2

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>

typedef struct tagCard
{
    const char* face;
    char suit;
    int value; // 增加一个成员用以记录点数
}Card;

// 初始化牌组
void InitDeck(Card* beg, Card* end)
{
    static const char *face[] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
    static const int value[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13}; // 点数, 对应牌面, 需要的话可以自己修改下
    int i, j;
    
    for(i = 0; beg != end; ++i)
    {
        for(j = 3; j <= 6; ++j, ++beg)
        {
            beg->face = face[i];
            beg->suit = j;
            beg->value = value[i];
        }
    }
}

// 打乱
void Shuffle(Card* beg, Card* end)
{
    int i;
    Card tmp, *p;
    for(p = beg; p != end; ++p)
    {
        // 随机交换
        i = rand() % 52;
        
        memcpy(&tmp, p, sizeof(Card));
        memcpy(p, &beg[i], sizeof(Card));
        memcpy(&beg[i], &tmp, sizeof(Card));
    }
}

// 在四个人手中随机各抽一张牌放到pick数组中
void RandomPick(Card* beg, Card* end, Card* pickBeg, Card* pickEnd)
{
    int i;
    while(pickBeg != pickEnd)
    {
        i = rand() % 13 + 13 * (4 - (pickEnd - pickBeg));
        memcpy(pickBeg++, &beg[i], sizeof(Card));
    }
}

// 展示每个人的牌面
void ShowCard(Card* beg, Card* end)
{
    int i, j;
    printf("=============================\n");
    printf("1st\t2nd\t3rd\t4th\n");
    printf("=============================\n");
    
    for(i = 0; i < 13; ++i)
    {
        for(j = 0; j < 4; ++j)
            printf("%c %s\t", beg[i + 13 * j].suit, beg[i + 13 * j].face);
        putchar('\n');
    }
}

int main()
{
    Card card[52], pick[4];
    int sum = 0, i;
    
    srand((unsigned)time(0));
    InitDeck(card, card + 52);
    Shuffle(card, card + 52);
    ShowCard(card, card + 52);
    RandomPick(card, card + 52, pick, pick + 4);
    
    printf("\nFace:\tSuit:\n");
    for(i = 0; i < 4; ++i)
    {
        printf("%s\t%c\n", pick[i].face, pick[i].suit);
        sum += pick[i].value;
    }
    printf("\nSum: %d", sum);
    
    return 0;
}

热心网友 时间:1天前

/*在vc6下面编译通过,运行正常*/
#include <stdio.h>
#include <stdlib.h>
#include <memory.h>
#include <string.h>
#include <time.h>

struct card
{
  char face[4];
  int suit ;
  int score;
};/*定义完之后要加一个分号*/
typedef struct card Card ;

void a (Card * , char *[] , int [], int []);
void b (Card *);
void c (Card *);
main()
{
  Card deck [52] ;
  char *face[] ={"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
  int suit[] ={259,260,261,262};
  int score[] = {11,2,3,4,5,6,7,8,9,10,10,10,10}; //A算11分,JQK算10分;可以自己改
  srand (time(NULL));
  a(deck,face,suit,score);
  b(deck);
  c(deck);
  return 0;
}

void a (Card *wDeck, char *wFace[], int wSuit[], int score[])
{
  int i;
  for (i=0 ; i<52 ; i++ )
  {
    strcpy(wDeck[i].face, wFace[i%13]);
    wDeck[i].suit = wSuit[i/13];
    wDeck[i].score = score[i%13] ;
  }
}

void b (Card * wDeck)
{
  int i , j ;
  Card temp ;
  for (i=0 ; i <52 ; i++)
  {
    j = rand()%52;
    memcpy(&temp, &wDeck[i],sizeof(Card));
    memcpy(&wDeck[i], &wDeck[j],sizeof(Card));
    memcpy(&wDeck[j],&temp,sizeof(Card));
  }
}

void c (Card *wDeck)
{
  int i,a,b,c,d,sum;
  for (i=0 ; i<52; i++)
  {
    printf("%c%s%c",wDeck[i].suit ,wDeck[i].face, (i+1)%4?'\t':'\n');
  }

  a = 0+(rand()%13)*4;
  b = 1+(rand()%13)*4;
  c = 2+(rand()%13)*4;
  d = 3+(rand()%13)*4;
  sum = wDeck[a].score +wDeck[b].score +wDeck[c].score +wDeck[d].score;
  printf ("1st:%c%s\n", wDeck[a].suit ,wDeck[a].face);
  printf ("2nd:%c%s\n", wDeck[b].suit ,wDeck[b].face);
  printf ("3rd:%c%s\n", wDeck[c].suit ,wDeck[c].face);
  printf ("4th:%c%s\n", wDeck[d].suit ,wDeck[d].face);
  printf ("sum:%d\n",sum);
}

热心网友 时间:1天前

//已经修改

#include <stdio.h>
#include <stdlib.h>
#include<time.h>

struct card {
char face;
char suit;
};/*定义完之后要加一个分号*/
typedef struct card Card ;

void a (Card *wDeck);
void b (Card *);
void c (Card *,char face[13][4],char suit[4][4]);
main()
{ Card deck [52] ;
char face[13][4] ={"A" ,"2" ,"3","4","5","6","7","8","9","10","J","Q","K"};
char suit[4][4] ={"1","2","3","4"};
srand (time(NULL));
a(deck);
b(deck);
c(deck,face,suit);

return 0;
}

void a (Card *wDeck)
{ int i;
for (i=0 ; i<52 ; i++ ) {
wDeck[i].face = i%13;
wDeck[i].suit = i/13;
}
}

void b (Card * wDeck)
{
int i , j ;
Card temp ;
for (i=0 ; i <52 ; i++) {
j = rand()%52;
temp = wDeck[i];
wDeck[i] = wDeck[j];
wDeck[j] = temp;
}
}

void c (Card *wDeck,char face[13][4],char suit[4][4])
{
int i=0 ;
printf (" 1\t 2\t 3\t 4\n");
for (i=0 ; i<52; i++)
printf ("%s,%s%c",suit[wDeck[i].suit] ,face[wDeck[i].face] ,(i+1)%4?'\t' :'\n');

printf("\r\n");
//抽出四张牌
int nSum=0;
int nIndex[4];
for(i=0;i<4;i++)
{
nIndex[i]=(rand()%13)*4+i;
printf ("%s,%s%c",suit[wDeck[nIndex[i]].suit] ,face[wDeck[nIndex[i]].face] ,(i+1)%4?'\t' :'\n');
// if(wDeck[nIndex[i]].face<10)
nSum+=(wDeck[nIndex[i]].face+1);//这里A-10按1-10分计算
// else
// nSum+=1;//其它的牌按1分计算
}
printf("nSum=%d\r\n",nSum);
}

热心网友 时间:1天前

void c (Card *wDeck)
{
int i ;
for (i=0 ; i<52; i++)
printf ("%-1c%-12s%c",wDeck[i] .suit ,wDeck[i] .face ,(i+1)%4?'\t' :'\n');
} 这一段稍改一下就行了

热心网友 时间:1天前

我也不会撒

声明声明:本网页内容为用户发布,旨在传播知识,不代表本网认同其观点,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。E-MAIL:11247931@qq.com