public async Task Run(CreateNewMazeCommand createNewMazeCommand) { const string activeState = "active"; var mazeId = await _ponyApi.CreateNewMaze(createNewMazeCommand); var state = activeState; while (state == activeState) { var maze = await GetMaze(mazeId); var bestMove = maze.FindBestMove(maze); var directionToMove = _moveDirectionExtractor.GetDirectionToMove(bestMove, maze); state = await _ponyApi.Move(mazeId, directionToMove); var map = await _ponyApi.Preview(mazeId); PonyMoved?.Invoke(this, new PonyMovedEventArgs( maze.ShortestPathToEndpoint.Value.Select(path => path.Count), maze.ShortestPathToDomokun.Value.Count, map)); } if (state == "over") { WeLost?.Invoke(this, EventArgs.Empty); } else { WeWon?.Invoke(this, EventArgs.Empty); } }