Tile sliding puzzle help
in
Programming Questions
•
10 months ago
Intro to programming class student here, currently have to complete a slider puzzle program. I've been working on it a lot over the past few days and have a decent amount of code done I think. I have the picture loading into tiles, it recognizes mouse input and the location of each tile relative to one empty tile, and I have a scramble button with no functionality yet. IDK what to do from here though :( I need to get the program to do three things, swap a tile I click with the empty tile when they are adjacent, scramble the tiles using the scramble button, and have the program recognize when the puzzle is put together correctly.
final color BG = color(204);
final String IMAGE_FILE = "simpsons.jpg";
final int NROWS = 4, NCOLS = 5;
PImage fullImage;
int fullWidth, fullHeight;
int tileWidth, tileHeight;
Board board;
Button button;
void setup()
{
fullImage = loadImage(IMAGE_FILE);
fullWidth = fullImage.width;
fullHeight = fullImage.height;
size (fullWidth + (fullWidth / 2), fullHeight);
tileWidth = fullWidth / NCOLS;
tileHeight = fullHeight / NROWS;
board = new Board();
button = new Button();
}
void draw()
{
background(BG);
board.draw();
board.mouseClicked();
button.draw();
button.mouseClicked();
}
class Board
{
Tile[][] board;
int emptyX;
int emptyY;
Board()
{
initialize();
}
void initialize()
{
board = new Tile[NROWS][NCOLS];
for (int row = 0; row < NROWS; row++) {
for (int col = 0; col < NCOLS; col++) {
int x = xFromCol(col);
int y = yFromRow(row);
PImage tileImage = fullImage.get(x, y, tileWidth, tileHeight);
board[row][col] = new Tile(tileImage, row, col);
}
}
board[NROWS - 1][NCOLS - 1].img = null;
emptyX = NCOLS - 1;
emptyY = NROWS - 1;
}
void swap(int col, int row)
{
}
int xFromCol(int col)
{
return col * tileWidth;
}
int yFromRow(int row)
{
return row * tileHeight;
}
int colFromMouseX()
{
return mouseX / tileWidth;
}
int rowFromMouseY()
{
return mouseY / tileHeight;
}
void draw()
{
for (int row = 0; row <NROWS; row++) {
int y = yFromRow(row);
for (int col = 0; col < NCOLS; col++) {
int x = xFromCol(col);
PImage tileImage = board[row][col].img;
if (tileImage != null)
image(tileImage, x, y);
line(x, 0, x, fullHeight);
}
line(0, y, fullWidth, y);
}
line(fullWidth - 1, 0, fullWidth - 1, fullHeight);
line(0, fullHeight - 1, fullWidth, fullHeight - 1);
}
void mouseClicked()
{
if (mousePressed) {
if (mouseButton == LEFT) {
if((colFromMouseX() == emptyX - 1) && (rowFromMouseY() == emptyY))
println(colFromMouseX() + ", " + rowFromMouseY());
}
{
if((colFromMouseX() == emptyX + 1) && (rowFromMouseY() == emptyY))
println(colFromMouseX() + ", " + rowFromMouseY());
}
{
if((colFromMouseX() == emptyX) && (rowFromMouseY() == emptyY - 1))
println(colFromMouseX() + ", " + rowFromMouseY());
}
{
if((colFromMouseX() == emptyX) && (rowFromMouseY() == emptyY + 1))
println(colFromMouseX() + ", " + rowFromMouseY());
}
}
}
}
class Button
{
void draw()
{
final color RECTANGLE = color(100);
fill(RECTANGLE);
stroke(0);
rect(fullWidth, 0, fullWidth / 2, fullHeight);
final color SCRAMBLE = color(0);
textSize(30);
fill(SCRAMBLE);
text("Scramble", fullWidth + 1, fullHeight / 2 + 10);
}
void mouseClicked()
{
if (mousePressed) {
if (mouseButton == LEFT) {
if (mouseX > fullWidth)
println("Yes");
}
}
}
}
class Tile
{
PImage img;
int initRow, initCol;
Tile(PImage img, int iRow, int iCol)
{
this.img = img;
this.initRow = iRow;
this.initCol = iCol;
}
}
1