Beispiel #1
0
    public Brain(Grid gameGrid)
    {
        _stateMemory = new List<TankState>();
        _actionMemory = new List<TankAction>();
        _currentBehavior = null;
        _defaultBehavior = new TakeAStroll();

        _gameGrid = gameGrid;
    }
Beispiel #2
0
    public Brain(Grid gameGrid)
    {
        _stateMemory     = new List <TankState>();
        _actionMemory    = new List <TankAction>();
        _currentBehavior = null;
        _defaultBehavior = new TakeAStroll();

        _gameGrid = gameGrid;
    }
Beispiel #3
0
    public TankAction TakeDecision(TankState state)
    {
        var closeIn        = default(Direction?);
        var nextAction     = TankAction.Unknown;
        var previousState  = _stateMemory.LastOrDefault();
        var previousAction = _actionMemory.Select(x => (TankAction?)x).LastOrDefault();

        if (previousState != null &&
            previousAction.HasValue)
        {
            closeIn = DetectCloseIn(previousAction.Value, previousState, state);
        }

        if (_currentBehavior == null)
        {
            if (closeIn.HasValue)
            {
                _currentBehavior = new TurnShootTurn(closeIn.Value);
            }
        }

        if (_currentBehavior == null)
        {
            nextAction = GetDefaultBehavior().GetNextAction(previousAction);
        }
        else
        {
            nextAction = _currentBehavior.GetNextAction(previousAction);

            if (nextAction == TankAction.Unknown)   // the previous behaviour ended
            {
                _currentBehavior = null;

                nextAction = GetDefaultBehavior().GetNextAction(previousAction);
            }
        }

        if (nextAction == TankAction.Unknown)
        {
            throw new ApplicationException("Tank is at a loss! It doesn't know what to do next!!");
        }

        _stateMemory.Add(state);
        _actionMemory.Add(nextAction);
        return(nextAction);
    }
Beispiel #4
0
    public TankAction TakeDecision(TankState state)
    {
        var closeIn = default(Direction?);
        var nextAction = TankAction.Unknown;
        var previousState = _stateMemory.LastOrDefault();
        var previousAction = _actionMemory.Select(x => (TankAction?)x).LastOrDefault();

        if (previousState != null &&
            previousAction.HasValue) {

            closeIn = DetectCloseIn(previousAction.Value, previousState, state);
        }

        if (_currentBehavior == null) {
            if (closeIn.HasValue) {
                _currentBehavior = new TurnShootTurn(closeIn.Value);
            }
        }

        if (_currentBehavior == null) {
            nextAction = GetDefaultBehavior().GetNextAction(previousAction);
        } else {
            nextAction = _currentBehavior.GetNextAction(previousAction);

            if (nextAction == TankAction.Unknown) { // the previous behaviour ended
                _currentBehavior = null;

                nextAction = GetDefaultBehavior().GetNextAction(previousAction);
            }
        }

        if (nextAction == TankAction.Unknown) {
            throw new ApplicationException("Tank is at a loss! It doesn't know what to do next!!");
        }

        _stateMemory.Add(state);
        _actionMemory.Add(nextAction);
        return nextAction;
    }