void GameInfoView::endTurn() { emit turnEnded(); }
void PlanetWarsGame::completeStep() { //Proceed only if there's an unfinished step. if (STEPPING != m_state) { return; } m_state = PROCESSING; //Clear the old new fleets. m_newFleets.clear(); //Read and process the the responses from each of the players. std::string firstPlayerOutput(m_firstPlayer->readCommands()); std::string secondPlayerOutput(m_secondPlayer->readCommands()); bool isFirstPlayerRunning = this->processOrders(firstPlayerOutput, m_firstPlayer); bool isSecondPlayerRunning = this->processOrders(secondPlayerOutput, m_secondPlayer); //Check whether the players are still alive. if (!isFirstPlayerRunning || !isSecondPlayerRunning) { this->stop(); return; } this->advanceGame(); //Check each player's position. int firstPlayerShips = 0; int secondPlayerShips = 0; const int numPlanets = static_cast<int>(m_planets.size()); for (int i = 0; i < numPlanets; ++i) { Planet* planet = m_planets[i]; const int ownerId = planet->getOwner()->getId(); if (1 == ownerId) { firstPlayerShips += planet->getNumShips(); } else if (2 == ownerId) { secondPlayerShips += planet->getNumShips(); } } for (FleetList::iterator it = m_fleets.begin(); it != m_fleets.end(); ++it) { Fleet* fleet = *it; if (1 == fleet->getOwner()->getId()) { firstPlayerShips += fleet->getNumShips(); } else { secondPlayerShips += fleet->getNumShips(); } } emit turnEnded(); //Check for game end conditions. bool isGameOver = false; if (firstPlayerShips == 0 && secondPlayerShips == 0) { this->logMessage("Draw."); isGameOver = true; } else if (firstPlayerShips == 0) { this->logMessage("Player 2 wins."); isGameOver = true; } else if (secondPlayerShips == 0) { this->logMessage("Player 1 wins."); isGameOver = true; } else if (m_turn >= m_maxTurns) { if (firstPlayerShips > secondPlayerShips) { this->logMessage("Player 1 wins."); isGameOver = true; } else if (firstPlayerShips < secondPlayerShips) { this->logMessage("Player 2 wins."); isGameOver = true; } else { this->logMessage("Draw."); isGameOver = true; } } if (isGameOver) { this->stop(); return; } //Game is not over. m_state = READY; this->continueRunning(); }
void AIPlayer::beginTurn() { emit turnEnded(); }