This commit is contained in:
GabrieleRadice
2026-05-03 17:49:48 +02:00
6 changed files with 109 additions and 65 deletions
@@ -4,6 +4,9 @@ import it.polimi.ingsw.gc14.Network.NetworkEvent;
import it.polimi.ingsw.gc14.Network.RMI.Client.RMIClient;
import it.polimi.ingsw.gc14.Network.TCP.Client.TCPClient;
import it.polimi.ingsw.gc14.View.TUI.TUI;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
/**
@@ -14,6 +17,8 @@ import java.util.Scanner;
*/
public class ClientLauncherTUI {
List<String> admissibleChar=new ArrayList<>();
/**
* The TUI view associated with this client.
*/
@@ -29,6 +34,23 @@ public class ClientLauncherTUI {
*/
public void main() throws InterruptedException {
view = new TUI(null);
admissibleChar.add("0");
admissibleChar.add("1");
admissibleChar.add("2");
admissibleChar.add("3");
admissibleChar.add("4");
admissibleChar.add("5");
admissibleChar.add("6");
admissibleChar.add("7");
admissibleChar.add("8");
admissibleChar.add("9");
admissibleChar.add("A");
admissibleChar.add("B");
admissibleChar.add("C");
admissibleChar.add("a");
admissibleChar.add("b");
admissibleChar.add("c");
ClientController controller = new ClientController(view);
Scanner scanner = new Scanner(System.in);
System.out.println("Insert username: ");
@@ -103,29 +125,37 @@ public class ClientLauncherTUI {
private void getInput(Scanner scanner, ClientController controller, String username) {
String action = scanner.next();
int pos = -1;
if (!action.equals("7") && !action.equals("8") && !action.equals("9") && !action.equals("A") && !action.equals("B") && !action.equals("C")) {
try {
System.out.println("Insert the required position:");
pos = scanner.nextInt();
} catch (Exception e) {
System.out.println("ERROR: Invalid input(expected number)");
if(admissibleChar.contains(username))
{
if (!action.equals("7") && !action.equals("8") && !action.equals("9") && !action.equals("A") && !action.equals("B") && !action.equals("C")) {
try {
System.out.println("Insert the required position:");
pos = scanner.nextInt();
} catch (Exception e) {
System.out.println("ERROR: Invalid input(expected number)");
}
}
switch (action) {
case "0" -> controller.slotChoice(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);
case "8" -> controller.skipUpper(username);
case "9" -> controller.skipLower(username);
case "A", "a" -> view.fullRender();
case "B", "b" -> view.renderBoard();
case "C", "c" -> view.renderPlayer();
default -> {}
}
}
switch (action) {
case "0" -> controller.slotChoice(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);
case "8" -> controller.skipUpper(username);
case "9" -> controller.skipLower(username);
case "A" -> view.fullRender();
case "B" -> view.renderBoard();
case "C" -> view.renderPlayer();
default -> {}
else
{
System.out.println("ERROR: Invalid input(action not valid)");
}
}
}
@@ -232,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);
}
@@ -264,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;
@@ -288,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;
}
@@ -324,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;
}
@@ -355,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;
}
@@ -389,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;
}
@@ -430,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;
}
@@ -469,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;
}
@@ -514,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())){
@@ -549,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())){
@@ -581,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())){
@@ -620,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);
@@ -636,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()) {
@@ -655,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();
@@ -676,7 +690,7 @@ public class Game implements Serializable {
return;
}
currentState.GameStageUpdate(GameStages.RESOLVING_EVENT);
currentState.GameStageUpdate(GameStages.RES_EVENT);
if (currentState.getRound() < 10) {
nextRound();
@@ -691,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) {
@@ -699,7 +713,7 @@ public class Game implements Serializable {
return;
}
currentState.GameStageUpdate(GameStages.RESOLVING_EVENT);
currentState.GameStageUpdate(GameStages.RES_EVENT);
if (currentState.getRound() < 10) {
nextRound();
@@ -748,7 +762,7 @@ public class Game implements Serializable {
*/
private void EventResolution() {
if(currentState.getGameStage()!= GameStages.RESOLVING_EVENT)
if(currentState.getGameStage()!= GameStages.RES_EVENT)
{
return;
}
@@ -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
}
@@ -36,7 +36,7 @@ class GameControllerTest {
assertTrue(controller.slotChoice(current.getUserName(), i));
}
assertEquals(GameStages.RESOLVING_ACTIONS, game.getCurrentState().getGameStage());
assertEquals(GameStages.RES_ACTIONS, game.getCurrentState().getGameStage());
return order;
}
@@ -89,12 +89,12 @@ class GameControllerTest {
private void resolveActionsUntilOptionalCardEffect(Game game, GameController controller) {
int guard = 0;
while (game.getCurrentState().getGameStage() == GameStages.RESOLVING_ACTIONS && guard < 100) {
while (game.getCurrentState().getGameStage() == GameStages.RES_ACTIONS && guard < 100) {
guard++;
resolveOneMandatoryAction(game, controller);
}
assertEquals(GameStages.OPTIONAL_CARD_EFFECT, game.getCurrentState().getGameStage());
assertEquals(GameStages.OPT_CARD_E, game.getCurrentState().getGameStage());
}
private void resolveOneMandatoryAction(Game game, GameController controller) {
@@ -154,7 +154,7 @@ class GameControllerTest {
assertTrue(controller.slotChoice(current.getUserName(), slotIndexes[i]));
}
assertEquals(GameStages.RESOLVING_ACTIONS, game.getCurrentState().getGameStage());
assertEquals(GameStages.RES_ACTIONS, game.getCurrentState().getGameStage());
return order;
}
@@ -206,7 +206,7 @@ class GameControllerTest {
Queue<Player> order = completeSlotChoice(game, controller);
assertEquals(GameStages.RESOLVING_ACTIONS, game.getCurrentState().getGameStage());
assertEquals(GameStages.RES_ACTIONS, game.getCurrentState().getGameStage());
assertEquals(order.poll(), game.getCurrentState().getCurrentPlayer());
}
@@ -320,7 +320,7 @@ class GameControllerTest {
assertTrue(controller.slotChoice(current.getUserName(), i));
}
assertEquals(GameStages.RESOLVING_ACTIONS, game.getCurrentState().getGameStage());
assertEquals(GameStages.RES_ACTIONS, game.getCurrentState().getGameStage());
Player tempPlayer = players.poll();
assertNotNull(tempPlayer);
@@ -395,7 +395,7 @@ class GameControllerTest {
completeSlotChoice(game, controller);
assertEquals(GameStages.RESOLVING_ACTIONS, game.getCurrentState().getGameStage());
assertEquals(GameStages.RES_ACTIONS, game.getCurrentState().getGameStage());
String cur = game.getCurrentState().getCurrentPlayer().getUserName();
@@ -70,7 +70,7 @@ class CurrentStateTest {
void getGameStage() {
Player p = new Player("test");
Slot s = new Slot('A');
GameStages g = GameStages.RESOLVING_ACTIONS;
GameStages g = GameStages.RES_ACTIONS;
CurrentState curr = new CurrentState();
curr.PlayerUpdate(p, s);
curr.GameStageUpdate(g);
@@ -122,12 +122,12 @@ class CurrentStateTest {
void gameStageUpdate() {
Player p = new Player("test");
Slot s = new Slot('A');
GameStages g = GameStages.RESOLVING_ACTIONS;
GameStages g = GameStages.RES_ACTIONS;
CurrentState curr = new CurrentState();
assertEquals(GameStages.WAITING, curr.getGameStage());
curr.PlayerUpdate(p, s);
curr.GameStageUpdate(g);
assertEquals(GameStages.RESOLVING_ACTIONS, curr.getGameStage());
assertEquals(GameStages.RES_ACTIONS, curr.getGameStage());
}
@Test
@@ -30,7 +30,7 @@ class GameTest {
assertTrue(game.SlotChoiceByIndex(current, i));
}
assertEquals(GameStages.RESOLVING_ACTIONS, game.getCurrentState().getGameStage());
assertEquals(GameStages.RES_ACTIONS, game.getCurrentState().getGameStage());
return order;
}
@@ -63,7 +63,7 @@ class GameTest {
}
private void resolveAllMandatoryActions(Game game) {
while (game.getCurrentState().getGameStage() == GameStages.RESOLVING_ACTIONS) {
while (game.getCurrentState().getGameStage() == GameStages.RES_ACTIONS) {
resolveOneMandatoryAction(game);
}
}
@@ -126,15 +126,15 @@ class GameTest {
}
private void resolveActionsUntilOptionalCardEffect(Game game) {
while (game.getCurrentState().getGameStage() == GameStages.RESOLVING_ACTIONS) {
while (game.getCurrentState().getGameStage() == GameStages.RES_ACTIONS) {
resolveOneMandatoryAction(game);
}
assertEquals(GameStages.OPTIONAL_CARD_EFFECT, game.getCurrentState().getGameStage());
assertEquals(GameStages.OPT_CARD_E, game.getCurrentState().getGameStage());
}
private void resolveOptionalPhaseIfPresent(Game game) {
while (game.getCurrentState().getGameStage() == GameStages.OPTIONAL_CARD_EFFECT) {
while (game.getCurrentState().getGameStage() == GameStages.OPT_CARD_E) {
Player current = game.getCurrentState().getCurrentPlayer();
assertNotNull(current);
@@ -147,7 +147,7 @@ class GameTest {
completeSlotChoice(game);
assertEquals(GameStages.RESOLVING_ACTIONS, game.getCurrentState().getGameStage());
assertEquals(GameStages.RES_ACTIONS, game.getCurrentState().getGameStage());
resolveAllMandatoryActions(game);
@@ -157,7 +157,7 @@ class GameTest {
private void resolveCurrentPlayerCompletely(Game game, Player expectedPlayer) {
assertEquals(expectedPlayer, game.getCurrentState().getCurrentPlayer());
while (game.getCurrentState().getGameStage() == GameStages.RESOLVING_ACTIONS
while (game.getCurrentState().getGameStage() == GameStages.RES_ACTIONS
&& expectedPlayer.equals(game.getCurrentState().getCurrentPlayer())) {
resolveOneMandatoryAction(game);
}
@@ -357,7 +357,7 @@ class GameTest {
assertNotNull(thirdPlayer);
assertEquals(thirdPlayer, game.getCurrentState().getCurrentPlayer());
while (game.getCurrentState().getGameStage() == GameStages.RESOLVING_ACTIONS
while (game.getCurrentState().getGameStage() == GameStages.RES_ACTIONS
&& thirdPlayer.equals(game.getCurrentState().getCurrentPlayer())) {
resolveOneMandatoryAction(game);
}
@@ -497,7 +497,7 @@ class GameTest {
assertTrue(game.NoOptionalCard(current));
assertEquals(GameStages.OPTIONAL_CARD_EFFECT, game.getCurrentState().getGameStage());
assertEquals(GameStages.OPT_CARD_E, game.getCurrentState().getGameStage());
assertNotEquals(current, game.getCurrentState().getCurrentPlayer());
}
@@ -662,7 +662,7 @@ class GameTest {
assertDoesNotThrow(() -> resolveAllMandatoryActions(game));
assertNotEquals(GameStages.OPTIONAL_CARD_EFFECT, game.getCurrentState().getGameStage());
assertNotEquals(GameStages.OPT_CARD_E, game.getCurrentState().getGameStage());
}
@@ -879,7 +879,7 @@ class GameTest {
completeSlotChoice(game);
}
if (game.getCurrentState().getGameStage() == GameStages.RESOLVING_ACTIONS) {
if (game.getCurrentState().getGameStage() == GameStages.RES_ACTIONS) {
Player current = game.getCurrentState().getCurrentPlayer();
assertNotNull(current);
@@ -920,7 +920,7 @@ class GameTest {
resolveOneMandatoryAction(game);
}
if (game.getCurrentState().getGameStage() == GameStages.OPTIONAL_CARD_EFFECT) {
if (game.getCurrentState().getGameStage() == GameStages.OPT_CARD_E) {
resolveOptionalPhaseIfPresent(game);
}
@@ -939,7 +939,7 @@ class GameTest {
completeSlotChoice(game);
while (game.getCurrentState().getGameStage() == GameStages.RESOLVING_ACTIONS) {
while (game.getCurrentState().getGameStage() == GameStages.RES_ACTIONS) {
Player current = game.getCurrentState().getCurrentPlayer();
if (game.getCurrentState().getNUpper() > 0
@@ -977,7 +977,7 @@ class GameTest {
Player thirdChooser = game.getCurrentState().getCurrentPlayer();
assertTrue(game.SlotChoiceByIndex(thirdChooser, 1));
assertEquals(GameStages.RESOLVING_ACTIONS, game.getCurrentState().getGameStage());
assertEquals(GameStages.RES_ACTIONS, game.getCurrentState().getGameStage());
assertEquals(secondChooser, game.getCurrentState().getCurrentPlayer());
@@ -1009,7 +1009,7 @@ class GameTest {
completeSlotChoice(game);
resolveAllMandatoryActions(game);
assertEquals(GameStages.OPTIONAL_CARD_EFFECT, game.getCurrentState().getGameStage());
assertEquals(GameStages.OPT_CARD_E, game.getCurrentState().getGameStage());
assertDoesNotThrow(() -> resolveOptionalPhaseIfPresent(game));
@@ -1032,7 +1032,7 @@ class GameTest {
completeSlotChoice(game);
resolveAllMandatoryActions(game);
assertEquals(GameStages.OPTIONAL_CARD_EFFECT, game.getCurrentState().getGameStage());
assertEquals(GameStages.OPT_CARD_E, game.getCurrentState().getGameStage());
Player player = players.get(0);
@@ -1068,7 +1068,7 @@ class GameTest {
completeSlotChoice(game);
resolveAllMandatoryActions(game);
assertEquals(GameStages.OPTIONAL_CARD_EFFECT, game.getCurrentState().getGameStage());
assertEquals(GameStages.OPT_CARD_E, game.getCurrentState().getGameStage());
Player player = players.get(0);
assertNotNull(player);