diff --git a/bin/cards/maumau/model/ActionHandler.class b/bin/cards/maumau/model/ActionHandler.class index 836885c..32c8091 100644 Binary files a/bin/cards/maumau/model/ActionHandler.class and b/bin/cards/maumau/model/ActionHandler.class differ diff --git a/bin/cards/maumau/model/Initialized.class b/bin/cards/maumau/model/Initialized.class index b6430f9..26d0b8f 100644 Binary files a/bin/cards/maumau/model/Initialized.class and b/bin/cards/maumau/model/Initialized.class differ diff --git a/bin/cards/maumau/model/JackChosen.class b/bin/cards/maumau/model/JackChosen.class index 1dfe43f..ecc281c 100644 Binary files a/bin/cards/maumau/model/JackChosen.class and b/bin/cards/maumau/model/JackChosen.class differ diff --git a/bin/cards/maumau/model/MauMau.class b/bin/cards/maumau/model/MauMau.class index 937b9c2..9b551a5 100644 Binary files a/bin/cards/maumau/model/MauMau.class and b/bin/cards/maumau/model/MauMau.class differ diff --git a/bin/cards/maumau/model/Normal.class b/bin/cards/maumau/model/Normal.class index b028978..6ff27b7 100644 Binary files a/bin/cards/maumau/model/Normal.class and b/bin/cards/maumau/model/Normal.class differ diff --git a/bin/cards/maumau/model/PlayerHandler.class b/bin/cards/maumau/model/PlayerHandler.class index 580b21d..ae217e0 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/SevenChosen.class b/bin/cards/maumau/model/SevenChosen.class index 450ca4e..c7f3ded 100644 Binary files a/bin/cards/maumau/model/SevenChosen.class and b/bin/cards/maumau/model/SevenChosen.class differ diff --git a/bin/cards/maumau/model/WaitForMauMauState.class b/bin/cards/maumau/model/WaitForMauMauState.class index 44237d6..859a950 100644 Binary files a/bin/cards/maumau/model/WaitForMauMauState.class 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 index e67812c..e676356 100644 Binary files a/bin/cards/maumau/model/WaitForMauState.class and b/bin/cards/maumau/model/WaitForMauState.class differ diff --git a/bin/test/cards/maumau/model/MauMau1Test.class b/bin/test/cards/maumau/model/MauMau1Test.class index 9e78f1b..8456cc3 100644 Binary files a/bin/test/cards/maumau/model/MauMau1Test.class and b/bin/test/cards/maumau/model/MauMau1Test.class differ diff --git a/bin/test/cards/maumau/model/MauMau2Test.class b/bin/test/cards/maumau/model/MauMau2Test.class index 6900e05..a0b8112 100644 Binary files a/bin/test/cards/maumau/model/MauMau2Test.class and b/bin/test/cards/maumau/model/MauMau2Test.class differ diff --git a/bin/test/cards/maumau/model/MauMau4Test.class b/bin/test/cards/maumau/model/MauMau4Test.class index 7e2a885..7b9976e 100644 Binary files a/bin/test/cards/maumau/model/MauMau4Test.class and b/bin/test/cards/maumau/model/MauMau4Test.class differ diff --git a/src/cards/maumau/model/ActionHandler.java b/src/cards/maumau/model/ActionHandler.java index b71e657..49574f0 100644 --- a/src/cards/maumau/model/ActionHandler.java +++ b/src/cards/maumau/model/ActionHandler.java @@ -2,12 +2,13 @@ package cards.maumau.model; import cards.Card; +import cards.Rank; import cards.Suit; /** * Manages the actions and state transitions within a MauMau game. */ -class ActionHandler { +public class ActionHandler { private final MauMau game; private Suit chosenSuit; private int ctr7 = 0; @@ -32,6 +33,10 @@ class ActionHandler { this.handlerState = handlerState; } + public HandlerState getHandlerState() { + return handlerState; + } + /** * Adds the specified player to the game. * @@ -45,21 +50,23 @@ class ActionHandler { * Starts the game. */ void startGame() { - //TODO implement + handlerState.startGame(); } /** * Transitions the game state to GAME_OVER. */ void finishGame() { - //TODO implement + // handlerState.finishGame(); + gameState = GameState.GAME_OVER; + handlerState = new Finished(this); } /** * Transitions the game state to GAME_CANCELED. */ void cancelGame() { - //TODO implement + handlerState.cancelGame(); } /** @@ -68,7 +75,7 @@ class ActionHandler { * @param c The card chosen by the player. */ void chooseCard(Card c) { - //TODO implement + handlerState.chooseCard(c); } /** @@ -77,21 +84,21 @@ class ActionHandler { * @param suit The suit chosen by the player. */ void chooseSuit(Suit suit) { - //TODO implement + handlerState.chooseSuit(suit); } /** * Lets the player skip a round. **/ void skip() { - //TODO implement + handlerState.skip(); } /** * Handles the player saying "no 7" in the current state. */ void no7() { - //TODO implement + handlerState.no7(); } /** @@ -157,8 +164,23 @@ class ActionHandler { * @param c The card being played. * @return True if the card can be played, false otherwise. */ - boolean canPlay(Card c) { - //TODO implement - return false; + public boolean canPlay(Card c) { + if (chosenSuit == null) { + if (c.rank() == Rank.JACK) { + return true; + } else if (c.rank() == game.getCardHandler().getDiscardPile().getFirst().rank()) { + return true; + } else if (c.suit() == game.getCardHandler().getDiscardPile().getFirst().suit()) { + return true; + } + } + + if (c.suit() == chosenSuit) { + return true; + } + + else { + return false; + } } } diff --git a/src/cards/maumau/model/Initialized.java b/src/cards/maumau/model/Initialized.java index 88f829a..296a733 100644 --- a/src/cards/maumau/model/Initialized.java +++ b/src/cards/maumau/model/Initialized.java @@ -19,12 +19,12 @@ public class Initialized implements HandlerState { public void startGame() { try { handler.getGame().getCardHandler().dealCards(); - handler.setHandlerState(new Normal(handler)); handler.setGameState(GameState.PLAY); + handler.setHandlerState(new Normal(handler)); } catch (Exception e) { System.err.println("Not enough Cards!"); - handler.setHandlerState(new Canceled(handler)); handler.setGameState(GameState.GAME_CANCELED); + handler.setHandlerState(new Canceled(handler)); } } diff --git a/src/cards/maumau/model/JackChosen.java b/src/cards/maumau/model/JackChosen.java index 5a9bb82..c9a7743 100644 --- a/src/cards/maumau/model/JackChosen.java +++ b/src/cards/maumau/model/JackChosen.java @@ -14,8 +14,9 @@ public class JackChosen implements HandlerState{ public void chooseSuit(Suit suit){ handler.setChosenSuit(suit); + handler.getGame().getPlayerHandler().nextTurn(1); + handler.setGameState(GameState.PLAY); handler.setHandlerState(new SuitChosen(handler)); - //handler.setGameState(PLAY); } public void startGame(){} diff --git a/src/cards/maumau/model/MauMau.java b/src/cards/maumau/model/MauMau.java index 1b5bfd0..7e4fafa 100644 --- a/src/cards/maumau/model/MauMau.java +++ b/src/cards/maumau/model/MauMau.java @@ -78,7 +78,7 @@ public class MauMau { * * @return The action handler. */ - ActionHandler getActionHandler() { + public ActionHandler getActionHandler() { return actionHandler; } diff --git a/src/cards/maumau/model/Normal.java b/src/cards/maumau/model/Normal.java index 206b498..9b3f898 100644 --- a/src/cards/maumau/model/Normal.java +++ b/src/cards/maumau/model/Normal.java @@ -13,7 +13,10 @@ public class Normal implements HandlerState { public void addPlayer(Player player){} public void startGame(){} - public void finishGame(){} + public void finishGame(){ + handler.setGameState(GameState.GAME_OVER); + handler.setHandlerState(new Finished(handler)); + } public void cancelGame(){} public void chooseCard(Card c){ @@ -24,6 +27,7 @@ public class Normal implements HandlerState { handler.getGame().getPlayerHandler().nextTurn(1); handler.setHandlerState(new SevenChosen(handler)); } else if (c.rank() == Rank.JACK) { + handler.setGameState(GameState.CHOOSE_SUIT); handler.setHandlerState(new JackChosen(handler)); } else if (c.rank() == Rank.EIGHT) { handler.getGame().getPlayerHandler().nextTurn(2); diff --git a/src/cards/maumau/model/PlayerHandler.java b/src/cards/maumau/model/PlayerHandler.java index 85d9819..1f4a44a 100644 --- a/src/cards/maumau/model/PlayerHandler.java +++ b/src/cards/maumau/model/PlayerHandler.java @@ -56,12 +56,13 @@ public class PlayerHandler { public void localNextTurn(int n) { for(int i = 0; i < n; i++){ - players.addLast(players.getFirst()); + players.addLast(players.removeFirst()); + // players.removeFirst(); } } public void finishPlayer(Player p) { - ranking.addLast(players.removeFirst()); + ranking.addLast(players.removeLast()); } public Player getCurrentPlayer() { @@ -97,6 +98,6 @@ public class PlayerHandler { } public void finishGame() { - finishPlayer(players.getFirst()); + game.getActionHandler().finishGame(); } } diff --git a/src/cards/maumau/model/SevenChosen.java b/src/cards/maumau/model/SevenChosen.java index 2ede84b..b999aa1 100644 --- a/src/cards/maumau/model/SevenChosen.java +++ b/src/cards/maumau/model/SevenChosen.java @@ -20,16 +20,21 @@ public class SevenChosen implements HandlerState{ handler.getGame().getPlayerHandler().getCurrentPlayer().playCard(c); handler.increment7Counter(); handler.getGame().getPlayerHandler().nextTurn(1); - } else { - } } public void chooseSuit(Suit suit){} public void skip(){} + public void no7(){ - handler.getGame().getPlayerHandler().getCurrentPlayer().drawCards(2 * handler.get7Counter()); - handler.reset7Counter(); - handler.setHandlerState(new Normal(handler)); + if (handler.get7Counter() * 2 > handler.getGame().getCardHandler().getDrawPile().size()) { + handler.setGameState(GameState.GAME_CANCELED); + handler.setHandlerState(new Canceled(handler)); + } else { + handler.getGame().getPlayerHandler().getCurrentPlayer().drawCards(2 * handler.get7Counter()); + handler.reset7Counter(); + handler.setHandlerState(new Normal(handler)); + } + } } diff --git a/src/cards/maumau/model/WaitForMauMauState.java b/src/cards/maumau/model/WaitForMauMauState.java index 52c48c9..95acf68 100644 --- a/src/cards/maumau/model/WaitForMauMauState.java +++ b/src/cards/maumau/model/WaitForMauMauState.java @@ -10,9 +10,19 @@ public class WaitForMauMauState implements PlayerState { @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()); + handler.getRemember().drawCards(1); + if (handler.getCurrentPlayer().getCards().isEmpty()) { + handler.setRemember(handler.getCurrentPlayer()); + handler.localNextTurn(n); + handler.setCurrentState(handler.getWaitForMauMauState()); + } else if (handler.getCurrentPlayer().getCards().size() == 1) { + handler.setRemember(handler.getCurrentPlayer()); + handler.localNextTurn(n); + handler.setCurrentState(handler.getWaitForMauState()); + } else { + handler.localNextTurn(n); + handler.setCurrentState(handler.getWaitForNextTurnState()); + } } @Override @@ -23,13 +33,20 @@ public class WaitForMauMauState implements PlayerState { @Override public void maumau(Player p) { if (p == handler.getRemember()) { + + handler.finishPlayer(p); + + if (handler.getPlayers().size() == 1) { + handler.finishPlayer(handler.getCurrentPlayer()); 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 index 774645c..24c3386 100644 --- a/src/cards/maumau/model/WaitForMauState.java +++ b/src/cards/maumau/model/WaitForMauState.java @@ -10,15 +10,25 @@ public class WaitForMauState implements PlayerState { @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()); + handler.getRemember().drawCards(1); + if (handler.getCurrentPlayer().getCards().isEmpty()) { + handler.setRemember(handler.getCurrentPlayer()); + handler.localNextTurn(n); + handler.setCurrentState(handler.getWaitForMauMauState()); + } else if (handler.getCurrentPlayer().getCards().size() == 1) { + handler.setRemember(handler.getCurrentPlayer()); + handler.localNextTurn(n); + handler.setCurrentState(handler.getWaitForMauState()); + } else { + handler.localNextTurn(n); + handler.setCurrentState(handler.getWaitForNextTurnState()); + } } @Override public void mau(Player p) { if (p == handler.getRemember()) { - handler.setCurrentState(handler.getWaitForMauMauState()); + handler.setCurrentState(handler.getWaitForNextTurnState()); } } diff --git a/src/test/cards/maumau/model/MauMau1Test.java b/src/test/cards/maumau/model/MauMau1Test.java index 10ae63e..13ba1a3 100644 --- a/src/test/cards/maumau/model/MauMau1Test.java +++ b/src/test/cards/maumau/model/MauMau1Test.java @@ -4,7 +4,9 @@ import cards.Card; import cards.Rank; import cards.Suit; import cards.maumau.model.MauMau; +import cards.maumau.model.Normal; import cards.maumau.model.Player; +import cards.maumau.model.SevenChosen; import org.junit.Test; @@ -150,8 +152,10 @@ public class MauMau1Test { assertNull(game.getChosenSuit()); assertEquals(0, game.get7Counter()); + assertEquals(Normal.class, game.getActionHandler().getHandlerState().getClass()); chantal.skip(); assertEquals(PLAY, game.getGameState()); + assertEquals(Normal.class, game.getActionHandler().getHandlerState().getClass()); assertEquals(List.of(jacqueline, chantal), game.getPlayers()); assertEquals(List.of(), game.getRanking()); assertEquals("[A♦︎]", jacqueline.getCards().toString()); diff --git a/src/test/cards/maumau/model/MauMau2Test.java b/src/test/cards/maumau/model/MauMau2Test.java index f62fe2a..a2e28b4 100644 --- a/src/test/cards/maumau/model/MauMau2Test.java +++ b/src/test/cards/maumau/model/MauMau2Test.java @@ -4,6 +4,7 @@ import cards.Card; import cards.Rank; import cards.Suit; import cards.maumau.model.MauMau; +import cards.maumau.model.Normal; import cards.maumau.model.Player; import org.junit.Test; @@ -26,6 +27,7 @@ import static cards.maumau.model.GameState.GAME_OVER; import static cards.maumau.model.GameState.PLAY; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; public class MauMau2Test { private static Card c(Rank r, Suit s) { @@ -186,17 +188,22 @@ public class MauMau2Test { assertEquals(List.of(jacqueline, chantal), game.getPlayers()); assertEquals(List.of(), game.getRanking()); assertEquals("[Q♠︎, Q♣︎]", jacqueline.getCards().toString()); + // assertEquals("[Q♣︎]", jacqueline.getCards().toString()); assertEquals("[8♥︎]", chantal.getCards().toString()); assertEquals("[9♥︎, K♣︎, 7♣︎, J♦︎, 9♦︎, A♣︎, 8♠︎, 10♦︎, J♠︎, J♥︎, J♣︎, 10♥︎, K♥︎, Q♥︎, 7♥︎, 7♦︎, 8♣︎, 9♣︎, 9♠︎]", game.getDrawPile().toString()); assertEquals("[Q♦︎, K♦︎, 8♦︎, A♦︎, A♥︎, A♠︎, K♠︎, 7♠︎, 10♠︎, 10♣︎]", game.getDiscardPile().toString()); assertNull(game.getChosenSuit()); assertEquals(0, game.get7Counter()); + // assertEquals("[Q♣︎]", jacqueline.getCards().toString()); + // assertEquals(Normal.class, game.getActionHandler().getHandlerState().getClass()); + assertTrue(game.getActionHandler().canPlay(c(QUEEN, SPADES))); jacqueline.chooseCard(c(QUEEN, SPADES)); assertEquals(PLAY, game.getGameState()); assertEquals(List.of(chantal, jacqueline), game.getPlayers()); assertEquals(List.of(), game.getRanking()); assertEquals("[8♥︎]", chantal.getCards().toString()); + // assertEquals(Normal.class, game.getActionHandler().getHandlerState().getClass()); assertEquals("[Q♣︎]", jacqueline.getCards().toString()); assertEquals("[9♥︎, K♣︎, 7♣︎, J♦︎, 9♦︎, A♣︎, 8♠︎, 10♦︎, J♠︎, J♥︎, J♣︎, 10♥︎, K♥︎, Q♥︎, 7♥︎, 7♦︎, 8♣︎, 9♣︎, 9♠︎]", game.getDrawPile().toString()); assertEquals("[Q♠︎, Q♦︎, K♦︎, 8♦︎, A♦︎, A♥︎, A♠︎, K♠︎, 7♠︎, 10♠︎, 10♣︎]", game.getDiscardPile().toString()); diff --git a/src/test/cards/maumau/model/MauMau4Test.java b/src/test/cards/maumau/model/MauMau4Test.java index 54a10af..dcba3f6 100644 --- a/src/test/cards/maumau/model/MauMau4Test.java +++ b/src/test/cards/maumau/model/MauMau4Test.java @@ -26,7 +26,9 @@ import static cards.maumau.model.GameState.GAME_INITIALIZED; import static cards.maumau.model.GameState.GAME_OVER; import static cards.maumau.model.GameState.PLAY; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; public class MauMau4Test { private static Card c(Rank r, Suit s) { @@ -128,6 +130,7 @@ public class MauMau4Test { assertEquals(0, game.get7Counter()); chantal.chooseCard(c(JACK, SPADES)); + assertFalse(game.getActionHandler().canPlay(c(JACK, SPADES))); assertEquals(PLAY, game.getGameState()); assertEquals(List.of(chantal, jacqueline), game.getPlayers()); assertEquals(List.of(), game.getRanking());