/* Copyright 1996 Rujith de Silva. This code is in the public domain. */ package NET.industry.pento; import java.awt.*; import java.util.*; import NET.industry.util.*; import NET.industry.pento.*; public class BoardPiece extends Piece { private Board board; protected int xpos, ypos; private int status = 0; public BoardPiece (int init[][], Board b, int x, int y) { super (init); board = b; xpos = x; ypos = y; if (pieceClear ()) { setBottom (); setStatus (0); updatePiece (); board.repaint (); } } public int getStatus () { return status; } public void setStatus (int s) { if (s >= 0 && s < 3 && s != status) { status = s; updatePiece (); board.repaint (); } } private void updatePiece () { for (int i = 0; i < 5; ++i) board.updateSquare (current[i][0] + xpos, current[i][1] + ypos); } private boolean pieceInBounds () { for (int i = 0; i < 5; ++i) if (! board.inBounds (current[i][0] + xpos, current[i][1] + ypos)) return false; return true; } private boolean pieceClear () { for (int i = 0; i < 5; ++i) { int ix = current[i][0] + xpos; int iy = current[i][1] + ypos; if (! board.inBounds (ix, iy)) return false; BoardPiece bp = board.piece (ix, iy); if (bp != null && bp != this) return false; } return true; } public boolean place (int x, int y) { if (board == null) return false; for (int i = 0; i < 5; ++i) { if (! board.inBounds (current[i][0] + x, current[i][1] + y)) return false; BoardPiece p = board.piece (x, y); if (p != null && p != this) return false; } for (int i = 0; i < 5; ++i) { int ix = current[i][0] + xpos; int iy = current[i][1] + ypos; board.board[ix][iy] = this; board.boardtop[ix][iy] = null; } xpos = x; ypos = y; setStatus (0); return true; } public boolean toTop () { clearBottom (); setTop (); setStatus (2); return true; } public boolean toBottom () { if (! pieceClear ()) return false; clearTop (); setBottom (); setStatus (0); return true; } private void clearBottom () { updatePiece (); for (int i = 0; i < 5; ++i) board.board[current[i][0] + xpos][current[i][1] + ypos] = null; } private void setBottom () { updatePiece (); for (int i = 0; i < 5; ++i) board.board[current[i][0] + xpos][current[i][1] + ypos] = this; } private void clearTop () { updatePiece (); for (int i = 0; i < 5; ++i) board.boardtop[current[i][0] + xpos][current[i][1] + ypos] = null; } private void setTop () { for (int i = 0; i < 5; ++i) board.boardtop[current[i][0] + xpos][current[i][1] + ypos] = this; updatePiece (); } public boolean moveTop (int x, int y) { if (status != 2) return false; clearTop (); xpos = x; ypos = y; setTop (); board.repaint (); return true; } public void doSwap () { if (status != 2) return; clearTop (); boolean oldSwap = getSwap (); setSwap (! oldSwap); if (! pieceInBounds ()) setSwap (oldSwap); setTop (); board.repaint (); } public void doMove (int x, int y) { if (status != 2) return; clearTop (); int oldxpos = xpos; int oldypos = ypos; xpos = x; ypos = y; if (! pieceInBounds ()) { xpos = oldxpos; ypos = oldypos; } setTop (); board.repaint (); } public void changeDir (int dir) { if (status != 2) return; clearTop (); int olddir = direction (); setDirection (dir); if (! pieceInBounds ()) setDirection (olddir); setTop (); board.repaint (); } }