微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

cocos2dx《单机斗地主》源码解剖之五 叫地主

发完牌,下面就该叫地主了,首先从玩家(人)开始叫地主,下一回合所有玩家(包括电脑玩家)按逆时针顺序叫地主。我们来看一下叫地主的模块:

void GameScene::Call(float dt){
	//是否都叫过地主
	if(!m_player->getCall() || !m_npcOne->getCall() || !m_npcTwo->getCall())
	{
		m_iCallTime%=3;
		switch (m_iCallTime)
		{
		case 0://玩家选择地主分数
			m_menu->setVisible(true);
			if (m_bCall[0])//1分
			{
				CcmenuItemFont* itemFont = (CcmenuItemFont*)m_menu->getChildByTag(1);
				itemFont->setEnabled(false);
			}
			else
			{
				CcmenuItemFont* itemFont = (CcmenuItemFont*)m_menu->getChildByTag(1);
				itemFont->setEnabled(true);
			}
			if (m_bCall[1])//2分
			{
				CcmenuItemFont* itemFont = (CcmenuItemFont*)m_menu->getChildByTag(2);
				itemFont->setEnabled(false);
			}
			else
			{
				CcmenuItemFont* itemFont = (CcmenuItemFont*)m_menu->getChildByTag(2);
				itemFont->setEnabled(true);
			}
			if (m_bCall[2])//3分
			{
				CcmenuItemFont* itemFont = (CcmenuItemFont*)m_menu->getChildByTag(3);
				itemFont->setEnabled(false);
			}
			else//不叫
			{
				CcmenuItemFont* itemFont = (CcmenuItemFont*)m_menu->getChildByTag(3);
				itemFont->setEnabled(true);
			}
			break;
		case 1://电脑1选择地主分数
			++m_iCallTime;
			NpcCall(m_npcTwo,m_npcOne);//电脑玩家叫地主
			ShowFenShu(ccp(m_npcTwo->getPoint().x-88,m_npcTwo->getPoint().y),m_npcTwo->getCallNum());
			break;
		case 2://电脑2选择地主分数
			++m_iCallTime;
			NpcCall(m_npcOne,m_npcTwo);
			ShowFenShu(ccp(m_npcOne->getPoint().x+88,m_npcOne->getPoint().y),m_npcOne->getCallNum());
			break;
		}
	}
	else//判断谁是地主并把三张牌给他
	{
		//如果都没叫地主,把地主给一开始选地主那个
		if(m_player->getCallNum() == 0 && m_npcOne->getCallNum() == 0 && m_npcTwo->getCallNum() == 0)
		{
			switch (m_iCall%3)
			{
			case 0:
				m_player->setCallNum(3);
				break;
			case 1:
				m_npcTwo->setCallNum(3);
				break;
			case 2:
				m_npcOne->setCallNum(3);
				break;
			default:
				break;
			}
		}
		//谁的值大谁当地主
		CCObject* object;
		if(m_player->getCallNum() > m_npcOne->getCallNum() && m_player->getCallNum() > m_npcTwo->getCallNum()){
			CCArray* arrTem = CCArray::create();
			CCARRAY_FOREACH(m_Three->getArrPk(),object){
				Poker* pk = (Poker *)object;
				Poker* pkcopy = pk->copy();
				arrTem->addobject(pkcopy);
				addChild(pkcopy);
				m_player->getArrPk()->addobject(pk);
				m_player->setIsDiZhu(true);
				m_npcOne->setIsDiZhu(false);
				m_npcTwo->setIsDiZhu(false);
				m_IoUtCard = 0;
			}
			m_Three->getArrPk()->removeAllObjects();
			m_Three->getArrPk()->addobjectsFromArray(arrTem);
			m_Three->updatePkWeiZhi();
			m_player->updatePkWeiZhi();
			//显示地主标签
			m_lableDiZhu->setPosition(playerDiZhuLablePt);
			m_lableDiZhu->setVisible(true);
		}
		if(m_npcOne->getCallNum() > m_player->getCallNum() && m_npcOne->getCallNum() > m_npcTwo->getCallNum()){
			
			CCArray* arrTem = CCArray::create();
			CCARRAY_FOREACH(m_Three->getArrPk(),object){
				Poker* pk = (Poker *)object;
				Poker* pkcopy = pk->copy();
				arrTem->addobject(pkcopy);
				addChild(pkcopy);
				m_npcOne->getArrPk()->addobject(pk);
				m_player->setIsDiZhu(false);
				m_npcOne->setIsDiZhu(true);
				m_npcTwo->setIsDiZhu(false);
				m_IoUtCard = 2;
			}
			m_Three->getArrPk()->removeAllObjects();
			m_Three->getArrPk()->addobjectsFromArray(arrTem);
			m_Three->updatePkWeiZhi();
			//重新分拆牌
			m_npcOne->m_vecPX.clear();
			m_npcOne->updatePkWeiZhi();
			FenChaiNpcPai(m_npcOne);
			//显示地主标签
			m_lableDiZhu->setPosition(npcOneDiZhuLablePt);
			m_lableDiZhu->setVisible(true);
		}
		if(m_npcTwo->getCallNum() > m_npcOne->getCallNum() && m_npcTwo->getCallNum() > m_player->getCallNum()){
			CCArray* arrTem = CCArray::create();
			CCARRAY_FOREACH(m_Three->getArrPk(),object){
				Poker* pk = (Poker *)object;
				Poker* pkcopy = pk->copy();
				arrTem->addobject(pkcopy);
				addChild(pkcopy);
				m_npcTwo->getArrPk()->addobject(pk);
				m_player->setIsDiZhu(false);
				m_npcOne->setIsDiZhu(false);
				m_npcTwo->setIsDiZhu(true);
				m_IoUtCard = 1;
			}
			m_Three->getArrPk()->removeAllObjects();
			m_Three->getArrPk()->addobjectsFromArray(arrTem);
			m_Three->updatePkWeiZhi();
			//重新分拆牌
			m_npcTwo->m_vecPX.clear();
			m_npcTwo->updatePkWeiZhi();
			FenChaiNpcPai(m_npcTwo);
			//显示地主标签
			m_lableDiZhu->setPosition(npcTwoDiZhuLablePt);
			m_lableDiZhu->setVisible(true);
		}
		m_iState = 2;
		++m_iCall;
		m_iCallTime = m_iCall;
		unschedule(schedule_selector(GameScene::Call));
		//选定地主后显示三张牌
		CCARRAY_FOREACH(m_Three->getArrPk(),object){
			Poker* pk = (Poker*)object;
			pk->showFront();
		}
		//移除叫地主分数的显示
		this->removeChildByTag(FenShu);
		this->removeChildByTag(FenShu);
		this->removeChildByTag(FenShu);
		//使主玩家的牌为可点击状态
		CCARRAY_FOREACH(m_player->getArrPk(),object){
			Poker* pk = (Poker *)object;
			pk->setDianJi(true);
		}
	}
}
上面判断其实很简单:首先判断各玩家是否都已经叫分,如果是就判断谁是斗地主并把三张牌给他,如果都没有叫斗地主的话,我这里的逻辑是把斗地主判给第一个叫分的玩家。

接下来我们看看电脑玩家是如何叫分的,为了简单,我让电脑玩家随机选择分数,代码如下:

void GameScene::NpcCall(Player* npc,Player* npc1){
	int i = rand()%4;
	if(i == 3) //3表示不叫
	{
		npc->setCall(true);//设置已叫地主
		npc->setCallNum(0);//分数设置为0
	}
	else
	{
		while (m_bCall[i] == true)//bool m_bCall[3];分数是否被叫 m_bCall[0]:1分 m_bCall[1]:2分 m_bCall[2]:3分
			i = rand()%3; //重新选择分数
		m_bCall[i] = true;
		npc->setCall(true);
		npc->setCallNum(i+1);
		//如果叫3分,设置其他玩家已叫地主状态
		if(i == 2)
		{
			m_player->setCall(true);
			npc1->setCall(true);
		}
	}
}
源码请往前三章下载!

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐