package Core;

import java.util.HashSet;
import java.util.Set;

/**
 * A bomb in the game
 *
 */
public class Bomb extends PlayboardObject {
	/**
	 * Constants
	 */
	private final int MIN_FIELD = 0;
	private final int EXPLODE_AT = 0;
	private final int ONE_FIELD = 1;
	/**
	 * Instance variables
	 */
	private int counter;
	private int explosionRadius;
	private int playerId;
	private boolean exploded = false;
	
	/**
	 * Constructor
	 * @param counter			Interations needed for a bomb to explode
	 * @param field				Field where the bomb lies on
	 * @param explosionRadius	Count of fields a bomb causes to explode in every direction
	 * @param playerId			Id of the player who laid the bomb
	 */
	public Bomb(int counter, Field field, int explosionRadius, int playerId) {	
		super(field);
		this.playerId = playerId;
		this.counter = counter;
		this.explosionRadius = explosionRadius;
	}
	
	/**
	 * Constructor for cloning
	 * @param bomb	another bomb
	 */
	private Bomb(Bomb bomb) {
		super(bomb.getField().clone());
		this.counter = bomb.counter;
		this.explosionRadius = bomb.explosionRadius;
		this.exploded = bomb.exploded;
	}
	
	/**
	 * Returns a copie of the object
	 */
	public Bomb clone() {
		return new Bomb(this);
	}

	/********* Getter *********/
	
	/**
	 * Returns the count of fields in X and Y direction of the playboard
	 * @return explosionRadius
	 */
	public int getExplosionRadius() {
		return explosionRadius;
	}
	
	/**
	 * Returns the id of the player who laid the bomb
	 * @return playerId
	 */
	public int getPlayerId() {
		return playerId;
	}

	/**
	 * Is the bomb already exploded?
	 * @return exploded?
	 */
	public boolean isExploded() {
		return exploded;
	}
	
	/**
	 * Returns the amount of interations left for the bomb to explode
	 * @return counter
	 */	
	public int getCounter() {
		return counter;
	}
	
	/**
	 * Is the bomb suppose to exlode this round?
	 * @return should explode?
	 */
	public boolean shouldExplode() {		
		return !exploded && counter == EXPLODE_AT;
	}

	/********* Setter *********/
	
	/**
	 * Sets the counter when the bomb will explode
	 * @param counter
	 */
	public void setCounter(int counter) {
		this.counter = counter;
	}
	
	/**
	 * Sets the counter when the bomb will explode down by one
	 */
	public void countDown() {
		counter--;
	}	
	
	/**
	 * Causes this bomb to explode and returns all fields that will explode
	 * @param board
	 * @return explodingFields
	 */
	public Set<Field> explode(Field[][] board) {
		Set<Field> explodingFields = new HashSet<>();
		explodingFields.add(getField());
		for (int x = getX() + ONE_FIELD; x < explosionRadius + getX() && x < board.length; x++) {
			Field field = board[x][getY()];
			if (field.isExplodable()) {
				explodingFields.add(field);
			} else {
				break;
			}
		}
		for (int y = getY() + ONE_FIELD; y < explosionRadius + getY() && y < board[MIN_FIELD].length; y++) {
			Field field = board[getX()][y];
			if (field.isExplodable()) {
				explodingFields.add(field);
			} else {
				break;
			}
		}		
		for (int x = getX() - ONE_FIELD; x > getX() - explosionRadius && x >= MIN_FIELD; x--) {
			Field field = board[x][getY()];
			if (field.isExplodable()) {
				explodingFields.add(field);
			} else {
				break;
			}
		}
		for (int y = getY() - ONE_FIELD; y > getY() - explosionRadius && y >= MIN_FIELD ; y--) {
			Field field = board[getX()][y];
			if (field.isExplodable()) {
				explodingFields.add(field);
			} else {
				break;
			}
		}
		exploded = true;
		return explodingFields;
	}

}
最近下载更多
adap12345  LV5 2023年6月8日
hkxyyz  LV6 2023年3月2日
总有人间一两风  LV8 2022年12月12日
liangge2115  LV27 2022年11月7日
微信网友_6004879537377280  LV3 2022年6月22日
jytjyt  LV1 2021年12月16日
ssssqweq  LV1 2021年11月1日
此次东方  LV1 2021年10月20日
qliu0130  LV1 2021年10月10日
zpy123  LV1 2021年7月18日
最近浏览更多
ClydeSon  LV5 2023年12月27日
bangyiyang  LV2 2023年12月21日
1112WHQ  LV7 2023年11月3日
微信网友_6672184532766720  LV3 2023年10月6日
srrrrrr 2023年9月19日
暂无贡献等级
adap12345  LV5 2023年6月8日
laa94698 2023年4月21日
暂无贡献等级
hkxyyz  LV6 2023年3月2日
k麝神k  LV10 2022年12月21日
wwfl02  LV3 2022年12月16日
顶部 客服 微信二维码 底部
>扫描二维码关注最代码为好友扫描二维码关注最代码为好友