Skip to content

Instantly share code, notes, and snippets.

@Jorger
Created September 15, 2019 21:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Jorger/20c56c403a80158b929a8987e84e457d to your computer and use it in GitHub Desktop.
Save Jorger/20c56c403a80158b929a8987e84e457d to your computer and use it in GitHub Desktop.
/**
* 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