diff --git a/src/java/com/threerings/puzzle/drop/data/DropBoard.java b/src/java/com/threerings/puzzle/drop/data/DropBoard.java index 979524a02..a39f12fcf 100644 --- a/src/java/com/threerings/puzzle/drop/data/DropBoard.java +++ b/src/java/com/threerings/puzzle/drop/data/DropBoard.java @@ -1,10 +1,11 @@ // -// $Id: DropBoard.java,v 1.5 2004/07/13 16:34:49 mdb Exp $ +// $Id: DropBoard.java,v 1.6 2004/08/19 00:55:27 eric Exp $ package com.threerings.puzzle.drop.data; import java.awt.Point; import java.awt.Rectangle; +import java.util.Arrays; import org.apache.commons.lang.StringUtils; @@ -16,11 +17,11 @@ import com.threerings.puzzle.drop.data.DropPieceCodes; import com.threerings.puzzle.drop.util.DropBoardUtil; /** - * An abstract class that provides for various useful logical operations - * to be enacted on a two-dimensional board and provides an easier - * mechanism for referencing pieces by position. + * A class that provides for various useful logical operations to be + * enacted on a two-dimensional board and provides an easier mechanism for + * referencing pieces by position. */ -public abstract class DropBoard extends Board +public class DropBoard extends Board implements DropPieceCodes { /** The rotation constant for rotation around a central piece. */ @@ -57,6 +58,44 @@ public abstract class DropBoard extends Board public boolean execute (DropBoard board, int col, int row); } + /** + * Constructs an empty drop board for use when unserializing. + */ + public DropBoard () + { + this(null, 0, 0); + } + + /** + * Constructs a drop board of the given dimensions with its + * pieces initialized to PIECE_NONE. + */ + public DropBoard (int bwid, int bhei) + { + this(new int[bwid*bhei], bwid, bhei); + fill(PIECE_NONE); + } + + /** + * Constructs a drop board of the given dimensions with its + * pieces initialized to the given piece. + */ + public DropBoard (int bwid, int bhei, int piece) + { + this(new int[bwid*bhei], bwid, bhei); + fill(piece); + } + + /** + * Constructs a drop board with the given board and dimensions. + */ + public DropBoard (int[] board, int bwid, int bhei) + { + _board = board; + _bwid = bwid; + _bhei = bhei; + } + /** * Returns the width of the board in columns. */ @@ -76,7 +115,17 @@ public abstract class DropBoard extends Board /** * Returns the piece at the given column and row in the board. */ - public abstract int getPiece (int col, int row); + public int getPiece (int col, int row) + { + try { + return _board[(row*_bwid) + col]; + } catch (Exception e) { + Log.warning("Failed getting piece [col=" + col + + ", row=" + row + ", error=" + e + "]."); + Log.logStackTrace(e); + return -1; + } + } /** * Returns the distance the piece at the given column and row can drop @@ -138,7 +187,7 @@ public abstract class DropBoard extends Board { return (col >= 0 && row >= 0 && col < getWidth() && row < getHeight()); } - + /** * Returns whether the specified block in the board is empty. The * block is allowed to occupy space off the top of the board as long @@ -339,7 +388,7 @@ public abstract class DropBoard extends Board setPiece(xx, ypos, pieces[xx]); } } - + /** * Returns true if the specified row (which count down, with zero at * the top of the board) contains any pieces. @@ -361,14 +410,28 @@ public abstract class DropBoard extends Board /** * Fills the board contents with the given piece. */ - public abstract void fill (int piece); + public void fill (int piece) + { + Arrays.fill(_board, (int)piece); + } /** * Sets the piece at the given coordinates. * * @return true if the piece was set, false if it was invalid. */ - public abstract boolean setPiece (int col, int row, int piece); + public boolean setPiece (int col, int row, int piece) + { + if (col >= 0 && row >= 0 && col < _bwid && row < _bhei) { + _board[(row*_bwid) + col] = (int)piece; + return true; + + } else { + Log.warning("Attempt to set piece outside board bounds " + + "[col=" + col + ", row=" + row + ", p=" + piece + "]."); + return false; + } + } /** * Sets the pieces within the specified rectangle to the given piece. @@ -519,24 +582,6 @@ public abstract class DropBoard extends Board } } - /** - * Returns the number of characters to which a single piece should be - * padded when dumping the board for debugging purposes. - */ - protected int getPadWidth () - { - return DEFAULT_PAD_WIDTH; - } - - /** - * Returns a string representation of the given piece for use when - * dumping the board. - */ - protected String formatPiece (int piece) - { - return (piece == PIECE_NONE) ? "." : String.valueOf(piece); - } - /** Returns a string representation of this instance. */ public String toString () { @@ -613,7 +658,84 @@ public abstract class DropBoard extends Board * Copies the contents of this board directly into the supplied board, * overwriting the destination board in its entirety. */ - public abstract void copyInto (DropBoard board); + public void copyInto (DropBoard board) + { + // make sure the target board is a valid target + if (board.getWidth() != _bwid || board.getHeight() != _bhei) { + Log.warning("Can't copy board into destination board with " + + "different dimensions [src=" + this + + ", dest=" + board + "]."); + return; + } + + // copy our pieces directly into the board, avoiding any unsightly + // object allocation which is largely the point of this method, + // after all. + int[] dest = ((DropBoard)board).getBoard(); + System.arraycopy(_board, 0, dest, 0, (_bwid*_bhei)); + } + + /** + * Returns the raw board data associated with this board. One + * shouldn't fiddle about with this unless one knows what one is + * doing. + */ + public int[] getBoard () + { + return _board; + } + + /** + * Sets the board data and board dimensions. + */ + public void setBoard (int[] board, int bwid, int bhei) + { + _board = board; + _bwid = bwid; + _bhei = bhei; + } + + /** + * Sets the board pieces. + */ + public void setBoard (int[] board) + { + int size = (_bwid*_bhei); + if (board.length < size) { + Log.warning("Attempt to set board with invalid data size " + + "[len=" + board.length + ", expected=" + size + "]."); + return; + } + + _board = board; + } + + // documentation inherited + public Object clone () + { + int size = _bwid*_bhei; + int[] data = new int[size]; + System.arraycopy(_board, 0, data, 0, size); + return new DropBoard(data, _bwid, _bhei); + } + + /** + * Returns the number of characters to which a single piece should be + * padded when dumping the board for debugging purposes. + */ + protected int getPadWidth () + { + return DEFAULT_PAD_WIDTH; + } + + /** + * Returns a string representation of the given piece for use when + * dumping the board. + */ + protected String formatPiece (int piece) + { + return (piece == PIECE_NONE) ? "." : String.valueOf(piece); + } /** An operation that sets the pieces in a board segment to a * specified array of pieces. */ @@ -687,6 +809,9 @@ public abstract class DropBoard extends Board protected boolean _error; } + /** The board data. */ + protected int[] _board; + /** The board dimensions in pieces. */ protected int _bwid, _bhei;