| |
---|
| | InvalidTiles = "Maze contains invalid tiles.", |
---|
| | PathWithoutDestination = "Pathways must have a corresponding destination tile.", |
---|
| | WrongShape = "Maze must be shaped like a rectangle.", |
---|
| | NoHolesAllowed = "Maze has null-tiles.", |
---|
| | TileIdsNotUnique = "Some tiles have the same id.", |
---|
| | StartPositionNotOnMap = "The starting tile has to be on the map.", |
---|
| | InvalidStartPosition = "The starting tile needs to have pathways.", |
---|
| | } |
---|
| | |
---|
| | export class MazeMap |
---|
| | { |
---|
| | public id:string; |
---|
| | public layout:MazeTile[][]; |
---|
| | public startPosition: number[]; |
---|
| | |
---|
| | constructor(id:string) |
---|
| | { |
---|
| | this.id = id; |
---|
| | this.layout = []; |
---|
| | this.startPosition = [0, 0]; |
---|
| | } |
---|
| | |
---|
| | public getRepresentation(): number[][] |
---|
| | { |
---|
| |
---|
| | } |
---|
| | return output; |
---|
| | } |
---|
| | |
---|
| | public validate(): string[] |
---|
| | { |
---|
| | const errors:string[] = []; |
---|
| | public validate(): MazeValidationError[] |
---|
| | { |
---|
| | const errors:MazeValidationError[] = []; |
---|
| | |
---|
| | if(this.id == null || this.id === "") { |
---|
| | errors.push(MazeValidationError.InvalidID); |
---|
| | } |
---|
| | |
---|
| | let hasInvalidTiles = false; |
---|
| | let hasNullTiles = false; |
---|
| | let hasIDDuplicates = false; |
---|
| | for (const row of this.layout) { |
---|
| | if(row == null) |
---|
| | { |
---|
| | hasNullTiles = true; |
---|
| |
---|
| | if(tile.validate().length > 0) |
---|
| | { |
---|
| | hasInvalidTiles = true; |
---|
| | } |
---|
| | |
---|
| | if(!hasIDDuplicates){ |
---|
| | for (const row2 of this.layout) { |
---|
| | if(row2 == null){ |
---|
| | continue; |
---|
| | } |
---|
| | for (const tile2 of row2) { |
---|
| | if(tile2 == null || tile2 === tile) |
---|
| | { |
---|
| | continue; |
---|
| | } |
---|
| | if(tile2.id === tile.id) |
---|
| | { |
---|
| | hasIDDuplicates = true; |
---|
| | } |
---|
| | } |
---|
| | } |
---|
| | } |
---|
| | } |
---|
| | } |
---|
| | if(hasInvalidTiles) |
---|
| | { |
---|
| |
---|
| | if(hasNullTiles) |
---|
| | { |
---|
| | errors.push(MazeValidationError.NoHolesAllowed); |
---|
| | } |
---|
| | if(hasIDDuplicates) |
---|
| | { |
---|
| | errors.push(MazeValidationError.TileIdsNotUnique); |
---|
| | } |
---|
| | |
---|
| | let hasWrongShape = false; |
---|
| | for (const row of this.layout) { |
---|
| | if(row == null || row.length !== this.layout[0].length) |
---|
| | { |
---|
| | errors.push(MazeValidationError.WrongShape); |
---|
| | hasWrongShape = true; |
---|
| | break; |
---|
| | if(this.layout.length === 0) |
---|
| | { |
---|
| | errors.push(MazeValidationError.WrongShape); |
---|
| | hasWrongShape = true; |
---|
| | } |
---|
| | else |
---|
| | { |
---|
| | for (const row of this.layout) { |
---|
| | if(row == null || row.length !== this.layout[0].length) |
---|
| | { |
---|
| | errors.push(MazeValidationError.WrongShape); |
---|
| | hasWrongShape = true; |
---|
| | break; |
---|
| | } |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | // Only do the complex path validation if there are no violations of shape requirements. |
---|
| |
---|
| | errors.push(MazeValidationError.PathWithoutDestination); |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | if(this.layout != null){ |
---|
| | if(this.startPosition[0] < 0 || this.startPosition[0] >= this.layout.length) |
---|
| | { |
---|
| | errors.push(MazeValidationError.StartPositionNotOnMap); |
---|
| | } |
---|
| | else if(this.layout.length>0 |
---|
| | && (this.startPosition[1] < 0 || this.startPosition[1] >= this.layout[0].length)) |
---|
| | { |
---|
| | errors.push(MazeValidationError.StartPositionNotOnMap); |
---|
| | } |
---|
| | else if(this.layout.length > 0 |
---|
| | && this.layout[this.startPosition[0]][this.startPosition[1]] != null) |
---|
| | { |
---|
| | const start = this.layout[this.startPosition[0]][this.startPosition[1]]; |
---|
| | let hasPaths = false; |
---|
| | for (const dir of start.paths) { |
---|
| | if(dir !== Direction.none){ |
---|
| | hasPaths = true; |
---|
| | break; |
---|
| | } |
---|
| | } |
---|
| | if(!hasPaths) |
---|
| | { |
---|
| | errors.push(MazeValidationError.InvalidStartPosition); |
---|
| | } |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | return errors; |
---|
| | } |
---|
| | } |
---|
| | |
---|
| | |