/************************************************************* * A class to represent a moving square for the EightPuzzle GUI. *************************************************************/ public class Square { //Stores the number in this square: private String number; //The length of a side: private int side; //The font used: private PFont font; //x, y are the coordinates of the upper left corner: private int x, y; //row, column are the *intended* row and column for this square: private int row, column; //coordinates (x, y and row and column) when Square is originally created: private int origX, origY, origRow, origColumn; //records whether Square is at correct location: private boolean correctLocation; //records how far Square is from correct location: private int displacementX, displacementY; //an offset up or down just to put the whole array ofSquares at //the right y-coordinate: private int yOffset; //red, green and blue values of the Square's colors: private int r, g, b; /************************************************************* * Constructor takes the following parameters: * -- The number that goes in the square, * -- The size ("side") of the square, * -- The fond ("font") of the number going in the square, * -- The initial x and y coordinates of the square's upper left-hand corner * -- The initial row and column of the square *************************************************************/ public Square (String number, int side, int x, int y, int row, int column) { this.number = number; this.side = side; this.font = font; this.x = x; this.y = y; this.row = row; this.column = column; //Square's background color is random (but fixed for lifetime of square): r = (int) (220*random(0.4, 0.8)); g = (int) (220*random(0.4, 0.8)); b = (int) (220*random(0.4, 0.8)); //Adjust to get vertical positions of squares correct: yOffset = 3; correctLocation = true; //Store original x, y, row and column of square (for re-initializing): origX = x; origY = y; origRow = row; origColumn = column; font = loadFont("Monaco-48.vlw"); } /************************************************************* * Reset square to initial state (position, row, etc.) *************************************************************/ public void reset() { correctLocation = true; row = origRow; column = origColumn; x = origX; y = origY; } /************************************************************* * Helper method to record that the square is (a) not at its correct * location, and (b) determine how far it has to go (and in what * direction) to get there. *************************************************************/ private void setNotAtLocation () { correctLocation = false; displacementX = column*side - x; displacementY = row*side - y; } /************************************************************* * Precondition: Moving from (row, column) to (newRow, newColumn) * is a legal move. * Returns: true, if this square is the one that should move. *************************************************************/ //Side effect: If it is to move, the row of this square is // set to newRow and the column to newColumn. Furthermore, // invoke setNotAtLocation() (which see). public boolean canMove (int row, int column, int newRow, int newColumn) { if (this.row == row && this.column == column) { this.row = newRow; this.column = newColumn; setNotAtLocation(); return true; } return false; } /************************************************************* * Move the square, if it needs moving. *************************************************************/ public void move () { if (!correctLocation) { x = column*side; y = row*side + topMargin; //MODIFY: When you're ready to get the squares moving, comment out //the above two statements and implement the following pseudocode: // if we're not at the correct x-position then // if we want to increase x then // increment x // else // decrement x // if we're not at the correct y-position then // if we want to increase y then // increment y // else // decrement y //Assuming the above is all working, this sets correctLocation //to true when the Square reaches its destination: if (x == column*side && y == row*side + topMargin) correctLocation = true; } } /************************************************************* * Render the square. *************************************************************/ public void render () { //The square itself: fill(r, g, b); stroke(0); rect(x, y + yOffset, side, side); //The number on the square: fill(0, 0, 0); textFont(font, 48); text(number, x + side/4 + 2, y + 3*side/4); } }