Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@parzibyte
Created November 2, 2020 18:10
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 parzibyte/eaef6df7a158a6015002cf5983f2063a to your computer and use it in GitHub Desktop.
Save parzibyte/eaef6df7a158a6015002cf5983f2063a to your computer and use it in GitHub Desktop.
/**
*
* @param point An object that has x and y properties; the coordinates shouldn't be global, but relative to the point
* @returns {boolean}
*/
relativePointOutOfLimits(point) {
const absoluteX = point.x + this.globalX;
const absoluteY = point.y + this.globalY;
return this.absolutePointOutOfLimits(absoluteX, absoluteY);
}
/**
* @param absoluteX
* @param absoluteY
* @returns {boolean}
*/
absolutePointOutOfLimits(absoluteX, absoluteY) {
return absoluteX < 0 || absoluteX > Game.COLUMNS - 1 || absoluteY < 0 || absoluteY > Game.ROWS - 1;
}
// It returns true even if the point is not valid (for example if it is out of limit, because it is not the function's responsibility)
isEmptyPoint(x, y) {
if (!this.existingPieces[y]) return true;
if (!this.existingPieces[y][x]) return true;
if (this.existingPieces[y][x].taken) {
return false;
} else {
return true;
}
}
/**
* Check if a point (in the game board) is valid to put another point there.
* @param point the point to check, with relative coordinates
* @param points an array of points that conforms a figure
*/
isValidPoint(point, points) {
const emptyPoint = this.isEmptyPoint(this.globalX + point.x, this.globalY + point.y);
const hasSameCoordinateOfFigurePoint = points.findIndex(p => {
return p.x === point.x && p.y === point.y;
}) !== -1;
const outOfLimits = this.relativePointOutOfLimits(point);
if ((emptyPoint || hasSameCoordinateOfFigurePoint) && !outOfLimits) {
return true;
} else {
return false;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment