public static Action NextAction() { Action action = default(Action); int actionScore = int.MinValue; int score; var paths = BreadthFirstSearch.CreatePaths(); var selfUntouchedWallPositions = QuoridorGraph.WallPossibilities(); if (PlayerExtension.Self.NumberOfWalls() > 0) { if (dijkstraSelf.Path.Count > dijkstraOpponent.Path.Count || dijkstraSelf.OpponentCloseBy) { var wallPositions = dijkstraSelf.OpponentCloseBy ? QuoridorGraph.WallPossibilities(dijkstraOpponent.Path, LIMIT_WALL_BEGIN_COUNT, LIMIT_WALL_END_COUNT) .Join(selfUntouchedWallPositions) : QuoridorGraph.WallPossibilities(dijkstraOpponent.Path, LIMIT_WALL_BEGIN_COUNT, LIMIT_WALL_END_COUNT); #region EvaluateHorizontal foreach (int horizontalWallPosition in wallPositions[WallOrientation.Horizontal]) { PlayerExtension.Self.PlaceHorizontalWall(horizontalWallPosition); score = AlphaBetaPruning.Evaluate(ALPHA_BETA_PRUNING_DEPTH, int.MinValue, int.MaxValue, false); if (actionScore < score) { action = new PlaceWallAction(QuoridorGraph.ToX(horizontalWallPosition), QuoridorGraph.ToY(horizontalWallPosition), WallOrientation.Horizontal); actionScore = score; } PlayerExtension.Self.RemoveHorizontalWall(horizontalWallPosition); } #endregion #region EvaluateVertical foreach (int verticalWallPosition in wallPositions[WallOrientation.Vertical]) { PlayerExtension.Self.PlaceVerticalWall(verticalWallPosition); score = AlphaBetaPruning.Evaluate(ALPHA_BETA_PRUNING_DEPTH, int.MinValue, int.MaxValue, false); if (actionScore < score) { action = new PlaceWallAction(QuoridorGraph.ToX(verticalWallPosition), QuoridorGraph.ToY(verticalWallPosition), WallOrientation.Vertical); actionScore = score; } PlayerExtension.Self.RemoveVerticalWall(verticalWallPosition); } #endregion } } #region EvaluateMove if (dijkstraSelf.Path.Count > 0) { int v = dijkstraSelf.Path.Peek(); PlayerExtension.Self.Move(v); score = AlphaBetaPruning.Evaluate(ALPHA_BETA_PRUNING_DEPTH, int.MinValue, int.MaxValue, false); if (actionScore < score) { action = new MoveAction(QuoridorGraph.ToX(v), QuoridorGraph.ToY(v)); actionScore = score; } PlayerExtension.Self.RevertMove(); } #endregion Debug.Print(action.GetType().IsAssignableFrom(typeof(MoveAction)) ? ((Func <MoveAction, string>)((MoveAction moveAction) => "(" + moveAction.Column + ", " + moveAction.Row + ")"))((MoveAction)action) : ((Func <PlaceWallAction, string>)((PlaceWallAction placeWallAction) => "(" + placeWallAction.Column + ", " + placeWallAction.Row + ") " + placeWallAction.WallAlignment))((PlaceWallAction)action)); return(action); }
public override Action DoAction(GameData status) { tiles = status.Tiles; player = status.Self; xDim = tiles.GetLength(0); yDim = tiles.GetLength(1); if (status.Players[0] == player) { enemy = status.Players[1]; playerGoal = 0; enemyGoal = tiles.Length - xDim; yOff = 1; } else { enemy = status.Players[0]; playerGoal = tiles.Length - xDim; enemyGoal = 0; yOff = 0; } foreach (Player currentPlayer in status.Players) //Får info om spelarna { current1DPos = currentPlayer.Position.Y * tiles.GetLength(0) + currentPlayer.Position.X; if (currentPlayer == player) //Spelaren { nextPlayer1DPos = enemy.Position.Y * tiles.GetLength(0) + enemy.Position.X; CreateGraph(status, enemy); RemoveEdge(nextPlayer1DPos); playerWalls = currentPlayer.NumberOfWalls; playerPath = Search(graph, current1DPos, playerGoal); } else //Motståndaren { CreateGraph(status, player); enemyPos = currentPlayer.Position; enemyWalls = currentPlayer.NumberOfWalls; enemyPath = Search(graph, current1DPos, enemyGoal); } } if (playerPath != null) { if (enemyPath.Count <= playerPath.Count && enemyPath.Count <= 4) //Fienden har en närmare väg! { if (playerWalls > 0) { placingWall = true; ConvertToMove(enemyPath); wallX = nextCoordinates.Item2; wallY = nextCoordinates.Item1; while (placingWall) { if (enemyPos.Y == nextCoordinates.Item1) //fienden ska rör sig i X-riktning { isVertical = true; if (LegalWall(status)) { Console.WriteLine("Next wall is on (" + wallX + ", " + wallY + "). The wall is: Vertical"); placeWall = new PlaceWallAction(wallX, wallY, WallOrientation.Vertical); return(placeWall); } if (wallY < (nextCoordinates.Item1 + 1)) { placingWall = false; //Muren är onödig } } else //fienden ska röra sig i Y riktning { isVertical = false; if (LegalWall(status)) { Console.WriteLine("Next wall is on (" + wallX + ", " + wallY + "). The wall is: Horizontal"); placeWall = new PlaceWallAction(wallX, wallY, WallOrientation.Horizontal); return(placeWall); } if (wallX < (nextCoordinates.Item2 - 1)) { placingWall = false; //Muren är onödig } } } } } ConvertToMove(playerPath); } else { Stall(); } Console.WriteLine("Move to: (" + nextCoordinates.Item2 + ", " + nextCoordinates.Item1 + ")"); move = new MoveAction(nextCoordinates.Item2, nextCoordinates.Item1); return(move); }