Ejemplo n.º 1
0
 public Planet GetFutureState(int numberOfTurns)
 {
     FillFutureStatesIfNeeded();
     if (numberOfTurns > TurnsCount)
     {
         Planet futureState = new Planet(futureStates[TurnsCount]);
         if (futureState.Owner() <= 0) return futureState;
         futureState.NumShips(futureState.NumShips() +
                              futureState.GrowthRate() * (numberOfTurns - TurnsCount));
         return futureState;
     }
     return futureStates[numberOfTurns];
 }
Ejemplo n.º 2
0
 private static void PlanetGrowth(Planet planetInFuture)
 {
     if (planetInFuture.Owner() != 0)
     {
         planetInFuture.NumShips(planetInFuture.NumShips() + planetInFuture.GrowthRate());
     }
 }
Ejemplo n.º 3
0
        private void CalcFutureState()
        {
            futureStates.Clear();
            futureStates.Add(thisPlanet);

            canSend = thisPlanet.NumShips();

            for (int turn = 1; turn <= TurnsCount; turn++)
            {
                Planet planetInFuture = new Planet(futureStates[turn - 1]);
                PlanetGrowth(planetInFuture);

                Fleets thisTurnFleets = GetThisTurnFleets(turn);

                int oldPlanetOwner = planetInFuture.Owner();

                CalcFleetsOnPlanet(planetInFuture, thisTurnFleets);

                if (planetInFuture.Owner() != oldPlanetOwner)
                {

                    PlanetOwnerSwitch pos = new PlanetOwnerSwitch(
                        oldPlanetOwner, planetInFuture.Owner(), turn);

                    ownerSwitches.Add(pos);
                }

                futureStates.Add(planetInFuture);

                if (planetInFuture.Owner() != 1) canSend = 0;
                if (planetInFuture.NumShips() >= canSend) continue;
                canSend = planetInFuture.NumShips();
            }
        }
Ejemplo n.º 4
0
        private static void CalcFleetsOnPlanet(Planet planetInFuture, Fleets thisTurnFleets)
        {
            // First is ownerID, second is number of ships
            List<Pair<int, int>> ships = new List<Pair<int, int>>();

            if (thisTurnFleets.Count <= 0) return;
            const int owners = 2;

            for (int id = 1; id <= owners; ++id)
            {
                Fleets ownerFleets = PlanetWars.FleetsWithGivenOwner(thisTurnFleets, id);
                Pair<int, int> ownerShips = new Pair<int, int>(id, 0);

                // Add up fleets with the same owner
                foreach (Fleet ownerFleet in ownerFleets)
                {
                    ownerShips.Second += ownerFleet.NumShips();
                }

                // Add the ships from the planet to the corresponding fleet
                if (planetInFuture.Owner() == id)
                {
                    ownerShips.Second += planetInFuture.NumShips();
                }

                ships.Add(ownerShips);
            }

            // If the planet was neutral, it has it's own fleet
            if (planetInFuture.Owner() == 0)
            {
                ships.Add(new Pair<int, int>(0, planetInFuture.NumShips()));
            }

            BattleForPlanet(planetInFuture, ships);
        }
Ejemplo n.º 5
0
        private static void BattleForPlanet(Planet planetInFuture, List<Pair<int, int>> ships)
        {
            // Were there any fleets other than the one on the planet?
            if (ships.Count <= 1) return;
            // Sorts the fleets in descending order by the number of ships in the fleet
            ships.Sort(Pair<int, int>.CompareSecondOfPair);

            Pair<int, int> winner = ships[0];
            Pair<int, int> secondToWinner = ships[1];

            if (winner.Second == secondToWinner.Second)
            {
                //old owner stays
                planetInFuture.NumShips(0);
            }
            else
            {
                planetInFuture.Owner(winner.First);
                planetInFuture.NumShips(winner.Second - secondToWinner.Second);
            }
        }
Ejemplo n.º 6
0
		public int CanSend(Planet planet)
		{
			if (planet.Owner() != 1) return 0;

			return GetPlanetHolder(planet).CanSend();
		}
Ejemplo n.º 7
0
		public bool IsValid(Planet source, Planet dest, int numShips)
		{
			if (source.Owner() != 1)
			{
				//Logger.Log("InValid : not my planet: source = " + source + "    Move: dest = " + dest + " num = " + numShips);
				return false;
			}
			if (source.PlanetID() == dest.PlanetID())
			{
				//Logger.Log("InValid : source = dest: source = " + source + "    Move: dest = " + dest + " num = " + numShips);
				return false;
			}
			if (numShips > source.NumShips())
			{
				//Logger.Log("InValid : > numShips: source = " + source + "    Move: dest = " + dest + " num = " + numShips);
				return false;
			}
			if (numShips > CanSendByPlanets(source, dest))
			{
				//Logger.Log("InValid : > canSend: source = " + source + "    Move: dest = " + dest + " num = " + numShips + " canSend = "  +CanSend(source));
				return false;
			}
			return true;
		}
Ejemplo n.º 8
0
		//# Generates a string representation of a planet. This is used to send data
		//# about the planets to the client programs.
		public static string SerializePlanet(Planet planet)
		{
			int owner = planet.Owner();
			string message = 
				"P " + 
				string.Format("{0:R}", planet.X()) + 
				" " + 
				string.Format("{0:R}", planet.Y()) + 
				" " + 
				owner +
				" " + 
				planet.NumShips() + 
				" " + 
				planet.GrowthRate();
			return message.Replace(".0 ", " ");
		}