Пример #1
0
        public void AddUnit(CardImpl card, OrientedCell new_cell)
        {
            var command = commands [cur_command_idx];

            if (new_cell != null)
            {
                UnitImpl unit = new UnitImpl(card, cur_command_idx, new_cell);

                SetUnitPlace(new_cell, unit);

                command.staff.Add(unit);

                if (notificator != null)
                {
                    notificator.UnitAdded(unit);
                }
            }

            command.hand.Remove(card);

            command.crystals_count -= card.cost;
            if (notificator != null)
            {
                notificator.PlayerUIUpdated();
            }
        }
Пример #2
0
        private void CalculateRoute(OrientedCell from, Func <CellCandidate, bool> CheckCand, Func <CellCandidate, bool> NewCand)
        {
            var  candidats = new CandidatesQueue();           //ToDo calculate for giant
            bool end       = false;

            UnitImpl unit = from.cell.unit;

            candidats.Enqueue(new CellCandidate(from, 0, null));
            while (candidats.Count > 0 && !end)
            {
                CellCandidate cand = candidats.Dequeue();
                if (CheckCand(cand))
                {
                    List <OrientedCell> neighbor_cells = gameboard.GetOneMoveCells(cand.cell_ref, unit);
                    foreach (var cell in neighbor_cells)
                    {
                        CellCandidate cell_cand = candidats.FindCandidate(cell);
                        if (cell_cand == null || cell_cand.route_cnt > cand.route_cnt + 1)
                        {
                            var new_cand = new CellCandidate(cell, cand.route_cnt + 1, cand);
                            candidats.Enqueue(new_cand);

                            if (!NewCand(new_cand))
                            {
                                end = true;
                                break;
                            }
                        }
                    }
                }
            }
        }
Пример #3
0
            public CellCandidate FindCandidate(OrientedCell cell_ref)
            {
                CellCandidate ret = null;

                candidats_map.TryGetValue(cell_ref, out ret);
                return(ret);
            }
Пример #4
0
 public UnitImpl(Figure other, int command_idx, OrientedCell cell_ref)
     : base(other)
 {
     hp = defaultHP;
     this.command_idx   = command_idx;
     this.oriented_cell = cell_ref;
     made_move          = !other.isHaste;
 }
Пример #5
0
 public void SetUnitPlace(OrientedCell oriented_cell, UnitImpl unit)
 {
     foreach (var cell in GetOccupatedCells(oriented_cell))
     {
         cell.unit = unit;
         if (cell.notificator != null)
         {
             cell.notificator.UnitAdded(unit);
         }
     }
 }
Пример #6
0
        private void AddNeighborKills(GameboardImpl gameboard_ref, OrientedCell cell, List <Move> ret, int steps)
        {
            List <GameboardCell> neighbor_cells = gameboard_ref.GetNeighborCells(cell);

            foreach (GameboardCell neighbor_cell in neighbor_cells)
            {
                if (neighbor_cell.unit != null && neighbor_cell.unit.command_idx != command_idx)//Frendly fire
                {
                    ret.Add(new KillMoveImpl(cell.cell, cell.orientation, oriented_cell.cell, neighbor_cell, steps, false));
                }
            }
        }
Пример #7
0
        public List <CAvailableCells> CalcAvailableCells(OrientedCell from, int steps)
        {
            List <CAvailableCells> ret = new List <CAvailableCells>();

            CalculateRoute(from,
                           cand => cand.route_cnt < steps,
                           new_cand =>
            {
                ret.Add(new CAvailableCells(new_cand.cell_ref, new_cand.route_cnt));
                return(true);
            }
                           );

            return(ret);
        }
Пример #8
0
        private MoveInfo BestMoveForGoToTargetCell(List <MoveInfo> candidates, List <MoveInfo> targetMoves, int minDist, int speed)  //To do: it can return List<MoveInfo> - think about it
        {
            int[] scoreArr = new int[candidates.Count];
            for (int i = 0; i < scoreArr.Length; i++)
            {
                scoreArr[i] = 0;
            }

            foreach (MoveInfo targetMove in targetMoves)
            {
                OrientedCell targetCell = targetMove.move.GetTargetCell(targetMove.board);

                List <OrientedCell> startCells = new List <OrientedCell>();
                foreach (MoveInfo startMove in candidates)
                {
                    OrientedCell startCell = startMove.move.GetTargetCell(startMove.board);
                    startCells.Add(startCell);
                }

                DateTime t0        = DateTime.Now;
                int[]    distances = new Routing(targetMove.board).CalcRouteToMulty(targetCell, startCells, null);
                calcRouteToMultyCount++;
                calcRouteToMulty += (DateTime.Now - t0).TotalMilliseconds;

                for (int i = 0; i < scoreArr.Length; i++)
                {
                    int distance = (int)Math.Ceiling(distances[i] / (double)speed);
                    if (distance == minDist)
                    {
                        scoreArr[i]++;
                    }
                }
            }

            MoveInfo res       = null;      //To do: it can return List<MoveInfo> - think about it
            int      bestScore = -1;

            for (int i = 0; i < scoreArr.Length; i++)
            {
                if (scoreArr[i] > bestScore)
                {
                    bestScore = scoreArr[i];
                    res       = candidates[i];
                }
            }

            return(res);
        }
Пример #9
0
        public List <OrientedCell> CalcAvailableCellsForCard(bool is_giant)       //ToDo do it quicker //ToDo calculate for giant
        {
            List <OrientedCell> ret = new List <OrientedCell>();

            Func <int, int> y_st = x => (cur_command_idx == 0) ? 0 : cells.GetLength(1) - 2 - (x % 2);
            int             y_en = (cur_command_idx == 0) ? 2 : cells.GetLength(1);

            for (int x = 0; x < cells.GetLength(0); ++x)
            {
                for (int y = y_st(x); y < y_en; ++y)
                {
                    if (cells[x, y].active && cells[x, y].unit == null)
                    {
                        ret.Add(new OrientedCell {
                            cell = cells[x, y]
                        });
                    }
                }
            }

            if (is_giant)
            {
                List <OrientedCell> giant_cells = new List <OrientedCell>();
                foreach (var cell in ret)
                {
                    for (int or = 1; or <= 3; ++or)
                    {
                        OrientedCell giant_cell = new OrientedCell {
                            cell        = cell.cell,
                            orientation = (OrientedCell.CellOrientation)or
                        };

                        var occu = GetOccupatedCells(giant_cell);

                        if (Array.TrueForAll(occu, oc_cell => oc_cell != null && oc_cell.active && oc_cell.unit == null &&
                                             ret.Contains(new OrientedCell {
                            cell = oc_cell
                        })))
                        {
                            giant_cells.Add(giant_cell);
                        }
                    }
                }
                ret = giant_cells;
            }

            return(ret);
        }
Пример #10
0
        public int[] CalcRouteToMulty(OrientedCell from, List <OrientedCell> to, List <OrientedCell>[] routes) //ToDo calculate for giant
        {
            var to_idx = new Dictionary <OrientedCell, int>();                                                 // to[i] -> i

            for (int i = 0; i < to.Count; ++i)
            {
                to_idx[to[i]] = i;
            }

            int[] ret = new int[to.Count];

            CalculateRoute(from,
                           _ => true,
                           new_cand =>
            {
                int idx;
                if (to_idx.TryGetValue(new_cand.cell_ref, out idx))
                {
                    ret[idx] = new_cand.route_cnt;
                    if (routes != null)
                    {
                        routes[idx] = new List <OrientedCell>();

                        routes[idx].Add(new_cand.cell_ref);

                        while (new_cand.prev_cand != null)
                        {
                            routes[idx].Add(new_cand.prev_cand.cell_ref);
                            new_cand = new_cand.prev_cand;
                        }
                        routes[idx].Reverse();
                    }

                    to_idx.Remove(new_cand.cell_ref);

                    if (to_idx.Count == 0)
                    {
                        return(false);
                    }
                }
                return(true);
            }
                           );

            return(ret);
        }
Пример #11
0
        public List <OrientedCell> GetOneMoveCells(OrientedCell cell, UnitImpl excl_unit)       //ToDo it could be optimized
        {
            List <OrientedCell> ret = null;

            if (cell.orientation == OrientedCell.CellOrientation.Default)
            {
                ret = new List <OrientedCell> (6);
                var neigh = GetNeighborCells(cell.cell);
                foreach (var neigh_cell in neigh)
                {
                    ret.Add(new OrientedCell {
                        cell = neigh_cell
                    });
                }
            }
            else
            {
                ret = new List <OrientedCell> (4);

                GameboardCell[] oc_cell = GetOccupatedCells(cell);

                Func <int, int> p = x => (x > 1 ? x - 1 : 3);
                Func <int, int> n = x => (x < 3 ? x + 1 : 1);

                int o = (int)cell.orientation;

                ret.Add(new OrientedCell {
                    cell = oc_cell[n(o)], orientation = (OrientedCell.CellOrientation)n(o)
                });
                ret.Add(new OrientedCell {
                    cell = oc_cell[n(o)], orientation = (OrientedCell.CellOrientation)p(o)
                });
                ret.Add(new OrientedCell {
                    cell = oc_cell[p(o)], orientation = (OrientedCell.CellOrientation)n(o)
                });
                ret.Add(new OrientedCell {
                    cell = oc_cell[o], orientation = (OrientedCell.CellOrientation)p(o)
                });
            }

            ret.RemoveAll(or_cell => or_cell.cell == null ||
                          Array.Exists(GetOccupatedCells(or_cell), oc_cell => oc_cell == null ||
                                       (oc_cell.unit != null && oc_cell.unit != excl_unit)));
            return(ret);
        }
Пример #12
0
        public List <GameboardCell> GetNeighborCells(OrientedCell cell)       //ToDo it could be optimized
        {
            List <GameboardCell> ret = new List <GameboardCell>(6);

            var occupated = GetOccupatedCells(cell);

            foreach (var occu_cell in occupated)
            {
                var neigh = GetNeighborCells(occu_cell);
                foreach (var neigh_cell in neigh)
                {
                    if (!ret.Contains(neigh_cell) && Array.IndexOf(occupated, neigh_cell) == -1)
                    {
                        ret.Add(neigh_cell);
                    }
                }
            }
            return(ret);
        }
Пример #13
0
        public GameboardCell[] GetOccupatedCells(OrientedCell cell)
        {
            GameboardCell[] ret = null;
            if (cell != null)
            {
                if (cell.orientation == OrientedCell.CellOrientation.Default)
                {
                    ret = new GameboardCell[1] {
                        cell.cell
                    };
                }
                else
                {
                    ret     = new GameboardCell[4];
                    ret [1] = cell.cell;

                    ret [0] = GetNearCell(cell.cell, 0);

                    switch (cell.orientation)
                    {
                    case OrientedCell.CellOrientation.EastNorth:
                        ret [2] = GetNearCell(cell.cell, 4);
                        ret [3] = GetNearCell(cell.cell, 5);
                        break;

                    case OrientedCell.CellOrientation.EastSouth:
                        ret [2] = GetNearCell(cell.cell, 1);
                        ret [3] = GetNearCell(cell.cell, 2);
                        break;

                    case OrientedCell.CellOrientation.East:
                        ret [2] = GetNearCell(cell.cell, 1);
                        ret [3] = GetNearCell(cell.cell, 5);
                        break;
                    }
                }
            }
            return(ret);
        }
Пример #14
0
 public CAvailableCells(OrientedCell cell, int steps)
 {
     this.cell = cell.cell; this.orientation = cell.orientation; this.steps = steps;
 }
Пример #15
0
 public CellCandidate(OrientedCell cell_ref, int route_cnt, CellCandidate prev_cand)
 {
     this.cell_ref  = cell_ref;
     this.route_cnt = route_cnt;
     this.prev_cand = prev_cand;
 }