public class Piece { Board myBoard; int x,y; int ox=-1,oy=-1; long tickLastMoved = -1; float portion = 2.0/3.0; color c; boolean selected = false; public Piece(Board _board, int _x, int _y) { myBoard = _board; x = _x; y = _y; c = color(200, 100, 250,pieceAlpha); } public boolean moveRandom() { int nx, ny; HashSet set = getNeighbors(myBoard, x, y); Tile t; ArrayList filled = new ArrayList(); Iterator it = set.iterator(); while (it.hasNext()) { t = (Tile) it.next(); if (!t.empty()) filled.add(t); } set.removeAll(filled); if (set.size()>0) { int choice = (int) random(set.size()); t = (Tile) set.toArray()[choice]; nx = t.x; ny = t.y; myBoard.movePiece(this,nx,ny); return true; } return false; } public boolean moveGreedy() { int nx, ny; HashSet set = getNeighbors(myBoard, x, y); Tile t; ArrayList filled = new ArrayList(); Iterator it = set.iterator(); while (it.hasNext()) { t = (Tile) it.next(); if (!t.empty()) filled.add(t); } set.removeAll(filled); if (set.size()>0) { float best = myBoard.filled[x][y].distanceToExit; ArrayList cands = new ArrayList(); it = set.iterator(); while (it.hasNext()) { t = (Tile) it.next(); if (t.distanceToExit < best) { cands.clear(); cands.add(t); best = t.distanceToExit; } else if (best0) { int choice = (int) random(cands.size()); t = (Tile) cands.get(choice); nx = t.x; ny = t.y; myBoard.movePiece(this,nx,ny); return true; } } return false; } public boolean exit() { Iterator it = myBoard.exits.iterator(); Tile t; while(it.hasNext()) { t = (Tile) it.next(); if ((x == t.x) && (y == t.y) ) { //if (pickedPiece == myBoard.get(x,y).get(0)) pickedPiece = null; myBoard.remove(x,y); x = -1; y = -1; penniesExited += 1; return true; } } return false; } public boolean move() { if (moveGreedy()) { tickLastMoved = ticks; return true; } return false; } public boolean legal(int _x, int _y) { return (myBoard.empty(_x,_y)) && ((sq(x-_x) + sq(y-_y)) < 4); } public void draw() { float hTS = myBoard.tileSize/2; float pX = myBoard.getSX(x) + hTS; float pY = myBoard.getSY(y) + hTS; float pOX = myBoard.getSX(ox) + hTS; float pOY = myBoard.getSY(oy) + hTS; float pR = myBoard.tileSize*portion; noStroke(); fill(c); ellipse(pX,pY,max(pR,1),max(pR,1)); /* if (myBoard.mouseIn(x,y)) { fill(0,0,0); ellipse(pX,pY,pR/2,pR/2); } */ if (ox>=0 && oy>=0 && ( tickLastMoved == ticks)) { fill(c); ellipse(pOX,pOY,max(pR,1)/2,max(pR,1)/2); drawArrow(pX,pY,pX-pOX,pY-pOY,myBoard.tileSize,color(255,255,0)); } } }