Compare commits

...

2 Commits

Author SHA1 Message Date
87bc479f12 started on implementing functionality of GamesStates 2024-06-09 15:13:53 +02:00
1178730173 refactored the GameStates 2024-06-09 14:52:57 +02:00
38 changed files with 178 additions and 56 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -2,7 +2,6 @@ package cards.maumau.model;
import cards.Card;
import cards.Suit;
import cards.maumau.model.gamestate.*;
/**
* Manages the actions and state transitions within a MauMau game.
@ -21,7 +20,7 @@ class ActionHandler {
*/
ActionHandler(MauMau game) {
this.game = game;
gameState = new Initialized();
gameState = new Initialized(this);
}
/**
@ -30,28 +29,28 @@ class ActionHandler {
* @param player The player to be added to the game.
*/
void addPlayer(Player player) {
//TODO implement
gameState.addPlayer(player);
}
/**
* Starts the game.
*/
void startGame() {
//TODO implement
gameState.startGame();
}
/**
* Transitions the game state to GAME_OVER.
*/
void finishGame() {
//TODO implement
gameState.finishGame();
}
/**
* Transitions the game state to GAME_CANCELED.
*/
void cancelGame() {
//TODO implement
gameState.cancelGame();
}
/**
@ -60,7 +59,7 @@ class ActionHandler {
* @param c The card chosen by the player.
*/
void chooseCard(Card c) {
//TODO implement
gameState.chooseCard(c);
}
/**
@ -69,21 +68,21 @@ class ActionHandler {
* @param suit The suit chosen by the player.
*/
void chooseSuit(Suit suit) {
//TODO implement
gameState.chooseSuit(suit);
}
/**
* Lets the player skip a round.
**/
void skip() {
//TODO implement
gameState.skip();
}
/**
* Handles the player saying "no 7" in the current state.
*/
void no7() {
//TODO implement
gameState.no7();
}
/**
@ -140,8 +139,11 @@ class ActionHandler {
* Returns the current state of the game.
*/
GameState getGameState() {
//TODO implement
return null;
return gameState;
}
void setGameState(GameState gameState) {
this.gameState = gameState;
}
/**

View File

@ -0,0 +1,10 @@
package cards.maumau.model;
public class Canceled implements GameState{
private final ActionHandler handler;
Canceled(ActionHandler handler) {
this.handler = handler;
}
}

View File

@ -0,0 +1,9 @@
package cards.maumau.model;
public class Finished implements GameState{
private final ActionHandler handler;
Finished(ActionHandler handler) {
this.handler = handler;
}
}

View File

@ -0,0 +1,17 @@
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);
}

View File

@ -0,0 +1,34 @@
package cards.maumau.model;
import cards.Card;
import cards.Suit;
public class Initialized implements GameState {
private final ActionHandler handler;
Initialized(ActionHandler handler) {
this.handler = handler;
}
public void addPlayer(Player player) {
handler.getGame().getPlayerHandler().addPlayer(player);
}
public void startGame() {
try {
handler.getGame().getCardHandler().dealCards();
handler.setGameState(new Normal(handler));
} catch (Exception e) {
handler.setGameState(new Canceled(handler));
}
}
public void finishGame(){}
public void cancelGame(){}
public void chooseCard(Card c){}
public void chooseSuit(Suit suit){}
public void skip(){}
public void no7(){}
}

View File

@ -0,0 +1,27 @@
package cards.maumau.model;
import cards.Card;
import cards.Suit;
public class JackChosen implements GameState{
private final ActionHandler handler;
JackChosen(ActionHandler handler) {
this.handler = handler;
}
public void chooseSuit(Suit suit){
handler.setChosenSuit(suit);
handler.setGameState(new SuitChosen(handler));
}
public void startGame(){}
public void addPlayer(Player player){}
public void finishGame(){}
public void cancelGame(){}
public void chooseCard(Card c){}
public void skip(){}
public void no7(){}
}

View File

@ -2,7 +2,6 @@ package cards.maumau.model;
import cards.Card;
import cards.Suit;
import cards.maumau.model.gamestate.*;
import java.util.ArrayList;
import java.util.List;
@ -61,7 +60,7 @@ public class MauMau {
*
* @return The player handler.
*/
PlayerHandler getPlayerHandler() {
public PlayerHandler getPlayerHandler() {
return playerHandler;
}
@ -70,7 +69,7 @@ public class MauMau {
*
* @return The card handler.
*/
CardHandler getCardHandler() {
public CardHandler getCardHandler() {
return cardHandler;
}

View File

@ -0,0 +1,9 @@
package cards.maumau.model;
public class Normal implements GameState{
private final ActionHandler handler;
Normal(ActionHandler handler) {
this.handler = handler;
}
}

View File

@ -0,0 +1,9 @@
package cards.maumau.model;
public class SevenChosen implements GameState{
private final ActionHandler handler;
SevenChosen(ActionHandler handler) {
this.handler = handler;
}
}

View File

@ -0,0 +1,47 @@
package cards.maumau.model;
import cards.Card;
import cards.Suit;
import cards.Rank;
public class SuitChosen implements GameState{
private final ActionHandler handler;
SuitChosen(ActionHandler handler) {
this.handler = handler;
}
public void addPlayer(Player player){}
public void startGame(){}
public void finishGame(){}
public void cancelGame(){}
public void chooseCard(Card c){
if (handler.canPlay(c)) {
handler.setChosenSuit(null);
handler.getGame().getPlayerHandler().getCurrentPlayer().playCard(c);
if(c.rank() == Rank.SEVEN) {
handler.increment7Counter();
handler.getGame().getPlayerHandler().nextTurn(1);
handler.setGameState(new SevenChosen(handler));
} else if (c.rank() == Rank.EIGHT) {
handler.getGame().getPlayerHandler().nextTurn(2);
handler.setGameState(new Normal(handler));
} else {
handler.getGame().getPlayerHandler().nextTurn(1);
handler.setGameState(new Normal(handler));
}
}
}
public void chooseSuit(Suit suit){}
public void skip(){
handler.getGame().getPlayerHandler().getCurrentPlayer().drawCards(1);
handler.getGame().getPlayerHandler().nextTurn(1);
}
public void no7(){}
}

View File

@ -1,5 +0,0 @@
package cards.maumau.model.gamestate;
public class Canceled implements GameState{
}

View File

@ -1,5 +0,0 @@
package cards.maumau.model.gamestate;
public class Finished implements GameState{
}

View File

@ -1,5 +0,0 @@
package cards.maumau.model.gamestate;
public interface GameState {
}

View File

@ -1,6 +0,0 @@
package cards.maumau.model.gamestate;
public class Initialized implements GameState {
}

View File

@ -1,5 +0,0 @@
package cards.maumau.model.gamestate;
public class JackChosen implements GameState{
}

View File

@ -1,5 +0,0 @@
package cards.maumau.model.gamestate;
public class Normal implements GameState{
}

View File

@ -1,5 +0,0 @@
package cards.maumau.model.gamestate;
public class SevenChosen implements GameState{
}

View File

@ -1,5 +0,0 @@
package cards.maumau.model.gamestate;
public class SuitChosen implements GameState{
}