代码拉取完成,页面将自动刷新
同步操作将从 ibc-dabing/Landlords 强制同步,此操作会覆盖自 Fork 仓库以来所做的任何修改,且无法恢复!!!
确定后同步将在后台操作,完成时将刷新页面,请耐心等待。
#include "gamecontrol.h"
#include "playhand.h"
#include <QRandomGenerator>
#include <QTimer>
#include <QDebug>
GameControl::GameControl(QObject *parent) : QObject(parent)
{
}
void GameControl::playerInit()
{
m_robotLeft = new Robot("机器人A", this);
m_robotRight = new Robot("机器人B", this);
m_user = new UserPlayer("我自己", this);
m_robotLeft->setDirection(Player::Left);
m_robotRight->setDirection(Player::Right);
m_user->setDirection(Player::Right);
Player::Sex sex;
sex = (Player::Sex)QRandomGenerator::global()->bounded(2);
m_robotLeft->setSex(sex);
sex = (Player::Sex)QRandomGenerator::global()->bounded(2);
m_robotRight->setSex(sex);
sex = (Player::Sex)QRandomGenerator::global()->bounded(2);
m_user->setSex(sex);
m_user->setPrevPlayer(m_robotLeft);
m_user->setNextPlayer(m_robotRight);
m_robotLeft->setPrevPlayer(m_robotRight);
m_robotLeft->setNextPlayer(m_user);
m_robotRight->setPrevPlayer(m_user);
m_robotRight->setNextPlayer(m_robotLeft);
m_currPlayer = m_user;
connect(m_user, &UserPlayer::notifyGrabLordBet, this, &GameControl::onGrabBet);
connect(m_robotLeft, &UserPlayer::notifyGrabLordBet, this, &GameControl::onGrabBet);
connect(m_robotRight, &UserPlayer::notifyGrabLordBet, this, &GameControl::onGrabBet);
connect(this, &GameControl::pendingInfo, m_robotLeft, &Robot::storePendingInfo);
connect(this, &GameControl::pendingInfo, m_robotRight, &Robot::storePendingInfo);
connect(this, &GameControl::pendingInfo, m_user, &Robot::storePendingInfo);
connect(m_robotLeft, &Robot::notifyPlayHand, this, &GameControl::onPlayHand);
connect(m_robotRight, &Robot::notifyPlayHand, this, &GameControl::onPlayHand);
connect(m_user, &Robot::notifyPlayHand, this, &GameControl::onPlayHand);
}
Robot *GameControl::getLeftRobot()
{
return m_robotLeft;
}
Robot *GameControl::getRightRobot()
{
return m_robotRight;
}
UserPlayer *GameControl::getUserPlayer()
{
return m_user;
}
void GameControl::setCurrentPlayer(Player *player)
{
m_currPlayer = player;
}
Player *GameControl::getCurrentPlayer()
{
return m_currPlayer;
}
Player *GameControl::getPendPlayer()
{
return m_pendPlayer;
}
Cards GameControl::getPendCards()
{
return m_pendCards;
}
void GameControl::initAllCards()
{
m_allCards.clear();
for(int p = Card::Card_Begin+1; p<Card::Card_SJ; ++p)
{
for(int s = Card::Suit_Begin+1; s<Card::Suit_End; ++s)
{
Card c((Card::CardPoint)p, (Card::CardSuit)s);
m_allCards.add(c);
}
}
m_allCards.add(Card(Card::Card_SJ, Card::Suit_Begin));
m_allCards.add(Card(Card::Card_BJ, Card::Suit_Begin));
}
Card GameControl::takeOneCard()
{
#if 0
// 测试飞机
static bool flag = true;
static Cards tmp;
if(flag)
{
Card c1(Card::Card_10, Card::Club);
Card c2(Card::Card_10, Card::Diamond);
Card c3(Card::Card_10, Card::Heart);
Card c4(Card::Card_J, Card::Club);
Card c5(Card::Card_J, Card::Diamond);
Card c6(Card::Card_J, Card::Heart);
tmp << c1 << c2 << c3 << c4 << c5 << c6;
m_allCards.remove(tmp);
flag = false;
}
if(getCurrentPlayer() == m_user && !tmp.isEmpty())
{
return tmp.takeRandomCard();
}
else
{
return m_allCards.takeRandomCard();
}
#else
// not test code
return m_allCards.takeRandomCard();
#endif
}
Cards GameControl::getSurplusCards()
{
return m_allCards;
}
void GameControl::resetCardData()
{
initAllCards();
m_robotLeft->clearCards();
m_robotRight->clearCards();
m_user->clearCards();
m_pendPlayer = nullptr;
m_pendCards.clear();
}
void GameControl::startLordCard()
{
m_currPlayer->prepareCallLord();
emit playerStatusChanged(m_currPlayer, ThinkingForCallLord);
}
void GameControl::becomeLord(Player *player, int bet)
{
m_curBet = bet;
player->setRole(Player::Lord);
player->getPrevPlayer()->setRole(Player::Farmer);
player->getNextPlayer()->setRole(Player::Farmer);
m_currPlayer = player;
player->storeDispatchCard(m_allCards);
QTimer::singleShot(1000, this, [=]()
{
emit gameStatusChanged(PlayingHand);
emit playerStatusChanged(player, ThinkingForPlayHand);
m_currPlayer->preparePlayHand();
});
}
void GameControl::clearPlayerScore()
{
m_robotLeft->setScore(0);
m_robotRight->setScore(0);
m_user->setScore(0);
}
int GameControl::getPlayerMaxBet()
{
return m_betRecord.bet;
}
void GameControl::onGrabBet(Player *player, int bet)
{
if(bet == 0 || m_betRecord.bet >= bet)
{
emit notifyGrabLordBet(player, 0, false);
}
else if(bet > 0 && m_betRecord.bet == 0)
{
emit notifyGrabLordBet(player, bet, true);
}
else
{
emit notifyGrabLordBet(player, bet, false);
}
if(bet == 3)
{
becomeLord(player, bet);
m_betRecord.reset();
return;
}
if(m_betRecord.bet < bet)
{
m_betRecord.bet = bet;
m_betRecord.player = player;
}
m_betRecord.times ++;
if(m_betRecord.times == 3)
{
if(m_betRecord.bet == 0)
{
emit gameStatusChanged(DispatchCard);
}
else
{
becomeLord(m_betRecord.player, m_betRecord.bet);
}
m_betRecord.reset();
return;
}
m_currPlayer = player->getNextPlayer();
emit playerStatusChanged(m_currPlayer, ThinkingForCallLord);
m_currPlayer->prepareCallLord();
}
void GameControl::onPlayHand(Player *player, const Cards &card)
{
emit notifyPlayHand(player, card);
if(!card.isEmpty())
{
m_pendCards = card;
m_pendPlayer = player;
emit pendingInfo(player, card);
}
Cards myCards = const_cast<Cards&>(card);
PlayHand::HandType type = PlayHand(myCards).getHandType();
if(type == PlayHand::Hand_Bomb || type == PlayHand::Hand_Bomb_Jokers)
{
m_curBet = m_curBet * 2;
}
if(player->getCards().isEmpty())
{
Player* prev = player->getPrevPlayer();
Player* next = player->getNextPlayer();
if(player->getRole() == Player::Lord)
{
player->setScore(player->getScore() + 2 * m_curBet);
prev->setScore(prev->getScore() - m_curBet);
next->setScore(next->getScore() - m_curBet);
player->setWin(true);
prev->setWin(false);
next->setWin(false);
}
else
{
player->setWin(true);
player->setScore(player->getScore() + m_curBet);
if(prev->getRole() == Player::Lord)
{
prev->setScore(prev->getScore() - 2 * m_curBet);
next->setScore(next->getScore() + m_curBet);
prev->setWin(false);
next->setWin(true);
}
else
{
next->setScore(next->getScore() - 2 * m_curBet);
prev->setScore(prev->getScore() + m_curBet);
next->setWin(false);
prev->setWin(true);
}
}
emit playerStatusChanged(player, GameControl::Winning);
return;
}
m_currPlayer = player->getNextPlayer();
m_currPlayer->preparePlayHand();
emit playerStatusChanged(m_currPlayer, GameControl::ThinkingForPlayHand);
}
此处可能存在不合适展示的内容,页面不予展示。您可通过相关编辑功能自查并修改。
如您确认内容无涉及 不当用语 / 纯广告导流 / 暴力 / 低俗色情 / 侵权 / 盗版 / 虚假 / 无价值内容或违法国家有关法律法规的内容,可点击提交进行申诉,我们将尽快为您处理。