added classes for PlayerSatetes

This commit is contained in:
Johannes Schmelz 2024-05-20 14:28:49 +02:00
parent 4a8b342577
commit f98d860b97
12 changed files with 184 additions and 72 deletions

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

Binary file not shown.

View File

@ -0,0 +1,18 @@
package cards.maumau.model;
public class FinishedState implements PlayerState {
@Override
public void nextTurn(int n) {
// Do nothing, the game is finished
}
@Override
public void mau(Player p) {
// Do nothing, the game is finished
}
@Override
public void maumau(Player p) {
// Do nothing, the game is finished
}
}

View File

@ -4,103 +4,94 @@ import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
/**
* Handles players in a MauMau game.
*/
class PlayerHandler {
public class PlayerHandler {
private final MauMau game;
private final List<Player> players = new LinkedList<>();
private final List<Player> ranking = new ArrayList<>();
private Player remember;
/**
* Constructs a PlayerHandler for the specified MauMau game.
*
* @param game The MauMau game instance.
*/
PlayerHandler(MauMau game) {
private PlayerState waitForNextTurnState;
private PlayerState waitForMauState;
private PlayerState waitForMauMauState;
private PlayerState finishedState;
private PlayerState currentState;
public PlayerHandler(MauMau game) {
this.game = game;
this.waitForNextTurnState = new WaitForNextTurnState(this);
this.waitForMauState = new WaitForMauState(this);
this.waitForMauMauState = new WaitForMauMauState(this);
this.finishedState = new FinishedState();
this.currentState = waitForNextTurnState;
}
/**
* Initiates the next turn in the game.
*
* @param n The number of turns to proceed.
*/
void nextTurn(int n) {
//TODO implement
public void nextTurn(int n) {
currentState.nextTurn(n);
}
/**
* Handles a player calling "Mau".
*
* @param p The player calling "Mau".
*/
void mau(Player p) {
//TODO implement
public void mau(Player p) {
currentState.mau(p);
}
/**
* Handles a player calling "Mau-Mau".
*
* @param p The player calling "Mau-Mau".
*/
void maumau(Player p) {
//TODO implement
public void maumau(Player p) {
currentState.maumau(p);
}
/**
* Returns the list of players participating in the game.
*
* @return The list of players.
*/
List<Player> getPlayers() {
public List<Player> getPlayers() {
return players;
}
/**
* Returns the ranking of players based on the order they finished the game.
*
* @return The ranking of players.
*/
List<Player> getRanking() {
public List<Player> getRanking() {
return ranking;
}
/**
* Adds a player to the game.
*
* @param player The player to add.
* @throws IllegalArgumentException if a player with the same name already exists.
*/
void addPlayer(Player player) {
//TODO implement
public void addPlayer(Player player) {
// Add player logic
}
/**
* Moves to the next player's turn in the game.
*
* @param n The number of turns to proceed.
*/
private void localNextTurn(int n) {
//TODO implement
public void localNextTurn(int n) {
// Local next turn logic
}
/**
* Finishes a player's participation in the game.
*
* @param p The player to finish.
*/
private void finishPlayer(Player p) {
//TODO implement
public void finishPlayer(Player p) {
// Finish player logic
}
/**
* Returns the current player whose turn it is.
*
* @return The current player.
*/
Player getCurrentPlayer() {
return players.isEmpty() ? null : players.getFirst();
public Player getCurrentPlayer() {
return players.isEmpty() ? null : players.get(0);
}
public void setRemember(Player player) {
this.remember = player;
}
public Player getRemember() {
return remember;
}
public PlayerState getWaitForNextTurnState() {
return waitForNextTurnState;
}
public PlayerState getWaitForMauState() {
return waitForMauState;
}
public PlayerState getWaitForMauMauState() {
return waitForMauMauState;
}
public PlayerState getFinishedState() {
return finishedState;
}
public void setCurrentState(PlayerState state) {
this.currentState = state;
}
public void finishGame() {
// Finish game logic
}
}

View File

@ -0,0 +1,7 @@
package cards.maumau.model;
public interface PlayerState {
void nextTurn(int n);
void mau(Player p);
void maumau(Player p);
}

View File

@ -0,0 +1,35 @@
package cards.maumau.model;
public class WaitForMauMauState implements PlayerState {
private final PlayerHandler handler;
public WaitForMauMauState(PlayerHandler handler) {
this.handler = handler;
}
@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());
}
@Override
public void mau(Player p) {
// Do nothing or handle invalid state transition
}
@Override
public void maumau(Player p) {
if (p == handler.getRemember()) {
handler.finishPlayer(p);
if (handler.getPlayers().size() == 1) {
handler.finishGame();
handler.setCurrentState(handler.getFinishedState());
} else {
handler.setCurrentState(handler.getWaitForNextTurnState());
}
}
}
}

View File

@ -0,0 +1,29 @@
package cards.maumau.model;
public class WaitForMauState implements PlayerState {
private final PlayerHandler handler;
public WaitForMauState(PlayerHandler handler) {
this.handler = handler;
}
@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());
}
@Override
public void mau(Player p) {
if (p == handler.getRemember()) {
handler.setCurrentState(handler.getWaitForMauMauState());
}
}
@Override
public void maumau(Player p) {
// Do nothing or handle invalid state transition
}
}

View File

@ -0,0 +1,32 @@
package cards.maumau.model;
public class WaitForNextTurnState implements PlayerState {
private final PlayerHandler handler;
public WaitForNextTurnState(PlayerHandler handler) {
this.handler = handler;
}
@Override
public void nextTurn(int n) {
// Logic for next turn
if (handler.getCurrentPlayer().getCards().isEmpty()) {
handler.setRemember(handler.getCurrentPlayer());
handler.localNextTurn(n);
handler.setCurrentState(handler.getWaitForMauMauState());
} else {
handler.localNextTurn(n);
handler.setCurrentState(handler.getWaitForMauState());
}
}
@Override
public void mau(Player p) {
// Do nothing or handle invalid state transition
}
@Override
public void maumau(Player p) {
// Do nothing or handle invalid state transition
}
}