当前位置:编程学习 > C/C++ >>

一个 C语言源代码算法

掼蛋的扑克牌排序(大小为一级排序和花色为二级排序排序),除了大小王每次最大牌要考虑。
答案:牌类这样定义:

class CCard
{
public:
 int m_nColor;  //花色;
 int m_nValue;  //面值;
 int m_nSize;   //实际计算的大小
public:
 CCard();
};

CCard::CCard()
{
 m_nColor = 0;     //0表示没有任意花色;
 m_nValue = 0;   //0表示还没有牌;2-10-K-A对应2-10-13-14,小、大王对应16,17
 m_nSize = 0;      //打几子那张牌大小设为15,用于比较牌值
}

//全局

static HBITMAP g_hBmpCard[55];//前0-51主牌52牌背53小王54大王

CCard g_cAllCards[108];

CPlayer pPlayers[4];//记录玩家信息的类

int g_nDaJiZi = 2;    //本次打几子,针对m_nValue: 2-10-K-A对应2-10-13-14

//牌的初始化及洗牌

void ShuffleCards(int nDaJiZi)
{
 //将牌放入;
 for(int i=2,j=0;j<13;i++)
 {
  //g_cAllCards[95]总是在前96放入小牌面值,留下96-103放牌值牌
  if(i == nDaJiZi) continue; 
  //d方块 m_nColor = 1
  g_cAllCards[j*8+0].m_nColor = 1;
  g_cAllCards[j*8+1].m_nColor = 1;
  //c梅花 m_nColor = 2
  g_cAllCards[j*8+2].m_nColor = 2;
  g_cAllCards[j*8+3].m_nColor = 2;
  //h红桃 m_nColor = 3
  g_cAllCards[j*8+4].m_nColor = 3;
  g_cAllCards[j*8+5].m_nColor = 3;
  //s黑桃 m_nColor = 4
  g_cAllCards[j*8+6].m_nColor = 4;
  g_cAllCards[j*8+7].m_nColor = 4;
  //0-95用于牌值真正大小比较的数据
  for(int k=0; k<8; k++)
  {
   g_cAllCards[j*8+k].m_nValue = i;
   g_cAllCards[j*8+k].m_nSize = i;
  }
  j++;
 }
 //g_cAllCards的96-103放牌值牌
 g_cAllCards[96].m_nColor = 1;
 g_cAllCards[97].m_nColor = 1;
 //梅花 m_nColor = 2
 g_cAllCards[98].m_nColor = 2;
 g_cAllCards[99].m_nColor = 2;
 //红桃 m_nColor = 3
 g_cAllCards[100].m_nColor = 3;
 g_cAllCards[101].m_nColor = 3;
 //黑桃 m_nColor = 4
 g_cAllCards[102].m_nColor = 4;
 g_cAllCards[103].m_nColor = 4;
 //96-103用于牌值真正大小比较的数据
 for(int l=96; l<104; l++)
 {
  g_cAllCards[l].m_nValue = nDaJiZi;
  g_cAllCards[l].m_nSize = 15;
 }

 g_cAllCards[104].m_nColor = 4;   //大王;
 g_cAllCards[104].m_nValue = 16; 
 g_cAllCards[104].m_nSize = 16;
 g_cAllCards[105].m_nColor = 4;  
 g_cAllCards[105].m_nValue = 16;
 g_cAllCards[105].m_nSize = 16;
 g_cAllCards[106].m_nColor = 4;   //小王;
 g_cAllCards[106].m_nValue = 17;
 g_cAllCards[106].m_nSize = 17;
 g_cAllCards[107].m_nColor = 4;
 g_cAllCards[107].m_nValue = 17; 
 g_cAllCards[107].m_nSize = 17;


 //洗牌;
 int nRandPos;  
 CCard cTempCard;
 time_t t;
 srand((unsigned) time(&t));//设置rand函数所用的启始种子值,以期每次产生的随机数序列均不相同。
 for(int k=0;k<108;k++)
 {
  //产生一个随机位置;
  nRandPos = rand()%108;
  //把当前牌与这个随机位置的牌交换;
  cTempCard = g_cAllCards[k];
  g_cAllCards[k] = g_cAllCards[nRandPos];
  g_cAllCards[nRandPos] = cTempCard; 
 }
}

 

//理牌,按m_nSize冒泡排序(BubbleSort)
void SortCards(CCard* pzCards,int nNumCards, BOOL Ascend = FALSE)
{

 if (Ascend)// 升序
 {
  for (int i = 0; i < nNumCards-1; i++)
   for (int j = 0; j < nNumCards-1-i; j++)
   {
    if (pzCards[j].m_nSize > pzCards[j+1].m_nSize)
    {
     CCard temp = pzCards[j];
     pzCards[j] = pzCards[j+1];
     pzCards[j+1] = temp;
    }
    if(pzCards[j].m_nSize == pzCards[j+1].m_nSize && pzCards[j].m_nColor < pzCards[j+1].m_nColor)
    {
     CCard temp = pzCards[j];
     pzCards[j] = pzCards[j+1];
     pzCards[j+1] = temp;
    }
   }
 }
 else// 降序
 {   
  for (int i = 0; i < nNumCards-1; i++)
   for (int j = 0; j < nNumCards-1-i; j++)
   {
    if (pzCards[j].m_nSize < pzCards[j+1].m_nSize)
    {
     CCard temp = pzCards[j];
     pzCards[j] = pzCards[j+1];
     pzCards[j+1] = temp;
    }
    if(pzCards[j].m_nSize == pzCards[j+1].m_nSize && pzCards[j].m_nColor < pzCards[j+1].m_nColor)
    {
     CCard temp = pzCards[j];
     pzCards[j] = pzCards[j+1];
     pzCards[j+1] = temp;
    }
   }
 }
}

 

void main()

{

 ShuffleCards(g_nDaJiZi);
 DealingCards();

}

//牌的加载

void Load()

{

   //面值牌      
   int k=0;
   for(int i=1; i<5; i++)
   {
    for(int j =2; j<15; j++)
    {
     TCHAR szCard[22] = {0};
     _stprintf_s(szCard, _T(".\\sk\\%d-%d.BMP"), i, j);
     g_hBmpCard[k] = (HBITMAP)LoadImage(NULL, szCard, IMAGE_BITMAP, 71, 96, LR_LOADFROMFILE);
     //g_hBmpCard[k] = LoadBitmapW(hInst,szCard);    
     if(g_hBmpCard[k] == NULL)
     { 
      TCHAR szError[38];
      _stprintf_s(szError,_T("加载%s失败!(0x%x);"),szCard,GetLastError());
      if(IDOK == MessageBox(hWnd,szError,_T("error"),MB_OK|MB_ICONERROR ))
       DestroyWindow(hWnd);
     }
     k++;
    }
   }     


   //牌背和小、大王   
   g_hBmpCard[52] = (HBITMAP)::LoadImage(NULL,_T(".\\sk\\111.BMP"),IMAGE_BITMAP,71,96,LR_LOADFROMFILE);
   g_hBmpCard[53] = (HBITMAP)::LoadImage(NULL,_T(".\\sk\\4-16.BMP"),IMAGE_BITMAP,71,96,LR_LOADFROMFILE);
   g_hBmpCard[54] = (HBITMAP)::LoadImage(NULL,_T(".\\sk\\4-17.BMP"),IMAGE_BITMAP,71,96,LR_LOADFROMFILE);

}

根据牌算得牌是数组g_hBmpCard[]中的第几个

    int nCard = (pPlayers[0].m_Card[i].m_nColor-1)*13+pPlayers[0].m_Card[i].m_nValue-2;
   //(4-1)*13+16-3=52是小王

上一个:C语言求完数。
下一个:C语言的一个字符串问题

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,