This commit is contained in:
Johannes Schmelz 2024-06-13 16:40:15 +00:00
parent 0f91dc2a13
commit ad3ec29962
25 changed files with 82 additions and 45 deletions

Binary file not shown.

Binary file not shown.

View File

@ -12,6 +12,7 @@ class ActionHandler {
private int ctr7 = 0; private int ctr7 = 0;
private GameState gameState; private GameState gameState;
private HandlerState handlerState;
/** /**
* Constructs an ActionHandler for the specified MauMau game. * Constructs an ActionHandler for the specified MauMau game.
@ -20,7 +21,14 @@ class ActionHandler {
*/ */
ActionHandler(MauMau game) { ActionHandler(MauMau game) {
this.game = game; this.game = game;
gameState = new Initialized(this); }
void setGameState(GameState gameState) {
this.gameState = gameState;
}
void setHandlerState (HandlerState handlerState) {
this.handlerState = handlerState;
} }
/** /**
@ -29,28 +37,28 @@ class ActionHandler {
* @param player The player to be added to the game. * @param player The player to be added to the game.
*/ */
void addPlayer(Player player) { void addPlayer(Player player) {
gameState.addPlayer(player); //TODO implement
} }
/** /**
* Starts the game. * Starts the game.
*/ */
void startGame() { void startGame() {
gameState.startGame(); //TODO implement
} }
/** /**
* Transitions the game state to GAME_OVER. * Transitions the game state to GAME_OVER.
*/ */
void finishGame() { void finishGame() {
gameState.finishGame(); //TODO implement
} }
/** /**
* Transitions the game state to GAME_CANCELED. * Transitions the game state to GAME_CANCELED.
*/ */
void cancelGame() { void cancelGame() {
gameState.cancelGame(); //TODO implement
} }
/** /**
@ -59,7 +67,7 @@ class ActionHandler {
* @param c The card chosen by the player. * @param c The card chosen by the player.
*/ */
void chooseCard(Card c) { void chooseCard(Card c) {
gameState.chooseCard(c); //TODO implement
} }
/** /**
@ -68,21 +76,21 @@ class ActionHandler {
* @param suit The suit chosen by the player. * @param suit The suit chosen by the player.
*/ */
void chooseSuit(Suit suit) { void chooseSuit(Suit suit) {
gameState.chooseSuit(suit); //TODO implement
} }
/** /**
* Lets the player skip a round. * Lets the player skip a round.
**/ **/
void skip() { void skip() {
gameState.skip(); //TODO implement
} }
/** /**
* Handles the player saying "no 7" in the current state. * Handles the player saying "no 7" in the current state.
*/ */
void no7() { void no7() {
gameState.no7(); //TODO implement
} }
/** /**
@ -142,10 +150,6 @@ class ActionHandler {
return gameState; return gameState;
} }
void setGameState(GameState gameState) {
this.gameState = gameState;
}
/** /**
* Checks if a card can be played by the current player in the current state. * Checks if a card can be played by the current player in the current state.
* *

View File

@ -3,7 +3,7 @@ package cards.maumau.model;
import cards.Card; import cards.Card;
import cards.Suit; import cards.Suit;
public class Canceled implements GameState{ public class Canceled implements HandlerState {
private final ActionHandler handler; private final ActionHandler handler;

View File

@ -9,7 +9,7 @@ import java.util.List;
/** /**
* Manages the draw pile and discard pile in a MauMau game. * Manages the draw pile and discard pile in a MauMau game.
*/ */
class CardHandler { public class CardHandler {
private final MauMau game; private final MauMau game;
private final int numCardsPerPlayer; private final int numCardsPerPlayer;
private final List<Card> drawPile = new LinkedList<>(); private final List<Card> drawPile = new LinkedList<>();

View File

@ -3,7 +3,7 @@ package cards.maumau.model;
import cards.Card; import cards.Card;
import cards.Suit; import cards.Suit;
public class Finished implements GameState{ public class Finished implements HandlerState{
private final ActionHandler handler; private final ActionHandler handler;
Finished(ActionHandler handler) { Finished(ActionHandler handler) {

View File

@ -1,17 +1,29 @@
package cards.maumau.model; package cards.maumau.model;
import cards.Card; /**
import cards.Suit; * Represents the state of the Mau-Mau game.
*/
public enum GameState {
interface GameState { /**
void addPlayer(Player player); * The game has been initialized, but has not yet started.
void startGame(); */
void finishGame(); GAME_INITIALIZED,
void cancelGame(); /**
void chooseCard(Card c); * The game is over. The final ranking of players can be
void chooseSuit(Suit suit); * obtained using {@link MauMau#getRanking()}.
void skip(); */
void no7(); GAME_OVER,
// boolean canPlay(Card c); /**
* The game has been canceled due to insufficient cards.
*/
GAME_CANCELED,
/**
* The game is currently in progress with players taking turns.
*/
PLAY,
/**
* The game is in progress and the current player has played
* a Jack, and is required to choose a suit.
*/
CHOOSE_SUIT
} }

View File

@ -0,0 +1,17 @@
package cards.maumau.model;
import cards.Card;
import cards.Suit;
public interface HandlerState {
void addPlayer(Player player);
void startGame();
void finishGame();
void cancelGame();
void chooseCard(Card c);
void chooseSuit(Suit suit);
void skip();
void no7();
}

View File

@ -3,7 +3,7 @@ package cards.maumau.model;
import cards.Card; import cards.Card;
import cards.Suit; import cards.Suit;
public class Initialized implements GameState { public class Initialized implements HandlerState {
private final ActionHandler handler; private final ActionHandler handler;
@ -19,9 +19,12 @@ public class Initialized implements GameState {
public void startGame() { public void startGame() {
try { try {
handler.getGame().getCardHandler().dealCards(); handler.getGame().getCardHandler().dealCards();
handler.setGameState(new Normal(handler)); handler.setHandlerState(new Normal(handler));
handler.setGameState(GameState.PLAY);
} catch (Exception e) { } catch (Exception e) {
handler.setGameState(new Canceled(handler)); System.err.println("Not enough Cards!");
handler.setHandlerState(new Canceled(handler));
handler.setGameState(GameState.GAME_CANCELED);
} }
} }

View File

@ -3,7 +3,7 @@ package cards.maumau.model;
import cards.Card; import cards.Card;
import cards.Suit; import cards.Suit;
public class JackChosen implements GameState{ public class JackChosen implements HandlerState{
private final ActionHandler handler; private final ActionHandler handler;
JackChosen(ActionHandler handler) { JackChosen(ActionHandler handler) {
@ -14,7 +14,8 @@ public class JackChosen implements GameState{
public void chooseSuit(Suit suit){ public void chooseSuit(Suit suit){
handler.setChosenSuit(suit); handler.setChosenSuit(suit);
handler.setGameState(new SuitChosen(handler)); handler.setHandlerState(new SuitChosen(handler));
//handler.setGameState(PLAY);
} }
public void startGame(){} public void startGame(){}

View File

@ -3,7 +3,7 @@ package cards.maumau.model;
import cards.Card; import cards.Card;
import cards.Suit; import cards.Suit;
public class Normal implements GameState{ public class Normal implements HandlerState {
private final ActionHandler handler; private final ActionHandler handler;
Normal(ActionHandler handler) { Normal(ActionHandler handler) {
@ -15,7 +15,7 @@ public class Normal implements GameState{
public void finishGame(){ public void finishGame(){
// Logic for finishing Game ???? // Logic for finishing Game ????
if (handler.getGame().getPlayerHandler().getPlayers().isEmpty()) { if (handler.getGame().getPlayerHandler().getPlayers().isEmpty()) {
handler.setGameState(new Finished(handler)); handler.setHandlerState(new Finished(handler));
} }
} }
public void cancelGame(){} public void cancelGame(){}

View File

@ -3,7 +3,7 @@ package cards.maumau.model;
import cards.Card; import cards.Card;
import cards.Suit; import cards.Suit;
public class SevenChosen implements GameState{ public class SevenChosen implements HandlerState{
private final ActionHandler handler; private final ActionHandler handler;
SevenChosen(ActionHandler handler) { SevenChosen(ActionHandler handler) {
@ -30,6 +30,6 @@ public class SevenChosen implements GameState{
public void no7(){ public void no7(){
handler.getGame().getPlayerHandler().getCurrentPlayer().drawCards(2 * handler.get7Counter()); handler.getGame().getPlayerHandler().getCurrentPlayer().drawCards(2 * handler.get7Counter());
handler.reset7Counter(); handler.reset7Counter();
handler.setGameState(new Normal(handler)); handler.setHandlerState(new Normal(handler));
} }
} }

View File

@ -4,7 +4,7 @@ import cards.Card;
import cards.Suit; import cards.Suit;
import cards.Rank; import cards.Rank;
public class SuitChosen implements GameState{ public class SuitChosen implements HandlerState{
private final ActionHandler handler; private final ActionHandler handler;
SuitChosen(ActionHandler handler) { SuitChosen(ActionHandler handler) {
@ -24,13 +24,13 @@ public class SuitChosen implements GameState{
if(c.rank() == Rank.SEVEN) { if(c.rank() == Rank.SEVEN) {
handler.increment7Counter(); handler.increment7Counter();
handler.getGame().getPlayerHandler().nextTurn(1); handler.getGame().getPlayerHandler().nextTurn(1);
handler.setGameState(new SevenChosen(handler)); handler.setHandlerState(new SevenChosen(handler));
} else if (c.rank() == Rank.EIGHT) { } else if (c.rank() == Rank.EIGHT) {
handler.getGame().getPlayerHandler().nextTurn(2); handler.getGame().getPlayerHandler().nextTurn(2);
handler.setGameState(new Normal(handler)); handler.setHandlerState(new Normal(handler));
} else { } else {
handler.getGame().getPlayerHandler().nextTurn(1); handler.getGame().getPlayerHandler().nextTurn(1);
handler.setGameState(new Normal(handler)); handler.setHandlerState(new Normal(handler));
} }
} }