Skip to content

Commit

Permalink
Незначительные оптимизации
Browse files Browse the repository at this point in the history
  • Loading branch information
voidmain02 committed Aug 11, 2019
1 parent c4cc2d1 commit 754b61c
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 26 deletions.
5 changes: 3 additions & 2 deletions MiniAiCup.Paperio.Core/BestTrajectoryFinder.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,9 @@ public class BestTrajectoryFinder
public BestTrajectoryFinder(int depth)
{
_depth = depth;
_simulator = new SimpleGameSimulator();
_scorer = new GameStateScorer();
var capturer = new BfsTerritoryCapturer();
_simulator = new SimpleGameSimulator(capturer);
_scorer = new GameStateScorer(capturer);
}

public GameStateInternal FindBestState(GameStateInternal initialState)
Expand Down
21 changes: 7 additions & 14 deletions MiniAiCup.Paperio.Core/BfsTerritoryCapturer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,14 @@ public class BfsTerritoryCapturer

private readonly bool[,] _emptyVisited;

private readonly Queue<Point> _queue;

public BfsTerritoryCapturer()
{
_mapBoundaryPoints = GetBoundary(Game.Params.MapLogicSize).ToList();
_visited = Game.GetNewMap<bool>();
_emptyVisited = Game.GetNewMap<bool>();
_queue = new Queue<Point>(Game.Params.MapLogicSize.Width * Game.Params.MapLogicSize.Height);
}

public PointsSet Capture(PointsSet territory, Path tail)
Expand All @@ -25,16 +28,6 @@ public PointsSet Capture(PointsSet territory, Path tail)
GameDebugData.Current.CaptureCount++;
#endif

if (tail.Length <= 1)
{
return PointsSet.Empty;
}

if (!territory.Contains(tail.Last()))
{
return PointsSet.Empty;
}

ResetVisited();

var usedPoints = territory.UnionWith(tail);
Expand All @@ -43,12 +36,12 @@ public PointsSet Capture(PointsSet territory, Path tail)
foreach (var outsidePoint in outsidePoints)
{
_visited[outsidePoint.X, outsidePoint.Y] = true;
_queue.Enqueue(outsidePoint);
}

var queue = new Queue<Point>(outsidePoints);
while (queue.Count > 0)
while (_queue.Count > 0)
{
var point = queue.Dequeue();
var point = _queue.Dequeue();
foreach (var neighbor in point.GetNeighbors())
{
if (!Game.Params.MapLogicSize.ContainsPoint(neighbor))
Expand All @@ -67,7 +60,7 @@ public PointsSet Capture(PointsSet territory, Path tail)
continue;
}
outsidePoints.Add(neighbor);
queue.Enqueue(neighbor);
_queue.Enqueue(neighbor);
}
}

Expand Down
23 changes: 18 additions & 5 deletions MiniAiCup.Paperio.Core/GameStateScorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,13 @@ namespace MiniAiCup.Paperio.Core
{
public class GameStateScorer
{
private readonly BfsTerritoryCapturer _territoryCapturer;

public GameStateScorer(BfsTerritoryCapturer territoryCapturer)
{
_territoryCapturer = territoryCapturer;
}

public int Score(GameStateInternal state)
{
#if DEBUG
Expand Down Expand Up @@ -57,12 +64,18 @@ public int Score(GameStateInternal state)

public int CalcPotentialTerritoryCaptureScore(GameStateInternal state)
{
var territoryCapturer = new BfsTerritoryCapturer();
var tailWithPathToHome = new Point[state.Me.Tail.Length + state.Me.PathToHome.Length - 1];
for (int i = 0; i < state.Me.Tail.Length; i++)
{
tailWithPathToHome[i] = state.Me.Tail[i];
}

for (int i = 0; i < state.Me.PathToHome.Length - 1; i++)
{
tailWithPathToHome[state.Me.Tail.Length + i] = state.Me.PathToHome[i];
}

var tailWithPathToHome = new List<Point>(state.Me.Tail.Length + state.Me.PathToHome.Length);
tailWithPathToHome.AddRange(state.Me.Tail);
tailWithPathToHome.AddRange(state.Me.PathToHome);
var capturedTerritory = territoryCapturer.Capture(state.Me.Territory, new Path(tailWithPathToHome));
var capturedTerritory = _territoryCapturer.Capture(state.Me.Territory, new Path(tailWithPathToHome));

int score = capturedTerritory.Count*Constants.NeutralTerritoryScore;
foreach (var enemy in state.Enemies)
Expand Down
8 changes: 3 additions & 5 deletions MiniAiCup.Paperio.Core/SimpleGameSimulator.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using System;
using System.Collections.Generic;
using System.Linq;
using MiniAiCup.Paperio.Core.Debug;

Expand All @@ -9,9 +7,9 @@ public class SimpleGameSimulator
{
private readonly BfsTerritoryCapturer _territoryCapturer;

public SimpleGameSimulator()
public SimpleGameSimulator(BfsTerritoryCapturer territoryCapturer)
{
_territoryCapturer = new BfsTerritoryCapturer();
_territoryCapturer = territoryCapturer;
}

public GameStateInternal Simulate(GameStateInternal state, int currentDepth, Move move)
Expand Down Expand Up @@ -49,7 +47,7 @@ public GameStateInternal Simulate(GameStateInternal state, int currentDepth, Mov
return GetDeadState();
}

var capturedTerritory = _territoryCapturer.Capture(me.Territory, me.Tail.Append(me.Position)); // TODO: Надо бы переделать Capturer, чтобы не приходилось добавлять в конец текущее положение
var capturedTerritory = _territoryCapturer.Capture(me.Territory, me.Tail);
me.Tail = Path.Empty;
me.Territory = me.Territory.UnionWith(capturedTerritory);
me.Score += capturedTerritory.Count*Constants.NeutralTerritoryScore;
Expand Down

0 comments on commit 754b61c

Please sign in to comment.