最近做的一個小工具,要將斗地主的撲克牌從大到小排列,花色按黑>紅>梅>方
也就是,大王,小王,2,A,K,Q,J,10….3這樣的順序,
假設你現在有一手牌該如何排列?
我的做法是,定義一個數組,數組內保存了需要排列的順序,也就是:
大王,小王,黑桃2,紅桃2,梅花2,方塊2,黑桃A,紅桃A,梅花A,方塊A———->黑桃3,紅桃3,梅花3,方塊3
然後從遍歷這個數組,和手上的牌對比,如果存在則打印.
大致代碼如下
#define TOTAL_CARDS (54)
/* 牌信息定義
int CardInfo[TOTAL_CARDS]
{
0,1,2,3,4,5,6,7,8,9,10,11,12, //黑桃的A–K
13,14,15,16,17,18,19,20,21,22,23,24,25, //紅桃A–K
26,27,28,29,30,31,32,33,34,35,36,37,38, //梅花A–K
39,40,41,42,43,44,45,46,47,48,49,50,51, //方塊A–K
52,53 //小王大王
};
*/
/*用於排序的數組*/
int SortedCard[TOTAL_CARDS]={
53,52, // 大王,小王
1,14,27,40, // 2
0,13,26,39, //A
2,15,28,41, //3
3,16,29,42, //4
4,17,30,43, //5
5,18,31,44, //6
6,19,32,45, //7
7,20,33,46, //8
8,21,34,47, //9
9,22,35,48, //10
10,23,36,49, //J
11,24,37,50, //Q
12,25,38,51 //K
};
// getUserCards();
for (int i=0; i
{
if ( 0!= CardInfo[SortedCard[i]] )
{
printf( “%d,”,CardInfo[SortedCard[i]] );
}
}