예제 #1
0
std::string PlanetWarsGame::toString(Player* pov) const {
    std::stringstream gameState;
    const int numPlanets = static_cast<int>(m_planets.size());

    //Write the planets.
    for (int i = 0; i < numPlanets; ++i) {
        Planet* planet = m_planets[i];
        gameState << "P " << planet->getX()
                << " " << planet->getY()
                << " " << pov->povId(planet->getOwner())
                << " " << planet->getNumShips()
                << " " << planet->getGrowthRate()
                << std::endl;
    }

    //Write the fleets.
    for (FleetList::const_iterator it = m_fleets.begin(); it != m_fleets.end(); ++it) {
        Fleet* fleet = (*it);
        gameState << "F " << pov->povId(fleet->getOwner())
                << " " << fleet->getNumShips()
                << " " << fleet->getSource()->getId()
                << " " << fleet->getDestination()->getId()
                << " " << fleet->getTotalTripLength()
                << " " << fleet->getTurnsRemaining()
                << std::endl;
    }

    gameState << "go" << std::endl;

    return gameState.str();

}
예제 #2
0
void GameController::onSetSelectPlanet(Planet* planet)
{
  Fleet* fleet = NULL;
  
  /* on first selection and since last assumes that the vector is not empty */
  if(fleets.isEmpty())
    {
      fleet = new Fleet();
      fleets.append(fleet);
    }
  
  fleet = fleets.last();
  
  if(fleet->getSource() == NULL) /* selecting source */
    {

      if(planet->getOwner() != players[currentPlayer]) /* if trying to choose a source planet that isn't yours */
	return;

      planet->blink();
      fleet->setSource(planet);
      fleet->setKillPercent(planet->getKillPercent());
      fleet->setOwner(players[currentPlayer]);

      emit displayControl(QString("<font color=%1>%2</font>:Select destination planet").arg(players[currentPlayer]->getColor().name()).arg(players[currentPlayer]->getName()));
    }
  else if(fleet->getDestination() == NULL) /* selecting destination */
    {
      if(fleet->getSource() == planet) /* source and destination are the same */
	return;

      planet->blink();
      fleet->setDestination(planet);
      
      emit displayShipNumber();
      
      emit displayControl(QString("<font color=%1>%2</font>:Number of ships").arg(players[currentPlayer]->getColor().name()).arg(players[currentPlayer]->getName()));
      
    }
  

}
예제 #3
0
void GameController::onSetFleetShipNumber(int ships)
{
  Fleet* fleet = NULL;

  fleet = fleets.last();

  fleet->getSource()->setShips(fleet->getSource()->getShips() - ships);
  
  fleet->setNumShips(ships);
  fleet->setKillPercent(fleet->getSource()->getKillPercent());
  fleet->setArrivalTurn(currentTurn+2);

  fleet->getSource()->blink();
  fleet->getDestination()->blink();

  fleet = new Fleet();
  fleets.append(fleet);
  
  emit displayControl(QString("<font color=%1>%2</font>:Select source planet").arg(players[currentPlayer]->getColor().name()).arg(players[currentPlayer]->getName()));

}
예제 #4
0
void GameController::onChangeTurn()
{
  currentTurn++;

  /* impose production on owned planets */
  for(int i=0; i<players.size(); i++)
    {
      Player* tempP = players[i];
      for(int j=0; j<players[i]->getPlanets().size(); j++)
	{
	  Planet* tempPl = tempP->getPlanets()[j];
	  tempPl->setShips( tempPl->getShips() +  tempPl->getProduction());
	}
    }

  for(int k=0; k<fleets.size(); k++)
    {
      Fleet* tempF = fleets[k];
      /* fleet has arrived */
      if(fleets[k]->getArrivalTurn() == currentTurn)
	{
	  /* fleet owner same as destination owner, so reinforcing a planet */
	  if(tempF->getOwner() == tempF->getDestination()->getOwner())
	    {
	      tempF->getDestination()->setShips(tempF->getDestination()->getShips() + tempF->getNumShips());
	    }
	  else /* attacking planet */
	    {
	      double attackScore, defenseScore;

	      attackScore = tempF->getNumShips()*tempF->getKillPercent();
	      defenseScore = tempF->getDestination()->getShips()*tempF->getDestination()->getKillPercent();

	      if(attackScore >= defenseScore) /* attackers win */
		{
		  if(tempF->getDestination()->getOwner() != NULL)
		    tempF->getDestination()->getOwner()->removeFromPlanets(tempF->getDestination());

		  tempF->getOwner()->addToPlanets(tempF->getDestination());
		  tempF->getDestination()->setOwner(tempF->getOwner());
		  
		  tempF->getDestination()->setShips((int)attackScore-defenseScore);
		  emit displayInfo(QString("Turn %1: Planet %2 has fallen to %3").arg(currentTurn).arg(tempF->getDestination()->getName()).arg(tempF->getOwner()->getName()));
		  
		}
	      else /* defense wins */
		{
		  tempF->getDestination()->setShips((int)defenseScore-attackScore);
		  emit displayInfo(QString("Turn %1: Planet %2 has held against an attack from %3.").arg(currentTurn).arg(tempF->getDestination()->getName()).arg(tempF->getOwner()->getName()));
		}
	    }
	  /* processed fleet, so remove it */
	  fleets.removeAt(k);
	}
    }
     

  if(currentPlayer < players.size() - 1)
    currentPlayer++;
  else
    currentPlayer = 0;

  emit displayControl(QString("<font color=%1>%2</font>:Select source planet").arg(players[currentPlayer]->getColor().name()).arg(players[currentPlayer]->getName()));
}