// GET: pony-challenge/Maze/GUID/print public HttpResponseMessage PrintAction(Guid mazeId) { string mazeString = String.Empty; try { if (mazeId != null && mazeId != Guid.Empty) { var maze = MemoryCacher.GetMazeFromCache(mazeId); mazeString = MazeHelper.PrintMazeAsHTML(maze); } else { HttpResponseException exception = CreateResponseException(HttpStatusCode.BadRequest, ERRORMESSAGE_MAZEID_INVALID); throw exception; } } catch (Exception ex) { HttpResponseException exception = CreateResponseException(HttpStatusCode.InternalServerError, ex.Message); throw exception; } return(Request.CreateResponse(HttpStatusCode.OK, mazeString)); }
public void TestBFSAlgorithm() { foreach (var mazeId in mazeIds) { maze = MemoryCacher.GetMazeFromCache(mazeId); Stack <int> stack = BreadthFirstSearchAlgorithm.BFSAlgorithm(maze, false, false); Assert.True(stack.Count() > 0); stack = BreadthFirstSearchAlgorithm.BFSAlgorithm(maze, true, false); Assert.True(stack.Count() > 0); stack = BreadthFirstSearchAlgorithm.BFSAlgorithm(maze, true, true); Assert.True(stack.Count() > 0); stack = BreadthFirstSearchAlgorithm.BFSAlgorithm(maze, false, true); Assert.True(stack.Count() > 0); } }
// GET: pony-challenge/Maze/GUID public GameState MoveCharacters(Guid mazeId, MoveCharactersInMazeModel moveModel) { string direction = moveModel.Direction; try { if (mazeId != null && mazeId != Guid.Empty) { Maze maze = MemoryCacher.GetMazeFromCache(mazeId); CellState[] mazeCells = maze.GetCells(); int ponyLocation = maze.GetPonyId(); int mazeWidth = maze.GetMazeWidth(); int mazeHeight = maze.GetMazeHeight(); StateResult result = MazeHelper.MoveCharacter(maze, direction, CellState.Pony); if (result == StateResult.MoveAccepted) { if (DifficultyManager.CheckIfCurrentDifficultyIsAdaptible(maze)) { MemoryCacher.AppendDomokunNextMove(mazeId, direction); } string DomokunDirection = DifficultyManager.GetDomokunDirection(maze); result = MazeHelper.MoveCharacter(maze, DomokunDirection, CellState.Domokun); } MemoryCacher.UpdateMazeInCache(mazeId, maze); return(new GameState { State = State.Active, StateResult = result }); } else { HttpResponseException exception = CreateResponseException(HttpStatusCode.BadRequest, ERRORMESSAGE_MAZEID_INVALID); throw exception; } } catch (Exception ex) { HttpResponseException exception = CreateResponseException(HttpStatusCode.InternalServerError, ex.Message); throw exception; } }
public MazeModel CreateMazeCharacters(Guid mazeId) { try { if (mazeId != null && mazeId != Guid.Empty) { Random randomGenerator = new Random(); Maze maze = MemoryCacher.GetMazeFromCache(mazeId); MemoryCacher.DeleteDomokunMovesFromCache(mazeId); //clear characters if new game is choosed MazeHelper.ClearMazeCharacters(maze); int mazeHeight = maze.GetMazeHeight(); int mazeWidth = maze.GetMazeWidth(); int randomPonyId = 0; int randomDomokunId = 0; int randomEndpointId = 0; do { randomPonyId = randomGenerator.Next(mazeHeight * mazeWidth); randomDomokunId = randomGenerator.Next(mazeHeight * mazeWidth); randomEndpointId = randomGenerator.Next(mazeHeight * mazeWidth); }while (CheckIfTwoSameRandoms(randomPonyId, randomDomokunId, randomEndpointId)); maze.SetDomokunId(randomDomokunId); maze.SetPonyId(randomPonyId); maze.SetEndpointId(randomEndpointId); MemoryCacher.UpdateMazeInCache(mazeId, maze); DifficultyManager.DifficultyCheckForPrecalculation(maze); return(new MazeModel { Pony = randomPonyId, Domokun = randomDomokunId, Endpoint = randomEndpointId, Maze = maze, Difficulty = maze.GetDifficulty(), Size = new List <int> { mazeHeight, mazeWidth }, GameState = new GameState { State = State.Active, StateResult = StateResult.SuccesfullyCreated }, MazeId = mazeId }); } else { HttpResponseException exception = CreateResponseException(HttpStatusCode.BadRequest, ERRORMESSAGE_MAZEID_INVALID); throw exception; } } catch (Exception ex) { HttpResponseException exception = CreateResponseException(HttpStatusCode.InternalServerError, ex.Message); throw exception; } }