/* puzzle.pde Written by Justin Hsia Implement the game mechanics of a 15 Puzzle Click an adjacent tile to "slide" it into the open position. Clicking "Reset" will return the board to the ordered state. Color of reset button changes while mouse is hovering over it. */ int boardX = 100; // x-position of upper-left corner of game board int boardY = 50; // y-position of upper-left corner of game board int[] tiles = new int[16]; // array to hold state of game board int gridX, gridY; // variables to detect grid coordinates when user clicks on game board int openX, openY; // variables to store current grid coordinate of open tile void setup() { size(400,400); // drawing canvas size textAlign(CENTER); // center align text (give coordinates for bottom center of where text will appear) reset(); // use reset() function to initialize tiles[] } void draw() { background(0,100,100); // dark turquoise drawReset(); // draw the reset button drawBoard(); // draw the game board } void mousePressed() { int temp; // temporary variable for swapping/sliding // detect clicking of Reset button if( overReset() ) { //println("Reset clicked"); // for testing purposes reset(); } // detect clicking on game board if( mouseX >= boardX && mouseX <= boardX+200 && mouseY >= boardY && mouseY <= boardY+200 ) { //println("Grid clicked"); // for testing purposes gridX = (mouseX-boardX)/50; // x grid coordinate of clicked tile gridY = (mouseY-boardY)/50; // y grid coordinate of clicked tile println("gridX = " + gridX); // for testing purposes println("gridY = " + gridY); // for testing purposes // clicked tile is a neighbor if difference in coordinates is exactly 1 if( abs(openX-gridX) + abs(openY-gridY) == 1 ) { //println("I think this is a neighbor."); // for testing purposes // "slide" by doing swap // a - tiles[index(openX,openY)], b - tiles[index(gridX,gridY)] temp = tiles[index(openX,openY)]; tiles[index(openX,openY)] = tiles[index(gridX,gridY)]; tiles[index(gridX,gridY)] = temp; // update open coordinates openX = gridX; openY = gridY; } } } // returns true if mouse is currently over the Reset button boolean overReset() { return mouseX >= 150 && mouseX <= 250 && mouseY >= 300 && mouseY <= 340; } // draw the game board: grid of tiles void drawBoard() { // draw border fill(0,100,200); rect(boardX,boardY,200,200); // draw tiles and tile numbers for(int i=0; i<4; i=i+1) { // loop over columns (x grid position) for(int j=0; j<4; j=j+1) { // loop over rows (y grid position) if(tiles[4*j+i] > 0) { // skip open square // draw tile fill(0,200,200); // bright turquoise rect(boardX+2+50*i,boardY+2+50*j,46,46); // display tile number fill(255); // white text text(tiles[index(i,j)], boardX+2+50*i+23, boardY+2+50*j+30); } } } } // take in grid coordinates and calculate array index int index(int col, int row) { return 4*row+col; } // draw the reset button at the bottom (positioned arbitrarily) void drawReset() { stroke(255); if (overReset()) { fill(255,0,0); // red fill } else { fill(0,100,200); // light blue fill } rect(width/2-50,300,100,40); textSize(32); fill(255); text("Reset", width/2, 330); } // reset tiles to be in order with open tile at position (0,0) void reset() { for(int i=0; i