From dcc286e873b6c100443361b9ba0d9815c32532d3 Mon Sep 17 00:00:00 2001 From: Rob Date: Tue, 11 Jul 2023 22:38:11 +0200 Subject: [PATCH] Completed semi-smart bot --- Bobo.System.Maze.Bot/SimpleBot.cs | 152 +++++++++++++++++----------- Bobo.System.Maze.Console/Program.cs | 2 +- 2 files changed, 95 insertions(+), 59 deletions(-) diff --git a/Bobo.System.Maze.Bot/SimpleBot.cs b/Bobo.System.Maze.Bot/SimpleBot.cs index 74327d5..a731ee8 100644 --- a/Bobo.System.Maze.Bot/SimpleBot.cs +++ b/Bobo.System.Maze.Bot/SimpleBot.cs @@ -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 directions = new List(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 directions = new List(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(Directions)); + Logger.Log(LogLevel.Information, "Found an exit!"); + List directions = new List(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(Directions)); - - List list = new List(Directions); + Logger.Log(LogLevel.Information, "Found a collection point!"); + List directions = new List(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 fromStart = new List(Directions); - - // see what collection point is closest (if multiple) - List collectionPointDirections = CollectionPointDirections.First(); - int collectionCommonPointFromStart = int.MaxValue; - foreach(List 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 MoveUsingList(List> possibleDirectionsFromStart) + { + PossibleActionsAndCurrentScore returnResult = new(); + + // re-create a list of the directions to the current point + List fromStart = new List(Directions); + // reverse the list so that index 0 is the first move + fromStart.Reverse(); + + // see what collection point is closest (if multiple) + List finalDirections = possibleDirectionsFromStart.First(); + int commonIndex = int.MinValue; + foreach (List 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 MoveBackOne() { diff --git a/Bobo.System.Maze.Console/Program.cs b/Bobo.System.Maze.Console/Program.cs index 34cbc92..195d2b1 100644 --- a/Bobo.System.Maze.Console/Program.cs +++ b/Bobo.System.Maze.Console/Program.cs @@ -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);