<commit_before>//
// $Id$
//
// Vilya library - tools for developing networked games
// Copyright (C) 2002-2010 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/vilya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.threerings.puzzle.drop.data;

import java.util.Arrays;

import java.awt.Point;
import java.awt.Rectangle;

import com.samskivert.util.StringUtil;

import com.threerings.puzzle.data.Board;
import com.threerings.puzzle.drop.client.DropControllerDelegate;
import com.threerings.puzzle.drop.util.DropBoardUtil;

import static com.threerings.puzzle.Log.log;

/**
 * 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 class DropBoard extends Board
    implements DropPieceCodes
{
    /** The rotation constant for rotation around a central piece. */
    public static final int RADIAL_ROTATION = 0;

    /** The rotation constant for rotation wherein the block occupies the same columns when
     * rotating. */
    public static final int INPLACE_ROTATION = 1;

    /** An operation that does naught but clear pieces, which proves to be generally useful. */
    public static final PieceOperation CLEAR_OP = new PieceOperation () {
        public boolean execute (DropBoard board, int col, int row) {
            board.setPiece(col, row, PIECE_NONE);
            return true;
        }
    };

    /**
     * An interface to be implemented by classes that would like to apply some operation to each
     * piece in a column or row segment in the board.
     */
    public interface PieceOperation
    {
        /**
         * Called for each piece in the board segment the operation is being applied to.
         *
         * @return true if the operation should continue to be applied if being applied to multiple
         * pieces, or false if it should terminate after this application.
         */
        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.
     */
    public int getWidth()
    {
        return _bwid;
    }

    /**
     * Returns the height of the board in rows.
     */
    public int getHeight()
    {
        return _bhei;
    }

    /**
     * Returns the piece at the given column and row in the board.
     */
    public int getPiece (int col, int row)
    {
        try {
            return _board[coordsToIndex(col, row)];
        } catch (Exception e) {
            log.warning("Failed getting piece", "col", col, "row", row, e);
            return -1;
        }
    }

    /**
     * For boards that are always filled, this method is called to obtain pieces to fill the board.
     */
    public int getNextPiece ()
    {
        return PIECE_NONE;
    }

    /**
     * Returns the distance the piece at the given column and row can drop until it hits a
     * non-empty piece (defined as {@link #PIECE_NONE}).
     */
    public int getDropDistance (int col, int row)
    {
        int dist = 0;
        for (int yy = row + 1; yy < _bhei; yy++) {
            if (getPiece(col, yy) != PIECE_NONE) {
                return dist;
            }
            dist++;
        }
        return dist;
    }<commit_msg>We were only catching exceptions if we ran outside our piece array.<commit_after>//
// $Id$
//
// Vilya library - tools for developing networked games
// Copyright (C) 2002-2010 Three Rings Design, Inc., All Rights Reserved
// http://www.threerings.net/code/vilya/
//
// This library is free software; you can redistribute it and/or modify it
// under the terms of the GNU Lesser General Public License as published
// by the Free Software Foundation; either version 2.1 of the License, or
// (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA

package com.threerings.puzzle.drop.data;

import java.util.Arrays;

import java.awt.Point;
import java.awt.Rectangle;

import com.samskivert.util.StringUtil;

import com.threerings.puzzle.data.Board;
import com.threerings.puzzle.drop.client.DropControllerDelegate;
import com.threerings.puzzle.drop.util.DropBoardUtil;

import static com.threerings.puzzle.Log.log;

/**
 * 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 class DropBoard extends Board
    implements DropPieceCodes
{
    /** The rotation constant for rotation around a central piece. */
    public static final int RADIAL_ROTATION = 0;

    /** The rotation constant for rotation wherein the block occupies the same columns when
     * rotating. */
    public static final int INPLACE_ROTATION = 1;

    /** An operation that does naught but clear pieces, which proves to be generally useful. */
    public static final PieceOperation CLEAR_OP = new PieceOperation () {
        public boolean execute (DropBoard board, int col, int row) {
            board.setPiece(col, row, PIECE_NONE);
            return true;
        }
    };

    /**
     * An interface to be implemented by classes that would like to apply some operation to each
     * piece in a column or row segment in the board.
     */
    public interface PieceOperation
    {
        /**
         * Called for each piece in the board segment the operation is being applied to.
         *
         * @return true if the operation should continue to be applied if being applied to multiple
         * pieces, or false if it should terminate after this application.
         */
        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.
     */
    public int getWidth()
    {
        return _bwid;
    }

    /**
     * Returns the height of the board in rows.
     */
    public int getHeight()
    {
        return _bhei;
    }

    /**
     * Returns the piece at the given column and row in the board.
     */
    public int getPiece (int col, int row)
    {
        if (!inBounds(col, row)) {
            log.warning("Requested piece from invalid coordinate",
                "col", col, "row", row, "width", _bwid, "height", _bhei);
        }
        try {
            return _board[coordsToIndex(col, row)];
        } catch (Exception e) {
            log.warning("Failed getting piece", "col", col, "row", row, e);
            return -1;
        }
    }

    /**
     * For boards that are always filled, this method is called to obtain pieces to fill the board.
     */
    public int getNextPiece ()
    {
        return PIECE_NONE;
    }

    /**
     * Returns the distance the piece at the given column and row can drop until it hits a
     * non-empty piece (defined as {@link #PIECE_NONE}).
     */
    public int getDropDistance (int col, int row)
    {
        int dist = 0;
        for (int yy = row + 1; yy < _bhei; yy++) {
            if (getPiece(col, yy) != PIECE_NONE) {
                return dist;
            }
            dist++;
        }
        return dist;
    }
{'fraction_non_alphanumeric': 0.0858433734939759, 'fraction_numerical': 0.006224899598393574, 'mean_word_length': 2.2723390275952693, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2271831', 'n_tokens_mistral': 2872, 'n_tokens_neox': 2750, 'n_words': 1149}