Add: Decks Create Slots

This commit is contained in:
rubenpirreram
2026-03-21 17:15:10 +01:00
parent c0733f4cd6
commit 1f966484b8
6 changed files with 160 additions and 34 deletions
@@ -1,6 +1,10 @@
package it.polimi.ingsw.gc14.Model;
import com.google.gson.*;
import it.polimi.ingsw.gc14.Model.Cards.Building.EffectType;
import it.polimi.ingsw.gc14.Model.Cards.Building.Effects.*;
import it.polimi.ingsw.gc14.Model.Cards.BuildingCard;
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.CharacterType;
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.Characters.*;
import it.polimi.ingsw.gc14.Model.Cards.TribeCard;
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.Events.CavePaintings;
@@ -15,9 +19,9 @@ import java.util.*;
public class DecksCreator {
public static List<TribeCard> loadDeck(String resourcePath) {
public static List<TribeCard> loadTribeDeck(String resourcePath) {
Gson gson = new Gson();
Type listType = new com.google.gson.reflect.TypeToken<List<CardDefinition>>(){}.getType();
Type listType = new com.google.gson.reflect.TypeToken<List<TribeCardDefinition>>(){}.getType();
InputStream is = DecksCreator.class.getResourceAsStream(resourcePath);
if (is == null) {
@@ -25,9 +29,9 @@ public class DecksCreator {
}
try (Reader reader = new InputStreamReader(is)) {
List<CardDefinition> definitions = gson.fromJson(reader, listType);
List<TribeCardDefinition> definitions = gson.fromJson(reader, listType);
List<TribeCard> cards = new ArrayList<>();
for (CardDefinition def : definitions) {
for (TribeCardDefinition def : definitions) {
cards.add(createCard(def));
}
return cards;
@@ -36,7 +40,36 @@ public class DecksCreator {
}
}
private static TribeCard createCard(CardDefinition def) {
public static List<BuildingCard> loadBuildingDeck(String resourcePath) {
Gson gson = new Gson();
Type listType = new com.google.gson.reflect.TypeToken<List<BuildingCardDefinition>>(){}.getType();
InputStream is = DecksCreator.class.getResourceAsStream(resourcePath);
if (is == null) {
throw new RuntimeException("File non trovato: " + resourcePath);
}
try (Reader reader = new InputStreamReader(is)) {
List<BuildingCardDefinition> definitions = gson.fromJson(reader, listType);
List<BuildingCard> cards = new ArrayList<>();
for (BuildingCardDefinition def : definitions) {
cards.add(createCard(def));
}
return cards;
} catch (IOException e) {
throw new RuntimeException("Errore caricamento mazzo: " + resourcePath, e);
}
}
public static List<Slot> loadSlotDeck()
{
List<Slot> slots = new ArrayList<>();
char[]chars={'A','B','C','D','E','F','G'};
for(char c : chars)
slots.add(new Slot(c));
return slots;
}
private static TribeCard createCard(TribeCardDefinition def) {
int era = def.era;
int[] p = def.params.stream().mapToInt(d -> ((Double) d).intValue()).toArray();
@@ -83,11 +116,34 @@ public class DecksCreator {
};
}
private static class CardDefinition {
private static BuildingCard createCard(BuildingCardDefinition def) {
return switch (def.effectId) {
case 0 -> new Building0(def.era, def.price, def.prestigeValue);
case 1 -> new Building1(def.era, def.price, def.prestigeValue, CharacterType.valueOf(((String) def.params.get(0)).toUpperCase()));
case 4 -> new Building4(def.era, def.price, def.prestigeValue);
case 8 -> new Building8(def.era, def.price, def.prestigeValue);
case 10 -> new Building10(def.era, def.price, def.prestigeValue);
case 11 -> new Building11(def.era, def.price, def.prestigeValue, CharacterType.valueOf(((String) def.params.get(0)).toUpperCase()), ((Double) def.params.get(1)).intValue());
case 13 -> new Building13(def.era, def.price, def.prestigeValue);
default -> new BuildingCard(def.effectId, def.era, def.price, def.prestigeValue);
};
}
private static class TribeCardDefinition {
String type;
int era;
boolean armed;
boolean isEvent;
List<Object> params; // Object per gestire boolean e int misti
}
private static class BuildingCardDefinition {
int effectId;
int era;
int price;
int prestigeValue;
List<Object> params; // Object per gestire boolean e int misti
}
}
@@ -1,7 +1,7 @@
package it.polimi.ingsw.gc14.Model;
import it.polimi.ingsw.gc14.Model.Cards.BuildingCard;
import it.polimi.ingsw.gc14.Model.Cards.TribeCard;
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.CharacterType;
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.Characters.Inventor;
import java.util.*;
@@ -10,6 +10,7 @@ public class Main {
public static void main(String[] args) {
Inventor inventor= new Inventor(1,0);
System.out.println(inventor);
List<TribeCard>cards= DecksCreator.loadDeck("/Cards/cards_era1.json");
List<TribeCard>cards= DecksCreator.loadTribeDeck("/Cards/tribe_era1.json");
List<BuildingCard> buildingCards = DecksCreator.loadBuildingDeck("/Cards/buildingcards.json");
}
}
@@ -21,20 +21,60 @@ public class Slot {
return Food;
}
// End getters
private char slotId;
// Setters
// End setters
// Constructors
public Slot(int NUpper, int NLower, int Food,int nMinPlayer) throws IllegalArgumentException {
this.NUpper = NUpper;
this.NLower = NLower;
this.Food = Food;
if(Food != 0 && (NUpper != 0 || NLower != 0)){
throw new IllegalArgumentException();
public Slot(char slotId) throws IllegalArgumentException
{
this.slotId = slotId;
this.nMinPlayer=0;
switch(slotId)
{
case 'A':
this.Food = 3;
this.NLower = 0;
this.NUpper = 0;
this.nMinPlayer=5;
break;
case 'B':
this.Food = 0;
this.NLower = 1;
this.NUpper = 0;
break;
case 'C':
this.Food = 0;
this.NLower = 0;
this.NUpper = 1;
break;
case 'D':
this.Food = 0;
this.NLower = 2;
this.NUpper = 0;
this.nMinPlayer = 3;
break;
case 'E':
this.Food = 0;
this.NLower = 1;
this.NUpper = 1;
break;
case 'F':
this.Food = 0;
this.NLower = 0;
this.NUpper = 2;
break;
case 'G':
this.Food = 0;
this.NLower = 1;
this.NUpper = 2;
this.nMinPlayer = 4;
break;
default:
throw new IllegalArgumentException();
}
this.nMinPlayer = nMinPlayer;
}
// Constructors
// End Constructors
// Functions
@@ -0,0 +1,26 @@
[
{"effectId": "0","era": 1,"price":4,"prestigeValue": 3,"params": []},
{"effectId": "1","era": 1,"price":4,"prestigeValue": 4,"params": ["Gatherer"]},
{"effectId": "1","era": 1,"price":5,"prestigeValue": 3,"params": ["Artist"]},
{"effectId": "2","era": 1,"price":5,"prestigeValue": 2,"params": []},
{"effectId": "3","era": 1,"price":3,"prestigeValue": 3,"params": []},
{"effectId": "4","era": 1,"price":3,"prestigeValue": 4,"params": []},
{"effectId": "6","era": 2,"price":4,"prestigeValue": 0,"params": []},
{"effectId": "5","era": 2,"price":6,"prestigeValue": 4,"params": []},
{"effectId": "1","era": 2,"price":7,"prestigeValue": 4,"params": ["Inventor"]},
{"effectId": "7","era": 2,"price":7,"prestigeValue": 2,"params": []},
{"effectId": "8","era": 2,"price":6,"prestigeValue": 4,"params": []},
{"effectId": "9","era": 2,"price":5,"prestigeValue": 6,"params": []},
{"effectId": "10","era": 2,"price":5,"prestigeValue": 6,"params": []},
{"effectId": "11","era": 3,"price":8,"prestigeValue": 8,"params": ["Hunter",3]},
{"effectId": "11","era": 3,"price":7,"prestigeValue": 6,"params": ["Gatherer",4]},
{"effectId": "11","era": 3,"price":7,"prestigeValue": 4,"params": ["Shaman",4]},
{"effectId": "11","era": 3,"price":6,"prestigeValue": 3,"params": ["Builder",4]},
{"effectId": "11","era": 3,"price":7,"prestigeValue": 4,"params": ["Artist",4]},
{"effectId": "11","era": 3,"price":6,"prestigeValue": 6,"params": ["Inventor",2]},
{"effectId": "12","era": 3,"price":9,"prestigeValue": 3,"params": []},
{"effectId": "13","era": 3,"price":10,"prestigeValue": 0,"params": []}
]
@@ -11,11 +11,11 @@ class DecksCreatorTest {
@Test
void loadDeck() {
List<TribeCard> cards = DecksCreator.loadDeck("/Cards/cards_era1.json");
List<TribeCard> cards = DecksCreator.loadTribeDeck("/Cards/tribe_era1.json");
assertEquals(33,cards.size());
List<TribeCard> cards2 = DecksCreator.loadDeck("/Cards/cards_era2.json");
List<TribeCard> cards2 = DecksCreator.loadTribeDeck("/Cards/tribe_era2.json");
assertEquals(32,cards2.size());
List<TribeCard> cards3 = DecksCreator.loadDeck("/Cards/cards_era3.json");
List<TribeCard> cards3 = DecksCreator.loadTribeDeck("/Cards/tribe_era3.json");
assertEquals(29,cards3.size());
}
}
@@ -7,29 +7,32 @@ import static org.junit.jupiter.api.Assertions.*;
class SlotTest {
@Test
void SlotWrongArgument() { // TODO: Cos'è il parametro nMinPlayer? come va testato?
void SlotWrongArgument() {
assertDoesNotThrow(() -> {
Slot sl1 = new Slot(0,0,1,1);
Slot sl1 = new Slot('A');
});
assertDoesNotThrow(() -> {
Slot sl1 = new Slot(0,1,0,1);
Slot sl1 = new Slot('B');
});
assertDoesNotThrow(() -> {
Slot sl1 = new Slot(1,0,0,1);
Slot sl1 = new Slot('C');
});
assertDoesNotThrow(() -> {
Slot sl1 = new Slot(1,1,0,1);
Slot sl1 = new Slot('D');
});
assertDoesNotThrow(() -> {
Slot sl1 = new Slot('E');
});
assertDoesNotThrow(() -> {
Slot sl1 = new Slot('F');
});
assertDoesNotThrow(() -> {
Slot sl1 = new Slot('G');
});
assertThrows(IllegalArgumentException.class, () -> {
Slot sl1 = new Slot('H');
});
assertThrows(IllegalArgumentException.class, () -> {
Slot sl1 = new Slot(1,0,1,1);
});
assertThrows(IllegalArgumentException.class, () -> {
Slot sl1 = new Slot(0,1,1,1);
});
assertThrows(IllegalArgumentException.class, () -> {
Slot sl1 = new Slot(1,1,1,1);
});
}
}