⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 board.java

📁 非常好的java collapse游戏代码
💻 JAVA
字号:
/*
 * JCollapse - Java Collapse Game
 * Copyright (C) 2005 Erico Gon鏰lves Rimoli
 * 
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 * 
 * This program 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 General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
 */

package sourceforge.net.projects.jcollapse.engine.board;

import java.util.ArrayList;

import sourceforge.net.projects.jcollapse.engine.InvalidDimensionException;
import sourceforge.net.projects.jcollapse.engine.block.Block;

public class Board {
	private Block m_Board[];
	private int m_BoardWidth;
	private int m_BoardHeigth;
	private ArrayList<BoardListener> m_BoardListener = new ArrayList<BoardListener>(1);
	
	/**
	 * Constructor
	 * @param width the board width 
	 * @param height the board height
	 * @throws InvalidDimensionException fired when an invalid dimension is set
	 */
	public Board( int width, int height ) throws InvalidDimensionException {
		setBoardSize( width, height );
	}
	
	/**
	 * Add a board listener
	 * @param boardListener The BoardListener object
	 */
	public void addBoardListener( BoardListener boardListener ) {
		m_BoardListener.add( boardListener );
	}
	
	/**
	 * Remove a board listener
	 * @param boardListener The board listener object
	 * @return True if listener was removed
	 */
	public boolean removeBoardListener( BoardListener boardListener ) {
		return m_BoardListener.remove( boardListener );
	}
	
	private void fireBlockMovedEvent( int sourceX, int sourceY, int destX, int destY ) {
		for( BoardListener l : m_BoardListener )
			l.moved( sourceX, sourceY, destX, destY );
	}
	
	private void fireBlockSetedEvent( int posX, int posY ) {
		for( BoardListener l : m_BoardListener )
		l.seted( posX, posY );
	}
	
	private void fireBlockRemovedEvent( int posX, int posY ) {
		for( BoardListener l : m_BoardListener )
			l.removed( posX, posY );
	}
	
	/**
	 * Set the size of board
	 * @param width the width
	 * @param height the height
	 * @throws InvalidDimensionException fired when an invalid dimension is set
	 */
	void setBoardSize( int width, int height ) throws InvalidDimensionException {
		if( width < 1 || height < 1 )
			throw new InvalidDimensionException();
	
		m_BoardWidth = width;
		m_BoardHeigth = height;
		m_Board = new Block[ width * height ];
	}
	
	/**
	 * Get the board width
	 * @return the width
	 */
	public int getBoardWidth() {
		return m_BoardWidth;
	}
	
	/**
	 * Get the board height
	 * @return the height
	 */
	public int getBoardHeight() {
		return m_BoardHeigth;
	}
	
	/**
	 * Get block from specified position
	 * @param x the x position on board
	 * @param y the y position on board
	 * @return the Block object. Null for none.
	 */
	public Block getBlock( int x, int y ) {
		return getBlock( y * getBoardWidth() + x );
	}

	/**
	 * Get block from specified position
	 * @param pos position of block on board
	 * @return the Block object. Null for none
	 */
	public Block getBlock( int pos ) {
		return m_Board[pos];
	}
	
	/**
	 * Check if position on board is empty or not
	 * @param x the x position on board
	 * @param y the y position on board
	 * @return true if specified position is not null
	 */
	public boolean isNullBlock( int x, int y ) {
		return isNullBlock( y * getBoardWidth() + x );
	}

	/**
	 * Check if position on board is empty or not
	 * @param pos position on board
	 * @return true if specified position is not null
	 */
	public boolean isNullBlock( int pos ) {
		return m_Board[pos] == null;
	}
	
	/**
	 * Remove block from specified position on board
	 * @param x the x position on board
	 * @param y the y position on board
	 */
	public void clearBlock( int x, int y ) {
		clearBlock( y * getBoardWidth() + x );
	}
	
	/**
	 * Remove block from specified position on board
	 * @param pos position on board 
	 */
	public void clearBlock( int pos ) {
		setBlockA( pos, null );
		
		fireBlockRemovedEvent( pos % getBoardWidth(), pos / getBoardWidth() );
	}
	
	/**
	 * Set Block object on specified position on board
	 * @param x the x position on board
	 * @param y the y position on board
	 * @param block The Block object. You can set null for none
	 */
	public void setBlock( int x, int y, Block block ) {
		setBlock( y * getBoardWidth() + x, block );
	}

	/**
	 * Set Block object on specified position on board
	 * @param pos the position on board
	 * @param block The Block object. You can set null for none
	 */
	public void setBlock( int pos, Block block ) {
		setBlockA( pos, block );
		
		fireBlockSetedEvent( pos % getBoardWidth(), pos / getBoardWidth() );
	}
	
	private void setBlockA( int pos, Block block ) {
		m_Board[pos] = block;
	}
	
	/**
	 * Clear the board
	 */
	public void clear() {
		for( int i = 0; i < m_Board.length; i++ )
			clearBlock( i );
	}

	/**
	 * Move block from one to another position.
	 * @param srcX the source x position
	 * @param srcY the source y position
	 * @param destX the destiny x position
	 * @param destY the destiny y position
	 */
	public void moveBlock( int srcX, int srcY, int destX, int destY ) {
		moveBlock( srcY * getBoardWidth() + srcX, destY * getBoardWidth() + destX );
	}	

	/**
	 * Move block from one to another position.
	 * @param sourcePos the source position
	 * @param destPos the destiny position
	 */	
	public void moveBlock( int sourcePos, int destPos ) {
		Block o = getBlock( sourcePos );
		Block d = getBlock( destPos );
		
		if( o == null && d == null )
			return;
		
		setBlockA( sourcePos, d );
		setBlockA( destPos, o );
		
		fireBlockMovedEvent( sourcePos % getBoardWidth(), sourcePos / getBoardWidth(), 
							 destPos % getBoardWidth(), destPos / getBoardWidth() );
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -