Coverage Summary for Class: Building0 (it.polimi.ingsw.gc14.Model.Cards.Building.Effects)
| Class |
Class, %
|
Method, %
|
Branch, %
|
Line, %
|
| Building0 |
100%
(1/1)
|
100%
(6/6)
|
81.2%
(13/16)
|
100%
(30/30)
|
package it.polimi.ingsw.gc14.Model.Cards.Building.Effects;
import it.polimi.ingsw.gc14.Model.Cards.Building.EffectType;
import it.polimi.ingsw.gc14.Model.Cards.BuildingCard;
import it.polimi.ingsw.gc14.Model.Player;
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.CharacterType;
/**
* Building effect that grants 5 food for each new complete set of
* 6 different character types obtained after purchasing this building.
*
* <p>Character sets already completed before the building is purchased
* are stored during initialization and do not provide food.
*/
public class Building0 extends BuildingCard {
/** Food units granted per newly completed character set. */
private static final int FOOD_PER_CHARACTER_SET = 5;
/**
* Indicates whether the building effect has already been initialized
* after purchase.
*/
private boolean purchased;
/**
* Number of complete character sets already counted by this building effect.
*/
private int numSet;
/**
* Creates a building with the specified image identifier, era, price
* and prestige value.
*
* @param idImage the image identifier of the building.
* @param era the game era of the building.
* @param price the price in food of the building.
* @param prestigeValue the number of Prestige Points granted by this building
* at the end of the game.
*/
public Building0(String idImage,int era,int price,int prestigeValue) {
super(idImage,era,price,prestigeValue);
effectType=EffectType.CARD_SET;
effectId=0;
purchased = false;
numSet = 0;
}
/**
* Creates a building with the specified era, price and prestige value.
*
* @param era the game era of the building.
* @param price the price in food of the building.
* @param prestigeValue the number of Prestige Points granted by this building
* at the end of the game.
*/
public Building0(int era,int price,int prestigeValue) {
super(era,price,prestigeValue);
effectType=EffectType.CARD_SET;
effectId=0;
purchased = false;
numSet = 0;
}
/**
* Attempts to purchase the building and initializes its effect if the
* purchase succeeds.
*
* @param player the player who attempts to buy the building.
* @return {@code true} if the building is successfully purchased and initialized,
* {@code false} otherwise.
*/
@Override
public boolean buy(Player player)
{
if(!super.buy(player))
return false;
initialize(player);
return true;
}
/**
* Initializes the building effect by storing the number of complete
* character sets already owned by the player at the moment of purchase.
*
* <p>The initialization is performed only once.
*
* @param player the player who owns the building.
*/
public void initialize(Player player)
{
if(purchased)
{
return;
}
numSet=player.getNType(CharacterType.INVENTOR);
for(CharacterType type : CharacterType.values())
{
if(player.getNType(type)<=numSet)
numSet=player.getNType(type);
}
purchased = true;
}
/**
* Creates a new building instance with the same configuration values
* as this card.
*
* <p>The runtime state of the effect, such as initialization status and
* counted character sets, is not copied.
*
* @return a new building card with the same base properties.
*/
@Override
public BuildingCard clone() {
return new Building0(getIdIMG(),getEra(),getPrice(),getPrestigeValue());
}
/**
* Applies the building effect to the specified player.
*
* <p>The method counts the complete character sets currently owned by the player
* and grants 5 food for each set completed since the last stored value.
*
* @param player the player who owns the building.
* @throws IllegalArgumentException if the specified player does not own this card.
*/
public void applyEffect(Player player) throws IllegalArgumentException {
if(!player.getBuildingCards().contains(this))
throw new IllegalArgumentException();
int min_temp=player.getNType(CharacterType.INVENTOR);
for(CharacterType type : CharacterType.values())
{
if(player.getNType(type)<=min_temp)
min_temp=player.getNType(type);
}
if(min_temp<=numSet)
{
return;
}
player.addFood(FOOD_PER_CHARACTER_SET *(min_temp-numSet));
numSet=min_temp;
}
}