started on implementing functionality of GamesStates

This commit is contained in:
Johannes Schmelz 2024-06-09 15:13:53 +02:00
parent 1178730173
commit 87bc479f12
22 changed files with 124 additions and 24 deletions

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.
@ -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

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

View File

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

View File

@ -4,14 +4,14 @@ import cards.Card;
import cards.Suit;
public interface GameState {
public void addPlayer(Player player);
public void startGame();
public void finishGame();
public void cancelGame();
public void chooseCard(Card c);
public void chooseSuit(Suit suit);
public void skip();
public void no7();
boolean canPlay(Card c);
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

@ -1,5 +1,8 @@
package cards.maumau.model;
import cards.Card;
import cards.Suit;
public class Initialized implements GameState {
private final ActionHandler handler;
@ -13,4 +16,19 @@ public class Initialized implements GameState {
}
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

@ -1,5 +1,27 @@
package cards.maumau.model;
public class JackChosen implements GameState{
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;

View File

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

View File

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

View File

@ -1,5 +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(){}
}