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 GameState gameState;
private HandlerState handlerState;
/**
* Constructs an ActionHandler for the specified MauMau game.
@ -20,7 +21,14 @@ class ActionHandler {
*/
ActionHandler(MauMau 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.
*/
void addPlayer(Player player) {
gameState.addPlayer(player);
//TODO implement
}
/**
* Starts the game.
*/
void startGame() {
gameState.startGame();
//TODO implement
}
/**
* Transitions the game state to GAME_OVER.
*/
void finishGame() {
gameState.finishGame();
//TODO implement
}
/**
* Transitions the game state to GAME_CANCELED.
*/
void cancelGame() {
gameState.cancelGame();
//TODO implement
}
/**
@ -59,7 +67,7 @@ class ActionHandler {
* @param c The card chosen by the player.
*/
void chooseCard(Card c) {
gameState.chooseCard(c);
//TODO implement
}
/**
@ -68,21 +76,21 @@ class ActionHandler {
* @param suit The suit chosen by the player.
*/
void chooseSuit(Suit suit) {
gameState.chooseSuit(suit);
//TODO implement
}
/**
* Lets the player skip a round.
**/
void skip() {
gameState.skip();
//TODO implement
}
/**
* Handles the player saying "no 7" in the current state.
*/
void no7() {
gameState.no7();
//TODO implement
}
/**
@ -142,10 +150,6 @@ class ActionHandler {
return gameState;
}
void setGameState(GameState gameState) {
this.gameState = gameState;
}
/**
* 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.Suit;
public class Canceled implements GameState{
public class Canceled implements HandlerState {
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.
*/
class CardHandler {
public class CardHandler {
private final MauMau game;
private final int numCardsPerPlayer;
private final List<Card> drawPile = new LinkedList<>();

View File

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

View File

@ -1,17 +1,29 @@
package cards.maumau.model;
import cards.Card;
import cards.Suit;
interface GameState {
void addPlayer(Player player);
void startGame();
void finishGame();
void cancelGame();
void chooseCard(Card c);
void chooseSuit(Suit suit);
void skip();
void no7();
// boolean canPlay(Card c);
}
/**
* Represents the state of the Mau-Mau game.
*/
public enum GameState {
/**
* The game has been initialized, but has not yet started.
*/
GAME_INITIALIZED,
/**
* The game is over. The final ranking of players can be
* obtained using {@link MauMau#getRanking()}.
*/
GAME_OVER,
/**
* 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.Suit;
public class Initialized implements GameState {
public class Initialized implements HandlerState {
private final ActionHandler handler;
@ -19,9 +19,12 @@ public class Initialized implements GameState {
public void startGame() {
try {
handler.getGame().getCardHandler().dealCards();
handler.setGameState(new Normal(handler));
handler.setHandlerState(new Normal(handler));
handler.setGameState(GameState.PLAY);
} 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.Suit;
public class JackChosen implements GameState{
public class JackChosen implements HandlerState{
private final ActionHandler handler;
JackChosen(ActionHandler handler) {
@ -14,7 +14,8 @@ public class JackChosen implements GameState{
public void chooseSuit(Suit suit){
handler.setChosenSuit(suit);
handler.setGameState(new SuitChosen(handler));
handler.setHandlerState(new SuitChosen(handler));
//handler.setGameState(PLAY);
}
public void startGame(){}

View File

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

View File

@ -3,7 +3,7 @@ package cards.maumau.model;
import cards.Card;
import cards.Suit;
public class SevenChosen implements GameState{
public class SevenChosen implements HandlerState{
private final ActionHandler handler;
SevenChosen(ActionHandler handler) {
@ -30,6 +30,6 @@ public class SevenChosen implements GameState{
public void no7(){
handler.getGame().getPlayerHandler().getCurrentPlayer().drawCards(2 * handler.get7Counter());
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.Rank;
public class SuitChosen implements GameState{
public class SuitChosen implements HandlerState{
private final ActionHandler handler;
SuitChosen(ActionHandler handler) {
@ -24,13 +24,13 @@ public class SuitChosen implements GameState{
if(c.rank() == Rank.SEVEN) {
handler.increment7Counter();
handler.getGame().getPlayerHandler().nextTurn(1);
handler.setGameState(new SevenChosen(handler));
handler.setHandlerState(new SevenChosen(handler));
} else if (c.rank() == Rank.EIGHT) {
handler.getGame().getPlayerHandler().nextTurn(2);
handler.setGameState(new Normal(handler));
handler.setHandlerState(new Normal(handler));
} else {
handler.getGame().getPlayerHandler().nextTurn(1);
handler.setGameState(new Normal(handler));
handler.setHandlerState(new Normal(handler));
}
}

View File

@ -1,6 +1,6 @@
package cards.maumau.model;
public class WaitForNextTurnState implements PlayerState {
public class WaitForNextTurnState implements PlayerState {
private final PlayerHandler handler;
public WaitForNextTurnState(PlayerHandler handler) {