Coverage Summary for Class: Building4 (it.polimi.ingsw.gc14.Model.Cards.Building.Effects)
| Class |
Class, %
|
Method, %
|
Branch, %
|
Line, %
|
| Building4 |
100%
(1/1)
|
100%
(6/6)
|
95%
(19/20)
|
100%
(34/34)
|
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.Cards.TribeCards.Characters.Inventor;
import it.polimi.ingsw.gc14.Model.Player;
import java.util.HashMap;
/**
* Building effect that grants 3 food for each new pair of identical inventors
* obtained after purchasing this building.
*
* <p>Pairs of identical inventors already owned at the moment of purchase
* are stored during initialization and do not provide food.
*/
public class Building4 extends BuildingCard {
/** Food units granted per newly completed inventor pair. */
private static final int FOOD_PER_INVENTOR_PAIR = 3;
/**
* Indicates whether the building effect has already been initialized
* after purchase.
*/
private boolean purchased;
/**
* Number of inventor pairs already counted by this building effect.
*/
private int numPair;
/**
* 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 gained from this building at the end of the game.
*/
public Building4(int era, int price,int prestigeValue) {
super(era,price,prestigeValue);
effectType= EffectType.INVENTOR_PAIR;
effectId=4;
}
/**
* Creates a building with the specified image identifier, era, price
* and prestige value.
*
* @param idIMG 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 gained from this building at the end of the game.
*/
public Building4(String idIMG,int era, int price,int prestigeValue) {
super(idIMG,era,price,prestigeValue);
effectType= EffectType.INVENTOR_PAIR;
effectId=4;
}
/**
* Initializes the building effect by storing the number of pairs of identical
* inventors 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;
}
HashMap<Integer,Integer> map = new HashMap<>();
for(int i=0;i<10;i++)
{
map.put(i,0);
}
for(Inventor inv : player.getInventors())
{
map.merge(inv.getIcon(),1,Integer::sum);
}
numPair=0;
for(Integer i : map.values())
{
numPair+=(i/2);
}
purchased = true;
}
/**
* Attempts to purchase the building and initializes its effect if the
* purchase succeeds.
*
* @param player The player who buys 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;
}
/**
* 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 inventor pairs, is not copied.
*
* @return a new building card with the same base properties.
*/
@Override
public BuildingCard clone() {
return new Building4(getIdIMG(),getEra(),getPrice(),getPrestigeValue());
}
/**
* Applies the building effect to the specified player.
*
* <p>The method counts the current number of pairs of identical inventors
* and grants 3 food for each pair obtained since the last stored value.
*
* @param player The player who owns the building.
* @throws IllegalArgumentException Thrown if the specified player does not own this card.
*/
public void applyEffect(Player player) throws IllegalArgumentException {
if(!player.getBuildingCards().contains(this))
throw new IllegalArgumentException();
HashMap<Integer,Integer> map = new HashMap<>();
for(int i=0;i<10;i++)
{
map.put(i,0);
}
for(Inventor inv : player.getInventors())
{
map.merge(inv.getIcon(),1,Integer::sum);
}
int temp=0;
for(Integer i : map.values())
{
temp+=(i/2);
}
if(temp<= numPair)
{
return;
}
player.addFood(FOOD_PER_INVENTOR_PAIR *(temp-numPair));
numPair = temp;
}
}