public async Task MapShow() { try { var shows = await MazeHelper.GetShows(); using (var db = new MediaDbContext()) { foreach (var show in shows) { if (db.Shows.Find(show.Id) == null) { await db.AddAsync(show); await db.SaveChangesAsync(); } await MapPerson(show.Id); } } } catch (Exception ex) { var message = ex.Message; } }
public static IEnumerable <Neighbours> GetNeighboursBFS(CellState[] array, int xyPoint, int _width, int _height) { if (!MazeHelper.CheckIfLeftBorder(_width, xyPoint) && !array[xyPoint].HasFlag(CellState.West)) { yield return new Neighbours { NeighbourLocation = xyPoint - 1, Direction = WEST_MOVEMENT } } ; if (!MazeHelper.CheckIfUpperBorder(_width, xyPoint) && !array[xyPoint].HasFlag(CellState.North)) { yield return new Neighbours { NeighbourLocation = xyPoint - _width, Direction = NORTH_MOVEMENT } } ;; if (!MazeHelper.CheckIfRightBorder(_width, xyPoint) && !array[xyPoint + 1].HasFlag(CellState.West)) { yield return new Neighbours { NeighbourLocation = xyPoint + 1, Direction = EAST_MOVEMENT } } ; if (!MazeHelper.CheckIfLowerBorder(_width, _height, xyPoint) && !array[xyPoint + _width].HasFlag(CellState.North)) { yield return new Neighbours { NeighbourLocation = xyPoint + _width, Direction = SOUTH_MOVEMENT } } ; }
public List <Vector3> UpdatePath(Vector3 from, Vector3 to, Action <List <Vector3> > onPathUpdated = null) { if (_map == null) { return(null); } int targetX = Mathf.RoundToInt(to.x); int targetY = Mathf.RoundToInt(to.z); if (MazeHelper.IsWall(_map, targetX, targetY)) { return(null); } int fromX = Mathf.RoundToInt(from.x); int fromY = Mathf.RoundToInt(from.z); if (MazeHelper.IsWall(_map, fromX, fromY)) { return(null); } var path = GetPath(fromX, fromY, targetX, targetY); if (path != null & path.Count > 0) { onPathUpdated?.Invoke(path); } return(path); }
public static int GetRandomLegalMovement(Maze maze) { List <int> legalMoves = new List <int>(); Random random = new Random(); int DomokunId = maze.GetDomokunId(); int mazeWidth = maze.GetMazeWidth(); int mazeHeight = maze.GetMazeHeight(); CellState[] cells = maze.GetCells(); if (!MazeHelper.CheckIfUpperBorder(mazeWidth, DomokunId) && !cells[DomokunId].HasFlag(CellState.North)) { legalMoves.Add(NORTH_MOVEMENT); } if (!MazeHelper.CheckIfRightBorder(mazeWidth, DomokunId) && !cells[DomokunId].HasFlag(CellState.West)) { legalMoves.Add(WEST_MOVEMENT); } if (!MazeHelper.CheckIfLowerBorder(mazeWidth, mazeHeight, DomokunId) && !cells[DomokunId + mazeWidth].HasFlag(CellState.North)) { legalMoves.Add(SOUTH_MOVEMENT); } if (!MazeHelper.CheckIfLeftBorder(mazeWidth, DomokunId) && !cells[DomokunId + 1].HasFlag(CellState.West)) { legalMoves.Add(EAST_MOVEMENT); } return(legalMoves[random.Next(legalMoves.Count)]); }
private bool CanBeShortCut(bool[,] maze, int x, int y) { if (MazeHelper.IsRoad(maze, x, y)) { return(false); } bool isLeftRoad = MazeHelper.IsRoad(maze, x - 1, y); bool isRightRoad = MazeHelper.IsRoad(maze, x + 1, y); if (isLeftRoad && isRightRoad) { return(true); } bool isTopRoad = MazeHelper.IsRoad(maze, x, y + 1); bool isBottomRoad = MazeHelper.IsRoad(maze, x, y - 1); if (isTopRoad && isBottomRoad) { return(true); } return(false); }
private void Connect(bool[,] maze, int x, int y) { int[,] directions = new int[, ] { { -1, 0 }, { 1, 0 }, { 0, -1 }, { 0, 1 } }; ShuffleUtility.Shuffle(directions); for (int i = 0; i < directions.GetLength(0); i++) { // Нужно направление умножать на 2 // чтоб оставлять стенку размером в 1 ячейку int neighborX = x + directions[i, 0] * 2; int neighborY = y + directions[i, 1] * 2; if (MazeHelper.IsRoad(maze, neighborX, neighborY)) { int connectorX = x + directions[i, 0]; int connectorY = y + directions[i, 1]; maze[connectorY, connectorX] = true; return; } } }
// 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 async Task MapPerson(int showId) { try { var casts = await MazeHelper.GetCasts(showId); using (var db = new MediaDbContext()) { foreach (var cast in casts) { if (await db.Persons.FindAsync(cast.Person.Id) == null) { await db.AddAsync(cast.Person); await db.SaveChangesAsync(); } await MapShowPerson(showId, cast.Person.Id); } } } catch (Exception ex) { var message = ex.Message; } }
public void GenerateMaze() { var mg = new RecursiveBacktrackingMazeGenerator(); var m = mg.Create(20); Console.Write(MazeHelper.Draw(m)); }
public void Initialize(Vector2Int coord, float fillPercent, RoomData roomData) { m_Coord = coord; m_FillPercent = roomData.m_FillCurve.Evaluate(fillPercent); m_DirectionsList = MazeHelper.GetRandomDirectionsQueue(); m_RoomData = roomData; }
private void PlaceHero() { int x = UnityEngine.Random.Range(0, map.GetLength(1)); int y = UnityEngine.Random.Range(0, map.GetLength(0)); var closestRoad = MazeHelper.GetClosestRoad(map, x, y); _hero.position = new Vector3(closestRoad.Item1, 0, closestRoad.Item2); }
static void Main(string[] args) { Maze maze; //Check if a parameter has been specified, if (args.Length == 1) { //initialise maze using the specified file maze = MazeHelper.InitialiseMaze(args[0]); } else if (args.Length > 1) { Console.WriteLine("Please select one file only"); } else if (args.Length < 1) { Console.WriteLine("Please select a file"); } else { //Display DialogBox to allow a file to be selected OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*"; if (openFileDialog.ShowDialog() == DialogResult.OK) { var FilePath = File.ReadAllText(openFileDialog.FileName); } //initialise maze using the selected file maze = MazeHelper.InitialiseMaze(openFileDialog.FileName); } //solve maze, searches the maze for a path to the end location. var result = MazeHelper.SolveMaze(maze, maze.StartLocation); //Solve maze will return E if it has found the end location in the maze. if (result == "E") { Console.WriteLine("This maze can be solved: "); } //Otherwise the maze cannot be solved else { Console.WriteLine("There is no solution for this maze: "); } Console.WriteLine(); MazeHelper.DisplayMaze(maze); Console.ReadKey(); }
public void GetRandomDirection() { Maze target = new Maze(2, 2); MazeHelper.ResetRandomDirection(); Direction dir1 = MazeHelper.GetRandomDirection(); Direction dir2 = MazeHelper.GetRandomDirection(); Direction dir3 = MazeHelper.GetRandomDirection(); Direction dir4 = MazeHelper.GetRandomDirection(); Assert.IsTrue(dir1 != dir2 && dir1 != dir3 && dir1 != dir4); Assert.IsTrue(dir2 != dir3 && dir2 != dir4); Assert.IsTrue(dir3 != dir4); }
public void GetRightestDirectionTest_WhenStartingDown_ThenLeftDownRightUp() { MazeHelper.ResetRightestDirection(Direction.down); Direction rightest = MazeHelper.GetNextRightestDirection(); Assert.AreEqual(Direction.left, rightest); rightest = MazeHelper.GetNextRightestDirection(); Assert.AreEqual(Direction.down, rightest); rightest = MazeHelper.GetNextRightestDirection(); Assert.AreEqual(Direction.right, rightest); rightest = MazeHelper.GetNextRightestDirection(); Assert.AreEqual(Direction.up, rightest); }
// 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; } }
private void AddVisitPoints(HashSet <Tuple <int, int> > visited, LinkedList <Node> shouldVisit, int x, int y, Node parent) { var directions = MazeHelper.directions; for (int i = 0; i < directions.GetLength(0); i++) { int newX = x + directions[i, 0]; int newY = y + directions[i, 1]; bool isRoad = MazeHelper.IsRoad(_map, newX, newY); bool wasVisited = visited.Contains(new Tuple <int, int>(newX, newY)); if (isRoad && wasVisited == false) { shouldVisit.AddLast(new Node(newX, newY, parent)); } } }
public Maze(MazeInitModel model) { _playerName = model.PlayerName; _difficulty = (int)model.Difficulty; _width = model.MazeWidth; _height = model.MazeHeight; _cells = new CellState[_width * _height]; for (var x = 0; x < _height; x++) { for (var y = 0; y < _width; y++) { _cells[(x * _width) + y] = CellState.Initial; } } _rng = new Random(); MazeHelper.VisitCell(this, _rng.Next(_width * _height), _rng); }
private void AddVisitPoints(bool[,] maze, HashSet <Tuple <int, int> > points, int x, int y) { if (MazeHelper.IsPointInMaze(maze, x - 2, y) && MazeHelper.IsRoad(maze, x - 2, y) == false) { points.Add(new Tuple <int, int>(x - 2, y)); } if (MazeHelper.IsPointInMaze(maze, x + 2, y) && MazeHelper.IsRoad(maze, x + 2, y) == false) { points.Add(new Tuple <int, int>(x + 2, y)); } if (MazeHelper.IsPointInMaze(maze, x, y - 2) && MazeHelper.IsRoad(maze, x, y - 2) == false) { points.Add(new Tuple <int, int>(x, y - 2)); } if (MazeHelper.IsPointInMaze(maze, x, y + 2) && MazeHelper.IsRoad(maze, x, y + 2) == false) { points.Add(new Tuple <int, int>(x, y + 2)); } }
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; } }