This commit is contained in:
2026-04-11 18:44:53 +02:00
parent 644c1bc55d
commit 5f3c70c8b3
5 changed files with 145 additions and 127 deletions
@@ -23,6 +23,11 @@ public abstract class EventCard extends TribeCard {
@Override @Override
public abstract TribeCard clone(); public abstract TribeCard clone();
@Override
public String toString() {
return super.toString()+" "+type.toString();
}
public abstract void activateEvent (ArrayList <Player> playerList); public abstract void activateEvent (ArrayList <Player> playerList);
// End Functions // End Functions
} }
@@ -11,69 +11,28 @@ import it.polimi.ingsw.gc14.Model.Slot;
import java.util.*; import java.util.*;
import java.util.stream.Collectors; import java.util.stream.Collectors;
/**
* Board manages all the elements during the game as decks, upper and lower rows, totems, tiles...
*/
public class Board { public class Board {
/**
* slotList contains the ordered list of slots (tiles). These changes based on the number of players.
* Each slot (tile) has special action as drawing from the upper/lower row or taking food.
*/
private List<Slot> slotList; private List<Slot> slotList;
/** tribeDeck is the deck from where you draw tribe cards as characters and events. */
private Queue<TribeCard> tribeDeck; private Queue<TribeCard> tribeDeck;
/** The upper row of tribe cards. There must be (num. of players + 4) character cards + Event cards */
public List<TribeCard> upperListTribe; public List<TribeCard> upperListTribe;
/**
* The lower row of the tribe card. During the first round there will be (num. of players + 1) character cards.
* During the following rounds, lower row will be emptied and populated with upper row's cards.
* When an Event card get in the lower row, the event effect will be activated at the wnd of the round.
*/
public List<TribeCard> lowerListTribe; public List<TribeCard> lowerListTribe;
/** Contains all the building cards of the upper list. When a new era starts, all its building cards are placed here */
public List<BuildingCard> upperListBuilding; public List<BuildingCard> upperListBuilding;
/** Contains all the building cards of the upper list. When a new era starts, the old era's buildings are moved from the upper to the lower list */
public List<BuildingCard> lowerListBuilding; public List<BuildingCard> lowerListBuilding;
/** Number of players */
private int nTotem; private int nTotem;
/** Current era of the game */
private int era; private int era;
/**
* Creates and return a new list containing the slots (tiles) of the game
*
* @return a copy of the slot list
*/
public List<Slot> getSlotList(){ public List<Slot> getSlotList(){
return slotList.stream().map(x->new Slot(x.getSlotId())).collect(Collectors.toList()); return slotList.stream().map(x->new Slot(x.getSlotId())).collect(Collectors.toList());
} }
public Queue<TribeCard> getTribeDeck() {return new LinkedList<>(tribeDeck);}
/**
* Creates and initialize a new board:
* - Generate the slotList using a method of DecksCreator
* - Generate the tribeDeck
* - Populate the lower list (if there is an event card, it's added to the upper list)
* - Populate the upper list
* - Generate the buildingDeck using a method of DecksCreator
*
* @param nTotem the number of players
*/
public Board(int nTotem) { public Board(int nTotem) {
this.nTotem = nTotem; this.nTotem = nTotem;
this.slotList = DecksCreator.loadSlotDeck().stream().filter(x->x.getNMinPlayer()<=nTotem).sorted(Comparator.comparingInt(Slot::getSlotId)).toList(); this.slotList = DecksCreator.loadSlotDeck().stream().filter(x->x.getNMinPlayer()<=nTotem).sorted(Comparator.comparingInt(Slot::getSlotId)).toList();
upperListTribe=new ArrayList<>(); upperListTribe=new ArrayList<>();
lowerListTribe=new ArrayList<>(); lowerListTribe=new ArrayList<>();
lowerListBuilding=new ArrayList<>(); lowerListBuilding=new ArrayList<>();
tribeDeck=getTribeDeck(nTotem); tribeDeck=generateTribeDeck(nTotem);
era=1; era=1;
for(int i=0;i<nTotem+1;i++) for(int i=0;i<nTotem+1;i++)
{ {
TribeCard tempCard = tribeDeck.remove(); TribeCard tempCard = tribeDeck.remove();
@@ -87,8 +46,7 @@ public class Board {
lowerListTribe.add(tempCard); lowerListTribe.add(tempCard);
} }
} }
for(int i=upperListTribe.size();i<nTotem+5;i++)
for(int i=upperListTribe.size();i<nTotem+4;i++)
{ {
TribeCard tempCard = tribeDeck.remove(); TribeCard tempCard = tribeDeck.remove();
upperListTribe.add(tempCard); upperListTribe.add(tempCard);
@@ -102,18 +60,7 @@ public class Board {
upperListBuilding= buildingDeck.subList(0,2); upperListBuilding= buildingDeck.subList(0,2);
} }
/** private Queue<TribeCard> generateTribeDeck(int nPlayers) {
* Creates the tribeDeck. Loads the cards from each era, shuffle them, and then join them in the final deck.
* In the end, two special events (Sustenance and ShamanicRitual) are added.
*
* @param nPlayers the number of players
* @return a queue containing the cards in this order:
* - Era 1
* - Era 2
* - Era 3
* - Special events
*/
private Queue<TribeCard> getTribeDeck(int nPlayers) {
ArrayList<TribeCard> era1= DecksCreator.loadTribeDeckByEra(1).stream().filter(x->x.getNMin()<=nPlayers).collect(Collectors.toCollection(ArrayList::new)); ArrayList<TribeCard> era1= DecksCreator.loadTribeDeckByEra(1).stream().filter(x->x.getNMin()<=nPlayers).collect(Collectors.toCollection(ArrayList::new));
ArrayList<TribeCard> era2= DecksCreator.loadTribeDeckByEra(2).stream().filter(x->x.getNMin()<=nPlayers).collect(Collectors.toCollection(ArrayList::new)); ArrayList<TribeCard> era2= DecksCreator.loadTribeDeckByEra(2).stream().filter(x->x.getNMin()<=nPlayers).collect(Collectors.toCollection(ArrayList::new));
ArrayList<TribeCard> era3= DecksCreator.loadTribeDeckByEra(3).stream().filter(x->x.getNMin()<=nPlayers).collect(Collectors.toCollection(ArrayList::new)); ArrayList<TribeCard> era3= DecksCreator.loadTribeDeckByEra(3).stream().filter(x->x.getNMin()<=nPlayers).collect(Collectors.toCollection(ArrayList::new));
@@ -130,58 +77,22 @@ public class Board {
return tribeDeck_temp; return tribeDeck_temp;
} }
/** @return the queue of events from the lower list that need to be activated */
public Queue<EventCard> getPendingEvents(){ public Queue<EventCard> getPendingEvents(){
return lowerListTribe.stream().filter(TribeCard::IsEventCard).map(x->(EventCard)x).collect(Collectors.toCollection(LinkedList::new)); return lowerListTribe.stream().filter(TribeCard::IsEventCard).map(x->(EventCard)x).collect(Collectors.toCollection(LinkedList::new));
} }
/**
* Take the selected card from the upper tribe list of the board
*
* @param tribeCard is the card to remove
* @return true if succeed in removing the card
*/
public boolean removeUpperTribeCard(TribeCard tribeCard) { public boolean removeUpperTribeCard(TribeCard tribeCard) {
return upperListTribe.remove(tribeCard); return upperListTribe.remove(tribeCard);
} }
/**
* Take the selected card from the lower tribe list of the board
*
* @param tribeCard is the card to remove
* @return true if succeed in removing the card
*/
public boolean removeLowerTribeCard(TribeCard tribeCard) { public boolean removeLowerTribeCard(TribeCard tribeCard) {
return lowerListTribe.remove(tribeCard); return upperListTribe.remove(tribeCard);
} }
/**
* Take the selected card from the upper building list of the board
*
* @param buildingCard is the card to remove
* @return true if succeed in removing the card
*/
public boolean removeUpperBuildingCard(BuildingCard buildingCard) { public boolean removeUpperBuildingCard(BuildingCard buildingCard) {
return upperListBuilding.remove(buildingCard); return upperListTribe.remove(buildingCard);
} }
/**
* Take the selected card from the lower building list of the board
*
* @param buildingCard is the card to remove
* @return true if succeed in removing the card
*/
public boolean removeLowerBuildingCard(BuildingCard buildingCard) { public boolean removeLowerBuildingCard(BuildingCard buildingCard) {
return lowerListBuilding.remove(buildingCard); return upperListTribe.remove(buildingCard);
} }
/**
* Goes to the next round.
* - Clear the lower tribe row
* - Moves the upper tribe row to the lower one
* - Populate the upper tribe row
*
*/
public int nextRound() { public int nextRound() {
lowerListTribe.clear(); lowerListTribe.clear();
lowerListTribe.addAll(upperListTribe); lowerListTribe.addAll(upperListTribe);
@@ -195,23 +106,22 @@ public class Board {
} }
upperListTribe.add(tempCard); upperListTribe.add(tempCard);
} }
for(int i=upperListTribe.size();i<nTotem+5;i++)
{
TribeCard tempCard = tribeDeck.remove();
upperListTribe.add(tempCard);
}
return era; return era;
} }
public void nextEra() {
/**
* Change the era of the game:
* - Increase the era attribute
* - Clear the lower building list and replace it with the upper building list
* - Repopulate the upper building list
*/
private void nextEra() {
era=era+1; era=era+1;
lowerListBuilding.clear(); lowerListBuilding.clear();
lowerListBuilding.addAll(upperListBuilding); lowerListBuilding.addAll(upperListBuilding);
upperListBuilding.clear(); upperListBuilding.clear();
ArrayList<BuildingCard> buildingCards= new ArrayList<>(DecksCreator.loadBuildingDeckByEra(era)); List<BuildingCard> buildingCards=DecksCreator.loadBuildingDeckByEra(era);
Collections.shuffle(buildingCards); Collections.shuffle(buildingCards);
if(era==2) if(era==2)
{ {
@@ -3,7 +3,6 @@ package it.polimi.ingsw.gc14.Model.Orders;
import it.polimi.ingsw.gc14.Model.Cards.BuildingCard; import it.polimi.ingsw.gc14.Model.Cards.BuildingCard;
import it.polimi.ingsw.gc14.Model.OrderLogicCard; import it.polimi.ingsw.gc14.Model.OrderLogicCard;
import it.polimi.ingsw.gc14.Model.Player; import it.polimi.ingsw.gc14.Model.Player;
import jdk.jshell.spi.ExecutionControl;
import java.util.*; import java.util.*;
@@ -82,6 +82,7 @@ public class Slot {
public String toString() { public String toString() {
return ("SlotID: "+this.getSlotId()+"\nNUpper: "+this.getNUpper()+"\nNLower: "+this.getNLower()+"\nFood: "+this.getFood()+"\nNMinPlayer: "+this.getNMinPlayer()+"\n"); return ("SlotID: "+this.getSlotId()+"\nNUpper: "+this.getNUpper()+"\nNLower: "+this.getNLower()+"\nFood: "+this.getFood()+"\nNMinPlayer: "+this.getNMinPlayer()+"\n");
} }
// End Constructors // End Constructors
// Functions // Functions
@@ -1,54 +1,157 @@
package it.polimi.ingsw.gc14.Model.GamePackage; package it.polimi.ingsw.gc14.Model.GamePackage;
import it.polimi.ingsw.gc14.Model.Cards.BuildingCard;
import it.polimi.ingsw.gc14.Model.Cards.TribeCard;
import it.polimi.ingsw.gc14.Model.Cards.TribeCards.EventCard;
import it.polimi.ingsw.gc14.Model.Player;
import it.polimi.ingsw.gc14.Model.Slot;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.LinkedList;
import java.util.List;
import java.util.Queue;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
class BoardTest { class BoardTest {
@Test @Test
@DisplayName("Slot initialization")
void getSlotList() { void getSlotList() {
//Board board = new Board(3); // 0 players
//assertEquals("Ciao", board.getSlotList().toString()); Board b0 = new Board(0);
assertEquals('B', b0.getSlotList().get(0).getSlotId());
assertEquals('C', b0.getSlotList().get(1).getSlotId());
assertEquals('E', b0.getSlotList().get(2).getSlotId());
assertEquals('F', b0.getSlotList().get(3).getSlotId());
// 1 players
Board b1 = new Board(1);
assertEquals('B', b1.getSlotList().get(0).getSlotId());
assertEquals('C', b1.getSlotList().get(1).getSlotId());
assertEquals('E', b1.getSlotList().get(2).getSlotId());
assertEquals('F', b1.getSlotList().get(3).getSlotId());
// 2 players
Board b2 = new Board(2);
assertEquals('B', b2.getSlotList().get(0).getSlotId());
assertEquals('C', b2.getSlotList().get(1).getSlotId());
assertEquals('E', b2.getSlotList().get(2).getSlotId());
assertEquals('F', b2.getSlotList().get(3).getSlotId());
// 3 players
Board b3 = new Board(3);
assertEquals('B', b3.getSlotList().get(0).getSlotId());
assertEquals('C', b3.getSlotList().get(1).getSlotId());
assertEquals('D', b3.getSlotList().get(2).getSlotId());
assertEquals('E', b3.getSlotList().get(3).getSlotId());
assertEquals('F', b3.getSlotList().get(4).getSlotId());
// 4 players
Board b4 = new Board(4);
assertEquals('B', b4.getSlotList().get(0).getSlotId());
assertEquals('C', b4.getSlotList().get(1).getSlotId());
assertEquals('D', b4.getSlotList().get(2).getSlotId());
assertEquals('E', b4.getSlotList().get(3).getSlotId());
assertEquals('F', b4.getSlotList().get(4).getSlotId());
assertEquals('G', b4.getSlotList().get(5).getSlotId());
// 5 players
Board b5 = new Board(5);
assertEquals('A', b5.getSlotList().get(0).getSlotId());
assertEquals('B', b5.getSlotList().get(1).getSlotId());
assertEquals('C', b5.getSlotList().get(2).getSlotId());
assertEquals('D', b5.getSlotList().get(3).getSlotId());
assertEquals('E', b5.getSlotList().get(4).getSlotId());
assertEquals('F', b5.getSlotList().get(5).getSlotId());
assertEquals('G', b5.getSlotList().get(6).getSlotId());
} }
@Test @Test
@DisplayName("Testing constructor")
void testConstructor() {
}
// TODO non ho idea di come testare delle estrazioni randomiche
@Test
@DisplayName("Testing constructor")
void generateTribeDeck() {
}
@Test
@DisplayName("get events testing")
void getPendingEvents() { void getPendingEvents() {
Board bd = new Board(0);
Queue<EventCard> eventQueue = new LinkedList<>();
for (TribeCard card : bd.lowerListTribe) {
if (card.IsEventCard()) {
eventQueue.add((EventCard) card);
}
}
assertEquals(eventQueue, bd.getPendingEvents());
} }
@Test @Test
void containsUpperTribeCard() { @DisplayName("Removing a card from upper row of tribe cards")
}
@Test
void removeUpperTribeCard() { void removeUpperTribeCard() {
Board bd = new Board(3);
List<TribeCard> before = bd.upperListTribe;
TribeCard cardToRemove = before.get(2);
before.remove(2);
bd.removeUpperTribeCard(cardToRemove);
assertEquals(before, bd.upperListTribe);
} }
@Test @Test
void containsLowerTribeCard() { @DisplayName("Removing a card from lower row of tribe cards")
}
@Test
void removeLowerTribeCard() { void removeLowerTribeCard() {
Board bd = new Board(3);
List<TribeCard> before = bd.lowerListTribe;
TribeCard cardToRemove = before.get(2);
before.remove(2);
bd.removeLowerTribeCard(cardToRemove);
assertEquals(before, bd.lowerListTribe);
} }
@Test @Test
void containsUpperBuildingCard() { @DisplayName("Removing a card from upper row of building cards")
}
@Test
void removeUpperBuildingCard() { void removeUpperBuildingCard() {
Board bd = new Board(3);
List<BuildingCard> before = bd.upperListBuilding;
BuildingCard cardToRemove = before.get(0);
before.remove(0);
bd.removeUpperBuildingCard(cardToRemove);
assertEquals(before, bd.upperListBuilding);
} }
@Test @Test
void containsLowerBuildingCard() { @DisplayName("Removing a card from lower row of building cards")
}
@Test
void removeLowerBuildingCard() { void removeLowerBuildingCard() {
Board bd = new Board(3);
bd.nextEra();
List<BuildingCard> before = bd.lowerListBuilding;
BuildingCard cardToRemove = before.get(0);
before.remove(0);
bd.removeUpperBuildingCard(cardToRemove);
assertEquals(before, bd.lowerListBuilding);
} }
@Test
void nextRound() {
}
} }