Completed semi-smart bot

This commit is contained in:
Rob 2023-07-11 22:38:11 +02:00
parent fbded5932f
commit dcc286e873
2 changed files with 95 additions and 59 deletions

View File

@ -26,12 +26,32 @@ namespace Bobo.System.Maze.Bot
PossibleActionsAndCurrentScore result = await MazeClient.EnterMaze(maze.Name);
int collected = 0;
// --- not DRY but eww --- \\
// if this is an exit, copy the path so we know how to get there
if (result.CanExitMazeHere)
{
Logger.Log(LogLevel.Information, "Found an exit!");
List<Direction> directions = new List<Direction>(Directions);
directions.Reverse();
ExitDirections.Add(directions);
}
// if this is a collection point, copy the path so we know how to get there
if (result.CanCollectScoreHere)
{
Logger.Log(LogLevel.Information, "Found a collection point!");
List<Direction> directions = new List<Direction>(Directions);
directions.Reverse();
CollectionPointDirections.Add(directions);
}
do
{
if (result.PossibleMoveActions.All(m => m.HasBeenVisited))
{
Logger.Log(LogLevel.Debug, "All adjacent tiles have been visited! Going back.");
result = await MoveBackOne();
continue;
}
else
{
@ -47,88 +67,104 @@ namespace Bobo.System.Maze.Bot
Directions.Push(moveAction.Direction);
}
// if this is an exit, copy the path so we know how to get there
if (result.CanExitMazeHere)
{
ExitDirections.Add(new List<Direction>(Directions));
Logger.Log(LogLevel.Information, "Found an exit!");
List<Direction> directions = new List<Direction>(Directions);
directions.Reverse();
ExitDirections.Add(directions);
}
// if this is a collection point, copy the path so we know how to get there
if (result.CanCollectScoreHere)
{
CollectionPointDirections.Add(new List<Direction>(Directions));
List<Direction> list = new List<Direction>(Directions);
Logger.Log(LogLevel.Information, "Found a collection point!");
List<Direction> directions = new List<Direction>(Directions);
directions.Reverse();
CollectionPointDirections.Add(directions);
}
Logger.Log(LogLevel.Debug, $"New tile has {(!result.CanExitMazeHere ? "no" : string.Empty)} exit.");
} while (maze.PotentialReward != result.CurrentScoreInHand
&& CollectionPointDirections.Any()
&& ExitDirections.Any());
|| !CollectionPointDirections.Any()
|| !ExitDirections.Any());
do
if (!result.CanCollectScoreHere)
{
// create a list of the directions to the current point
List<Direction> fromStart = new List<Direction>(Directions);
// see what collection point is closest (if multiple)
List<Direction> collectionPointDirections = CollectionPointDirections.First();
int collectionCommonPointFromStart = int.MaxValue;
foreach(List<Direction> cdp in CollectionPointDirections)
{
int lowest = fromStart.Count() < cdp.Count() ? fromStart.Count() : cdp.Count();
int lastCommonIndex = 0;
// start from index 0 and work up to the lowest size
for (lastCommonIndex = 0; lastCommonIndex < lowest; lastCommonIndex++)
{
if (fromStart[lastCommonIndex] != cdp[lastCommonIndex])
{
// not common, break loop
break;
}
}
if (lastCommonIndex < collectionCommonPointFromStart)
{
collectionPointDirections = cdp;
collectionCommonPointFromStart = lastCommonIndex;
}
}
// first go back to the common index
for (int returnMoves = 0; returnMoves < fromStart.Count - collectionCommonPointFromStart; returnMoves ++)
{
await MoveBackOne();
}
for (int toCollectionIndex = collectionCommonPointFromStart + 1; toCollectionIndex < collectionPointDirections.Count(); toCollectionIndex++)
{
result = await MazeClient.Move(collectionPointDirections[toCollectionIndex]);
}
// go to collection
if (result.CanCollectScoreHere)
{
collected += result.CurrentScoreInHand;
await MazeClient.CollectScore();
}
// go to exit
result = await MoveUsingList(CollectionPointDirections);
}
while (!result.CanExitMazeHere);
// go to collection
collected += result.CurrentScoreInHand;
_ = await MazeClient.CollectScore();
result = await MoveUsingList(ExitDirections);
await MazeClient.ExitMaze();
Logger.Log(LogLevel.Information, "Collected {Collected}!", collected);
Logger.Log(LogLevel.Information, "Exited maze {MazeName}!", maze.Name);
// purge the data
Directions.Clear();
ExitDirections.Clear();
CollectionPointDirections.Clear();
return collected;
}
private async Task<PossibleActionsAndCurrentScore> MoveUsingList(List<List<Direction>> possibleDirectionsFromStart)
{
PossibleActionsAndCurrentScore returnResult = new();
// re-create a list of the directions to the current point
List<Direction> fromStart = new List<Direction>(Directions);
// reverse the list so that index 0 is the first move
fromStart.Reverse();
// see what collection point is closest (if multiple)
List<Direction> finalDirections = possibleDirectionsFromStart.First();
int commonIndex = int.MinValue;
foreach (List<Direction> directionList in possibleDirectionsFromStart)
{
int lowest = fromStart.Count() < directionList.Count() ? fromStart.Count() : directionList.Count();
int lastCommonIndex = 0;
// start from index 0 and work up to the lowest size
for (lastCommonIndex = 0; lastCommonIndex < lowest; lastCommonIndex++)
{
if (fromStart[lastCommonIndex] != directionList[lastCommonIndex])
{
// not common, break loop
break;
}
}
if (lastCommonIndex > commonIndex)
{
finalDirections = directionList;
commonIndex = lastCommonIndex;
}
}
// first go back to the common index
for (int returnMoves = 0; returnMoves < fromStart.Count - commonIndex; returnMoves++)
{
returnResult = await MoveBackOne();
}
// follow the directions after the common index
for (int toExtiIndex = commonIndex; toExtiIndex < finalDirections.Count(); toExtiIndex++)
{
returnResult = await MazeClient.Move(finalDirections[toExtiIndex]);
Directions.Push(finalDirections[toExtiIndex]);
}
return returnResult;
}
private Task<PossibleActionsAndCurrentScore> MoveBackOne()
{

View File

@ -41,7 +41,7 @@ if (!registered)
logger.Log(LogLevel.Information, $"Player '{username}' registered", username);
logger.Log(LogLevel.Information, $"Starting SimplerBot");
BaseBot bot = new SimplerBot(mazeClient, logger);
BaseBot bot = new SimpleBot(mazeClient, logger);
foreach (MazeInfo maze in mazes)
{
int collected = await bot.Run(maze);