/// <summary> /// Create a matrix of map pieces from Game object /// </summary> /// <param name="game"></param> private void GetWallsFromGame(Game.Game game, IReadOnlyCollection<CellLocation> doors) { // Populate the map matrix from the given list of walls foreach (var item in game.WallsAndDoors) { var x = item.X + 1; var y = item.Y + 1; _map[x, y] = new MapCellDetail(_map, x, y, BasicMapPiece.SingleWall); } foreach (var item in doors) { var x = item.X + 1; var y = item.Y + 1; _map[x, y] = new MapCellDetail(_map, x, y, BasicMapPiece.Door); } for (int y = 0; y < _height; y++) { for (int x = 0; x < _width; x++) { if (_map[x, y] == null) _map[x, y] = new MapCellDetail(_map, x, y, BasicMapPiece.Space); } } }
public BoardPiece FindBoardPiece(MapCellDetail board) { foreach (var pattern in _patterns) { if (pattern.DoCellsMatchPattern(board)) { return(pattern.BoardPiece); } } return(BoardPiece.Undefined); // Failed to match - loop through again to help debug layout //#if DEBUG // foreach (var pattern in _patterns) // { // if (pattern.DoCellsMatchPattern(board)) // return pattern.BoardPiece; // } //#endif // throw new Exception($"No matching board piece pattern @ c{board.X},{board.Y}"); }
/// <summary> /// Flood fill the matrix with the given type /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="type"></param> private void Flood(MapCellDetail cell, BasicMapPiece type) { if (cell.Filled || !cell.IsSpace) { return; } cell.Filled = true; cell.BasicType = type; if (cell.X > 0) { Flood(cell.CellLeft, type); } if (cell.X < _width - 1) { Flood(cell.CellRight, type); } if (cell.Y > 0) { Flood(cell.CellAbove, type); } if (cell.Y < _height - 1) { Flood(cell.CellBelow, type); } }
/// <summary> /// Find the pattern /// </summary> /// <param name="cell"></param> /// <returns></returns> private char TunnelPattern(MapCellDetail cell) { var pattern = (cell.CellAbove.IsWall ? "V" : "") + (cell.CellBelow.IsWall ? "V" : "") + (cell.CellLeft.IsWall ? "H" : "") + (cell.CellRight.IsWall ? "H" : ""); return pattern.Length == 1 ? pattern[0] : ' '; }
/// <summary> /// Find the ghost walls by starting from a ghost door and tracing /// </summary> private void TraceGhostHouseWalls(MapCellDetail cell) { if (cell.Filled || (cell.BasicType!=BasicMapPiece.SingleWall && cell.BasicType!=BasicMapPiece.Door)) return; if (cell.BasicType == BasicMapPiece.SingleWall) { cell.BasicType = BasicMapPiece.GhostWall; } cell.Filled = true; TraceGhostHouseWalls(cell.CellAbove); TraceGhostHouseWalls(cell.CellBelow); TraceGhostHouseWalls(cell.CellLeft); TraceGhostHouseWalls(cell.CellRight); }