Exemplo n.º 1
0
        public void ProcessMove(PlayerSide player, VectorTwo position, int direction)
        {
            if (!validator.IsLegalMove(player, position, direction))
            {
                return;
            }
            Troop troop = troopMap.Get(position);

            MoveTroop(position, direction);
            if (!board.IsInside(troop.Position))
            {
                ControlWithAi(troop);
            }

            while (!GameHasEnded())
            {
                if (movePointsLeft == 0)
                {
                    ToggleActivePlayer();
                }
                else
                {
                    return;
                }
            }
            OnGameEnded();
        }
Exemplo n.º 2
0
        public int GetOptimalDirection(Troop troop)
        {
            VectorTwo target = board.Center;

            int minDist = 1000000;
            int minDir  = 0;

            for (int dir = -1; dir <= 1; dir += 2)
            {
                int       direction = (6 + dir + troop.Orientation) % 6;
                VectorTwo neigh     = Hex.GetAdjacentHex(troop.Position, direction);
                if (troopMap.Get(neigh) != null)
                {
                    continue;
                }

                int dist = (target - neigh).SqrMagnitude;
                if (dist < minDist)
                {
                    minDist = dist;
                    minDir  = dir;
                }
            }
            return(minDir);
        }
Exemplo n.º 3
0
        public BattleResult GetFightResult(Troop defender, VectorTwo attackerPosition)
        {
            bool defenderDamaged = Random.Next(0, 6) < 3;
            bool attackerDamaged = defender.InControlZone(attackerPosition) && Random.Next(0, 6) < 3;

            return(new BattleResult(defenderDamaged, attackerDamaged));
        }
Exemplo n.º 4
0
        public Board(int xMax, int yMax)
        {
            this.XMax = xMax;
            this.YMax = yMax;

            Center = new VectorTwo(xMax / 2, yMax / 2);
        }
Exemplo n.º 5
0
 private void PositionContainsTroop(VectorTwo position)
 {
     if (map.Get(position) == null)
     {
         throw new IllegalMoveException("No troop at the specified hex!");
     }
 }
Exemplo n.º 6
0
        // TODO: Don't return cells outside the board
        private VectorTwo GetEmptyCell(VectorTwo seedPosition)
        {
            if (Get(seedPosition) == null)
            {
                return(seedPosition);
            }

            Queue <VectorTwo> q = new Queue <VectorTwo>();

            q.Enqueue(seedPosition);
            while (q.Count > 0)
            {
                VectorTwo position = q.Dequeue();
                if (Get(position) == null)
                {
                    return(position);
                }
                VectorTwo[] neighbours = Hex.GetNeighbours(seedPosition);
                foreach (VectorTwo neigh in neighbours)
                {
                    if (board.IsInside(neigh))
                    {
                        q.Enqueue(neigh);
                    }
                }
            }
            throw new Exception("Couldn't find an empty cell");
        }
Exemplo n.º 7
0
 private void DestroyTroop(Troop troop, VectorTwo startingPosition)
 {
     troopMap.Remove(troop, startingPosition);
     if (troop.Player == activePlayer)
     {
         movePointsLeft -= troop.MovePoints;
     }
 }
Exemplo n.º 8
0
        private Task MoveTroop(int fromClient, Packet packet)
        {
            VectorTwo position  = packet.ReadVector2Int();
            int       direction = packet.ReadInt();

            gameHandler.MoveTroop(fromClient, position, direction);
            return(Task.CompletedTask);
        }
Exemplo n.º 9
0
 public Troop(PlayerSide player, int movePoints, VectorTwo position, int orientation, int health)
 {
     Player            = player;
     InitialMovePoints = movePoints;
     MovePoints        = movePoints;
     Position          = position;
     Orientation       = orientation;
     Health            = health;
 }
Exemplo n.º 10
0
        public override bool Equals(object obj)
        {
            if (obj == null || GetType() != obj.GetType())
            {
                return(false);
            }
            VectorTwo v = (VectorTwo)obj;

            return((X == v.X) && (Y == v.Y));
        }
Exemplo n.º 11
0
        public void MoveTroop(int client, VectorTwo position, int direction)
        {
            if (direction < -1 || direction > 1)
            {
                Console.WriteLine($"Client {client} sent a move with illegal direction!");
                return;
            }
            Game game = clientToGame[client];

            game.MakeMove(client, position, direction);
        }
Exemplo n.º 12
0
 public Troop Get(VectorTwo position)
 {
     try
     {
         return(map[position]);
     }
     catch (KeyNotFoundException)
     {
         return(null);
     }
 }
Exemplo n.º 13
0
        private void AddTroopToRound(int round, VectorTwo position, PlayerSide player)
        {
            Troop troop = player == PlayerSide.Red ? TroopFactory.Red(position) : TroopFactory.Blue(position);

            try
            {
                troopsForRound[round].Add(troop);
            }
            catch (KeyNotFoundException)
            {
                troopsForRound[round] = new List <Troop>();
                troopsForRound[round].Add(troop);
            }
        }
Exemplo n.º 14
0
        private void ThrowIfNotBlocked(Troop troop, VectorTwo cell)
        {
            if (!area.IsInside(cell))
            {
                return;
            }

            Troop encounter = map.Get(cell);

            if (encounter == null || encounter.Player != troop.Player)
            {
                throw new IllegalMoveException("Attempting to enter a cell with friendly troop!");
            }
        }
Exemplo n.º 15
0
        private void NotEnteringFriendOrBlocked(Troop troop, int direction)
        {
            VectorTwo targetPosition = Hex.GetAdjacentHex(troop.Position, troop.Orientation + direction);
            Troop     encounter      = map.Get(targetPosition);

            if (encounter == null || encounter.Player != troop.Player)
            {
                return;
            }

            // Tries to enter a friend so throw if has some other legal move
            foreach (VectorTwo cell in troop.ControlZone)
            {
                ThrowIfNotBlocked(troop, cell);
            }
        }
Exemplo n.º 16
0
        private void ApplyDamage(Troop troop, VectorTwo startingPosition)
        {
            PlayerSide opponent = troop.Player.Opponent();

            score.Increment(opponent);

            if (troop.Player == activePlayer && troop.MovePoints > 0)
            {
                movePointsLeft--;
            }

            troop.ApplyDamage();
            if (troop.Health <= 0)
            {
                DestroyTroop(troop, startingPosition);
            }
        }
Exemplo n.º 17
0
        public bool IsLegalMove(PlayerSide player, VectorTwo position, int direction)
        {
            try
            {
                IsPlayersTurn(player);
                PositionContainsTroop(position);
                Troop troop = map.Get(position);
                PlayerControlsTroop(player, troop);
                TroopHasMovePoints(troop);
                NotEnteringFriendOrBlocked(troop, direction);

                message = "Move is valid.";
                return(true);
            }
            catch (IllegalMoveException ex)
            {
                message = ex.Message;
                Console.WriteLine(message);
                Console.WriteLine($"Pos: {position}, dir: {direction}");
                return(false);
            }
        }
 public BattleResult GetFightResult(Troop defender, VectorTwo attackerPosition)
 {
     return(new BattleResult(true, true));
 }
Exemplo n.º 19
0
 public bool IsInside(VectorTwo p)
 {
     return(p.X >= 0 && p.X <= XMax && p.Y >= 0 && p.Y <= YMax);
 }
Exemplo n.º 20
0
 public void AdjustPosition(Troop troop, VectorTwo startingPosition)
 {
     map.Remove(startingPosition);
     map.Add(troop.Position, troop);
 }
Exemplo n.º 21
0
 public void Write(VectorTwo value)
 {
     Write(value.X);
     Write(value.Y);
 }
Exemplo n.º 22
0
        private void MoveTroop(VectorTwo position, int direction)
        {
            movePointsLeft--;

            Troop     troop            = troopMap.Get(position);
            VectorTwo startingPosition = troop.Position;

            troop.MoveInDirection(direction);

            List <BattleResult> battleResults = new List <BattleResult>();
            Troop encounter = troopMap.Get(troop.Position);

            if (encounter == null)
            {
                troopMap.AdjustPosition(troop, startingPosition);
                OnTroopMoved(position, direction, battleResults);
                return;
            }

            BattleResult result = BattleResult.FriendlyCollision;

            if (encounter.Player != troop.Player)
            {
                result = battleResolver.GetFightResult(encounter, startingPosition);
            }

            battleResults.Add(result);
            if (result.AttackerDamaged)
            {
                ApplyDamage(troop, startingPosition);
            }
            if (result.DefenderDamaged)
            {
                ApplyDamage(encounter, encounter.Position);
            }

            troop.FlyOverOtherTroop();

            while ((encounter = troopMap.Get(troop.Position)) != null && troop.Health > 0)
            {
                result = battleResolver.GetCollisionResult();
                battleResults.Add(result);
                if (result.AttackerDamaged)
                {
                    ApplyDamage(troop, startingPosition);
                }
                if (result.DefenderDamaged)
                {
                    ApplyDamage(encounter, encounter.Position);
                }

                troop.FlyOverOtherTroop();
            }

            if (troop.Health > 0)
            {
                troopMap.AdjustPosition(troop, startingPosition);
            }

            OnTroopMoved(position, direction, battleResults);
        }
Exemplo n.º 23
0
 private void OnTroopMoved(VectorTwo position, int direction, List <BattleResult> battleResults)
 {
     TroopMoved?.Invoke(this, new TroopMovedEventArgs(position, direction, battleResults));
 }
Exemplo n.º 24
0
 public static Troop Blue(VectorTwo position, int orientation)
 {
     return(new Troop(PlayerSide.Blue, 5, position, orientation, 2));
 }
Exemplo n.º 25
0
 public static Troop Red(VectorTwo position)
 {
     return(new Troop(PlayerSide.Red, 5, position, 3, 2));
 }
Exemplo n.º 26
0
 public TroopMovedEventArgs(VectorTwo position, int direction, List <BattleResult> battleResults)
 {
     Position      = position;
     Direction     = direction;
     BattleResults = battleResults;
 }
Exemplo n.º 27
0
 public void Remove(Troop troop, VectorTwo startingPosition)
 {
     map.Remove(startingPosition);
     GetTroops(troop.Player).Remove(troop);
 }
Exemplo n.º 28
0
 public bool InControlZone(VectorTwo position)
 {
     return(ControlZone.Any(cell => cell == position));
 }
Exemplo n.º 29
0
        public void MakeMove(int client, VectorTwo position, int direction)
        {
            PlayerSide player = GetColor(client);

            controller.ProcessMove(player, position, direction);
        }