// // PuzzleBoard.java // Difficult Puzzle // // Created by Matthew Fahrenbacher on 8/13/04. // Copyright 2004 __MyCompanyName__. All rights reserved. // import java.awt.*; import javax.swing.*; import java.awt.event.*; import java.awt.font.*; public class PuzzleBoard extends JPanel implements ActionListener, MouseListener, KeyListener { private Rectangle[] rects; private static final int BORDER = 5; private static final int BOXES_WIDE = 4; private static final int BOXES_HIGH = 5; private int selectedPiece = -1; private int animatingPiece = -1; private Timer animationTimer = null; private int animationLocX = -1; private int animationLocY = -1; private int animationGoalX = -1; private int animationGoalY = -1; private int numMoves = 0; public PuzzleBoard() { super(); addMouseListener(this); addKeyListener(this); } public int numMoves() { return numMoves; } public boolean hasWon() { return rects[8].x == 1 && rects[8].y == 3; } public boolean isAnimating() { return animatingPiece != -1; } public void setUpBoard() { rects = new Rectangle[10]; //verticals rects[0] = new Rectangle(0,0,1,2); rects[1] = new Rectangle(0,2,1,2); rects[2] = new Rectangle(3,0,1,2); rects[3] = new Rectangle(3,2,1,2); //mini-squares rects[4] = new Rectangle(0,4,1,1); rects[5] = new Rectangle(1,3,1,1); rects[6] = new Rectangle(2,3,1,1); rects[7] = new Rectangle(3,4,1,1); //big square rects[8] = new Rectangle(1,0,2,2); //horizontal rects[9] = new Rectangle(1,2,2,1); numMoves = 0; } public void paint(Graphics g) { g.setColor(Color.white); g.fillRect(0,0,getWidth(),getHeight()); double boxWidth = getWidth()/BOXES_WIDE; double boxHeight = getHeight()/BOXES_HIGH; Rectangle r; for(int i=0; i animationGoalX) animationLocX-=5; if(animationLocY < animationGoalY) animationLocY+=5; else if(animationLocY > animationGoalY) animationLocY-=5; if(Math.abs(animationLocX - animationGoalX) < 5) animationLocX = animationGoalX; if(Math.abs(animationLocY - animationGoalY) < 5) animationLocY = animationGoalY; if(animationLocX == animationGoalX && animationLocY == animationGoalY) { animatingPiece = -1; animationTimer = null; } if(animatingPiece != -1) keepAnimating(); else { int col = (int)(animationGoalX/(getWidth()/BOXES_WIDE) + 0.5); int row = (int)(animationGoalY/(getHeight()/BOXES_HIGH) + 0.5); rects[selectedPiece] = new Rectangle(col, row, r.width, r.height); } repaint(); } public void mousePressed(MouseEvent e) { if(animatingPiece != -1) return; int x = e.getX(); int y = e.getY(); int col = (int)(x/(getWidth()/BOXES_WIDE) + 0.5); int row = (int)(y/(getHeight()/BOXES_HIGH) + 0.5); Rectangle r; selectedPiece = -1; for(int i=0; i col && r.y + r.height > row) { selectedPiece = i; break; } } repaint(); } public void mouseReleased(MouseEvent e){} public void mouseClicked(MouseEvent e){} public void mouseEntered(MouseEvent e){} public void mouseExited(MouseEvent e){} public void keyPressed(KeyEvent e) { moveSelected(e.getKeyChar()); } public void setSelection(int sel) { selectedPiece = sel; } public void moveSelected(char dir) { if(selectedPiece != -1 && animatingPiece == -1) { Rectangle r = rects[selectedPiece]; Rectangle newRect; switch(dir) { case 'w': newRect = new Rectangle(r.x, r.y-1, r.width, r.height); break; case 's': newRect = new Rectangle(r.x, r.y+1, r.width, r.height); break; case 'a': newRect = new Rectangle(r.x-1, r.y, r.width, r.height); break; case 'd': newRect = new Rectangle(r.x+1, r.y, r.width, r.height); break; default: System.out.println("Illegal Move: " + selectedPiece + " with key " + dir); return; } if(newRect.x < 0 || newRect.y < 0 || newRect.x + newRect.width > BOXES_WIDE || newRect.y + newRect.height > BOXES_HIGH) { System.out.println("Illegal Move: " + selectedPiece + " with key " + dir); return; } for(int i=0; i