主题:[练习] 13张牌排序
13张牌排序
大概是这样的,这个程序是排序,也就是将52张扑克排随机抽取13张扑克排,按花色不同从大到小排列,其中H表示红心,D表示方块,C为梅花,S为黑桃,
首先要初始化一个数组里面包含52个元素,也就是52张牌了,
第二步就是洗牌,就是将52张牌的顺序打乱
第三步呢,是抓牌了,随机出现13张牌,注意思路是4个人,一人一张的给,只要求得到一组就好了
第四步是将上面的13张牌用另外的数组放着
第五步是排序,具体是S.H.C.D.其中牌面的大小是按A, K, Q, J, 10, 9, 8, 7, 6, 5, 4, 3, 2
第六步是把这13张牌显示在屏幕上.
下面的这个就是所要求的格式了
AS 6S AH 10H 7H 4H KD 8D 5D 4D 3D 2D AC
我的程序是:
/*13张牌排序*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct card{ /*表示一张牌*/
int face;
int suit;
};
typedef struct card Card;
void fillDeck(Card *);
void shuffle(Card *);
void deal(Card *,Card *, Card *, Card *, Card *);
void cardSort(Card *);
void cardPrint(Card *);
main ()
{
Card deck[52];
Card player1[13], player2[13],
player3[13], player4[13];
srand(time(NULL));
fillDeck(deck);
shuffle(deck);
deal (deck,player1,player2,player3,player4);
cardSort(player1);
cardSort(player2);
cardSort(player3);
cardSort(player4);
printf("\n\nPlayer1: ");
cardPrint(player1);
printf("\nPlayer2: ");
cardPrint(player2);
printf("\nPlayer3: ");
cardPrint(player3);
printf("\nPlayer4: ");
cardPrint(player4);
getch();
return 0;
}
/*按花色牌面值初始化数组*/
void fillDeck ( Card * wDeck )
{
int i;
for (i=0; i<52; i++)
{
wDeck[i].face = i%13;
wDeck[i].suit = i/13;
}
}
/*洗牌*/
void shuffle (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;
}
}
/*给四个人每人发13张牌*/
void deal (Card * wDeck, Card * wPlayer1, Card * wPlayer2,
Card * wPlayer3, Card * wPlayer4)
{
int i, card=0;
for(i=0; i<=12&&card<=51; i++)
{
wPlayer1 [i] = wDeck [card];
wPlayer2 [i] = wDeck [++card];
wPlayer3 [i] = wDeck [++card];
wPlayer4 [i] = wDeck [++card];
card++;
}
}
/*排序*/
void cardSort (Card * wPlay)
{
int wSuit, wFace, i, star=0;
Card temp;
for (wSuit=3; wSuit>=0; wSuit--)
for (wFace=12; wFace>=0; wFace--)
for (i=star; i<=12; i++)
if (wPlay[i].suit==wSuit && wPlay[i].face==wFace)
{
temp=wPlay[i];
wPlay[i]=wPlay[star];
wPlay[star]=temp;
star++;
}
}
/*打印函数*/
/*用12-A,11-K,10-Q,9-J,8-10,7-9,*/
/*6-8,5-7,4-6,3-5,2-4,1-3,0-2*/
/*suit用3-S,2-H,1-C,0-D*/
void cardPrint(Card * wDeck)
{
int i;
for (i=0; i<13; i++)
{
switch(wDeck[i].face)
{
case 12:
putchar('A');
break;
case 11:
putchar('K');
break;
case 10:
putchar('Q');
break;
case 9:
putchar('J');
break;
case 8:
printf("10");
break;
case 7:
putchar('9');
break;
case 6:
putchar('8');
break;
case 5:
putchar('7');
break;
case 4:
putchar('6');
break;
case 3:
putchar('5');
break;
case 2:
putchar('4');
break;
case 1:
putchar('3');
break;
case 0:
putchar('2');
break;
}
switch(wDeck[i].suit)
{
case 3:
putchar('S');
break;
case 2:
putchar('H');
break;
case 1:
putchar('C');
break;
case 0:
putchar('D');
break;
}
putchar(' ');
}
}
大概是这样的,这个程序是排序,也就是将52张扑克排随机抽取13张扑克排,按花色不同从大到小排列,其中H表示红心,D表示方块,C为梅花,S为黑桃,
首先要初始化一个数组里面包含52个元素,也就是52张牌了,
第二步就是洗牌,就是将52张牌的顺序打乱
第三步呢,是抓牌了,随机出现13张牌,注意思路是4个人,一人一张的给,只要求得到一组就好了
第四步是将上面的13张牌用另外的数组放着
第五步是排序,具体是S.H.C.D.其中牌面的大小是按A, K, Q, J, 10, 9, 8, 7, 6, 5, 4, 3, 2
第六步是把这13张牌显示在屏幕上.
下面的这个就是所要求的格式了
AS 6S AH 10H 7H 4H KD 8D 5D 4D 3D 2D AC
我的程序是:
/*13张牌排序*/
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct card{ /*表示一张牌*/
int face;
int suit;
};
typedef struct card Card;
void fillDeck(Card *);
void shuffle(Card *);
void deal(Card *,Card *, Card *, Card *, Card *);
void cardSort(Card *);
void cardPrint(Card *);
main ()
{
Card deck[52];
Card player1[13], player2[13],
player3[13], player4[13];
srand(time(NULL));
fillDeck(deck);
shuffle(deck);
deal (deck,player1,player2,player3,player4);
cardSort(player1);
cardSort(player2);
cardSort(player3);
cardSort(player4);
printf("\n\nPlayer1: ");
cardPrint(player1);
printf("\nPlayer2: ");
cardPrint(player2);
printf("\nPlayer3: ");
cardPrint(player3);
printf("\nPlayer4: ");
cardPrint(player4);
getch();
return 0;
}
/*按花色牌面值初始化数组*/
void fillDeck ( Card * wDeck )
{
int i;
for (i=0; i<52; i++)
{
wDeck[i].face = i%13;
wDeck[i].suit = i/13;
}
}
/*洗牌*/
void shuffle (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;
}
}
/*给四个人每人发13张牌*/
void deal (Card * wDeck, Card * wPlayer1, Card * wPlayer2,
Card * wPlayer3, Card * wPlayer4)
{
int i, card=0;
for(i=0; i<=12&&card<=51; i++)
{
wPlayer1 [i] = wDeck [card];
wPlayer2 [i] = wDeck [++card];
wPlayer3 [i] = wDeck [++card];
wPlayer4 [i] = wDeck [++card];
card++;
}
}
/*排序*/
void cardSort (Card * wPlay)
{
int wSuit, wFace, i, star=0;
Card temp;
for (wSuit=3; wSuit>=0; wSuit--)
for (wFace=12; wFace>=0; wFace--)
for (i=star; i<=12; i++)
if (wPlay[i].suit==wSuit && wPlay[i].face==wFace)
{
temp=wPlay[i];
wPlay[i]=wPlay[star];
wPlay[star]=temp;
star++;
}
}
/*打印函数*/
/*用12-A,11-K,10-Q,9-J,8-10,7-9,*/
/*6-8,5-7,4-6,3-5,2-4,1-3,0-2*/
/*suit用3-S,2-H,1-C,0-D*/
void cardPrint(Card * wDeck)
{
int i;
for (i=0; i<13; i++)
{
switch(wDeck[i].face)
{
case 12:
putchar('A');
break;
case 11:
putchar('K');
break;
case 10:
putchar('Q');
break;
case 9:
putchar('J');
break;
case 8:
printf("10");
break;
case 7:
putchar('9');
break;
case 6:
putchar('8');
break;
case 5:
putchar('7');
break;
case 4:
putchar('6');
break;
case 3:
putchar('5');
break;
case 2:
putchar('4');
break;
case 1:
putchar('3');
break;
case 0:
putchar('2');
break;
}
switch(wDeck[i].suit)
{
case 3:
putchar('S');
break;
case 2:
putchar('H');
break;
case 1:
putchar('C');
break;
case 0:
putchar('D');
break;
}
putchar(' ');
}
}