Пример #1
0
        private void TestLifePoints(IUnit unit)
        {
            Assert.AreEqual(unit.DefaultLifePoints, unit.LifePoints);
            Assert.IsTrue(unit.IsAlive());
            Assert.IsTrue(unit.DecreaseLifePoints());
            Assert.AreEqual(unit.DefaultLifePoints - 1, unit.LifePoints);

            // Removes life points until the unit is dead:
            while(unit.LifePoints > 0) {
                Assert.IsTrue(unit.IsAlive());
                Assert.IsTrue(unit.DecreaseLifePoints());
            }
            Assert.IsFalse(unit.IsAlive());
        }
Пример #2
0
        /// <summary>
        /// Computes a fight between the currently selected unit
        /// and the best one from the selected destination.
        /// </summary>
        /// <param name="unit">The unit who fight.</param>
        /// <returns>A constant from the CombatResult enumeration to describe the result.</returns>
        private CombatResult Combat(IUnit unit)
        {
            IUnit enemy = GetBestUnit();

            Random randCombat = new Random();
            Random rand = new Random();

            int nbRound = 3 + randCombat.Next((Math.Max(unit.LifePoints, enemy.LifePoints)) + 2);
            int n = 0;

            while(nbRound > n && unit.IsAlive() && enemy.IsAlive()) {
                double ratioLife = (double)unit.LifePoints / (double)unit.DefaultLifePoints;
                double ratioLifeDef = (double)enemy.LifePoints / (double)enemy.DefaultLifePoints;
                double attaUnit = (double)unit.Attack * (double)ratioLife;
                double defUnitdef = (double)enemy.Defense * (double)ratioLifeDef;
                double ratioAttDef = (double)(attaUnit / defUnitdef);
                double ratioChanceDef = 0;
                if(ratioAttDef > 1) {
                    // Advantage for the attacker.
                    ratioChanceDef = (1 / ratioAttDef) / 2;
                    ratioChanceDef = (0.5 - ratioChanceDef) + 0.5;
                } else if(ratioAttDef == 1) {
                    // Draw: 50% chances to win.
                    ratioChanceDef = 0.5;
                } else {
                    // Advantage for the defender.
                    ratioChanceDef = ratioAttDef / 2;
                }
                double ratioCombat = (double)((double)rand.Next(100) / 100);

                if(ratioCombat <= ratioChanceDef) {
                    enemy.DecreaseLifePoints();
                } else {
                    unit.DecreaseLifePoints();
                }
                n++;
            }

            // Computes the result of the fight:
            if(!unit.IsAlive()) {
                this.game.Map.RemoveUnit(unit, this.selectedPosition);
                this.lastMoveInfo = this.player.Name + " lost the fight.";
                this.lastCombatResult = CombatResult.LOSE;
            } else if(!enemy.IsAlive()) {
                this.game.Map.RemoveUnit(enemy, this.destination);
                this.lastMoveInfo = this.player.Name + " won the fight.";
                this.lastCombatResult = CombatResult.WIN;
            } else {
                this.lastMoveInfo = "The fight ended with a draw";
                this.lastCombatResult = CombatResult.DRAW;
            }
            return this.lastCombatResult;
        }