Exemplo n.º 1
0
        bool CheckForFight(Location antLoc, GameState state, List <MyAnt> usedAnts, List <Location> usedLocs)
        {
            if (usedAnts == null)
            {
                usedAnts = new List <MyAnt>();
            }
            if (usedLocs == null)
            {
                usedLocs = new List <Location>();
            }
            List <MyAnt>    rUsedAnts = null;
            List <Location> rUsedLocs = null;

            int myPower = 0;

            for (int dir = 0; dir < 8; dir++)
            {
                var newLoc = antLoc + PathFinding.Dir8[dir];
                if (state.Map[newLoc.X, newLoc.Y] == Tile.Water)
                {
                    continue;
                }
                // if attack zone here
                if (state.GetAttackLoc(newLoc) > 0)
                {
                    // if there is ant here -> power ++
                    if (state.NextTurnPreview[newLoc.X, newLoc.Y])
                    {
                        myPower++;
                    }
                    else
                    {
                        // if not ant, find him
                        var  index   = state.MyAnts.BinarySearch(new MyAnt(newLoc));
                        bool findAnt = false;
                        if (index >= 0)
                        {
                            // if ant not moved
                            if (!state.MyAnts[index].Processed)
                            {
                                state.MyAnts[index].Processed             = true;
                                state.MyAnts[index].Move                  = false;
                                state.NextTurnPreview[newLoc.X, newLoc.Y] = true;
                                rUsedAnts = new List <MyAnt>();
                                rUsedLocs = new List <Location>();
                                if (CheckForFight(newLoc, state, rUsedAnts, rUsedLocs))
                                {
                                    myPower++;
                                    usedAnts.Add(state.MyAnts[index]);
                                    usedLocs.Add(newLoc);
                                    usedAnts.AddRange(rUsedAnts);
                                    rUsedLocs.AddRange(rUsedLocs);
                                    findAnt = true;
                                }
                                else
                                {
                                    state.MyAnts[index].Processed             = false;
                                    state.NextTurnPreview[newLoc.X, newLoc.Y] = false;
                                }
                            }
                        }
                        // try to move here neighbor ant
                        if (!findAnt)
                        {
                            for (int iDir = 0; iDir < 4; iDir++)
                            {
                                var neibLoc = newLoc + PathFinding.Directions[iDir];
                                if (state.Map[neibLoc.X, neibLoc.Y] == Tile.Water)
                                {
                                    continue;
                                }
                                index = state.MyAnts.BinarySearch(new MyAnt(neibLoc));
                                if (index >= 0 && !state.MyAnts[index].Processed)
                                {
                                    state.MyAnts[index].Processed             = true;
                                    state.MyAnts[index].Move                  = true;
                                    state.MyAnts[index].Direction             = ((Direction)iDir).Opposide();
                                    state.NextTurnPreview[newLoc.X, newLoc.Y] = true;
                                    rUsedAnts = new List <MyAnt>();
                                    rUsedLocs = new List <Location>();
                                    if (CheckForFight(newLoc, state, rUsedAnts, rUsedLocs))
                                    {
                                        myPower++;
                                        usedAnts.Add(state.MyAnts[index]);
                                        usedLocs.Add(newLoc);
                                        usedAnts.AddRange(rUsedAnts);
                                        rUsedLocs.AddRange(rUsedLocs);
                                        break;
                                    }
                                    else
                                    {
                                        state.MyAnts[index].Processed             = false;
                                        state.NextTurnPreview[newLoc.X, newLoc.Y] = false;
                                    }
                                }
                            }
                        }
                    }
                }
            }

            if (myPower < state.GetAttackLoc(antLoc))
            {
                for (int i = 0; i < usedAnts.Count; i++)
                {
                    usedAnts[i].Processed = false;
                }
                for (int i = 0; i < usedLocs.Count; i++)
                {
                    state.NextTurnPreview[usedLocs[i].X, usedLocs[i].Y] = false;
                }
                return(false);
            }

            return(true);
        }
Exemplo n.º 2
0
        bool ProcessAnt(MyAnt ant, GameState state, int depth, List <MyAnt> usedAnts, List <Location> usedLocs)
        {
            if (!ant.RightDirections[0] &&
                !ant.RightDirections[1] &&
                !ant.RightDirections[2] &&
                !ant.RightDirections[3])
            {
                int minAttack = int.MaxValue;
                int goodDir   = -1;
                for (int dir = 0; dir < 4; dir++)
                {
                    var newLoc = ant + PathFinding.Directions[dir];
                    if (state.Map[newLoc.X, newLoc.Y] != Tile.Food && state.Map[newLoc.X, newLoc.Y] != Tile.Water)
                    {
                        int index = state.MyAnts.BinarySearch(new MyAnt(newLoc));
                        if (index >= 0 && state.MyAnts[index].Processed)
                        {
                            continue;
                        }
                        if (!state.NextTurnPreview[newLoc.X, newLoc.Y])
                        {
                            int att = state.GetAttackLoc(newLoc);
                            if (att < minAttack)
                            {
                                goodDir   = dir;
                                minAttack = att;
                            }
                        }
                    }
                }
                if (minAttack != int.MaxValue)
                {
                    ant.Move      = true;
                    ant.Direction = (Direction)goodDir;
                    ant.Processed = true;
                    var newLoc = ant + PathFinding.Directions[goodDir];
                    state.NextTurnPreview[newLoc.X, newLoc.Y] = true;
                    return(true);
                }
                ant.Move      = false;
                ant.Processed = true;
                state.NextTurnPreview[ant.X, ant.Y] = true;
                return(false);
            }

            var possibleMoves = TryMove(ant, state);

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

            foreach (var md in possibleMoves)
            {
                md.Order = rnd.Next(0, 1000);
            }

            foreach (var md in possibleMoves.OrderBy(m => m.Order))
            {
                List <MyAnt>    rUsedAnts = null;
                List <Location> rUsedLocs = null;
                bool            result    = true;
                ant.Processed = true;
                state.NextTurnPreview[md.X, md.Y] = true;
                var index = state.MyAnts.BinarySearch(new MyAnt(md));
                if (index >= 0)
                {
                    if (!state.MyAnts[index].Processed)
                    {
                        rUsedAnts = new List <MyAnt>();
                        rUsedLocs = new List <Location>();
                        result    = ProcessAnt(state.MyAnts[index], state, depth + 1, rUsedAnts, rUsedLocs);
                    }
                }
                if (result)
                {
                    if (!md.Critical || ant.FightDirections[(int)md.Direction] > 1)
                    {
                        if (!md.DeathForFood || (ant.FightDirections[(int)md.Direction] > 1 && state.Players == 2))
                        {
                            if (ant.FightDirections[(int)md.Direction] > 0)
                            {
                                result = CheckForFight(md, state, null, null);
                                if (!result && rUsedAnts != null && rUsedLocs != null)
                                {
                                    for (int i = 0; i < rUsedAnts.Count; i++)
                                    {
                                        rUsedAnts[i].Processed = false;
                                    }
                                    for (int i = 0; i < rUsedLocs.Count; i++)
                                    {
                                        state.NextTurnPreview[rUsedLocs[i].X, rUsedLocs[i].Y] = false;
                                    }
                                }
                            }
                        }
                    }
                }
                if (!result)
                {
                    if (depth == 0)
                    {
                        ant.RightDirections[(int)md.Direction] = false;
                    }
                    ant.Processed = false;
                    ant.Move      = false;
                    state.NextTurnPreview[md.X, md.Y] = false;
                }
                if (result)
                {
                    ant.Processed       = true;
                    ant.Direction       = md.Direction;
                    ant.Move            = true;
                    ant.AttackEnemyHill = md.AttackEnemyHill;
                    if (usedAnts != null && usedLocs != null)
                    {
                        usedAnts.Add(ant);
                        usedLocs.Add(md);
                        if (rUsedAnts != null && rUsedLocs != null)
                        {
                            usedAnts.AddRange(rUsedAnts);
                            usedLocs.AddRange(rUsedLocs);
                        }
                    }
                    return(true);
                }
            }

            if (depth == 0)
            {
                return(ProcessAnt(ant, state, depth, null, null));
            }
            else
            {
                return(false);
            }
        }