“15 puzzle” by RedstoneTim
https://openprocessing.org/sketch/953828
License CreativeCommons Attribution ShareAlike
https://creativecommons.org/licenses/by-sa/3.0
{{filePath}}
{{width}} x {{height}}
Report Sketch
Oh, that naughty sketch! Please let us know what the issue is below.
Apply Template
Applying this template will reset your sketch and remove all your changes. Are you sure you would like to continue?
Report Sketch
Report Comment
Please confirm that you would like to report the comment below.
We will review your submission and take any actions necessary per our Community Guidelines. In addition to reporting this comment, you can also block the user to prevent any future interactions.
Please report comments only when necessary. Unnecessary or abusive use of this tool may result in your own account being suspended.
Are you sure you want to delete your sketch?
Any files uploaded will be deleted as well.
Delete Comment?
This will also delete all the replies to this comment.
Delete this tab? Any code in it will be deleted as well.
Select a collection to submit your sketch
We Need Your Support
Since 2008, OpenProcessing has provided tools for creative coders to learn, create, and share over a million open source projects in a friendly environment.
Niche websites like ours need your continued support for future development and maintenance, while keeping it an ad-free platform that respects your data and privacy!
Please consider subscribing below to show your support with a "Plus" badge on your profile and get access to many other features!
Move tiles by hovering over them with your mouse.
CC Attribution ShareAlike
15 puzzle
xxxxxxxxxx
int n = 4; // size of the puzzle
float pieceSize = 730 / n;
Piece[] pieces = new Piece[n][n];
float xOffset = (width - (n * pieceSize)) / 2;
float yOffset = (height - (n * pieceSize)) / 2;
EmptyPiece emptyPiece = new EmptyPiece(n);
void setup() {
fullscreen();
textAlign(CENTER, CENTER);
textSize(pieceSize / 3.2);
for(int y = 0; y < pieces.length; y++) {
for(int x = 0; x < pieces[y].length; x++) {
if((x == y) && (x == (n-1))) {
pieces[x][y] = emptyPiece;
} else {
pieces[x][y] = new Piece(x, y);
}
}
}
xOffset = (width - (n * pieceSize)) / 2;
yOffset = (height - (n * pieceSize)) / 2;
}
void draw() {
background(255);
for(int y = 0; y < pieces.length; y++) {
for(int x = 0; x < pieces[y].length; x++) {
pieces[x][y].draw(x, y);
}
}
}
void mouseMoved() {
int x = int(floor((mouseX - xOffset) / pieceSize));
int y = int(floor((mouseY - yOffset) / pieceSize));
if((x >= 0) && (x < n) && (y >= 0) && (y < n)) {
int xDiff = abs(x - emptyPiece.x);
int yDiff = abs(y - emptyPiece.y);
if(((xDiff == 1) && (yDiff == 0)) || ((xDiff == 0) && (yDiff == 1))) {
pieces[x][y].move();
}
}
}
class Piece {
final int initialX, initialY;
int x, y;
String name;
Color drawColor;
Piece(int x, int y) {
this.initialX = x;
this.initialY = y;
this.x = x;
this.y = y;
this.name = x + (y * n) + 1;
this.drawColor = color(255 / n * x, 255 / n * y, 200);
}
void draw(int x, int y) {
float xCoord = xOffset + (x * pieceSize);
float yCoord = yOffset + (y * pieceSize);
fill(drawColor);
rect(xCoord + 5, yCoord + 5, pieceSize - 5, pieceSize - 5);
fill(0);
text(name, xCoord + 5, yCoord + 5, pieceSize - 5, pieceSize - 5);
}
void move() {
int emptyX = emptyPiece.x;
int emptyY = emptyPiece.y;
// move empty piece
pieces[x][y] = emptyPiece;
emptyPiece.x = x;
emptyPiece.y = y;
// move this piece
pieces[emptyX][emptyY] = this;
x = emptyX;
y = emptyY;
}
}
class EmptyPiece extends Piece {
EmptyPiece(int n) {
super(n - 1, n - 1);
}
void draw(int x, int y) { }
}
See More Shortcuts