-
-
Save parzibyte/eaef6df7a158a6015002cf5983f2063a to your computer and use it in GitHub Desktop.
This file contains 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
/** | |
* | |
* @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