Created
September 15, 2019 21:38
-
-
Save Jorger/20c56c403a80158b929a8987e84e457d 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
/** | |
* Establece una posición aleatoria de una figura en el escenario | |
* @param {*} size | |
* @param {*} board | |
*/ | |
const locateFigure = (size, board) => { | |
const boardSize = DIMENSION_BOARD - 1; | |
let position = []; | |
do { | |
let isValid = false; | |
const direction = randomNumber(0, 1); | |
const row = randomNumber(0, boardSize); | |
const col = randomNumber(0, boardSize); | |
const [ | |
finalRow, | |
finalCol, | |
initialOverlay, | |
finalOverlay, | |
isInside | |
] = figureRange(row, col, size, direction); | |
if (isInside) { | |
let overlay = false; | |
for (let i = initialOverlay[0]; i <= finalOverlay[0]; i++) { | |
for (let c = initialOverlay[1]; c <= finalOverlay[1]; c++) { | |
// Validar que este dentro del board | |
if (i >= 0 && i <= boardSize && c >= 0 && c <= boardSize) { | |
if (board[i][c] !== 0) { | |
overlay = true; | |
break; | |
} | |
} | |
} | |
if (overlay) { | |
break; | |
} | |
} | |
if (!overlay) { | |
isValid = true; | |
} | |
} | |
if (isValid) { | |
/* | |
direction de giro del barco | |
Fila | |
Columna | |
Fila final | |
Columna filal, | |
*/ | |
position = [ | |
randomNumber(!direction ? 1 : 3, !direction ? 2 : 4), | |
row, | |
col, | |
finalRow, | |
finalCol | |
]; | |
break; | |
} | |
} while (1); | |
return position; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment