Exemplo n.º 1
0
        private void HandleCompteurRules(WorldState newWorld, int iplayer)
        {
            var player = this.World.Players[iplayer];
            var nplayer = newWorld.Players[iplayer];

            if (player.HasCompteur)
            {
                var home = newWorld.Caddies[iplayer];
                if (nplayer.Pos == home.Pos)
                {
                    nplayer.IncreaseScore(30);
                    newWorld.Compteurs.Remove(newWorld.Compteurs[player.CompteurId]);
                    newWorld.CheckCompteur(ref nplayer);
                }
                else
                {
                    newWorld.Compteurs[player.CompteurId] = new Compteur(nplayer.Pos.X, nplayer.Pos.Y);
                    nplayer.UpdateCompteur(player.CompteurId);
                }
            }
            else
            {
                newWorld.CheckCompteur(ref nplayer);
                if (nplayer.HasCompteur)
                {
                    nplayer.IncreaseScore(1);
                }
            }
            newWorld.Players[iplayer] = nplayer;
        }
Exemplo n.º 2
0
        public WorldState ApplyAction(int iplayer, Direction direction)
        {
            Point npos = this.GetNewPos(iplayer, direction);
            var nplayers = new List<Player>(this.World.Players);
            var ncompteurs = new List<Compteur>(this.World.Compteurs);
            WorldState newWorld = WorldState.Create(this.World.Round, nplayers, ncompteurs, this.World.Caddies);

            var player = this.World.Players[iplayer];
            if (this.IsValidMove(iplayer, npos))
            {
                var nplayer = new Player(player.Id, npos.X, npos.Y, player.Score, PlayerState.Playing);
                newWorld.Players[iplayer] = nplayer;

                this.HandleCompteurRules(newWorld, iplayer);
                this.HandleBaffe(newWorld, iplayer);
            }
            else
            {
                newWorld.Players[iplayer] = new Player(iplayer, player.Pos.X, player.Pos.Y, player.Score - 5,
                                                       PlayerState.Stunned);
            }
            this.World = newWorld;

            return this.World;
        }
Exemplo n.º 3
0
 protected override Direction InnerDecide(WorldState world)
 {
     var me = world.Players[this.Id];
     if (!me.HasCompteur)
     {
         return this.GoClosestCompteur(world);
     }
     else
     {
         return this.GoHome(world);
     }
 }
Exemplo n.º 4
0
        public Direction GoClosestCompteur(WorldState world)
        {
            // utiliser un MoveTo ou on ne se preoccupe pas de prendre une baffe
            this.Distances = new DistanceMap(world, this.Id, 2, this.LastPlayerAttacked);
            this.Distances.BuildAllPath();

            var cpt = this.FindClosestCompteur(world);

            Log.Info("Player {0} is going compteur {1}", this.Id, cpt);

            var direction = this.MoveToByShortestPath(world, world.Compteurs[cpt].Pos);
            return direction;
        }
Exemplo n.º 5
0
        private int FindClosestRabbit(WorldState world)
        {
            Dictionary<int, List<Tuple<int, int>>> distances =
                new Dictionary<int, List<Tuple<int, int>>>();
            var players = world.Players;
            var compteurs = world.Compteurs;
            for (var p = 0; p < players.Count; p++)
            {
                var player = players[p];
                for (var c = 0; c < compteurs.Count; c++)
                {
                    var compteur = compteurs[c];
                    var dist = compteur.Pos.Dist(player.Pos);
                    List<Tuple<int, int>> cptList;
                    if (distances.TryGetValue(c, out cptList))
                    {
                        cptList.Add(new Tuple<int, int>(dist, p));
                    }
                    else
                    {
                        distances.Add(c, new List<Tuple<int, int>> { new Tuple<int, int>(dist, p) });
                    }
                }
            }

            int myCpt = -1;
            int minDist = int.MaxValue;

            for (int pos = 0; pos < players.Count; pos++)
            {
                foreach (var cpt in distances.Keys)
                {
                    var cptDist = distances[cpt];
                    cptDist.Sort();
                    if (cptDist[pos].Item2 == this.Id && cptDist[pos].Item1 < minDist)
                    {
                        myCpt = cpt;
                        minDist = cptDist[pos].Item1;
                    }
                }
                if (myCpt != -1)
                {
                    break;
                }
            }
            return myCpt;
        }
Exemplo n.º 6
0
        public Direction GoBaffePlayer(WorldState world, int player)
        {
            Log.Info("Player {0} is going to beat rabbit {1} (forbidden rabbit {2})", this.Id, player,
                this.LastPlayerAttacked);

            var opponent = world.Players[player].Pos;
            var baffable = opponent.Adjacents();
            Point shortest = opponent;
            int minDist = int.MaxValue;
            foreach (var pt in baffable)
            {
                var dist = this.Distances.Cost(pt);
                if (dist < minDist)
                {
                    shortest = pt;
                    minDist = dist;
                }
            }
            return this.MoveToByShortestPath(world, shortest);
        }
Exemplo n.º 7
0
        private Func<WorldState, Direction> SelectTactic(WorldState world)
        {
            var me = world.Players[this.Id];
            var cpt = this.FindClosestFreeCompteur(world);

            if (cpt == -1 ||
                me.HasCompteur && me.Pos.Dist(me.Caddy) > world.RemainingRounds || // caddy trop loin
                !me.HasCompteur && me.Pos.Dist(world.Compteurs[cpt].Pos) + me.Caddy.Dist(world.Compteurs[cpt].Pos) > world.RemainingRounds) // compteur trop loin
            {
                int closestPlayer = this.FindClosestRabbit(world);
                return (aWorld) => this.GoBaffePlayer(aWorld, closestPlayer);
            }

            if (!me.HasCompteur)
            {
                return this.GoClosestCompteur;
            }
            else
            {
                return this.GoHome;
            }
        }
Exemplo n.º 8
0
        public Direction Decide(WorldState world)
        {
            this.Distances = new DistanceMap(world, this.Id, 2, this.LastPlayerAttacked);
            this.Distances.BuildAllPath();

            var direction = this.InnerDecide(world);

            var next = world.Players[this.Id].Pos.Move(direction);
            int meId = world.Players[this.Id].Id;

            var adjacents = next.Adjacents();
            var baffedPlayers = world.Players.Where(p => p.Id != meId
                && adjacents.Any(adj => adj == p.Pos)).ToList();
            if (baffedPlayers.Count == 1)
            {
                this.LastPlayerAttacked = baffedPlayers[0].Id;
            }
            else if (baffedPlayers.Count > 0)
            {
                this.LastPlayerAttacked = null;
            }
            return direction;
        }
Exemplo n.º 9
0
 protected override Direction InnerDecide(WorldState world)
 {
     var decide = SelectTactic(world);
     return decide(world);
 }
Exemplo n.º 10
0
        private int ClosestCompteur(WorldState world, List<Tuple<int, int>>[] distances, int playerId,
            Func<Compteur, bool> compteurPredicate)
        {
            int myCpt = -1;
            int minDist = int.MaxValue;
            var len = distances[0].Count;

            for (int pos = 0; pos < len; pos++)
            {
                for (var cpt = 0; cpt < distances.Length; cpt++)
                {
                    var compteur = world.Compteurs[cpt];
                    if (compteurPredicate(compteur))
                    {
                        var cptDist = distances[cpt];
                        if (cptDist[pos].Item2 == playerId && cptDist[pos].Item1 < minDist)
                        {
                            myCpt = cpt;
                            minDist = cptDist[pos].Item1;
                        }
                    }
                }
                if (myCpt != -1)
                {
                    break;
                }
            }
            return myCpt;
        }
Exemplo n.º 11
0
 protected override Direction InnerDecide(WorldState world)
 {
     return Direction.N;
 }
Exemplo n.º 12
0
 private void HandleBaffe(WorldState newWorld, int iplayer)
 {
 }
Exemplo n.º 13
0
 public TestAi(int id, WorldState world)
     : base(id)
 {
 }
Exemplo n.º 14
0
 public Direction MoveToByShortestPath(WorldState world, Point cpos)
 {
     var direction = this.Distances.MoveTo(cpos);
     return direction.GetValueOrDefault(Direction.E);
 }
Exemplo n.º 15
0
        internal int FindClosestCompteur(WorldState world)
        {
            var myCpt = this.FindClosestFreeCompteur(world);

            if (myCpt == -1)
            {
                var players = world.Players;
                var compteurs = world.Compteurs;
                var distances = this.PlayerCompteurDistances(compteurs, players);

                myCpt = this.ClosestCompteur(world, distances, this.Id, cpt => true);
            }
            return myCpt;
        }
Exemplo n.º 16
0
        public Direction GoHome(WorldState world)
        {
            Log.Info("Player {0} is going home", this.Id);

            // utiliser un MoveTo ou on se preoccupe de prendre une baffe
            this.Distances = new DistanceMap(world, this.Id, 3, this.LastPlayerAttacked);
            this.Distances.BuildAllPath();

            var home = world.Caddies[this.Id].Pos;
            var direction = this.MoveToByShortestPath(world, home);
            return direction;
        }
Exemplo n.º 17
0
        internal int FindClosestFreeCompteur(WorldState world)
        {
            var players = world.Players;
            var compteurs = world.Compteurs;
            var distances = this.PlayerCompteurDistances(compteurs, players);

            return this.ClosestCompteur(world, distances, this.Id, cpt => !cpt.IsOwned);
        }
Exemplo n.º 18
0
 public WorldSimulator(WorldState world)
 {
     this.World = world;
 }
Exemplo n.º 19
0
        private static List<Direction> WhenMoving(
            WorldState world, Point depart, Point destination, out Point lastPosition, int cost = 0,
            int maxIteration = int.MinValue)
        {
            var distance = destination.Dist(depart);

            maxIteration = maxIteration < distance ? distance : maxIteration;

            var directions = new List<Direction>();
            int i = 0;
            lastPosition = depart;
            while (lastPosition != destination && i < maxIteration)
            {
                var map = new DistanceMap(world, 0, cost, null);
                map.BuildAllPath();
                var dir = map.MoveTo(destination);
                directions.Add(dir.Value);

                var next = lastPosition.Move(dir.Value);
                world.Players[0] = new Player(0, next.X, next.Y, 0, PlayerState.Playing);
                lastPosition = next;
                i++;
            }
            return directions;
        }
Exemplo n.º 20
0
 protected abstract Direction InnerDecide(WorldState world);
Exemplo n.º 21
0
        internal int FindClosestRabbit(WorldState world)
        {
            var players = world.Players;
            var mePos = world.Players[this.Id].Pos;

            int closestPlayer = -1;
            int minDist = int.MaxValue;
            for (var p = 0; p < players.Count; p++)
            {
                if (p != this.Id && (!this.LastPlayerAttacked.HasValue || this.LastPlayerAttacked != p))
                {
                    var player = players[p];
                    var dist = mePos.Dist(player.Pos);
                    if (dist < minDist)
                    {
                        minDist = dist;
                        closestPlayer = p;
                    }
                }
            }
            return closestPlayer;
        }
Exemplo n.º 22
0
 public static WorldState Create(int round, List<Player> players, List<Compteur> compteurs, List<Caddy> caddies)
 {
     var world = new WorldState(round, players, compteurs, caddies);
     world.Initialize();
     return world;
 }
Exemplo n.º 23
0
 protected override Direction InnerDecide(WorldState world)
 {
     return this.GoClosestCompteur(world);
 }