public void AreCountriesConnectedSuccess()
        {
            var mapTemplate = new MapTemplate("Name");
            mapTemplate.Countries.Add(new CountryTemplate("A", "A"));
            mapTemplate.Countries.Add(new CountryTemplate("B", "B"));
            mapTemplate.Countries.Add(new CountryTemplate("C", "C"));
            mapTemplate.Connections.Add(new Connection("A", "B"));

            Assert.IsTrue(mapTemplate.AreConnected("A", "B"));
            Assert.IsFalse(mapTemplate.AreConnected("B", "A"));
            Assert.IsFalse(mapTemplate.AreConnected("A", "C"));
        }
        public static Country GetCountryWithEnemyConnection(Game game, Player player, MapTemplate mapTemplate)
        {
            var enemyCountries = GetEnemyCountries(game, player);

            return player.Countries.First(
                    c => enemyCountries.Any(c2 => mapTemplate.AreConnected(c.CountryIdentifier, c2.CountryIdentifier)));
        }
 public static Country GetCountryWithFriendlyConnection(Game game, Player player, MapTemplate mapTemplate)
 {
     return player.Countries.First(
             c => player.Countries.Any(c2 => mapTemplate.AreConnected(c.CountryIdentifier, c2.CountryIdentifier)));
 }
 public static Country GetConnectedFriendlyCountry(Game game, Player player, Country origin, MapTemplate mapTemplate)
 {
     return game.Teams.Where(x => x.Players.Contains(player))
             .SelectMany(t => t.Players)
             .SelectMany(p => p.Countries).First(c => mapTemplate.AreConnected(origin.CountryIdentifier, c.CountryIdentifier));
 }
 public static Country GetConnectedEnemyCountry(Game game, Player player, Country origin, MapTemplate mapTemplate)
 {
     return GetEnemyTeams(game, player)
             .SelectMany(t => t.Players)
             .SelectMany(p => p.Countries).First(c => mapTemplate.AreConnected(origin.CountryIdentifier, c.CountryIdentifier));
 }
Example #6
0
        public void Move(
            MapTemplate mapTemplate,
            string sourceCountryIdentifier, 
            string destCountryIdentifier, 
            int numberOfUnits)
        {
            this.RequireGameActive();

            // Check connection
            if (!mapTemplate.AreConnected(sourceCountryIdentifier, destCountryIdentifier))
            {
                throw new DomainException(ErrorCode.CountriesNotConnected, "Countries are not connected");
            }

            var sourceCountry = this.Map.GetCountry(sourceCountryIdentifier);
            var destCountry = this.Map.GetCountry(destCountryIdentifier);

            if (numberOfUnits <= 0 || sourceCountry.Units - numberOfUnits < this.Options.MinUnitsPerCountry)
            {
                throw new DomainException(ErrorCode.NotEnoughUnits, "Cannot move that many units");
            }

            // Check ownership
            if (sourceCountry.TeamId != this.CurrentPlayer.TeamId)
            {
                throw new DomainException(ErrorCode.OriginCountryNotOwnedByTeam, "Can only initiate actions from countries that belong to the same team");
            }

            if (sourceCountry.TeamId != destCountry.TeamId)
            {
                throw new DomainException(ErrorCode.MoveOwnCountries, "Units can only be moved between countries that belong to the same team");
            }

            if (this.PlayState != PlayState.Move)
            {
                if (this.PlayState == PlayState.Attack)
                {
                    // Switch automatically to move and end attacking
                    this.PlayState = PlayState.Move;
                }
                else
                {
                    throw new DomainException(ErrorCode.MovingNotPossible, "Cannot move, state incorrect");
                }
            }

            // Perform move
            sourceCountry.Units -= numberOfUnits;
            destCountry.Units += numberOfUnits;

            this.GameHistory.RecordMove(
                this.CurrentPlayer,
                sourceCountry.CountryIdentifier, destCountry.CountryIdentifier, numberOfUnits);

            this.MovesInCurrentTurn++;
            if (this.MovesInCurrentTurn >= this.Options.MovesPerTurn)
            {
                this.PlayState = PlayState.Done;
            }
        }
Example #7
0
        public void Attack(
            IAttackService attackService,
            IRandomGen randomGen,
            MapTemplate mapTemplate,
            string sourceCountryIdentifier, 
            string destCountryIdentifier, 
            int numberOfUnits)
        {
            this.RequireGameActive();

            if (this.PlayState != PlayState.Attack)
            {
                throw new DomainException(ErrorCode.AttackingNotPossible, "Cannot attack, state incorrect");
            }

            var sourceCountry = this.Map.GetCountry(sourceCountryIdentifier);
            var destCountry = this.Map.GetCountry(destCountryIdentifier);

            // Check connection
            if (!mapTemplate.AreConnected(sourceCountryIdentifier, destCountryIdentifier))
            {
                throw new DomainException(ErrorCode.CountriesNotConnected, "There is no connection between those countries");
            }

            // Check ownership
            if (sourceCountry.TeamId != this.CurrentPlayer.TeamId)
            {
                throw new DomainException(ErrorCode.OriginCountryNotOwnedByTeam, "Can only initiate actions from countries that belong to the same team");
            }

            if (sourceCountry.PlayerId == destCountry.PlayerId)
            {
                throw new DomainException(ErrorCode.AttackOwnCountries, "Cannot attack own countries");
            }

            if (numberOfUnits <= 0 || sourceCountry.Units - numberOfUnits < this.Options.MinUnitsPerCountry)
            {
                throw new DomainException(ErrorCode.NotEnoughUnits, "Cannot attack with that many units");
            }

            var otherPlayer = this.GetPlayerById(destCountry.PlayerId);

            int attackerUnitsLost = 0;
            int defenderUnitsLost = 0;
            var result = attackService.Attack(numberOfUnits, destCountry.Units, out attackerUnitsLost, out defenderUnitsLost);

            if (result)
            {
                // Attack was successful
                destCountry.Units = numberOfUnits - attackerUnitsLost;

                this.Map.UpdateOwnership(otherPlayer, this.CurrentPlayer, destCountry);

                this.DistributeCard(randomGen);
            }
            else
            {
                // Attack failed
                destCountry.Units -= defenderUnitsLost;
            }

            // Reduce units in this country in any case
            sourceCountry.Units -= numberOfUnits;

            this.GameHistory.RecordAttack(
                this.CurrentPlayer, otherPlayer,
                sourceCountryIdentifier, destCountryIdentifier,
                numberOfUnits, attackerUnitsLost, defenderUnitsLost,
                result);

            // Reduce number of attacks left
            this.AttacksInCurrentTurn++;

            // Check for victory
            this.CheckForVictory(this.CurrentPlayer, otherPlayer);

            if (this.AttacksInCurrentTurn >= this.Options.AttacksPerTurn)
            {
                this.EndAttack();
            }
        }