Example #1
0
 public AiAgent(int tankId, bool checkForOpenPathToMiddle)
 {
     _tankId = tankId;
     _checkForOpenPathToMiddle = checkForOpenPathToMiddle;
     _lastAction = ChallengeService.action.NONE;
     _hasShotFromLastPosition = false;
     _headingToMiddle = false;
 }
Example #2
0
        public Move GetBestMove(ChallengeService.game game, BoardCell[][] board, ChallengeService.player me, ChallengeService.player enemy)
        {
            Move move = null;
            ChallengeService.unit tank = findTankInPlayer(_tankId, me);

            if (tank == null)
            {
                return null;
            }

            if (tank.x != _lastX || tank.y != _lastY)
            {
                _hasShotFromLastPosition = false;
            }

            var bulletInAir = checkIsBulletInAir(board, me, tank);
            var stuckLastTurn = checkStuckLastTurn(tank);

            var enemyBase = enemy.@base;

            var pastMidpoint = (Math.Abs(enemyBase.y-tank.y) < (board[0].Length / 2));

            if (stuckLastTurn && (tank.direction == ChallengeService.direction.UP || tank.direction == ChallengeService.direction.DOWN) && enemyBase.x != tank.x)
            {
                _targetX = tank.x + (pastMidpoint!=(tank.x > enemyBase.x) ? +1 : -1);
            }

            if (_checkForOpenPathToMiddle && !_headingToMiddle && tank.x != enemyBase.x)
            {
                _headingToMiddle = testPathToMiddleIsOpen(board, tank, enemyBase);
            }
            else if (_checkForOpenPathToMiddle && _headingToMiddle && tank.x == enemyBase.x)
            {
                _headingToMiddle = false;
            }

            ChallengeService.direction chosenDirection =
                _headingToMiddle ?
                (
                    tank.x > enemyBase.x ?
                    ChallengeService.direction.LEFT :
                    ChallengeService.direction.RIGHT
                ) :
                (
                    tank.y != enemyBase.y ?
                    (
                        _targetX.HasValue && _targetX != tank.x ?
                        (
                            tank.x > _targetX ?
                            ChallengeService.direction.LEFT :
                            ChallengeService.direction.RIGHT
                        ) :
                        (
                            tank.y > enemyBase.y ?
                            ChallengeService.direction.UP :
                            ChallengeService.direction.DOWN
                        )
                    ) :
                    (
                        tank.x > enemyBase.x ?
                        ChallengeService.direction.LEFT :
                        ChallengeService.direction.RIGHT
                    )
                );

            if (chosenDirection != tank.direction || bulletInAir || _headingToMiddle)
            {
                move = MoveInDirection(tank.id, chosenDirection);
            }
            else
            {
                move = new Move(tank.id, ChallengeService.action.FIRE);
                _hasShotFromLastPosition = true;
            }

            _lastX = tank.x;
            _lastY = tank.y;
            _lastAction = move.Action;

            return move;
        }