Last active
April 9, 2018 02:10
-
-
Save jpenney1/fbc19901f0b78f06914c4f6a7f321436 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const generate2dBoard = (cols, rows) => { | |
let board = generate2dArray(cols, rows); | |
let roomSpecs = { | |
heightMin: 5, | |
heightMax: 6, | |
widthMin: 5, | |
widthMax: 6, | |
paddingMin: 1, | |
paddingMax: 4 | |
}; | |
generateRooms(board, roomSpecs); | |
return board; | |
}; | |
const generate2dArray = (cols, rows) => { | |
return Array.apply(null,Array(rows-1)).map((_,i)=> | |
Array.apply(null,Array(cols-1)).map((_,i)=>1) | |
); | |
}; | |
const generateRoom = roomSpecs => { | |
let heightMax = roomSpecs.heightMax; | |
let heightMin = roomSpecs.heightMin; | |
let widthMax = roomSpecs.widthMax; | |
let widthMin = roomSpecs.widthMin; | |
let randomHeight = Math.ceil((Math.random()*(heightMax-heightMin+1))+heightMin); | |
let randomWidth = Math.ceil((Math.random()*(widthMax-widthMin+1))+widthMin); | |
let room = generate2dArray(randomWidth, randomHeight); | |
// printBoard(room); | |
return room; | |
}; | |
const generatePadding = roomSpecs => { | |
let paddingMin = roomSpecs.paddingMin; | |
let paddingMax = roomSpecs.paddingMax; | |
let randomPadding = Math.ceil((Math.random()*(paddingMax-paddingMin))+paddingMin); | |
return randomPadding; | |
}; | |
const generateRooms = (board, roomSpecs) => { | |
let roomsPlaced = 0; | |
let room1 = generateRoom(roomSpecs); | |
placeRoom(board, room1, roomSpecs, roomsPlaced); | |
roomsPlaced++; | |
let room2 = generateRoom(roomSpecs); | |
placeRoom(board, room2, roomSpecs, roomsPlaced); | |
roomsPlaced++; | |
return board; | |
}; | |
const changePosition = (coord, direction) => { | |
return (direction === 'increasing') ? (coord + 1) | |
: (direction === 'decreasing') ? (coord - 1) | |
: null; | |
}; | |
const horizontalSearch = (board, startingRow, startingCol, direction, width) => { | |
let start = 0, | |
end = width + 1, | |
nextCol = startingCol; | |
while(start < end) { | |
nextCol = changePosition(nextCol, direction); | |
let isOnBoard = (nextCol >= 0 && startingRow >= 0); | |
if(isOnBoard && board[startingRow][nextCol] === 0) { | |
return true; | |
} | |
start++; | |
} | |
return false; | |
}; | |
const verticalSearch = (board, startingRow, startingCol, direction, height) => { | |
let start = 0, | |
end = height + 1, | |
nextRow = startingRow; | |
while(start < end) { | |
nextRow = changePosition(nextRow, direction); | |
let isOnBoard = (nextRow >= 0 && startingCol >= 0); | |
if(isOnBoard && Number(board[nextRow][startingCol]) === 0) { | |
return true; | |
} | |
start++; | |
} | |
return false; | |
}; | |
const doesRoomHaveCollisions = (board, coordinates, width, height, iterations) => { | |
// to do a square search, I need to place start by creating the frist | |
// space to search in the algorighm | |
let initialRow = coordinates[0], | |
initialCol = coordinates[1], | |
counter = 1; | |
while (counter <= iterations) { | |
let topStartingRow = initialRow - counter, | |
topStartingCol = initialCol - counter, | |
rightStartingRow = topStartingRow, | |
rightStartingCol = topStartingCol + (width+counter), | |
bottomStartingRow = rightStartingRow + (height+counter), | |
bottomStartingCol = rightStartingCol, | |
leftStartingRow = bottomStartingRow, | |
leftStartingCol = bottomStartingCol - (width+counter); | |
let topHasCollision = horizontalSearch(board, topStartingRow, topStartingCol, 'increasing', width), | |
rightHasCollision = verticalSearch(board, rightStartingRow, rightStartingCol, 'increasing', height), | |
bottomHasCollision = horizontalSearch(board, bottomStartingRow, bottomStartingCol, 'decreasing', width), | |
leftHasCollision = verticalSearch(board, leftStartingRow, leftStartingCol, 'decreasing', height); | |
if(topHasCollision || rightHasCollision || bottomHasCollision || leftHasCollision) { | |
return true; | |
} | |
counter++; | |
} | |
return false; | |
}; | |
const placementIsValid = (board, room, coordinates, padding) => { | |
let roomHeight = room.length; | |
let roomWidth = room[0].length; | |
let startingRow = coordinates[0]; | |
let startingCol = coordinates[1]; | |
let placedWidth = roomWidth + startingCol; | |
let placedHeight = roomHeight + startingRow; | |
let boardWidth = board[0].length; | |
let boardHeight = board.length; | |
if(board[startingRow][startingCol] && placedWidth < boardWidth && placedHeight < boardHeight) { | |
console.log('here is the placement logic padding: ', padding); | |
let roomHasCollision = doesRoomHaveCollisions(board, coordinates, roomWidth, roomHeight, padding); | |
console.log('roomHasCollision: ', roomHasCollision); | |
if(!roomHasCollision) { | |
return true; | |
} | |
} else { | |
return false; | |
} | |
}; | |
const placeRoomAt = (board, room, coordinates) => { | |
let startingRow = coordinates[0]; | |
let startingCol = coordinates[1]; | |
let endingRow = startingRow + room.length; | |
let endingCol = startingCol + room[0].length; | |
for(let i = startingRow; i < endingRow; i++) { | |
for(let j = startingCol; j < endingCol; j++) { | |
board[i][j] = 0; | |
} | |
} | |
return board; | |
}; | |
const placeRoom = (board, room, roomSpecs, roomsPlaced) => { | |
let padding = generatePadding(roomSpecs); | |
for(let i = 1; i < board.length-1; i++) { | |
let row = board[i]; | |
for(let j = 1; j < row.length-1; j++) { | |
let col = board[i][j]; | |
let coordinates = [i, j]; | |
let checkPlacement = placementIsValid(board, room, coordinates, padding); | |
if(checkPlacement) { | |
placeRoomAt(board, room, coordinates); | |
i = board.length; | |
j = row.length; | |
} | |
} | |
} | |
return board; | |
}; | |
const printBoard = board => { | |
let boardString = ''; | |
board.forEach(row=> { | |
row.forEach(col=>boardString += col); | |
boardString += '\n'; | |
}); | |
console.log(boardString); | |
}; | |
let board = generate2dBoard(50, 25); | |
printBoard(board); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment