This commit is contained in:
MatteoPellegrino05
2026-05-03 18:05:06 +02:00
15 changed files with 467 additions and 179 deletions
@@ -42,7 +42,7 @@ public class Building8 extends BuildingCard {
if(!player.buildingCards.contains(this))
throw new IllegalArgumentException();
for (Builder builder : player.builders) {
player.addPrestige(builder.getPrestigeValue() * 2);
player.addPrestige(builder.getPrestigeValue());
}
}
@@ -15,10 +15,19 @@ import java.io.*;
import java.lang.reflect.Type;
import java.util.*;
//TODO javadoc
/**
* Utility class responsible for loading and creating decks of cards and slots for the game.
* Cards are loaded from JSON resource files and instantiated according to their type and parameters.
*/
public class DecksCreator {
//TODO javadoc
/**
* Loads the tribe card deck for the specified era from the corresponding JSON resource file.
*
* @param era the era number (1, 2, or 3).
* @return a list of {@link TribeCard} objects for the specified era.
* @throws IllegalArgumentException if {@code era} is not 1, 2, or 3.
*/
public static List<TribeCard> loadTribeDeckByEra(int era) throws IllegalArgumentException
{
return switch (era) {
@@ -29,7 +38,13 @@ public class DecksCreator {
};
}
//TODO javadoc
/**
* Loads a tribe card deck from the specified JSON resource file path.
*
* @param resourcePath the path to the JSON resource file.
* @return a list of {@link TribeCard} objects defined in the resource file.
* @throws RuntimeException if the resource file is not found or an error occurs while reading it.
*/
public static List<TribeCard> loadTribeDeck(String resourcePath) {
Gson gson = new Gson();
Type listType = new com.google.gson.reflect.TypeToken<List<TribeCardDefinition>>(){}.getType();
@@ -51,14 +66,26 @@ public class DecksCreator {
}
}
//TODO javadoc
public static List<BuildingCard> loadBuildingDeckByEra(int era) throws IllegalArgumentException
/**
* Loads the building card deck for the specified era, filtering cards from the global building deck.
*
* @param era the era number (1, 2, or 3).
* @return a list of {@link BuildingCard} objects belonging to the specified era.
* @throws IllegalArgumentException if {@code era} is not 1, 2, or 3.
*/
public static List<BuildingCard> loadBuildingDeckByEra(int era) throws IllegalArgumentException
{
if(era<=0 || era>3) throw new IllegalArgumentException();
return loadBuildingDeck("/Cards/buildingCards.json").stream().filter(x->x.getEra()==era).toList();
}
//TODO javadoc
/**
* Loads the full building card deck from the specified JSON resource file path.
*
* @param resourcePath the path to the JSON resource file.
* @return a list of {@link BuildingCard} objects defined in the resource file.
* @throws RuntimeException if the resource file is not found or an error occurs while reading it.
*/
public static List<BuildingCard> loadBuildingDeck(String resourcePath) {
Gson gson = new Gson();
Type listType = new com.google.gson.reflect.TypeToken<List<BuildingCardDefinition>>(){}.getType();
@@ -80,7 +107,12 @@ public class DecksCreator {
}
}
//TODO javadoc
/**
* Creates and returns the list of slots used as the game board.
* Slots are labeled with the letters A through G.
*
* @return a list of {@link Slot} objects representing the game board.
*/
public static List<Slot> loadSlotDeck()
{
List<Slot> slots = new ArrayList<>();
@@ -90,7 +122,15 @@ public class DecksCreator {
return slots;
}
//TODO javadoc
/**
* Instantiates a {@link TribeCard} from the given definition.
* Depending on whether the card is an event or a character, the appropriate subclass is created
* using the type and parameters specified in the definition.
*
* @param def the {@link TribeCardDefinition} containing the card's type, era, and parameters.
* @return the instantiated {@link TribeCard}.
* @throws IllegalArgumentException if the card type is unknown or the parameters are invalid.
*/
private static TribeCard createCard(TribeCardDefinition def) {
int era = def.era;
@@ -138,7 +178,14 @@ public class DecksCreator {
};
}
//TODO javadoc
/**
* Instantiates a {@link BuildingCard} from the given definition.
* The appropriate subclass is selected based on the effect ID specified in the definition.
* If no specific subclass matches the effect ID, a base {@link BuildingCard} is created.
*
* @param def the {@link BuildingCardDefinition} containing the card's effect ID, era, price, prestige value, and parameters.
* @return the instantiated {@link BuildingCard}.
*/
private static BuildingCard createCard(BuildingCardDefinition def) {
return switch (def.effectId) {
case 0 -> new Building0(def.era, def.price, def.prestigeValue);
@@ -153,7 +200,11 @@ public class DecksCreator {
}
//TODO javadoc
/**
* Internal data class representing the raw definition of a tribe card as loaded from a JSON file.
* Contains the card type, era, whether it is armed, whether it is an event card,
* and a list of additional parameters.
*/
private static class TribeCardDefinition {
String type;
int era;
@@ -162,7 +213,10 @@ public class DecksCreator {
List<Object> params; // Object per gestire boolean e int misti
}
//TODO javadoc
/**
* Internal data class representing the raw definition of a building card as loaded from a JSON file.
* Contains the effect ID, era, price, prestige value, and a list of additional parameters.
*/
private static class BuildingCardDefinition {
int effectId;
int era;
@@ -22,7 +22,6 @@ import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import it.polimi.ingsw.gc14.Network.Observer;
import it.polimi.ingsw.gc14.View.TUI.AsciiTable;
import it.polimi.ingsw.gc14.View.TUI.BorderStyle;
@@ -33,18 +32,6 @@ import it.polimi.ingsw.gc14.View.TUI.BorderStyle;
*/
public class Game implements Serializable {
private transient List<Observer> observers = new ArrayList<>(); // transient! non serializzare
public void addObserver(Observer observer) {
observers.add(observer);
}
private void notifyObservers() {
for (Observer o : observers) {
o.update(this);
}
}
/**
* Returns the list of players participating in the game.
* @return
@@ -245,6 +232,16 @@ public class Game implements Serializable {
orderLogicCard=new Order5(playersList);
break;
}
for(Player player : playersList)
{
switch(orderLogicCard.getPosition(player.getUserName()))
{
case 0: player.addFood(2); break;
case 1,2: player.addFood(3); break;
case 3,4: player.addFood(4); break;
}
}
currentState.PlayerUpdate(orderLogicCard.pull(),null);
currentState.GameStageUpdate(GameStages.SLOT_CHOICE);
}
@@ -277,6 +274,10 @@ public class Game implements Serializable {
{
return false;
}
if(slotPlayerEntry.getKey().getSlotId()=='A')
{
player.addFood(3);
}
slotMap.put(slotPlayerEntry.getKey(),player);
nextPlayerSetup();
return true;
@@ -301,7 +302,7 @@ public class Game implements Serializable {
public boolean DrawUpperTribeCardByIndex(Player player,int cardIndex) {
if( cardIndex<0 || cardIndex >=board.upperListTribe.size())
return false;
if(currentState.getGameStage()!= GameStages.RESOLVING_ACTIONS)
if(currentState.getGameStage()!= GameStages.RES_ACTIONS)
{
return false;
}
@@ -337,7 +338,7 @@ public class Game implements Serializable {
* @return {@code true} if the skip succeeds, {@code false} otherwise.
*/
public boolean SkipUpperDrawing(Player player) {
if(currentState.getGameStage()!= GameStages.RESOLVING_ACTIONS)
if(currentState.getGameStage()!= GameStages.RES_ACTIONS)
{
return false;
}
@@ -368,7 +369,7 @@ public class Game implements Serializable {
* @return {@code true} if the skip succeeds, {@code false} otherwise.
*/
public boolean SkipLowerDrawing(Player player) {
if(currentState.getGameStage()!= GameStages.RESOLVING_ACTIONS)
if(currentState.getGameStage()!= GameStages.RES_ACTIONS)
{
return false;
}
@@ -402,7 +403,7 @@ public class Game implements Serializable {
public boolean DrawLowerTribeCardByIndex(Player player, int cardIndex) {
if( cardIndex<0 || cardIndex >=board.lowerListTribe.size())
return false;
if(currentState.getGameStage()!= GameStages.RESOLVING_ACTIONS)
if(currentState.getGameStage()!= GameStages.RES_ACTIONS)
{
return false;
}
@@ -443,7 +444,7 @@ public class Game implements Serializable {
public boolean DrawUpperBuildingCardByIndex(Player player,int cardIndex) {
if( cardIndex<0 || cardIndex >=board.upperListBuilding.size())
return false;
if(currentState.getGameStage()!= GameStages.RESOLVING_ACTIONS)
if(currentState.getGameStage()!= GameStages.RES_ACTIONS)
{
return false;
}
@@ -482,7 +483,7 @@ public class Game implements Serializable {
public boolean DrawLowerBuildingCardByIndex(Player player,int cardIndex) {
if( cardIndex<0 || cardIndex >=board.lowerListBuilding.size())
return false;
if(currentState.getGameStage()!= GameStages.RESOLVING_ACTIONS)
if(currentState.getGameStage()!= GameStages.RES_ACTIONS)
{
return false;
}
@@ -527,7 +528,7 @@ public class Game implements Serializable {
* @return {@code true} if the operation succeeds, {@code false} otherwise.
*/
public boolean PickOptionalTribeCardByIndex(Player player,int cardIndex) {
if(currentState.getGameStage() != GameStages.OPTIONAL_CARD_EFFECT){
if(currentState.getGameStage() != GameStages.OPT_CARD_E){
return false;
}
if(!player.equals(currentState.getCurrentPlayer())){
@@ -562,7 +563,7 @@ public class Game implements Serializable {
* @return {@code true} if the operation succeeds, {@code false} otherwise.
*/
public boolean PickOptionalBuildingCard(Player player, int cardIndex) {
if(currentState.getGameStage() != GameStages.OPTIONAL_CARD_EFFECT){
if(currentState.getGameStage() != GameStages.OPT_CARD_E){
return false;
}
if(!player.equals(currentState.getCurrentPlayer())){
@@ -594,7 +595,7 @@ public class Game implements Serializable {
* @return {@code true} if the operation succeeds, {@code false} otherwise.
*/
public boolean NoOptionalCard(Player player) {
if(currentState.getGameStage() != GameStages.OPTIONAL_CARD_EFFECT){
if(currentState.getGameStage() != GameStages.OPT_CARD_E){
return false;
}
if(!player.equals(currentState.getCurrentPlayer())){
@@ -633,7 +634,7 @@ public class Game implements Serializable {
currentState.PlayerUpdate(tempPlayer, null);
return;
}
currentState.GameStageUpdate(GameStages.RESOLVING_ACTIONS);
currentState.GameStageUpdate(GameStages.RES_ACTIONS);
for (Slot s : slotMap.keySet()) {
if (slotMap.get(s) != null) {
currentState.PlayerUpdate(slotMap.get(s), s);
@@ -649,7 +650,7 @@ public class Game implements Serializable {
return;
}
if(GameStages.RESOLVING_ACTIONS==currentState.getGameStage()) {
if(GameStages.RES_ACTIONS ==currentState.getGameStage()) {
orderLogicCard.push(currentState.getCurrentPlayer());
slotMap.put(currentState.getSlot(), null);
for (Slot s : slotMap.keySet()) {
@@ -668,7 +669,7 @@ public class Game implements Serializable {
}
if(slotMap.values().stream().allMatch(v -> v == null))
{
currentState.GameStageUpdate(GameStages.OPTIONAL_CARD_EFFECT);
currentState.GameStageUpdate(GameStages.OPT_CARD_E);
HashMap<Player,Integer> optional=new LinkedHashMap<>();
for (Player p : orderLogicCard.players) {
int tempCount=(int)p.buildingCards.stream().filter(x->x.getEffectId()==12).count();
@@ -689,7 +690,7 @@ public class Game implements Serializable {
return;
}
currentState.GameStageUpdate(GameStages.RESOLVING_EVENT);
currentState.GameStageUpdate(GameStages.RES_EVENT);
if (currentState.getRound() < 10) {
nextRound();
@@ -704,7 +705,7 @@ public class Game implements Serializable {
}
}
if (GameStages.OPTIONAL_CARD_EFFECT == currentState.getGameStage()) {
if (GameStages.OPT_CARD_E == currentState.getGameStage()) {
Player optionalPlayer = OptionalCardQueue.poll();
if (optionalPlayer != null) {
@@ -712,7 +713,7 @@ public class Game implements Serializable {
return;
}
currentState.GameStageUpdate(GameStages.RESOLVING_EVENT);
currentState.GameStageUpdate(GameStages.RES_EVENT);
if (currentState.getRound() < 10) {
nextRound();
@@ -761,7 +762,7 @@ public class Game implements Serializable {
*/
private void EventResolution() {
if(currentState.getGameStage()!= GameStages.RESOLVING_EVENT)
if(currentState.getGameStage()!= GameStages.RES_EVENT)
{
return;
}
@@ -812,18 +813,12 @@ public class Game implements Serializable {
*/
private void endGame() {
Queue<EventCard> events;
events=Stream.concat(board.lowerListTribe.stream().filter(x->!x.IsEventCard()),board.upperListTribe.stream().filter(x->!x.IsEventCard())).map(x->((EventCard)x)).collect(Collectors.toCollection(LinkedList::new));
events=Stream.concat(board.lowerListTribe.stream().filter(TribeCard::IsEventCard),board.upperListTribe.stream().filter(TribeCard::IsEventCard)).map(x->((EventCard)x)).collect(Collectors.toCollection(LinkedList::new));
ArrayList<EventCard>sustenance=events.stream().filter(x->x.getType().equals(EventType.SUSTENANCE)).collect(Collectors.toCollection(ArrayList::new));
events.removeAll(sustenance);
for(EventCard event:events)
{
event.activateEvent(playersList);
}
events.forEach(event->event.activateEvent(playersList));
sustenance.forEach(event->event.activateEvent(playersList));
for(EventCard e : sustenance)
{
e.activateEvent(playersList);
}
playersList.forEach(p->{
int temp= p.builders.stream().mapToInt(Builder::getPrestigeValue).sum();
p.addPrestige(temp);
@@ -1,5 +1,5 @@
package it.polimi.ingsw.gc14.Model.GamePackage;
public enum GameStages {
WAITING, SLOT_CHOICE, RESOLVING_ACTIONS, OPTIONAL_CARD_EFFECT, RESOLVING_EVENT, ENDING, ENDED
WAITING, SLOT_CHOICE, RES_ACTIONS, OPT_CARD_E, RES_EVENT, ENDING, ENDED
}