Example #1
0
 public Pac(int id,
            bool mine,
            IActionStrategy strategy,
            GiveWayMovementStrategy giveWayMovementStrategy
            )
 {
     _giveWayMovementStrategy = giveWayMovementStrategy;
     Id              = id;
     Mine            = mine;
     LocationHistory = new List <Location>(50);
     SpeedTurnsLeft  = 0;
     AbilityCooldown = 0;
     Key             = new PacKey(Id, mine);
     CurrentStrategy = strategy;
 }
Example #2
0
        public void Run()
        {
            while (true)
            {
                if (_cancellation.IsCancellationRequested)
                {
                    break;
                }

                try
                {
                    string[]      inputs          = _inputOutput.ReadLine().Split(' ');
                    int           myScore         = int.Parse(inputs[0]);
                    int           opponentScore   = int.Parse(inputs[1]);
                    int           visiblePacCount = int.Parse(_inputOutput.ReadLine()); // all your pacs and enemy pacs in sight
                    List <PacKey> seenKeys        = new List <PacKey>();
                    for (int i = 0; i < visiblePacCount; i++)
                    {
                        var line = _inputOutput.ReadLine();
                        Console.Error.WriteLine($"Pac line {line}");
                        inputs = line.Split(' ');
                        int    pacId           = int.Parse(inputs[0]);   // pac number (unique within a team)
                        bool   mine            = inputs[1] != "0";       // true if this pac is yours
                        short  x               = short.Parse(inputs[2]); // position in the grid
                        short  y               = short.Parse(inputs[3]); // position in the grid
                        string typeId          = inputs[4];              // unused in wood leagues
                        short  speedTurnsLeft  = short.Parse(inputs[5]); // unused in wood leagues
                        short  abilityCooldown = short.Parse(inputs[6]); // unused in wood leagues
                        var    location        = new Location(x, y);

                        Pac pac;

                        var key = new PacKey(pacId, mine);
                        seenKeys.Add(key);
                        if (!_pacs.ContainsKey(key))
                        {
                            _pacs.Add(key, new Pac(pacId, mine, _actionStrategy, new GiveWayMovementStrategy(_gameGrid)));
                        }

                        pac = _pacs[key];

                        pac.AddLocation(location);
                        pac.AbilityCooldown = abilityCooldown;
                        pac.SpeedTurnsLeft  = speedTurnsLeft;
                        pac.Type            = typeId;

                        //_gameGrid.VisiblePelletsFrom(pac.Location);
                    }

                    var deletion = _pacs.Select(p => p.Key).Where(p => !seenKeys.Contains(p));
                    foreach (var d in deletion)
                    {
                        _pacs.Remove(d);
                    }

                    int visiblePelletCount = int.Parse(_inputOutput.ReadLine()); // all pellets in sight
                    _gameGrid.SetPellets(ParsePellets(visiblePelletCount));
                    _gameGrid.SetEnemies(_pacs.Values.Where(p => !p.Mine));
                    _gameGrid.SetMyPacs(_pacs.Values.Where(p => p.Mine && p.Type != PacType.Dead));
                    //Console.Error.WriteLine(_gameGrid.ToString());

                    // Write an action using Console.WriteLine()
                    // To debug: Console.Error.WriteLine("Debug messages...");

                    //_inputOutput.WriteLine("MOVE 0 15 10"); // MOVE <pacId> <x> <y>

                    var myPacs = _pacs.Values.Where(p => p.Mine);

                    var nextActions = myPacs.Select(pac => pac.NextAction(_gameGrid, _cancellation)).ToDictionary(p => p.Pac.Key);

                    var collisions =
                        nextActions.Values.Where(n => n is MoveAction).GroupBy(g => ((MoveAction)g).Location).Where(g => g.Count() > 1);
                    foreach (var collision in collisions)
                    {
                        var giveWayer = collision.First().Pac;
                        nextActions[giveWayer.Key] = giveWayer.GiveWay(_cancellation);
                    }


                    var moves = string.Join("|", nextActions.Values);
                    _inputOutput.WriteLine(moves);
                }
                catch (OperationCanceledException)
                {
                    break;
                }
            }
        }