/* puzzle_partial.pde Written by Justin Hsia Implement the game mechanics of a 15 Puzzle Clicking "Reset" will return the board to the ordered state. Reset button and game board implemented. "Sliding" not in place yet - currently testing mousePressed() */ 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 void setup() { size(400,400); // drawing canvas size textAlign(CENTER); // center align text (give coordinates for bottom center of where text will appear) background(0,100,100); // dark turquoise reset(); // use reset() function to initialize tiles[] } void draw() { drawReset(); // draw the reset button drawBoard(); // draw the game board } void mousePressed() { // detect clicking of Reset button if( mouseX >= 150 && mouseX <= 250 && mouseY >= 300 && mouseY <= 340 ) { 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 } } // 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[4*j+i], boardX+2+50*i+23, boardY+2+50*j+30); } } } } // draw the reset button at the bottom (positioned arbitrarily) void drawReset() { noFill(); stroke(255); 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