加入收藏 | 设为首页 | 会员中心 | 我要投稿 衡阳站长网 (https://www.0734zz.cn/)- 数据集成、设备管理、备份、数据加密、智能搜索!
当前位置: 首页 > 创业 > 经验 > 正文

麻将游戏算法深入解析及实现代码

发布时间:2021-01-01 04:47:23 所属栏目:经验 来源:网络整理
导读:麻将游戏算法深入解析及实现代码 这两天为了工具箱的完善,整理了这些年引擎开发的一些资料,无意中发现06年写的一个麻将算法,编译运行了一下,还是有点意思的,拿出来整理一下分享给大家。 麻将是一种大家最喜爱的娱乐活动之一,相信所有人都有接触过。我

    看,洗牌发牌类也搞定了,下面我们来将其在控制台上运行起来,打开控制台的main函数。

#include "AICMJ.h" 
#include "AICMJManage.h" 
 
#include <windows.h> 
#include <iostream> 
using namespace std; 
 
 
int _tmain(int argc,_TCHAR* argv[]) 
{ 
 //其它三个玩家 
 CMJ    t_OtherPlayer[3]; 
 //我 
 CMJ    t_MyPlayer; 
 //洗牌器 
 CMJManage  t_MJManage; 
 //分数 
 int    t_Score = 0; 
GameStart: 
 
 //初始化及洗牌 
 t_MJManage.InitPai();//初始化 
 t_MyPlayer.CleanUp(); 
 for(UINT i = 0 ; i < 3; i++ ) 
 { 
  t_OtherPlayer[i].CleanUp(); 
 } 
 cout<<"洗牌完成"<<endl; 
 cout<<"起牌:========================================================"<<endl; 
 for(UINT i = 0 ; i < 13 ; i++) 
 { 
 
  stPAIEx t_Pai = t_MJManage.GetAPai(); 
  t_MyPlayer.AddPai(t_Pai.m_NewPai.m_Type,t_Pai.m_NewPai.m_Value); 
 
  for(UINT j = 0 ; j < 3; j++ ) 
  { 
   stPAIEx t_Pai2 = t_MJManage.GetAPai(); 
   t_OtherPlayer[j].AddPai(t_Pai2.m_NewPai.m_Type,t_Pai2.m_NewPai.m_Value); 
  } 
 } 
 t_MyPlayer.Init(); 
 for(UINT j = 0 ; j < 3; j++ ) 
 { 
  t_OtherPlayer[j].Init(); 
 } 
 //打牌循环 
 bool t_Finish = false; 
 bool t_Ting  = false; 
 while(t_Finish == false) 
 { 
 
  t_MyPlayer.PrintAllPai(); 
  cout<<endl; 
  cout<<"起牌:========================================================"<<endl; 
  stPAIEx t_Pai = t_MJManage.GetAPai();  
  
  //刷新我方牌墙 
  t_MyPlayer.PrintPai(t_Pai.m_NewPai.m_Type,t_Pai.m_NewPai.m_Value); 
  cout<<endl; 
  //如果没有听头 
  if(t_Ting == false) 
  { 
   cout<<"要还是打?Y/N"; 
   char t_Result; 
   cin>>t_Result; 
   if(t_Result =='Y'||t_Result=='y') 
   { 
    //起牌 
    t_MyPlayer.AddPai(t_Pai.m_NewPai.m_Type,t_Pai.m_NewPai.m_Value); 
    //起牌后胡牌判断 
    t_Finish = t_MyPlayer.CheckAllPai(MJPAI_GETPAI); 
    if(t_Finish) 
    { 
     printf("胡啦!!!:%s-%d",t_MyPlayer.GetInfo()->m_GoodName,t_MyPlayer.GetInfo()->m_GoodValue); 
     ::_sleep(1000); 
    } 
    else 
    { 
     if(t_Pai.m_NewPai.m_Type == -1)//如果起牌数已达到上限 
     { 
      cout<<endl; 
      cout<<"黄庄了!!!!!!!!!!!!!"<<endl; 
      break; 
     } 
 
     t_MyPlayer.PrintAllPai(); 
     cout<<endl; 
OUTPai: 
     cout<<"请打牌(输入牌序号)"; 
     int PaiIndex; 
     cin>>PaiIndex; 
     if(t_MyPlayer.DelPai(PaiIndex)==false) 
     { 
      cout<<"没有此牌"<<endl; 
      goto OUTPai; 
     } 
     //==============================牌面刷新================================================ 
     cout<<"牌面刷新============================"<<endl; 
     t_MyPlayer.PrintAllPai(); 
     cout<<endl; 
     //============================================================================== 
 
     //======================包听======================================================== 
     if(t_MyPlayer.CheckAllPai(MJPAI_PUTPAI)) 
     { 
 
      char t_BTing; 
      cout<<"要包听吗?:(Y/N)"; 
      cin>>t_BTing; 
      if(t_BTing=='y'||t_BTing=='Y') 
      { 
       t_Ting = true; 
      } 
     } 
     //============================================================================== 
 
    } 
   } 
   else 
   { 
     //======================包听======================================================== 
     if(t_MyPlayer.CheckAllPai(MJPAI_PUTPAI)) 
     { 
 
      char t_BTing; 
      cout<<"要包听吗?:(Y/N)"; 
      cin>>t_BTing; 
      if(t_BTing=='y'||t_BTing=='Y') 
      { 
       t_Ting = true; 
      } 
     } 
     //============================================================================== 
   } 
  } 
  else 
  { 
     
    t_MyPlayer.AddPai(t_Pai.m_NewPai.m_Type,t_Pai.m_NewPai.m_Value); 
    //起牌 
    int iPaiIndex = t_MyPlayer.GetPaiIndex(t_Pai.m_NewPai.m_Type,t_Pai.m_NewPai.m_Value); 
    if(iPaiIndex>=0) 
    { 
     //起牌后胡牌判断 
     t_Finish = t_MyPlayer.CheckAllPai(MJPAI_GETPAI); 
     if(t_Finish) 
     { 
      cout<<endl; 
      printf("胡啦!!!:%s-合计%d番",t_MyPlayer.GetInfo()->m_GoodValue); 
      t_Score += t_MyPlayer.GetInfo()->m_GoodValue; 
      ::_sleep(1000); 
      break; 
     } 
     else 
     { 
      t_MyPlayer.DelPai(iPaiIndex); 
      cout<<"打牌"; 
      t_MyPlayer.PrintPai(t_Pai.m_NewPai.m_Type,t_Pai.m_NewPai.m_Value); 
      cout<<endl; 
      ::_sleep(1000); 
     } 
    } 
    else 
    { 
     cout<<"程序出错!"<<endl; 
    } 
 
  } 
  cout<<endl; 
  //其它玩家起牌出牌 
  for(UINT j = 0 ; j < 3; j++ ) 
  { 
   stPAIEx t_Pai2 = t_MJManage.GetAPai(); 
   if(j==0) 
   { 
    cout<<"南家起牌出牌:"; 
    t_MyPlayer.PrintPai(t_Pai2.m_NewPai.m_Type,t_Pai2.m_NewPai.m_Value); 
    cout<<endl; 
    ::_sleep(1000); 
   } 
   if(j==1) 
   { 
    cout<<"西家起牌出牌:"; 
    t_MyPlayer.PrintPai(t_Pai2.m_NewPai.m_Type,t_Pai2.m_NewPai.m_Value); 
    cout<<endl; 
    ::_sleep(1000); 
   } 
   if(j==2) 
   { 
    cout<<"北家起牌出牌:"; 
    t_MyPlayer.PrintPai(t_Pai2.m_NewPai.m_Type,t_Pai2.m_NewPai.m_Value); 
    cout<<endl; 
    ::_sleep(1000); 
   } 
    
   char t_Result; 
   if(t_Ting == false) 
   { 
    if(t_Pai2.m_IsHZ)//如果起牌数已达到上限 
    { 
     cout<<endl; 
     cout<<"黄庄了!!!!!!!!!!!!!"<<endl; 
     t_Finish = true; 
     break; 
    } 
 
    bool t_Check = false; 
    //检查吃牌 
    if(t_MyPlayer.CheckChiPai(t_Pai2.m_NewPai.m_Type,t_Pai2.m_NewPai.m_Value)) 
    { 
     if(t_Check==false) 
     { 
      cout<<"请选择:"; 
     } 
     cout<<"(吃)"; 
     t_Check = true; 
    } 
    //检查碰牌 
    if(t_MyPlayer.CheckPengPai(t_Pai2.m_NewPai.m_Type,t_Pai2.m_NewPai.m_Value)) 
    { 
     if(t_Check==false) 
     { 
      cout<<"请选择:"; 
     } 
     cout<<"(碰)"; 
     t_Check = true; 
    } 
    //检查杠牌 
    if(t_MyPlayer.CheckGangPai(t_Pai2.m_NewPai.m_Type,t_Pai2.m_NewPai.m_Value)) 
    { 
     if(t_Check==false) 
     { 
      cout<<"请选择:"; 
     } 
     cout<<"(杠)"; 
     t_Check = true; 
    } 
    //起牌 
    t_MyPlayer.AddPai(t_Pai2.m_NewPai.m_Type,t_Pai2.m_NewPai.m_Value); 
    //起牌后胡牌判断 
    if(t_MyPlayer.CheckAllPai(MJPAI_GETPAI)) 
    { 
     if(t_Check==false) 
     { 
      cout<<"请选择:"; 
     } 
     cout<<"(胡)"; 
     t_Check = true; 
    }  
    int iPaiIndex = t_MyPlayer.GetPaiIndex(t_Pai2.m_NewPai.m_Type,t_Pai2.m_NewPai.m_Value); 
    t_MyPlayer.DelPai(iPaiIndex); 
    //如果查到 
    if(t_Check) 
    { 
     cout<<endl; 
     cin>>t_Result; 
    } 
    else 
    { 
     //返回循环 
     continue; 
    } 
   } 
   else 
   { 
    t_Result = '4'; 
   } 
   //吃牌 
   if(t_Result =='1') 
   { 
    t_MyPlayer.PrintChiChosePai(); 
 
    int index = 0; 
    //如果吃牌组合大于 
    if(t_MyPlayer.GetChiChoseNum()>1) 
    { 
     cout<<"请输入组合号:"<<endl; 
     cin>>index; 
    } 
    t_MyPlayer.DoChiPai(index,t_Pai2.m_NewPai.m_Type,t_Pai2.m_NewPai.m_Value); 
 
    //============================================================================== 
    cout<<"牌面刷新============================"<<endl; 
    t_MyPlayer.PrintAllPai(); 
    cout<<endl; 
    //============================================================================== 
 

(编辑:衡阳站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

热点阅读