public override RuleOutcome ReplayAndChooseNextMove(GameState gameStateBeforeStep, GameState currentGameState) { int distance = Distance; int xOffset = GetFlippedXOffset(Direction.GetXOffset()); int yOffset = GetFlippedYOffset(Direction.GetYOffset()); if (GetDistanceFromLevel != null) { distance = GetDistanceFromLevel(Level); } Position nextPosition = gameStateBeforeStep.YourCell.Position; int nextMoveNumber = gameStateBeforeStep.YourCell.MoveNumber; for (int i = 0; i < distance; i++) { // Calculate move number, position and cell for the next move: nextMoveNumber++; int newX = GetNormalizedX(nextPosition.X + xOffset); int newY = nextPosition.Y + yOffset; nextPosition = new Position(newX, newY); RuleOutcome outcome = MoveToNextPosition(gameStateBeforeStep, currentGameState, nextPosition, nextMoveNumber); if (outcome.Status != RuleStatus.NoFurtherSuggestions) { return(outcome); } } // The move in a line has been completed: return(new RuleOutcome { Status = RuleStatus.NoFurtherSuggestions }); }
public RuleOutcome RunRulesEngine(GameState currentGameState) { try { RuleOutcome outcome; GameState gameStateBeforeStep = RulesEngineHelper.GetInitialGameState(currentGameState); do { outcome = ReplayAndChooseNextMove(gameStateBeforeStep, currentGameState); if (AfterEachRepetition != null) { AfterEachRepetition(this, gameStateBeforeStep, currentGameState, outcome); } }while (Repeat && outcome.Status == RuleStatus.NoFurtherSuggestions); if (outcome.Status == RuleStatus.NextMoveSuggested || outcome.Status == RuleStatus.NoFurtherSuggestions) { AfterStep(this, gameStateBeforeStep, currentGameState, outcome); } return(outcome); } catch (Exception exc) { RuleOutcome outcome = new RuleOutcome { Status = RuleStatus.ExceptionThrown, ExceptionThrown = exc }; return(outcome); } }
public override RuleOutcome ReplayAndChooseNextMove(GameState gameStateBeforeStep, GameState currentGameState) { Position to = To; if (GetPosition != null) { to = GetPosition(this, gameStateBeforeStep, currentGameState); } to = GetFlippedTo(to); Dijkstra.Perform(gameStateBeforeStep); CellState destinationState = gameStateBeforeStep[to]; int distanceFromYou = destinationState.DistanceFromYou; List <Position> positionsOnRoute = new List <Position>(); if (destinationState.OccupationStatus == OccupationStatus.Clear && (destinationState.CompartmentStatus == CompartmentStatus.InYourCompartment || destinationState.CompartmentStatus == CompartmentStatus.InSharedCompartment)) { for (int dist = distanceFromYou - 1; dist > 0; dist--) { int d = dist; destinationState = destinationState.GetAdjacentCellStates().Where( cs => cs.OccupationStatus == OccupationStatus.Clear && cs.DistanceFromYou == d).FirstOrDefault(); positionsOnRoute.Insert(0, destinationState.Position); } int nextMoveNumber = gameStateBeforeStep.YourCell.MoveNumber; foreach (Position nextPosition in positionsOnRoute) { nextMoveNumber++; RuleOutcome outcome = MoveToNextPosition(gameStateBeforeStep, currentGameState, nextPosition, nextMoveNumber); if (outcome.Status != RuleStatus.NoFurtherSuggestions) { return(outcome); } } return(new RuleOutcome { Status = RuleStatus.NoFurtherSuggestions }); } else { return(new RuleOutcome { Status = RuleStatus.OpponentBlockedMove }); } }
public override RuleOutcome ReplayAndChooseNextMove(GameState gameStateBeforeStep, GameState currentGameState) { // Allow chaining of levels: int previousLevel = 0; if (ParentStep != null) { previousLevel = ParentStep.Level; } int initialLevel = InitialLevel; if (GetInitialLevel != null) { initialLevel = GetInitialLevel(previousLevel); } int finalLevel = FinalLevel; if (GetFinalLevel != null) { finalLevel = GetFinalLevel(previousLevel); } for (int level = initialLevel; level <= finalLevel; level++) { foreach (Step <TState> step in Steps) { step.Level = level; RuleOutcome outcome = step.ReplayAndChooseNextMove(gameStateBeforeStep, currentGameState); switch (outcome.Status) { case RuleStatus.DeviationFromRuleDetected: case RuleStatus.NextMoveSuggested: case RuleStatus.OpponentBlockedMove: case RuleStatus.ExceptionThrown: return(outcome); // Otherwise RuleStatus.NoFurtherSuggestions } } } return(new RuleOutcome { Status = RuleStatus.NoFurtherSuggestions }); }
public override RuleOutcome ReplayAndChooseNextMove(GameState gameStateBeforeStep, GameState currentGameState) { foreach (Step <TState> step in Steps) { RuleOutcome outcome = step.ReplayAndChooseNextMove(gameStateBeforeStep, currentGameState); switch (outcome.Status) { case RuleStatus.DeviationFromRuleDetected: case RuleStatus.NextMoveSuggested: case RuleStatus.OpponentBlockedMove: case RuleStatus.ExceptionThrown: return(outcome); // Otherwise RuleStatus.NoFurtherSuggestions } } return(new RuleOutcome { Status = RuleStatus.NoFurtherSuggestions }); }
public static RuleOutcome MoveToPositionAlongAnyRoute(GameState mutableGameState, GameState actualGameState, ref int nextMoveNumber, Position targetPosition) { RuleOutcome outcome; bool isTargetReachable = mutableGameState.IsCellStateReachableByPlayer(PlayerType.You, mutableGameState[targetPosition]); List <Position> positionsOnRouteToTarget = mutableGameState.GetPositionsOnAnyRouteToTheTargetPosition(PlayerType.You, targetPosition); foreach (Position pos in positionsOnRouteToTarget) { nextMoveNumber++; outcome = RulesEngineHelper.MoveToNextPosition(mutableGameState, actualGameState, pos, nextMoveNumber); if (outcome.Status != RuleStatus.NoFurtherSuggestions) { return(outcome); } } outcome = new RuleOutcome { Status = RuleStatus.NoFurtherSuggestions }; return(outcome); }