Gamelib/src/main/java/uulm/teamname/marvelous/gamelibrary/gamelogic/WinConditionManager.java

77 lines
2.7 KiB
Java
Raw Normal View History

package uulm.teamname.marvelous.gamelibrary.gamelogic;
import uulm.teamname.marvelous.gamelibrary.Tuple;
import uulm.teamname.marvelous.gamelibrary.entities.EntityType;
import java.util.HashMap;
/** Represents a manager for win conditions. */
public class WinConditionManager {
/** The store of the {@link WinCondition} data for every win condition for each player */
private final HashMap<Tuple<EntityType, WinCondition>, Integer> winConditions = new HashMap<>();
/**
* Returns the current winner
* @return The {@link EntityType} that is currently winning the game according to overtime ruling
*/
public EntityType getWinner() {
int value1;
int value2;
for(WinCondition condition: WinCondition.values()) {
value1 = getValue(EntityType.P1, condition);
value2 = getValue(EntityType.P2, condition);
if(value1 > value2) {
return EntityType.P1;
}
if(value2 > value1) {
return EntityType.P2;
}
}
return EntityType.None;
}
/**
* Updates the value for a win condition for a specific player if the new value is greater than the old one.
* @param player The player to use
* @param condition The {@link WinCondition} to use
* @param value The new value
*/
public void updateValue(EntityType player, WinCondition condition, Integer value) {
Integer old = getValue(player, condition);
if(old < value) {
winConditions.put(new Tuple<>(player, condition), value);
}
}
/**
* Increases the value for a win condition for a specific player.
* @param player The player to use
* @param condition The {@link WinCondition} to use
* @param value The new value
*/
public void increaseValue(EntityType player, WinCondition condition, Integer value) {
winConditions.put(new Tuple<>(player, condition), getValue(player, condition) + value);
}
/**
* Gets the value for a specific player and win condition.
* @param player The player to use
* @param condition The {@link WinCondition} to use
* @return The value of the condition or 0 if it doesn't exist
*/
private Integer getValue(EntityType player, WinCondition condition) {
return winConditions.getOrDefault(new Tuple<>(player, condition), 0);
}
/**
* Takes over all the win conditions from a different {@link WinConditionManager}.
* @param other The win condition manager to take the data from
*/
public void cloneFrom(WinConditionManager other) {
winConditions.clear();
winConditions.putAll(other.winConditions);
}
}