Holy crap Batman! It's the beginnings of refactoring the basic puzzle
stuff into Narya. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@2876 542714f4-19e9-0310-aa3c-eee0fc999fb1
This commit is contained in:
@@ -0,0 +1,163 @@
|
||||
//
|
||||
// $Id: PieceDestroyer.java,v 1.1 2003/11/26 01:42:34 mdb Exp $
|
||||
|
||||
package com.threerings.puzzle.drop.util;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
|
||||
import com.threerings.puzzle.drop.data.DropBoard;
|
||||
import com.threerings.puzzle.drop.data.DropBoard.PieceOperation;
|
||||
import com.threerings.puzzle.drop.data.DropPieceCodes;
|
||||
import com.threerings.puzzle.drop.data.SegmentInfo;
|
||||
|
||||
/**
|
||||
* Handles destroying contiguous piece segments in a drop board.
|
||||
*/
|
||||
public class PieceDestroyer
|
||||
implements DropPieceCodes
|
||||
{
|
||||
/**
|
||||
* An interface to be implemented by specific puzzles to detail the
|
||||
* parameters and methodology by which pieces are destroyed in the
|
||||
* puzzle board.
|
||||
*/
|
||||
public interface DestroyLogic
|
||||
{
|
||||
/**
|
||||
* Returns the minimum length of a contiguously piece segment that
|
||||
* should be destroyed.
|
||||
*/
|
||||
public int getMinimumLength ();
|
||||
|
||||
/**
|
||||
* Returns whether piece <code>a</code> is equivalent to piece
|
||||
* <code>b</code> for the purposes of including it in a contiguous
|
||||
* piece segment to be destroyed.
|
||||
*/
|
||||
public boolean isEquivalent (int a, int b);
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructs a piece destroyer that destroys pieces as specified by
|
||||
* the supplied destroy logic.
|
||||
*/
|
||||
public PieceDestroyer (DestroyLogic logic)
|
||||
{
|
||||
_logic = logic;
|
||||
}
|
||||
|
||||
/**
|
||||
* Destroys all pieces in the given board that are in contiguous rows
|
||||
* or columns of pieces, returning a list of {@link SegmentInfo}
|
||||
* objects detailing the destroyed piece segments. Note that a single
|
||||
* list is used internally to gather the segment info, and so callers
|
||||
* that care to modify the list should create their own copy; also,
|
||||
* the pieces in the segments may overlap, i.e., two segments may
|
||||
* contain the same piece.
|
||||
*/
|
||||
public List destroyPieces (DropBoard board, PieceOperation destroyOp)
|
||||
{
|
||||
// find all horizontally-oriented destroyed segments
|
||||
int bwid = board.getWidth(), bhei = board.getHeight();
|
||||
_destroyed.clear();
|
||||
int end = bwid - _logic.getMinimumLength() + 1;
|
||||
for (int yy = (bhei - 1); yy >= 0; yy--) {
|
||||
int xx = 0;
|
||||
while (xx < end) {
|
||||
xx += findSegment(board, HORIZONTAL, xx, yy);
|
||||
}
|
||||
}
|
||||
|
||||
// find all vertically-oriented destroyed segments
|
||||
end = _logic.getMinimumLength() - 2;
|
||||
for (int xx = 0; xx < bwid; xx++) {
|
||||
int yy = bhei - 1;
|
||||
while (yy > end) {
|
||||
yy -= findSegment(board, VERTICAL, xx, yy);
|
||||
}
|
||||
}
|
||||
|
||||
// destroy the pieces
|
||||
int size = _destroyed.size();
|
||||
for (int ii = 0; ii < size; ii++) {
|
||||
SegmentInfo si = (SegmentInfo)_destroyed.get(ii);
|
||||
board.applyOp(si.dir, si.x, si.y, si.len, destroyOp);
|
||||
}
|
||||
|
||||
return _destroyed;
|
||||
}
|
||||
|
||||
/**
|
||||
* Searches for a contiguously colored piece segment with the
|
||||
* specified orientation and root coordinates in the supplied board
|
||||
* and returns the length of the segment traversed.
|
||||
*/
|
||||
protected int findSegment (DropBoard board, int dir, int x, int y)
|
||||
{
|
||||
_lengthOp.reset();
|
||||
board.applyOp(dir, x, y, _lengthOp);
|
||||
int len = _lengthOp.getLength();
|
||||
if (len >= _logic.getMinimumLength()) {
|
||||
_destroyed.add(new SegmentInfo(dir, x, y, len));
|
||||
}
|
||||
return len;
|
||||
}
|
||||
|
||||
/**
|
||||
* A piece operation that calculates the length of the contiguous
|
||||
* piece segment to which it is applied.
|
||||
*/
|
||||
protected class SegmentLengthOperation
|
||||
implements PieceOperation
|
||||
{
|
||||
/**
|
||||
* Resets the operation for application to a new piece segment.
|
||||
*/
|
||||
public void reset ()
|
||||
{
|
||||
_len = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the length of the contiguous piece segment.
|
||||
*/
|
||||
public int getLength ()
|
||||
{
|
||||
return _len;
|
||||
}
|
||||
|
||||
// documentation inherited
|
||||
public boolean execute (DropBoard board, int col, int row)
|
||||
{
|
||||
int piece = board.getPiece(col, row);
|
||||
if (_len == 0) {
|
||||
_len = 1;
|
||||
_piece = piece;
|
||||
return (piece != PIECE_NONE);
|
||||
|
||||
} else if (_logic.isEquivalent(piece, _piece)) {
|
||||
_len++;
|
||||
return true;
|
||||
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/** The root segment piece. */
|
||||
protected int _piece;
|
||||
|
||||
/** The segment length in pieces. */
|
||||
protected int _len;
|
||||
}
|
||||
|
||||
/** The puzzle-specific destroy logic with which we do our business. */
|
||||
protected DestroyLogic _logic;
|
||||
|
||||
/** The piece operation used to determine segment length. */
|
||||
protected SegmentLengthOperation _lengthOp = new SegmentLengthOperation();
|
||||
|
||||
/** The list of destroyed piece segments. */
|
||||
protected ArrayList _destroyed = new ArrayList();
|
||||
}
|
||||
Reference in New Issue
Block a user