This commit is contained in:
GabrieleRadice
2026-05-02 17:44:52 +02:00
7 changed files with 239 additions and 77 deletions
@@ -85,10 +85,10 @@ public class ClientLauncherTUI {
}
switch(action) {
case "0" -> controller.slotChoice(username, pos);
case "1" -> controller.drawUpperBuildingCard(username, pos);
case "2" -> controller.drawUpperTribeCard(username, pos);
case "3" -> controller.drawLowerBuildingCard(username, pos);
case "4" -> controller.drawLowerTribeCard(username, pos);
case "1" -> controller.drawUpperTribeCard(username, pos);
case "2" -> controller.drawUpperBuildingCard(username, pos);
case "3" -> controller.drawLowerTribeCard(username, pos);
case "4" -> controller.drawLowerBuildingCard(username, pos);
case "5" -> controller.pickOptionalTribeCard(username, pos);
case "6" -> controller.pickOptionalBuildingCard(username, pos);
case "7" -> controller.noOptionalCard(username);
@@ -346,7 +346,7 @@ public class Game implements Serializable {
{
return false;
}
if(currentState.getNUpper() <1)
if(currentState.getNLower() <1)
return false;
if(hasDrawableDown())
return false;
@@ -640,7 +640,7 @@ public class Game implements Serializable {
{
currentState.GameStageUpdate(GameStages.OPTIONAL_CARD_EFFECT);
HashMap<Player,Integer> optional=new LinkedHashMap<>();
for (Player p : playersList) {
for (Player p : orderLogicCard.players) {
int tempCount=(int)p.buildingCards.stream().filter(x->x.getEffectId()==12).count();
if(tempCount>0)
{
@@ -41,6 +41,8 @@ public class Board implements Serializable {
/** 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;
private final ArrayList<List<BuildingCard>> buildingCardsAllEras;
/** Number of players */
private int nTotem;
@@ -88,6 +90,7 @@ public class Board implements Serializable {
tribeDeck=generateTribeDeck(nTotem);
era=1;
for(int i=0;i<nTotem+1;i++)
{
TribeCard tempCard = tribeDeck.remove();
@@ -110,12 +113,46 @@ public class Board implements Serializable {
ArrayList<BuildingCard> buildingDeck = new ArrayList<>(DecksCreator.loadBuildingDeckByEra(1));
Collections.shuffle(buildingDeck);
buildingCardsAllEras= new ArrayList<>();
buildingCardsAllEras.add(new ArrayList<>());
buildingCardsAllEras.add(new ArrayList<>());
buildingCardsAllEras.add(new ArrayList<>());
if(nTotem==2)
{
buildingCardsAllEras.set(0, new ArrayList<>(buildingDeck.subList(0, 1)));
upperListBuilding = new ArrayList<>(buildingDeck.subList(0,1));
}
else
{
buildingCardsAllEras.set(0, new ArrayList<>(buildingDeck.subList(0, 2)));
upperListBuilding = new ArrayList<>(buildingDeck.subList(0,2));
}
buildingDeck=new ArrayList<>(DecksCreator.loadBuildingDeckByEra(2));
Collections.shuffle(buildingDeck);
if(nTotem<=3)
{
buildingCardsAllEras.set(1, new ArrayList<BuildingCard>(buildingDeck.subList(0, 2)));
}
else
{
buildingCardsAllEras.set(1, new ArrayList<BuildingCard>(buildingDeck.subList(0, 3)));
}
buildingDeck=new ArrayList<>(DecksCreator.loadBuildingDeckByEra(3));
Collections.shuffle(buildingDeck);
if(nTotem==2)
{
buildingCardsAllEras.set(2, new ArrayList<BuildingCard>(buildingDeck.subList(0, 3)));
}else if(nTotem==5)
{
buildingCardsAllEras.set(2, new ArrayList<BuildingCard>(buildingDeck.subList(0, 5)));
}
else {
buildingCardsAllEras.set(2, new ArrayList<BuildingCard>(buildingDeck.subList(0, 4)));
}
}
/**
* Creates the tribeDeck. Loads the cards from each era, shuffle them, and then combine them in the final deck.
* In the end, two special events (Sustenance and ShamanicRitual) are added.
@@ -230,27 +267,11 @@ public class Board implements Serializable {
Collections.shuffle(buildingCards);
if(era==2)
{
if(nTotem<=3)
{
upperListBuilding.addAll(buildingCards.subList(0,2));
upperListBuilding.addAll(buildingCardsAllEras.get(1));
}
else
{
upperListBuilding.addAll( buildingCards.subList(0,3));
}
}
else // Era 3
{
if(nTotem==2)
{
upperListBuilding.addAll( buildingCards.subList(0,3));
}else if(nTotem==5)
{
upperListBuilding.addAll( buildingCards.subList(0,5));
}
else {
upperListBuilding.addAll(buildingCards.subList(0,4));
}
upperListBuilding.addAll(buildingCardsAllEras.get(2));
}
}
}
@@ -90,6 +90,7 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer {
if (controller.addPlayer(username)) {
clients.put(username, callback);
playerList.add(username);
System.out.println("Accepted player: " + username);
return true;
}
return false;
@@ -7,6 +7,7 @@ import it.polimi.ingsw.gc14.Network.NetworkEvent;
import it.polimi.ingsw.gc14.Network.NetworkEvents.AddPlayer;
import it.polimi.ingsw.gc14.Network.RMI.Server.RMIServer;
import it.polimi.ingsw.gc14.Network.TCP.Server.TCPServer;
import it.polimi.ingsw.gc14.View.TUI.TUI;
import java.rmi.RemoteException;
import java.util.concurrent.BlockingQueue;
@@ -56,6 +57,8 @@ public class ServerLauncher {
*/
static LimitedList<String> playerList;
TUI view;
/**
* Class constructor that initializes the attributes.
@@ -137,11 +140,14 @@ public class ServerLauncher {
System.out.println("\n\nNotifying model");
serverRMI.notifyAll(gameController.getModel());
serverTCP.notifyAll(gameController.getModel());
this.view = new TUI(gameController.getModel());
this.view.fullRender();
// Game execution
while (true) {
try {
this.doFirstEvent();
this.view.fullRender();
} catch (InterruptedException e) {
Thread.currentThread().interrupt();
break;
@@ -125,11 +125,11 @@ class BoardTest {
void removeUpperTribeCard() {
Board bd = new Board(3);
List<TribeCard> before = bd.upperListTribe;
TribeCard cardToRemove = before.get(2);
List<TribeCard> before = new ArrayList<>( bd.upperListTribe);
TribeCard cardToRemove = before.get(0);
before.remove(2);
bd.removeUpperTribeCard(cardToRemove);
assertEquals(cardToRemove, before.remove(0));
assertTrue( bd.removeUpperTribeCard(cardToRemove));
assertEquals(before, bd.upperListTribe);
}
@@ -139,25 +139,25 @@ class BoardTest {
void removeLowerTribeCard() {
Board bd = new Board(3);
List<TribeCard> before = bd.lowerListTribe;
TribeCard cardToRemove = before.get(2);
List<TribeCard> before = new ArrayList<>(bd.lowerListTribe);
TribeCard cardToRemove = before.get(0);
before.remove(2);
bd.removeLowerTribeCard(cardToRemove);
assertEquals(cardToRemove, before.remove(0));
assertTrue( bd.removeLowerTribeCard(cardToRemove));
assertEquals(before, bd.lowerListTribe);
}
@Test
@DisplayName("RemgetTribeDeckSizeoving a card from upper row of building cards")
@DisplayName("Removing a card from upper row of building cards")
void removeUpperBuildingCard() {
Board bd = new Board(3);
List<BuildingCard> before = bd.upperListBuilding;
List<BuildingCard> before = new ArrayList<>(bd.upperListBuilding);
BuildingCard cardToRemove = before.get(0);
before.remove(0);
bd.removeUpperBuildingCard(cardToRemove);
assertEquals(cardToRemove, before.remove(0));
assertTrue( bd.removeUpperBuildingCard(cardToRemove));
assertEquals(before, bd.upperListBuilding);
}
@@ -171,12 +171,10 @@ class BoardTest {
bd.nextRound(); // Skip to era 2
}
List<BuildingCard> before = bd.lowerListBuilding;
List<BuildingCard> before = new ArrayList<>( bd.lowerListBuilding);
BuildingCard cardToRemove = before.get(0);
before.remove(0);
bd.removeLowerBuildingCard(cardToRemove);
assertEquals(cardToRemove, before.remove(0));
assertTrue(bd.removeLowerBuildingCard(cardToRemove));
assertEquals(before, bd.lowerListBuilding);
}
@@ -214,6 +212,8 @@ class BoardTest {
assertEquals(upperListBefore, bd1.lowerListBuilding);
assertNotEquals(upperListBefore, bd1.upperListBuilding);
assertEquals(2, bd1.upperListBuilding.size());
assertTrue(bd1.upperListBuilding.stream().allMatch(x->x.getEra()==2));
assertTrue(bd1.lowerListBuilding.stream().allMatch(x->x.getEra()==1));
// Era 2, nTotem > 3
@@ -227,7 +227,8 @@ class BoardTest {
assertEquals(upperListBefore, bd2.lowerListBuilding);
assertNotEquals(upperListBefore, bd2.upperListBuilding);
assertEquals(3, bd2.upperListBuilding.size());
assertTrue(bd2.upperListBuilding.stream().allMatch(x->x.getEra()==2));
assertTrue(bd2.lowerListBuilding.stream().allMatch(x->x.getEra()==1));
// Era 3, nTotem == 2
numPlayer = 2;
@@ -243,6 +244,8 @@ class BoardTest {
assertEquals(upperListBefore, bd3.lowerListBuilding);
assertNotEquals(upperListBefore, bd3.upperListBuilding);
assertEquals(3, bd3.upperListBuilding.size());
assertTrue(bd3.upperListBuilding.stream().allMatch(x->x.getEra()==3));
assertTrue(bd3.lowerListBuilding.stream().allMatch(x->x.getEra()==2));
// Era 3, nTotem == 5
@@ -259,6 +262,8 @@ class BoardTest {
assertEquals(upperListBefore, bd4.lowerListBuilding);
assertNotEquals(upperListBefore, bd4.upperListBuilding);
assertEquals(5, bd4.upperListBuilding.size());
assertTrue(bd4.upperListBuilding.stream().allMatch(x->x.getEra()==3));
assertTrue(bd4.lowerListBuilding.stream().allMatch(x->x.getEra()==2));
// Era 3, nTotem != 2, 5
@@ -275,6 +280,8 @@ class BoardTest {
assertEquals(upperListBefore, bd5.lowerListBuilding);
assertNotEquals(upperListBefore, bd5.upperListBuilding);
assertEquals(4, bd5.upperListBuilding.size());
assertTrue(bd5.upperListBuilding.stream().allMatch(x->x.getEra()==3));
assertTrue(bd5.lowerListBuilding.stream().allMatch(x->x.getEra()==2));
}
@@ -8,7 +8,6 @@ import it.polimi.ingsw.gc14.Model.GamePackage.GameStages;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.Timeout;
import java.io.IOException;
import java.util.*;
import java.util.concurrent.TimeUnit;
@@ -70,29 +69,44 @@ class GameTest {
Player current = game.getCurrentState().getCurrentPlayer();
assertNotNull(current);
if (game.getCurrentState().getNLower() > 0 && hasDrawableLower(game)) {
if (game.getCurrentState().getNLower() > 0) {
int index = firstNonEventIndexOrMinusOne(game.getLowerListTribeCards());
if (index != -1) {
assertTrue(game.DrawLowerTribeCardByIndex(current, index));
} else {
current.addFood(100);
assertTrue(game.DrawLowerBuildingCardByIndex(current, 0));
return;
}
} else if (game.getCurrentState().getNUpper() > 0 && hasDrawableUpper(game)) {
if (!game.getLowerListBuilding().isEmpty()) {
current.addFood(100);
assertTrue(game.DrawLowerBuildingCardByIndex(current, 0));
return;
}
assertTrue(game.SkipLowerDrawing(current));
return;
}
if (game.getCurrentState().getNUpper() > 0) {
int index = firstNonEventIndexOrMinusOne(game.getUpperListTribeCards());
if (index != -1) {
assertTrue(game.DrawUpperTribeCardByIndex(current, index));
} else {
current.addFood(100);
assertTrue(game.DrawUpperBuildingCardByIndex(current, 0));
return;
}
if (!game.getUpperListBuilding().isEmpty()) {
current.addFood(100);
assertTrue(game.DrawUpperBuildingCardByIndex(current, 0));
return;
}
assertTrue(game.SkipUpperDrawing(current));
return;
}
} else {
fail(
"Current player has no drawable cards, although the game is still resolving actions.\n" +
"Current player has no remaining draw actions.\n" +
"Current player: " + current + "\n" +
"Round: " + game.getCurrentState().getRound() + "\n" +
"Stage: " + game.getCurrentState().getGameStage() + "\n" +
@@ -109,7 +123,6 @@ class GameTest {
"First upper non-event: " + firstNonEventIndexOrMinusOne(game.getUpperListTribeCards())
);
}
}
private void resolveActionsUntilOptionalCardEffect(Game game) {
while (game.getCurrentState().getGameStage() == GameStages.RESOLVING_ACTIONS) {
@@ -140,12 +153,13 @@ class GameTest {
resolveOptionalPhaseIfPresent(game);
}
private boolean hasDrawableLower(Game game) {
return firstNonEventIndexOrMinusOne(game.getLowerListTribeCards()) != -1;
}
private void resolveCurrentPlayerCompletely(Game game, Player expectedPlayer) {
assertEquals(expectedPlayer, game.getCurrentState().getCurrentPlayer());
private boolean hasDrawableUpper(Game game) {
return firstNonEventIndexOrMinusOne(game.getUpperListTribeCards()) != -1;
while (game.getCurrentState().getGameStage() == GameStages.RESOLVING_ACTIONS
&& expectedPlayer.equals(game.getCurrentState().getCurrentPlayer())) {
resolveOneMandatoryAction(game);
}
}
@Test
@@ -806,7 +820,7 @@ class GameTest {
assertFalse(game.PickOptionalBuildingCard(current, 0));
}
@Test
void toStringModel() throws IOException, InterruptedException {
void toStringModel() {
Game game=new Game(5);
Player p1=new Player("p1");
Player p2=new Player("p2");
@@ -826,6 +840,119 @@ class GameTest {
assertTrue(game.SlotChoiceByIndex(game.getCurrentState().getCurrentPlayer(), i));
}
System.out.println(game.toString());
String result = game.toString();
assertNotNull(result);
assertFalse(result.isBlank());
assertTrue(result.contains("CURRENT STATE"));
}
@Test
@Timeout(value = 5, unit = TimeUnit.SECONDS)
void shouldDrawLowerBuildingWhenAvailableAndPlayerCanPay() {
Game game = new Game(3);
addPlayers(game, 3, "lower_building_");
for (int safety = 0; safety < 300; safety++) {
if (game.getCurrentState().getGameStage() == GameStages.SLOT_CHOICE) {
completeSlotChoice(game);
}
if (game.getCurrentState().getGameStage() == GameStages.RESOLVING_ACTIONS) {
Player current = game.getCurrentState().getCurrentPlayer();
assertNotNull(current);
if (game.getCurrentState().getNLower() > 0
&& !game.getLowerListBuilding().isEmpty()) {
current.addFood(100);
int buildingsBefore = current.buildingCards.size();
int lowerBuildingsBefore = game.getLowerListBuilding().size();
int foodBefore = current.getFoodValue();
assertTrue(
game.DrawLowerBuildingCardByIndex(current, 0),
"DrawLowerBuildingCardByIndex should return true when lower building exists, player can draw lower, and player has food."
);
assertEquals(buildingsBefore + 1, current.buildingCards.size());
assertEquals(lowerBuildingsBefore - 1, game.getLowerListBuilding().size());
assertTrue(current.getFoodValue() < foodBefore);
return;
}
resolveOneMandatoryAction(game);
}
if (game.getCurrentState().getGameStage() == GameStages.OPTIONAL_CARD_EFFECT) {
resolveOptionalPhaseIfPresent(game);
}
if (game.getCurrentState().getGameStage() == GameStages.ENDED) {
break;
}
}
fail("The test never reached a state with NLower > 0 and at least one lower building.");
}
@Test
void drawBuildingShouldReturnFalseIfPlayerCannotPay() {
Game game = new Game(3);
addPlayers(game, 3, "no_food_building_");
completeSlotChoice(game);
while (game.getCurrentState().getGameStage() == GameStages.RESOLVING_ACTIONS) {
Player current = game.getCurrentState().getCurrentPlayer();
if (game.getCurrentState().getNUpper() > 0
&& !game.getUpperListBuilding().isEmpty()
&& current.builders.isEmpty()) {
while (current.getFoodValue() > 0) {
assertTrue(current.removeFood(1));
}
assertEquals(0, current.getFoodValue());
assertFalse(game.DrawUpperBuildingCardByIndex(current, 0));
return;
}
resolveOneMandatoryAction(game);
}
fail("No upper building draw state reached with a player without builders.");
}
@Test
void shouldResolvePlayersAccordingToSlotOrder() {
Game game = new Game(3);
addPlayers(game, 3, "slot_order_");
Player firstChooser = game.getCurrentState().getCurrentPlayer();
assertTrue(game.SlotChoiceByIndex(firstChooser, 2));
Player secondChooser = game.getCurrentState().getCurrentPlayer();
assertTrue(game.SlotChoiceByIndex(secondChooser, 0));
Player thirdChooser = game.getCurrentState().getCurrentPlayer();
assertTrue(game.SlotChoiceByIndex(thirdChooser, 1));
assertEquals(GameStages.RESOLVING_ACTIONS, game.getCurrentState().getGameStage());
assertEquals(secondChooser, game.getCurrentState().getCurrentPlayer());
resolveCurrentPlayerCompletely(game, secondChooser);
assertEquals(thirdChooser, game.getCurrentState().getCurrentPlayer());
resolveCurrentPlayerCompletely(game, thirdChooser);
assertEquals(firstChooser, game.getCurrentState().getCurrentPlayer());
}
}