MazeRunner/Bobo.Systems.Maze.Console/Bot/SimplerBot.cs

86 lines
3.2 KiB
C#

using HightechICT.Amazeing.Client.Rest;
using LogConsole = System.Console;
namespace Bobo.System.Maze.Console.Bot
{
internal class SimplerBot
{
internal static async Task<int> Run(string mazeName, AmazeingClient mazeClient)
{
if (mazeClient == null)
throw new ArgumentException(nameof(mazeClient));
if (string.IsNullOrWhiteSpace(mazeName))
throw new ArgumentException(nameof(mazeName));
Random random = new Random();
LogConsole.WriteLine($"Entering maze '{mazeName}'");
PossibleActionsAndCurrentScore result = await mazeClient.EnterMaze(mazeName);
Stack<Direction> directions = new Stack<Direction>();
int collected = 0;
do
{
//await Task.Delay(500);
if (result.PossibleMoveActions.All(m => m.HasBeenVisited))
{
//LogConsole.WriteLine("All adjacent tiles have been visited! Going back.");
Direction direction = directions.Pop();
switch(direction)
{
case Direction.Left:
direction = Direction.Right;
break;
case Direction.Right:
direction = Direction.Left;
break;
case Direction.Up:
direction = Direction.Down;
break;
case Direction.Down:
direction = Direction.Up;
break;
}
//LogConsole.WriteLine($"Moving {direction}, I have been here.");
result = await mazeClient.Move(direction);
}
else
{
MoveAction[] notVisited = result.PossibleMoveActions.Where(m => !m.HasBeenVisited).ToArray();
//LogConsole.WriteLine($"{string.Join(", ", notVisited.Select(m => m.Direction.ToString()))} have not been visited, selecting next move.");
int selected = random.Next(notVisited.Length - 1);
MoveAction moveAction = notVisited[selected];
//LogConsole.WriteLine($"Moving {moveAction.Direction}, I have {(!moveAction.HasBeenVisited ? "not" : string.Empty)} been here.");
result = await mazeClient.Move(moveAction.Direction);
directions.Push(moveAction.Direction);
}
if (result.CanCollectScoreHere && result.CurrentScoreInHand > 0)
{
collected += result.CurrentScoreInHand;
await mazeClient.CollectScore();
}
//LogConsole.WriteLine($"New tile has {(!result.CanExitMazeHere ? "no" : string.Empty)} exit.");
} while (!result.CanExitMazeHere);
await mazeClient.ExitMaze();
LogConsole.WriteLine($"Collected {collected}!");
LogConsole.WriteLine($"Exited maze {mazeName}!");
return collected;
}
}
}