コード例 #1
0
        public void RemoveUnit(SPoint position)
        {
            ICell unitCell = GetCell(position);

            unitCell.Unit = null;
            MapUpdated?.Invoke(this, EventArgs.Empty);
        }
コード例 #2
0
        public async Task Connect(Guid userId, Guid connectionId)
        {
            EnsureGameIsNotStarted();

            if (!_playerListEditor.Contains(userId))
            {
                var player       = _playerListEditor.Add(userId);
                var playerJoined = new PlayerJoined(_state.RoomId, player);
                if (_playerListEditor.Count == 1)
                {
                    _mapEditor.Generate();
                }
                if (!_mapEditor.CanPlacePlanet)
                {
                    var options = new GameOptionsData
                    {
                        MapWidth            = _mapEditor.MapWidth + 1,
                        MapHeight           = _mapEditor.MapHeigth + 1,
                        NeutralPlanetsCount = _mapEditor.NeutralPlanetsCount
                    };
                    _mapEditor.Generate(options);
                }
                _mapEditor.PlacePlanet(userId);
                var mapUpdated = new MapUpdated(_state.RoomId, _state.MapEditor.Map);
                await _hub.NotifyEverybodyExcept(userId, playerJoined, mapUpdated);
            }
            await _hub.Connect(userId, connectionId);
        }
コード例 #3
0
        public Boolean SetUnit(IPositionable unit, SPoint point)
        {
            //TODO: Check cell on free space
            ICell cell = GetCell(point);

            cell.Unit = unit;
            MapUpdated?.Invoke(this, EventArgs.Empty);
            return(true);
        }
コード例 #4
0
        public async Task UpdateGameOptions(Guid userId, GameOptionsData options)
        {
            EnsureUserIsOnline(userId);
            EnsureGameIsNotStarted();

            _mapEditor.Generate(options);
            var mapUpdated = new MapUpdated(_state.RoomId, _state.MapEditor.Map);
            await _hub.NotifyEverybody(mapUpdated);
        }
コード例 #5
0
        public void ChangeMap(IAreaMap newMap, Point playerPosition)
        {
            log.Debug("Changing map");
            Map = newMap;
            Map.AddObject(playerPosition, Player);
            PlayerPosition = playerPosition;

            MapUpdated?.Invoke(this, EventArgs.Empty);
        }
コード例 #6
0
 private async Task OnMapUpdated(MapUpdated mapUpdated, CancellationToken ct)
 {
     var action = GetAction(mapUpdated);
     await _client.SendAsync(
         new RegisterMove(mapUpdated.ReceivingPlayerId)
     {
         GameId    = mapUpdated.GameId,
         GameTick  = mapUpdated.GameTick,
         Direction = action.ToString().ToUpper()
     }, ct);
 }
コード例 #7
0
        public void Move(IMovable unit, SPoint oldPosition, SPoint newPosition)
        {
            ICell oldCell        = GetCell(oldPosition);
            ICell newCell        = GetCell(newPosition);
            Int32 transferEnergy = GetPenalty(oldPosition);

            oldCell.Unit       = null;
            newCell.Unit       = unit;
            unit.MovingEnergy -= transferEnergy;
            unit.SetPosition(newPosition);
            MapUpdated?.Invoke(this, EventArgs.Empty);
        }
コード例 #8
0
        public override Action GetAction(MapUpdated mapUpdated)
        {
            Map      = mapUpdated.Map;
            PlayerId = mapUpdated.ReceivingPlayerId;
            MapUtils = new MapUtils(Map);
            UpdatePointsDictionary();

            if (currentActionSequence is null || !currentActionSequence.MoveNext())
            {
                currentActionSequence = GetActionSequence().GetEnumerator();
                currentActionSequence.MoveNext();
            }

            return(currentActionSequence.Current);
        }
コード例 #9
0
        public override Action GetAction(MapUpdated mapUpdated)
        {
            _mapUtils = new MapUtils(mapUpdated.Map);

            var myCharacter     = _mapUtils.GetCharacterInfoFor(mapUpdated.ReceivingPlayerId);
            var myCoordinate    = _mapUtils.GetCoordinateFrom(myCharacter.Position);
            var myColouredTiles = _mapUtils.GetCoordinatesFrom(myCharacter.ColouredPositions);


            if (myCharacter.CarryingPowerUp)
            {
                return(Action.Explode);
            }

            var coordinateLeft = myCoordinate.MoveIn(Action.Left);

            if (!myColouredTiles.Contains(coordinateLeft) &&
                _mapUtils.CanPlayerPerformAction(myCharacter.Id, Action.Left))
            {
                return(Action.Left);
            }

            var coordinateRight = myCoordinate.MoveIn(Action.Right);

            if (!myColouredTiles.Contains(coordinateRight) &&
                _mapUtils.CanPlayerPerformAction(myCharacter.Id, Action.Right))
            {
                return(Action.Right);
            }

            var coordinateUp = myCoordinate.MoveIn(Action.Up);

            if (!myColouredTiles.Contains(coordinateUp) &&
                _mapUtils.CanPlayerPerformAction(myCharacter.Id, Action.Up))
            {
                return(Action.Up);
            }

            var coordinateDown = myCoordinate.MoveIn(Action.Down);

            if (!myColouredTiles.Contains(coordinateDown) &&
                _mapUtils.CanPlayerPerformAction(myCharacter.Id, Action.Down))
            {
                return(Action.Down);
            }

            return(Action.Stay);
        }
コード例 #10
0
ファイル: Paintbot.cs プロジェクト: 89netraM/paintbot
 private Task HandleResponseAsync(Response response, CancellationToken ct)
 {
     return(response switch
     {
         PlayerRegistered playerRegistered => OnPlayerRegistered(playerRegistered, ct),
         MapUpdated mapUpdated => OnMapUpdated(mapUpdated, ct),
         GameLink gameLink => OnGameLink(gameLink),
         GameStarting gameStarting => OnGameStarting(gameStarting),
         GameResult gameResult => OnInfoEvent(gameResult),
         CharacterStunned characterStunned => OnInfoEvent(characterStunned),
         HeartBeatResponse heartBeatResponse => OnHearBeatEvent(heartBeatResponse, ct),
         GameEnded gameEnded => OnGameEnded(gameEnded),
         TournamentEnded tournamentEnded => OnTournamentEnded(tournamentEnded),
         InvalidPlayerName invalidPlayerName => OnInfoEvent(invalidPlayerName),
         _ => Task.CompletedTask
     });
コード例 #11
0
        public override Action GetAction(MapUpdated mapUpdated)
        {
            _mapUtils = new MapUtils(mapUpdated.Map); // Keep this

            // Implement your bot here!

            //1. Find Closest powerup
            //2. move to closest powerup
            //3. Move three more steps.
            //



            // The following is a simple example bot. It tries to
            // 1. Explode PowerUp
            // 2. Move to a tile that it is not currently owning
            // 3. Move in the direction where it can move for the longest time.

            var directions = new List <Action> {
                Action.Down, Action.Right, Action.Left, Action.Up
            };
            var playerInfos     = mapUpdated.Map.CharacterInfos;
            var myCharacter     = _mapUtils.GetCharacterInfoFor(mapUpdated.ReceivingPlayerId);
            var myCoordinate    = _mapUtils.GetCoordinateFrom(myCharacter.Position);
            var myColouredTiles = _mapUtils.GetCoordinatesFrom(myCharacter.ColouredPositions);
            var Pits            = _mapUtils.GetObstacleCoordinates();

            var powerUpCoordinates       = _mapUtils.GetPowerUpCoordinates();
            var prevDistancePow          = 999999;
            var distanceToClosestPowerUp = 999;

            var validActionsThatPaintsNotOwnedTile = directions.Where(dir =>
                                                                      !myColouredTiles.Contains(myCoordinate.MoveIn(dir)) && _mapUtils.IsMovementPossibleTo(myCoordinate.MoveIn(dir))).ToList();


            var distanceToClosestPlayerWithPowerUp    = 999;
            var closestPlayerWithoutPowerUp           = new MapCoordinate(999, 999);
            var closestPlayerWithPowerUp              = GetClosestPlayerWithPowerUp(myCoordinate, myCharacter, playerInfos);
            var distanceToclosestPlayerWithoutPowerUp = myCoordinate.GetManhattanDistanceTo(closestPlayerWithoutPowerUp);

            distanceToClosestPlayerWithPowerUp = myCoordinate.GetManhattanDistanceTo(closestPlayerWithPowerUp);

            var closestPowerUp = new MapCoordinate(999, 999);



            if (distanceToClosestPlayerWithPowerUp < 6 || distanceToclosestPlayerWithoutPowerUp < 2)
            {
                // Debug.WriteLine("Danger:" + distanceToClosestPlayerWithPowerUp);
                var BestAction             = Action.Up;
                var previousActionDistance = distanceToClosestPlayerWithPowerUp;

                validActionsThatPaintsNotOwnedTile = directions.Where(dir =>
                                                                      !Pits.Contains(myCoordinate.MoveIn(dir)) && _mapUtils.IsMovementPossibleTo(myCoordinate.MoveIn(dir))).ToList();

                foreach (var Action in validActionsThatPaintsNotOwnedTile)
                {
                    var TestCord       = myCoordinate.MoveIn(Action);
                    var ActionDistance = TestCord.GetManhattanDistanceTo(closestPlayerWithPowerUp);
                    if (_mapUtils.GetTileAt(TestCord) == Tile.Obstacle || _mapUtils.GetTileAt(TestCord) == Tile.Character)
                    {
                        if (Action == Action.Up)
                        {
                            return(BestAction = Action.Down);
                        }
                        if (Action == Action.Down)
                        {
                            return(BestAction = Action.Up);
                        }
                        if (Action == Action.Left)
                        {
                            return(BestAction = Action.Right);
                        }
                        if (Action == Action.Right)
                        {
                            return(BestAction = Action.Left);
                        }
                        continue;
                    }

                    if (ActionDistance > 5)
                    {
                        if (ActionDistance > previousActionDistance)
                        {
                            previousActionDistance = TestCord.GetManhattanDistanceTo(closestPlayerWithPowerUp);
                            BestAction             = Action;
                        }
                    }
                }
                // Debug.WriteLine("Safe:" + distanceToClosestPlayerWithPowerUp);
                return(BestAction);
            }


            if (myCharacter.CarryingPowerUp)
            {
                var closestPlayer = GetClosestPlayerWithoutPowerUp(myCoordinate, myCharacter, playerInfos);
                var distanceToClosestPlayerNoPowerUp = myCoordinate.GetManhattanDistanceTo(closestPlayer);

                closestPowerUp           = getClosestPowerUp(myCoordinate);
                distanceToClosestPowerUp = myCoordinate.GetManhattanDistanceTo(closestPowerUp);


                if (distanceToClosestPowerUp < 5 || distanceToClosestPlayerNoPowerUp < 5)
                {
                    return(Action.Explode);
                }
                else
                {
                    var BestActionPlayer = Action.Down;


                    validActionsThatPaintsNotOwnedTile = directions.Where(dir =>
                                                                          !Pits.Contains(myCoordinate.MoveIn(dir)) && _mapUtils.IsMovementPossibleTo(myCoordinate.MoveIn(dir))).ToList();


                    var previousActionDistancePlayer = 9999;

                    foreach (var Action in validActionsThatPaintsNotOwnedTile)
                    {
                        var TestCord = myCoordinate.MoveIn(Action);
                        if (_mapUtils.GetTileAt(TestCord) == Tile.Obstacle || _mapUtils.GetTileAt(TestCord) == Tile.Character)
                        {
                            continue;
                        }
                        var distanceToPlayer = TestCord.GetManhattanDistanceTo(closestPlayerWithoutPowerUp);

                        if (distanceToClosestPlayerNoPowerUp <= distanceToClosestPowerUp)
                        {
                            if (TestCord.GetManhattanDistanceTo(closestPlayer) < distanceToClosestPlayerNoPowerUp)
                            {
                                if (TestCord.GetManhattanDistanceTo(closestPlayer) < previousActionDistancePlayer)
                                {
                                    previousActionDistancePlayer = TestCord.GetManhattanDistanceTo(closestPlayer);
                                    BestActionPlayer             = Action;
                                }
                            }
                        }
                        else
                        {
                            if (TestCord.GetManhattanDistanceTo(closestPowerUp) < distanceToClosestPowerUp)
                            {
                                if (TestCord.GetManhattanDistanceTo(closestPowerUp) < previousActionDistancePlayer)
                                {
                                    previousActionDistancePlayer = TestCord.GetManhattanDistanceTo(closestPowerUp);
                                    BestActionPlayer             = Action;
                                }
                            }
                        }
                    }
                    return(BestActionPlayer);
                }
            }
            else
            {
                closestPowerUp           = getClosestPowerUp(myCoordinate);
                distanceToClosestPowerUp = myCoordinate.GetManhattanDistanceTo(closestPowerUp);

                if (powerUpCoordinates.Length > 0 || distanceToClosestPowerUp < 30)
                {
                    var ClosestPit           = GetClosestPit(myCoordinate, Pits);
                    var distanceToClosestPit = myCoordinate.GetManhattanDistanceTo(ClosestPit);
                    validActionsThatPaintsNotOwnedTile = directions.Where(dir =>
                                                                          !Pits.Contains(myCoordinate.MoveIn(dir)) && _mapUtils.IsMovementPossibleTo(myCoordinate.MoveIn(dir))).ToList();

                    if (distanceToClosestPit < 1)
                    {
                        validActionsThatPaintsNotOwnedTile = directions.Where(dir =>
                                                                              !Pits.Contains(myCoordinate.MoveIn(dir)) && !myColouredTiles.Contains(myCoordinate.MoveIn(dir)) && _mapUtils.IsMovementPossibleTo(myCoordinate.MoveIn(dir))).ToList();
                    }


                    var BestAction             = Action.Right;
                    var previousActionDistance = 9999;
                    foreach (var Action in validActionsThatPaintsNotOwnedTile)
                    {
                        var TestCord = myCoordinate.MoveIn(Action);


                        var TestDistance = TestCord.GetManhattanDistanceTo(closestPowerUp);

                        if (TestDistance < previousActionDistance)
                        {
                            previousActionDistance = TestDistance;
                            Debug.WriteLine(previousActionDistance);
                            BestAction = Action;
                        }
                    }
                    if (distanceToclosestPlayerWithoutPowerUp <= 2 && distanceToClosestPowerUp < 2)
                    {
                        if (BestAction == Action.Up)
                        {
                            return(Action.Down);
                        }
                        if (BestAction == Action.Down)
                        {
                            return(Action.Up);
                        }
                        if (BestAction == Action.Left)
                        {
                            return(Action.Right);
                        }
                        if (BestAction == Action.Right)
                        {
                            return(Action.Left);
                        }
                    }
                    if (_mapUtils.GetTileAt(myCoordinate.MoveIn(BestAction)) == Tile.Obstacle /* || _mapUtils.GetTileAt(TestCord) == Tile.Character*/)
                    {
                        if (BestAction == Action.Up)
                        {
                            return(Action.Right);
                        }
                        if (BestAction == Action.Down)
                        {
                            return(Action.Left);
                        }
                        if (BestAction == Action.Left)
                        {
                            return(Action.Up);
                        }
                        if (BestAction == Action.Right)
                        {
                            return(Action.Down);
                        }
                    }

                    return(BestAction);
                }
                else
                {
                    validActionsThatPaintsNotOwnedTile = directions.Where(dir =>
                                                                          !myColouredTiles.Contains(myCoordinate.MoveIn(dir)) && !Pits.Contains(myCoordinate.MoveIn(dir)) && _mapUtils.IsMovementPossibleTo(myCoordinate.MoveIn(dir))).ToList();


                    var BestAction       = Action.Left;
                    var distanceToCenter = 9999;
                    foreach (var Action in validActionsThatPaintsNotOwnedTile)
                    {
                        var TestCord = myCoordinate.MoveIn(Action);

                        //if (_mapUtils.GetTileAt(TestCord) == Tile.Obstacle || _mapUtils.GetTileAt(TestCord) == Tile.Character)
                        //{
                        //    if (Action == Action.Up)
                        //    {
                        //        return BestAction = Action.Down;
                        //    }
                        //    if (Action == Action.Down)
                        //    {
                        //        return BestAction = Action.Up;
                        //    }
                        //    if (Action == Action.Left)
                        //    {
                        //        return BestAction = Action.Right;
                        //    }
                        //    if (Action == Action.Right)
                        //    {
                        //        return BestAction = Action.Left;
                        //    }
                        //    continue;
                        //}
                        var TestDistance = TestCord.GetManhattanDistanceTo(new MapCoordinate(mapUpdated.Map.Width / 2, mapUpdated.Map.Height / 2));
                        if (TestDistance <= 3)
                        {
                            if (validActionsThatPaintsNotOwnedTile.Any())
                            {
                                return(validActionsThatPaintsNotOwnedTile.First());
                            }
                        }
                        if (TestDistance < distanceToCenter)
                        {
                            if (TestDistance < distanceToCenter)
                            {
                                distanceToCenter = TestDistance;

                                BestAction = Action;
                            }
                        }
                    }
                    return(BestAction);
                }
            }
        }
コード例 #12
0
ファイル: Paintbot.cs プロジェクト: 89netraM/paintbot
 public abstract Action GetAction(MapUpdated mapUpdated);
コード例 #13
0
 private void ProcessSystemTurn()
 {
     CurrentTurn++;
     Map.Update(this);
     MapUpdated?.Invoke(this, EventArgs.Empty);
 }
コード例 #14
0
 public override Action GetAction(MapUpdated mapUpdated)
 {
     return(Action.Stay);
 }
コード例 #15
0
        public override Action GetAction(MapUpdated mapUpdated)
        {
            _mapUtils = new MapUtils(mapUpdated.Map); // Keep this

            // Implement your bot here!

            // The following is a simple example bot. It tries to
            // 1. Explode PowerUp
            // 2. Move to a tile that it is not currently owning
            // 3. Move in the direction where it can move for the longest time.

            var directions = new List <Action> {
                Action.Down, Action.Right, Action.Left, Action.Up
            };

            var myCharacter     = _mapUtils.GetCharacterInfoFor(mapUpdated.ReceivingPlayerId);
            var myCoordinate    = _mapUtils.GetCoordinateFrom(myCharacter.Position);
            var myColouredTiles = _mapUtils.GetCoordinatesFrom(myCharacter.ColouredPositions);

            if (myCharacter.CarryingPowerUp)
            {
                return(Action.Explode);
            }

            var validActionsThatPaintsNotOwnedTile = directions.Where(dir =>
                                                                      !myColouredTiles.Contains(myCoordinate.MoveIn(dir)) && _mapUtils.IsMovementPossibleTo(myCoordinate.MoveIn(dir))).ToList();

            if (validActionsThatPaintsNotOwnedTile.Any())
            {
                return(validActionsThatPaintsNotOwnedTile.First());
            }

            var possibleLeftMoves  = 0;
            var possibleRightMoves = 0;
            var possibleUpMoves    = 0;
            var possibleDownMoves  = 0;

            var testCoordinate = _mapUtils.GetCoordinateFrom(myCharacter.Position).MoveIn(Action.Left);

            while (_mapUtils.IsMovementPossibleTo(testCoordinate))
            {
                possibleLeftMoves++;
                testCoordinate = testCoordinate.MoveIn(Action.Left);
            }

            testCoordinate = _mapUtils.GetCoordinateFrom(myCharacter.Position).MoveIn(Action.Right);
            while (_mapUtils.IsMovementPossibleTo(testCoordinate))
            {
                possibleRightMoves++;
                testCoordinate = testCoordinate.MoveIn(Action.Right);
            }

            testCoordinate = _mapUtils.GetCoordinateFrom(myCharacter.Position).MoveIn(Action.Up);
            while (_mapUtils.IsMovementPossibleTo(testCoordinate))
            {
                possibleUpMoves++;
                testCoordinate = testCoordinate.MoveIn(Action.Up);
            }

            testCoordinate = _mapUtils.GetCoordinateFrom(myCharacter.Position).MoveIn(Action.Down);
            while (_mapUtils.IsMovementPossibleTo(testCoordinate))
            {
                possibleDownMoves++;
                testCoordinate = testCoordinate.MoveIn(Action.Down);
            }

            var list = new List <(Action, int)>
            {
                (Action.Left, possibleLeftMoves),
                (Action.Right, possibleRightMoves),
                (Action.Up, possibleUpMoves),
                (Action.Down, possibleDownMoves)
            };

            list.Sort((first, second) => first.Item2.CompareTo(second.Item2));
            list.Reverse();

            return(list.FirstOrDefault(l => l.Item2 > 0).Item1);
        }