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) { switch(action) {
case "0" -> controller.slotChoice(username, pos); case "0" -> controller.slotChoice(username, pos);
case "1" -> controller.drawUpperBuildingCard(username, pos); case "1" -> controller.drawUpperTribeCard(username, pos);
case "2" -> controller.drawUpperTribeCard(username, pos); case "2" -> controller.drawUpperBuildingCard(username, pos);
case "3" -> controller.drawLowerBuildingCard(username, pos); case "3" -> controller.drawLowerTribeCard(username, pos);
case "4" -> controller.drawLowerTribeCard(username, pos); case "4" -> controller.drawLowerBuildingCard(username, pos);
case "5" -> controller.pickOptionalTribeCard(username, pos); case "5" -> controller.pickOptionalTribeCard(username, pos);
case "6" -> controller.pickOptionalBuildingCard(username, pos); case "6" -> controller.pickOptionalBuildingCard(username, pos);
case "7" -> controller.noOptionalCard(username); case "7" -> controller.noOptionalCard(username);
@@ -346,7 +346,7 @@ public class Game implements Serializable {
{ {
return false; return false;
} }
if(currentState.getNUpper() <1) if(currentState.getNLower() <1)
return false; return false;
if(hasDrawableDown()) if(hasDrawableDown())
return false; return false;
@@ -640,7 +640,7 @@ public class Game implements Serializable {
{ {
currentState.GameStageUpdate(GameStages.OPTIONAL_CARD_EFFECT); currentState.GameStageUpdate(GameStages.OPTIONAL_CARD_EFFECT);
HashMap<Player,Integer> optional=new LinkedHashMap<>(); 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(); int tempCount=(int)p.buildingCards.stream().filter(x->x.getEffectId()==12).count();
if(tempCount>0) 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 */ /** 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;
private final ArrayList<List<BuildingCard>> buildingCardsAllEras;
/** Number of players */ /** Number of players */
private int nTotem; private int nTotem;
@@ -88,6 +90,7 @@ public class Board implements Serializable {
tribeDeck=generateTribeDeck(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();
@@ -110,10 +113,44 @@ public class Board implements Serializable {
ArrayList<BuildingCard> buildingDeck = new ArrayList<>(DecksCreator.loadBuildingDeckByEra(1)); ArrayList<BuildingCard> buildingDeck = new ArrayList<>(DecksCreator.loadBuildingDeckByEra(1));
Collections.shuffle(buildingDeck); Collections.shuffle(buildingDeck);
buildingCardsAllEras= new ArrayList<>();
buildingCardsAllEras.add(new ArrayList<>());
buildingCardsAllEras.add(new ArrayList<>());
buildingCardsAllEras.add(new ArrayList<>());
if(nTotem==2) if(nTotem==2)
{
buildingCardsAllEras.set(0, new ArrayList<>(buildingDeck.subList(0, 1)));
upperListBuilding = new ArrayList<>(buildingDeck.subList(0,1)); upperListBuilding = new ArrayList<>(buildingDeck.subList(0,1));
}
else else
{
buildingCardsAllEras.set(0, new ArrayList<>(buildingDeck.subList(0, 2)));
upperListBuilding = 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)));
}
} }
/** /**
@@ -230,27 +267,11 @@ public class Board implements Serializable {
Collections.shuffle(buildingCards); Collections.shuffle(buildingCards);
if(era==2) if(era==2)
{ {
if(nTotem<=3) upperListBuilding.addAll(buildingCardsAllEras.get(1));
{
upperListBuilding.addAll(buildingCards.subList(0,2));
}
else
{
upperListBuilding.addAll( buildingCards.subList(0,3));
}
} }
else // Era 3 else
{ {
if(nTotem==2) upperListBuilding.addAll(buildingCardsAllEras.get(2));
{
upperListBuilding.addAll( buildingCards.subList(0,3));
}else if(nTotem==5)
{
upperListBuilding.addAll( buildingCards.subList(0,5));
}
else {
upperListBuilding.addAll(buildingCards.subList(0,4));
}
} }
} }
} }
@@ -90,6 +90,7 @@ public class RMIServer extends UnicastRemoteObject implements IGameServer {
if (controller.addPlayer(username)) { if (controller.addPlayer(username)) {
clients.put(username, callback); clients.put(username, callback);
playerList.add(username); playerList.add(username);
System.out.println("Accepted player: " + username);
return true; return true;
} }
return false; 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.NetworkEvents.AddPlayer;
import it.polimi.ingsw.gc14.Network.RMI.Server.RMIServer; import it.polimi.ingsw.gc14.Network.RMI.Server.RMIServer;
import it.polimi.ingsw.gc14.Network.TCP.Server.TCPServer; import it.polimi.ingsw.gc14.Network.TCP.Server.TCPServer;
import it.polimi.ingsw.gc14.View.TUI.TUI;
import java.rmi.RemoteException; import java.rmi.RemoteException;
import java.util.concurrent.BlockingQueue; import java.util.concurrent.BlockingQueue;
@@ -56,6 +57,8 @@ public class ServerLauncher {
*/ */
static LimitedList<String> playerList; static LimitedList<String> playerList;
TUI view;
/** /**
* Class constructor that initializes the attributes. * Class constructor that initializes the attributes.
@@ -137,11 +140,14 @@ public class ServerLauncher {
System.out.println("\n\nNotifying model"); System.out.println("\n\nNotifying model");
serverRMI.notifyAll(gameController.getModel()); serverRMI.notifyAll(gameController.getModel());
serverTCP.notifyAll(gameController.getModel()); serverTCP.notifyAll(gameController.getModel());
this.view = new TUI(gameController.getModel());
this.view.fullRender();
// Game execution // Game execution
while (true) { while (true) {
try { try {
this.doFirstEvent(); this.doFirstEvent();
this.view.fullRender();
} catch (InterruptedException e) { } catch (InterruptedException e) {
Thread.currentThread().interrupt(); Thread.currentThread().interrupt();
break; break;
@@ -125,11 +125,11 @@ class BoardTest {
void removeUpperTribeCard() { void removeUpperTribeCard() {
Board bd = new Board(3); Board bd = new Board(3);
List<TribeCard> before = bd.upperListTribe; List<TribeCard> before = new ArrayList<>( bd.upperListTribe);
TribeCard cardToRemove = before.get(2); TribeCard cardToRemove = before.get(0);
before.remove(2); assertEquals(cardToRemove, before.remove(0));
bd.removeUpperTribeCard(cardToRemove); assertTrue( bd.removeUpperTribeCard(cardToRemove));
assertEquals(before, bd.upperListTribe); assertEquals(before, bd.upperListTribe);
} }
@@ -139,25 +139,25 @@ class BoardTest {
void removeLowerTribeCard() { void removeLowerTribeCard() {
Board bd = new Board(3); Board bd = new Board(3);
List<TribeCard> before = bd.lowerListTribe; List<TribeCard> before = new ArrayList<>(bd.lowerListTribe);
TribeCard cardToRemove = before.get(2); TribeCard cardToRemove = before.get(0);
before.remove(2); assertEquals(cardToRemove, before.remove(0));
bd.removeLowerTribeCard(cardToRemove); assertTrue( bd.removeLowerTribeCard(cardToRemove));
assertEquals(before, bd.lowerListTribe); assertEquals(before, bd.lowerListTribe);
} }
@Test @Test
@DisplayName("RemgetTribeDeckSizeoving a card from upper row of building cards") @DisplayName("Removing a card from upper row of building cards")
void removeUpperBuildingCard() { void removeUpperBuildingCard() {
Board bd = new Board(3); Board bd = new Board(3);
List<BuildingCard> before = bd.upperListBuilding; List<BuildingCard> before = new ArrayList<>(bd.upperListBuilding);
BuildingCard cardToRemove = before.get(0); BuildingCard cardToRemove = before.get(0);
before.remove(0); assertEquals(cardToRemove, before.remove(0));
bd.removeUpperBuildingCard(cardToRemove); assertTrue( bd.removeUpperBuildingCard(cardToRemove));
assertEquals(before, bd.upperListBuilding); assertEquals(before, bd.upperListBuilding);
} }
@@ -171,12 +171,10 @@ class BoardTest {
bd.nextRound(); // Skip to era 2 bd.nextRound(); // Skip to era 2
} }
List<BuildingCard> before = bd.lowerListBuilding; List<BuildingCard> before = new ArrayList<>( bd.lowerListBuilding);
BuildingCard cardToRemove = before.get(0); BuildingCard cardToRemove = before.get(0);
assertEquals(cardToRemove, before.remove(0));
before.remove(0); assertTrue(bd.removeLowerBuildingCard(cardToRemove));
bd.removeLowerBuildingCard(cardToRemove);
assertEquals(before, bd.lowerListBuilding); assertEquals(before, bd.lowerListBuilding);
} }
@@ -214,6 +212,8 @@ class BoardTest {
assertEquals(upperListBefore, bd1.lowerListBuilding); assertEquals(upperListBefore, bd1.lowerListBuilding);
assertNotEquals(upperListBefore, bd1.upperListBuilding); assertNotEquals(upperListBefore, bd1.upperListBuilding);
assertEquals(2, bd1.upperListBuilding.size()); 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 // Era 2, nTotem > 3
@@ -227,7 +227,8 @@ class BoardTest {
assertEquals(upperListBefore, bd2.lowerListBuilding); assertEquals(upperListBefore, bd2.lowerListBuilding);
assertNotEquals(upperListBefore, bd2.upperListBuilding); assertNotEquals(upperListBefore, bd2.upperListBuilding);
assertEquals(3, bd2.upperListBuilding.size()); 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 // Era 3, nTotem == 2
numPlayer = 2; numPlayer = 2;
@@ -243,6 +244,8 @@ class BoardTest {
assertEquals(upperListBefore, bd3.lowerListBuilding); assertEquals(upperListBefore, bd3.lowerListBuilding);
assertNotEquals(upperListBefore, bd3.upperListBuilding); assertNotEquals(upperListBefore, bd3.upperListBuilding);
assertEquals(3, bd3.upperListBuilding.size()); 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 // Era 3, nTotem == 5
@@ -259,6 +262,8 @@ class BoardTest {
assertEquals(upperListBefore, bd4.lowerListBuilding); assertEquals(upperListBefore, bd4.lowerListBuilding);
assertNotEquals(upperListBefore, bd4.upperListBuilding); assertNotEquals(upperListBefore, bd4.upperListBuilding);
assertEquals(5, bd4.upperListBuilding.size()); 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 // Era 3, nTotem != 2, 5
@@ -275,6 +280,8 @@ class BoardTest {
assertEquals(upperListBefore, bd5.lowerListBuilding); assertEquals(upperListBefore, bd5.lowerListBuilding);
assertNotEquals(upperListBefore, bd5.upperListBuilding); assertNotEquals(upperListBefore, bd5.upperListBuilding);
assertEquals(4, bd5.upperListBuilding.size()); 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.Test;
import org.junit.jupiter.api.Timeout; import org.junit.jupiter.api.Timeout;
import java.io.IOException;
import java.util.*; import java.util.*;
import java.util.concurrent.TimeUnit; import java.util.concurrent.TimeUnit;
@@ -70,45 +69,59 @@ class GameTest {
Player current = game.getCurrentState().getCurrentPlayer(); Player current = game.getCurrentState().getCurrentPlayer();
assertNotNull(current); assertNotNull(current);
if (game.getCurrentState().getNLower() > 0 && hasDrawableLower(game)) { if (game.getCurrentState().getNLower() > 0) {
int index = firstNonEventIndexOrMinusOne(game.getLowerListTribeCards()); int index = firstNonEventIndexOrMinusOne(game.getLowerListTribeCards());
if (index != -1) { if (index != -1) {
assertTrue(game.DrawLowerTribeCardByIndex(current, index)); assertTrue(game.DrawLowerTribeCardByIndex(current, index));
} else { return;
current.addFood(100);
assertTrue(game.DrawLowerBuildingCardByIndex(current, 0));
} }
} 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()); int index = firstNonEventIndexOrMinusOne(game.getUpperListTribeCards());
if (index != -1) { if (index != -1) {
assertTrue(game.DrawUpperTribeCardByIndex(current, index)); assertTrue(game.DrawUpperTribeCardByIndex(current, index));
} else { return;
current.addFood(100);
assertTrue(game.DrawUpperBuildingCardByIndex(current, 0));
} }
} else { if (!game.getUpperListBuilding().isEmpty()) {
fail( current.addFood(100);
"Current player has no drawable cards, although the game is still resolving actions.\n" + assertTrue(game.DrawUpperBuildingCardByIndex(current, 0));
"Current player: " + current + "\n" + return;
"Round: " + game.getCurrentState().getRound() + "\n" + }
"Stage: " + game.getCurrentState().getGameStage() + "\n" +
"Slot: " + (game.getCurrentState().getSlot() == null assertTrue(game.SkipUpperDrawing(current));
? "null" return;
: game.getCurrentState().getSlot().getSlotId()) + "\n" +
"NLower: " + game.getCurrentState().getNLower() + "\n" +
"NUpper: " + game.getCurrentState().getNUpper() + "\n" +
"Lower tribe size: " + game.getLowerListTribeCards().size() + "\n" +
"Upper tribe size: " + game.getUpperListTribeCards().size() + "\n" +
"Lower building size: " + game.getLowerListBuilding().size() + "\n" +
"Upper building size: " + game.getUpperListBuilding().size() + "\n" +
"First lower non-event: " + firstNonEventIndexOrMinusOne(game.getLowerListTribeCards()) + "\n" +
"First upper non-event: " + firstNonEventIndexOrMinusOne(game.getUpperListTribeCards())
);
} }
fail(
"Current player has no remaining draw actions.\n" +
"Current player: " + current + "\n" +
"Round: " + game.getCurrentState().getRound() + "\n" +
"Stage: " + game.getCurrentState().getGameStage() + "\n" +
"Slot: " + (game.getCurrentState().getSlot() == null
? "null"
: game.getCurrentState().getSlot().getSlotId()) + "\n" +
"NLower: " + game.getCurrentState().getNLower() + "\n" +
"NUpper: " + game.getCurrentState().getNUpper() + "\n" +
"Lower tribe size: " + game.getLowerListTribeCards().size() + "\n" +
"Upper tribe size: " + game.getUpperListTribeCards().size() + "\n" +
"Lower building size: " + game.getLowerListBuilding().size() + "\n" +
"Upper building size: " + game.getUpperListBuilding().size() + "\n" +
"First lower non-event: " + firstNonEventIndexOrMinusOne(game.getLowerListTribeCards()) + "\n" +
"First upper non-event: " + firstNonEventIndexOrMinusOne(game.getUpperListTribeCards())
);
} }
private void resolveActionsUntilOptionalCardEffect(Game game) { private void resolveActionsUntilOptionalCardEffect(Game game) {
@@ -140,12 +153,13 @@ class GameTest {
resolveOptionalPhaseIfPresent(game); resolveOptionalPhaseIfPresent(game);
} }
private boolean hasDrawableLower(Game game) { private void resolveCurrentPlayerCompletely(Game game, Player expectedPlayer) {
return firstNonEventIndexOrMinusOne(game.getLowerListTribeCards()) != -1; assertEquals(expectedPlayer, game.getCurrentState().getCurrentPlayer());
}
private boolean hasDrawableUpper(Game game) { while (game.getCurrentState().getGameStage() == GameStages.RESOLVING_ACTIONS
return firstNonEventIndexOrMinusOne(game.getUpperListTribeCards()) != -1; && expectedPlayer.equals(game.getCurrentState().getCurrentPlayer())) {
resolveOneMandatoryAction(game);
}
} }
@Test @Test
@@ -806,7 +820,7 @@ class GameTest {
assertFalse(game.PickOptionalBuildingCard(current, 0)); assertFalse(game.PickOptionalBuildingCard(current, 0));
} }
@Test @Test
void toStringModel() throws IOException, InterruptedException { void toStringModel() {
Game game=new Game(5); Game game=new Game(5);
Player p1=new Player("p1"); Player p1=new Player("p1");
Player p2=new Player("p2"); Player p2=new Player("p2");
@@ -826,6 +840,119 @@ class GameTest {
assertTrue(game.SlotChoiceByIndex(game.getCurrentState().getCurrentPlayer(), i)); 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());
} }
} }