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

📄 simpleblock.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.block;

import java.util.Arrays;

import sourceforge.net.projects.jcollapse.engine.board.Board;

public abstract class SimpleBlock extends AbstractBlock {
	private int m_MinToRemove;
	
	private static int m_Visited[][] = null;
	private static int m_LastGeneratedVisitedValue;
	
	/**
	 * IMPORTANT:<br>
	 * Prefer to use this construtor because this class initialize internal array
	 * with width and height values. You get more agility in first call to the
	 * touched method.
	 * @param name the name of Block
	 * @param enable Block are enabled or not
	 * @param minToRemove minimum amount of blocks that can be considered to be removed when
	 * user touch the Board
	 * @param width the width of Board to initialize internal array
	 * @param height the height of Board to initialize internal array
	 */
	public SimpleBlock( String name, boolean enable, int minToRemove, int width, int height ) {
		this( name, enable, minToRemove );
		initializeVisitedArr( width, height );
	}
	
	/**
	 * Initialize this class.
	 * @param name the name of Block
	 * @param enable the Block can be enabled or not
	 * @param minToRemove minimum amount of Block's that can be considered to be removed when
	 * user touch the Board
	 */
	public SimpleBlock( String name, boolean enable, int minToRemove ) {
		super( name, enable );
		setMinBlocksToRemove( minToRemove );
	}
	
	/**
	 * The minimum amount of Block's that can be considered to be removed when user touch the Board
	 * @return minimum amount of Block's that can be considered to be removed when user touch the Board
	 */
	public int getMinBlocksToRemove() {
		return m_MinToRemove;
	}
	
	/**
	 * Set the minimum amount of Block's that game can remove from Board. If
	 * minimim amount of Block are affected, nothing happens
	 * @param minimum the minimum amount of Block's that can be removed from Board
	 */
	public void setMinBlocksToRemove( int minimum ) {
		m_MinToRemove = minimum;
	}
	

	/* (non-Javadoc)
	 * @see sourceforge.net.projects.jcollapse.engine.board.Block#touched(int, int, sourceforge.net.projects.jcollapse.engine.board.Board)
	 */
	public int touched( int x, int y, Board board ) {
		int visitValue;
		int markedBlocks;
		
		if( !isEnabled() )
			return 0;
		
		initializeVisitedArr( board.getBoardWidth(), board.getBoardHeight() );
		visitValue = getNextVisitedValue();

		Block block = board.getBlock( x, y );
		
		if( getMinBlocksToRemove() > 0 && ( markedBlocks = markBlocks( x, y, block, board, visitValue ) ) >= getMinBlocksToRemove() ) {
			removeMarkedBlocks( board, m_Visited, visitValue );
			return markedBlocks;
		}
		
		return 0;
	}
	
	/**
	 * These algorithm mark all Blocks that can be removed with a code that chance each
	 * time that this method run.
	 */
	private int markBlocks( int x, int y, Block block, Board board, int visitValue ) {
		if( board.getBlock( x, y ) != block )
			return 0;

		int ret = 1;
		m_Visited[x][y] = visitValue;
		
		//remove left
		if( x > 0 && m_Visited[x-1][y] != visitValue )
			ret += markBlocks( x - 1, y, block, board, visitValue );
		
		//remove top
		if( y < board.getBoardHeight() - 1 && m_Visited[x][y+1] != visitValue )
			ret += markBlocks( x, y + 1, block, board, visitValue );
		
		//remove right
		if( x < board.getBoardWidth() - 1 && m_Visited[x+1][y] != visitValue )
			ret += markBlocks( x+1, y, block, board, visitValue );

		//remove button
		if( y > 0 && m_Visited[x][y-1] != visitValue )
			ret += markBlocks( x, y - 1, block, board, visitValue );

		return ret;
	}
	
	private void initializeVisitedArr( int width, int height ) {
		if( m_Visited == null ||
				m_Visited.length != width ||
				m_Visited[0].length != height ||
				m_LastGeneratedVisitedValue == Integer.MAX_VALUE )
			{
				m_LastGeneratedVisitedValue = Integer.MIN_VALUE;
				m_Visited = new int[width][height];
				
				for( int i = 0; i < m_Visited.length; i++ )
					Arrays.fill( m_Visited[i], m_LastGeneratedVisitedValue );
			}
	}
	
	/**
	 * Get the next visited value
	 * @return the next visited value
	 */
	private int getNextVisitedValue() {
		return ( ++m_LastGeneratedVisitedValue ); 
	}
	
	/**
	 * Remove Block's marked to be removed
	 * @param board the Board object
	 * @param visitedArr the visited Array Object
	 * @param visitValue the value seted for visitValue
	 */
	private void removeMarkedBlocks( Board board, int visitedArr[][], int visitValue ) {
		for( int x = 0; x < visitedArr.length; x++ )
			for( int y = 0; y < visitedArr[x].length; y++ )
				if( visitedArr[x][y] == visitValue )
					board.clearBlock( x, y );
	}
}

⌨️ 快捷键说明

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