//making a slide puzzle //variables for the coordinates of the pictures int[] x = new int[9]; int[] y = new int[9]; //store image PImage cute; //set up void setup() { size(640,428); cute = loadImage("cute.jpg"); image(cute,0,0); println(x[5]); //Setup puzzle pieces (was created by playing) x[0] = 0; x[1] = 1; x[2] = 1; x[3] = 2; x[4] = 1; x[5] = 2; x[6] = 0; x[7] = 0; x[8] = 2; y[0] = 0; y[1] = 2; y[2] = 1; y[3] = 1; y[4] = 0; y[5] = 0; y[6] = 1; y[7] = 2; y[8] = 2; } //split the picture into sections and find the mouse coordinate and swap pieces if adjacent void draw () { background(0); for (int j = 2; j>=0; j--) { for (int i = 2; i>=0; i--) { if (i == 2 && j == 2){ copy(cute, width/3*i, height/3*j, width/3, height/3, width/3*x[j*3+i], height/3*y[j*3+i],width/3, height/3); filter(GRAY); } else { copy(cute, width/3*i, height/3*j, width/3, height/3, width/3*x[j*3+i], height/3*y[j*3+i],width/3, height/3); } } } if (mousePressed){ int squareX = mouseX/(width/3), squareY = mouseY/(height/3); for(int a = 0; a<9; a++) { if (squareX == x[a] && squareY == y[a]) { if (a != 8){ if ((x[a]+1 == x[8] && y[a] == y[8]) ||(x[a]-1 == x[8] && y[a] == y[8]) ||(y[a]+1 == y[8] && x[a] == x[8]) ||(y[a]-1 == y[8] && x[a] == x[8])) { int oldax = x[a]; int olday = y[a]; x[a] = x[8]; y[a] = y[8]; x[8] = oldax; y[8] = olday; } } } } } }