diff --git a/bin/cards/maumau/model/FinishedState.class b/bin/cards/maumau/model/FinishedState.class new file mode 100644 index 0000000..21de8fb Binary files /dev/null and b/bin/cards/maumau/model/FinishedState.class differ diff --git a/bin/cards/maumau/model/PlayerHandler.class b/bin/cards/maumau/model/PlayerHandler.class index 7ce73d1..d52ee66 100644 Binary files a/bin/cards/maumau/model/PlayerHandler.class and b/bin/cards/maumau/model/PlayerHandler.class differ diff --git a/bin/cards/maumau/model/PlayerState.class b/bin/cards/maumau/model/PlayerState.class new file mode 100644 index 0000000..4a0ce41 Binary files /dev/null and b/bin/cards/maumau/model/PlayerState.class differ diff --git a/bin/cards/maumau/model/WaitForMauMauState.class b/bin/cards/maumau/model/WaitForMauMauState.class new file mode 100644 index 0000000..44237d6 Binary files /dev/null and b/bin/cards/maumau/model/WaitForMauMauState.class differ diff --git a/bin/cards/maumau/model/WaitForMauState.class b/bin/cards/maumau/model/WaitForMauState.class new file mode 100644 index 0000000..e67812c Binary files /dev/null and b/bin/cards/maumau/model/WaitForMauState.class differ diff --git a/bin/cards/maumau/model/WaitForNextTurnState.class b/bin/cards/maumau/model/WaitForNextTurnState.class new file mode 100644 index 0000000..eef4723 Binary files /dev/null and b/bin/cards/maumau/model/WaitForNextTurnState.class differ diff --git a/src/cards/maumau/model/FinishedState.java b/src/cards/maumau/model/FinishedState.java new file mode 100644 index 0000000..afdd3ba --- /dev/null +++ b/src/cards/maumau/model/FinishedState.java @@ -0,0 +1,18 @@ +package cards.maumau.model; + +public class FinishedState implements PlayerState { + @Override + public void nextTurn(int n) { + // Do nothing, the game is finished + } + + @Override + public void mau(Player p) { + // Do nothing, the game is finished + } + + @Override + public void maumau(Player p) { + // Do nothing, the game is finished + } +} diff --git a/src/cards/maumau/model/PlayerHandler.java b/src/cards/maumau/model/PlayerHandler.java index 8a145c7..70402af 100644 --- a/src/cards/maumau/model/PlayerHandler.java +++ b/src/cards/maumau/model/PlayerHandler.java @@ -4,103 +4,94 @@ import java.util.ArrayList; import java.util.LinkedList; import java.util.List; -/** - * Handles players in a MauMau game. - */ -class PlayerHandler { +public class PlayerHandler { private final MauMau game; private final List players = new LinkedList<>(); private final List ranking = new ArrayList<>(); private Player remember; - /** - * Constructs a PlayerHandler for the specified MauMau game. - * - * @param game The MauMau game instance. - */ - PlayerHandler(MauMau game) { + private PlayerState waitForNextTurnState; + private PlayerState waitForMauState; + private PlayerState waitForMauMauState; + private PlayerState finishedState; + + private PlayerState currentState; + + public PlayerHandler(MauMau game) { this.game = game; + this.waitForNextTurnState = new WaitForNextTurnState(this); + this.waitForMauState = new WaitForMauState(this); + this.waitForMauMauState = new WaitForMauMauState(this); + this.finishedState = new FinishedState(); + + this.currentState = waitForNextTurnState; } - /** - * Initiates the next turn in the game. - * - * @param n The number of turns to proceed. - */ - void nextTurn(int n) { - //TODO implement + public void nextTurn(int n) { + currentState.nextTurn(n); } - /** - * Handles a player calling "Mau". - * - * @param p The player calling "Mau". - */ - void mau(Player p) { - //TODO implement + public void mau(Player p) { + currentState.mau(p); } - /** - * Handles a player calling "Mau-Mau". - * - * @param p The player calling "Mau-Mau". - */ - void maumau(Player p) { - //TODO implement + public void maumau(Player p) { + currentState.maumau(p); } - /** - * Returns the list of players participating in the game. - * - * @return The list of players. - */ - List getPlayers() { + public List getPlayers() { return players; } - /** - * Returns the ranking of players based on the order they finished the game. - * - * @return The ranking of players. - */ - List getRanking() { + public List getRanking() { return ranking; } - /** - * Adds a player to the game. - * - * @param player The player to add. - * @throws IllegalArgumentException if a player with the same name already exists. - */ - void addPlayer(Player player) { - //TODO implement + public void addPlayer(Player player) { + // Add player logic } - /** - * Moves to the next player's turn in the game. - * - * @param n The number of turns to proceed. - */ - private void localNextTurn(int n) { - //TODO implement + public void localNextTurn(int n) { + // Local next turn logic } - /** - * Finishes a player's participation in the game. - * - * @param p The player to finish. - */ - private void finishPlayer(Player p) { - //TODO implement + public void finishPlayer(Player p) { + // Finish player logic } - /** - * Returns the current player whose turn it is. - * - * @return The current player. - */ - Player getCurrentPlayer() { - return players.isEmpty() ? null : players.getFirst(); + public Player getCurrentPlayer() { + return players.isEmpty() ? null : players.get(0); + } + + public void setRemember(Player player) { + this.remember = player; + } + + public Player getRemember() { + return remember; + } + + public PlayerState getWaitForNextTurnState() { + return waitForNextTurnState; + } + + public PlayerState getWaitForMauState() { + return waitForMauState; + } + + public PlayerState getWaitForMauMauState() { + return waitForMauMauState; + } + + public PlayerState getFinishedState() { + return finishedState; + } + + public void setCurrentState(PlayerState state) { + this.currentState = state; + } + + public void finishGame() { + // Finish game logic } } diff --git a/src/cards/maumau/model/PlayerState.java b/src/cards/maumau/model/PlayerState.java new file mode 100644 index 0000000..713e99f --- /dev/null +++ b/src/cards/maumau/model/PlayerState.java @@ -0,0 +1,7 @@ +package cards.maumau.model; + +public interface PlayerState { + void nextTurn(int n); + void mau(Player p); + void maumau(Player p); +} diff --git a/src/cards/maumau/model/WaitForMauMauState.java b/src/cards/maumau/model/WaitForMauMauState.java new file mode 100644 index 0000000..52c48c9 --- /dev/null +++ b/src/cards/maumau/model/WaitForMauMauState.java @@ -0,0 +1,35 @@ +package cards.maumau.model; + +public class WaitForMauMauState implements PlayerState { + private final PlayerHandler handler; + + public WaitForMauMauState(PlayerHandler handler) { + this.handler = handler; + } + + @Override + public void nextTurn(int n) { + // Draw a card and proceed to next turn + handler.getCurrentPlayer().drawCards(1); + handler.localNextTurn(n); + handler.setCurrentState(handler.getWaitForNextTurnState()); + } + + @Override + public void mau(Player p) { + // Do nothing or handle invalid state transition + } + + @Override + public void maumau(Player p) { + if (p == handler.getRemember()) { + handler.finishPlayer(p); + if (handler.getPlayers().size() == 1) { + handler.finishGame(); + handler.setCurrentState(handler.getFinishedState()); + } else { + handler.setCurrentState(handler.getWaitForNextTurnState()); + } + } + } +} diff --git a/src/cards/maumau/model/WaitForMauState.java b/src/cards/maumau/model/WaitForMauState.java new file mode 100644 index 0000000..774645c --- /dev/null +++ b/src/cards/maumau/model/WaitForMauState.java @@ -0,0 +1,29 @@ +package cards.maumau.model; + +public class WaitForMauState implements PlayerState { + private final PlayerHandler handler; + + public WaitForMauState(PlayerHandler handler) { + this.handler = handler; + } + + @Override + public void nextTurn(int n) { + // Draw a card and proceed to next turn + handler.getCurrentPlayer().drawCards(1); + handler.localNextTurn(n); + handler.setCurrentState(handler.getWaitForNextTurnState()); + } + + @Override + public void mau(Player p) { + if (p == handler.getRemember()) { + handler.setCurrentState(handler.getWaitForMauMauState()); + } + } + + @Override + public void maumau(Player p) { + // Do nothing or handle invalid state transition + } +} diff --git a/src/cards/maumau/model/WaitForNextTurnState.java b/src/cards/maumau/model/WaitForNextTurnState.java new file mode 100644 index 0000000..ee44a6d --- /dev/null +++ b/src/cards/maumau/model/WaitForNextTurnState.java @@ -0,0 +1,32 @@ +package cards.maumau.model; + +public class WaitForNextTurnState implements PlayerState { + private final PlayerHandler handler; + + public WaitForNextTurnState(PlayerHandler handler) { + this.handler = handler; + } + + @Override + public void nextTurn(int n) { + // Logic for next turn + if (handler.getCurrentPlayer().getCards().isEmpty()) { + handler.setRemember(handler.getCurrentPlayer()); + handler.localNextTurn(n); + handler.setCurrentState(handler.getWaitForMauMauState()); + } else { + handler.localNextTurn(n); + handler.setCurrentState(handler.getWaitForMauState()); + } + } + + @Override + public void mau(Player p) { + // Do nothing or handle invalid state transition + } + + @Override + public void maumau(Player p) { + // Do nothing or handle invalid state transition + } +}