MazeRunner/Bobo.System.Maze.Bot/BumbBot.cs

56 lines
2.2 KiB
C#

using Bobo.System.Maze.Bot.Interface;
using HightechICT.Amazeing.Client.Rest;
using Microsoft.Extensions.Logging;
namespace Bobo.System.Maze.Bot
{
public class BumbBot : BaseBot
{
public BumbBot(AmazeingClient mazeClient, ILogger<BaseBot> logger) : base(mazeClient, logger)
{ }
public override async Task<int> Run(MazeInfo maze)
{
if (MazeClient == null)
throw new ArgumentException(nameof(MazeClient));
if (string.IsNullOrWhiteSpace(maze.Name))
throw new ArgumentException(nameof(maze.Name));
Random random = new Random();
Logger.Log(LogLevel.Information, "Entering maze '{MazeName}'", maze.Name);
PossibleActionsAndCurrentScore result = await MazeClient.EnterMaze(maze.Name);
do
{
// select a way to go
MoveAction[] moves = result.PossibleMoveActions.ToArray();
MoveAction[] notExploredMoves = moves.Any(m => !m.HasBeenVisited) ? moves.Where(m => !m.HasBeenVisited).ToArray() : moves;
if (moves.All(m => m.HasBeenVisited))
{
Logger.Log(LogLevel.Debug, "All adjacent tiles have been visited! Selecting next path random.");
}
else
{
Logger.Log(LogLevel.Debug, $"{string.Join(", ", notExploredMoves.Select(m => m.Direction.ToString()))} have not been visited, selecting next move.");
}
int selected = random.Next(notExploredMoves.Length - 1);
MoveAction moveAction = notExploredMoves[selected];
Logger.Log(LogLevel.Debug, $"Moving {moveAction.Direction}, I have {(!moveAction.HasBeenVisited ? "not" : string.Empty)} been here.");
result = await MazeClient.Move(moveAction.Direction);
Logger.Log(LogLevel.Debug, $"New tile has {(!result.CanExitMazeHere ? "no" : string.Empty)} exit.");
} while (!result.CanExitMazeHere);
await MazeClient.ExitMaze();
Logger.Log(LogLevel.Information, $"Exited the maze!");
return 0;
}
}
}