Created
March 22, 2019 14:13
-
-
Save ssaurel/360d553a32a96a875398bea3bba1bfee to your computer and use it in GitHub Desktop.
Game of Fifteen tutorial on the SSaurel's Channel
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
// get position of the click | |
int ex = e.getX() - margin; | |
int ey = e.getY() - margin; | |
// click in the grid ? | |
if (ex < 0 || ex > gridSize || ey < 0 || ey > gridSize) | |
return; | |
// get position in the grid | |
int c1 = ex / tileSize; | |
int r1 = ey / tileSize; | |
// get position of the blank cell | |
int c2 = blankPos % size; | |
int r2 = blankPos / size; | |
// we convert in the 1D coord | |
int clickPos = r1 * size + c1; | |
int dir = 0; | |
// we search direction for multiple tile moves at once | |
if (c1 == c2 && Math.abs(r1 - r2) > 0) | |
dir = (r1 - r2) > 0 ? size : -size; | |
else if (r1 == r2 && Math.abs(c1 - c2) > 0) | |
dir = (c1 - c2) > 0 ? 1 : -1; | |
if (dir != 0) { | |
// we move tiles in the direction | |
do { | |
int newBlankPos = blankPos + dir; | |
tiles[blankPos] = tiles[newBlankPos]; | |
blankPos = newBlankPos; | |
} while(blankPos != clickPos); | |
tiles[blankPos] = 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment