initial commit
This commit is contained in:
29
src/cards/Card.java
Normal file
29
src/cards/Card.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package cards;
|
||||
|
||||
/**
|
||||
* Represents a playing card with a rank and a suit.
|
||||
*/
|
||||
public record Card(Rank rank, Suit suit) implements Comparable<Card> {
|
||||
|
||||
/**
|
||||
* Returns a string representation of the card.
|
||||
*
|
||||
* @return A string representing the card's rank followed by its suit.
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return rank.toString() + suit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Compares this card with another card for order.
|
||||
*
|
||||
* @param other The card to be compared.
|
||||
* @return A negative integer, zero, or a positive integer if this card is less than, equal to, or greater than the specified card.
|
||||
*/
|
||||
@Override
|
||||
public int compareTo(Card other) {
|
||||
//TODO implement
|
||||
return 0;
|
||||
}
|
||||
}
|
||||
19
src/cards/Rank.java
Normal file
19
src/cards/Rank.java
Normal file
@@ -0,0 +1,19 @@
|
||||
package cards;
|
||||
|
||||
/**
|
||||
* Represents the ranks of playing cards.
|
||||
*/
|
||||
public enum Rank {
|
||||
TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE;
|
||||
|
||||
/**
|
||||
* Returns a string representation of the rank.
|
||||
*
|
||||
* @return A string representing the rank.
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
//TODO implement
|
||||
return super.toString();
|
||||
}
|
||||
}
|
||||
34
src/cards/Suit.java
Normal file
34
src/cards/Suit.java
Normal file
@@ -0,0 +1,34 @@
|
||||
package cards;
|
||||
|
||||
/**
|
||||
* Represents the suits of playing cards.
|
||||
*/
|
||||
public enum Suit {
|
||||
/**
|
||||
* Represents the Hearts suit.
|
||||
*/
|
||||
HEARTS,
|
||||
/**
|
||||
* Represents the Diamonds suit.
|
||||
*/
|
||||
DIAMONDS,
|
||||
/**
|
||||
* Represents the Clubs suit.
|
||||
*/
|
||||
CLUBS,
|
||||
/**
|
||||
* Represents the Spades suit.
|
||||
*/
|
||||
SPADES;
|
||||
|
||||
/**
|
||||
* Returns a string representation of the suit.
|
||||
*
|
||||
* @return A string representing the suit.
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
//TODO implement
|
||||
return super.toString();
|
||||
}
|
||||
}
|
||||
26
src/cards/maumau/MauMauDeck.java
Normal file
26
src/cards/maumau/MauMauDeck.java
Normal file
@@ -0,0 +1,26 @@
|
||||
package cards.maumau;
|
||||
|
||||
import cards.Card;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Represents a deck of cards used in the Mau-Mau game.
|
||||
*/
|
||||
public class MauMauDeck {
|
||||
/**
|
||||
* Private constructor to prevent instantiation of this class.
|
||||
*/
|
||||
private MauMauDeck() { /* do nothing */ }
|
||||
|
||||
/**
|
||||
* Generates a deck of Mau-Mau cards and shuffles it.
|
||||
*
|
||||
* @param numDecks Number of decks to be included in the game.
|
||||
* @return A list containing the generated deck of cards.
|
||||
*/
|
||||
public static List<Card> makeDeck(int numDecks) {
|
||||
//TODO implement
|
||||
return null;
|
||||
}
|
||||
}
|
||||
82
src/cards/maumau/MauMauGame.java
Normal file
82
src/cards/maumau/MauMauGame.java
Normal file
@@ -0,0 +1,82 @@
|
||||
package cards.maumau;
|
||||
|
||||
import cards.Card;
|
||||
import cards.maumau.gui.PlayerFrame;
|
||||
import cards.maumau.model.MauMau;
|
||||
import cards.maumau.model.Player;
|
||||
|
||||
import java.util.Arrays;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Represents the main class for running the Mau-Mau card game.
|
||||
*/
|
||||
public class MauMauGame {
|
||||
|
||||
/**
|
||||
* Prints usage information for running the game.
|
||||
*/
|
||||
private static void usage() {
|
||||
System.err.println("usage: <#cards per player> <#decks> <name1> <name2> ...");
|
||||
System.err.println(" where <#cards per player> >= 2 and <#decks> >= 1");
|
||||
System.exit(1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Main method for running the Mau-Mau game.
|
||||
*
|
||||
* @param args Command line arguments.
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
if (args.length == 0)
|
||||
makeSimpleGame();
|
||||
else
|
||||
makeGeneralGame(args);
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs a simple game with predefined settings.
|
||||
*/
|
||||
private static void makeSimpleGame() {
|
||||
final List<Card> deck = MauMauDeck.makeDeck(1);
|
||||
final MauMau game = new MauMau(5, deck);
|
||||
game.addPlayer("Jacqueline");
|
||||
game.addPlayer("Chantal");
|
||||
start(game);
|
||||
}
|
||||
|
||||
/**
|
||||
* Runs a game with customizable settings based on command line arguments.
|
||||
*
|
||||
* @param args Command line arguments specifying game parameters.
|
||||
*/
|
||||
private static void makeGeneralGame(String[] args) {
|
||||
if (args.length < 4) usage();
|
||||
final Iterator<String> it = Arrays.asList(args).iterator();
|
||||
try {
|
||||
final int numCardsPerPlayer = Integer.parseInt(it.next());
|
||||
final int numDecks = Integer.parseInt(it.next());
|
||||
if (numCardsPerPlayer < 2 || numDecks < 1) usage();
|
||||
final List<Card> deck = MauMauDeck.makeDeck(numDecks);
|
||||
final MauMau game = new MauMau(numCardsPerPlayer, deck);
|
||||
while (it.hasNext())
|
||||
game.addPlayer(it.next());
|
||||
start(game);
|
||||
}
|
||||
catch (NumberFormatException ex) {
|
||||
usage();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the Mau-Mau game.
|
||||
*
|
||||
* @param game The Mau-Mau game to start.
|
||||
*/
|
||||
private static void start(MauMau game) {
|
||||
game.startGame();
|
||||
for (Player player : game.getPlayers())
|
||||
new PlayerFrame(player).setVisible(true);
|
||||
}
|
||||
}
|
||||
49
src/cards/maumau/gui/CardListRenderer.java
Normal file
49
src/cards/maumau/gui/CardListRenderer.java
Normal file
@@ -0,0 +1,49 @@
|
||||
package cards.maumau.gui;
|
||||
|
||||
import cards.Card;
|
||||
import cards.maumau.model.Player;
|
||||
|
||||
import javax.swing.DefaultListCellRenderer;
|
||||
import javax.swing.JList;
|
||||
import java.awt.Component;
|
||||
|
||||
import static java.awt.Color.GREEN;
|
||||
import static java.awt.Color.RED;
|
||||
|
||||
/**
|
||||
* Custom renderer for rendering cards in a JList representing the cards on a player's hand.
|
||||
*/
|
||||
class CardListRenderer extends DefaultListCellRenderer {
|
||||
private final transient Player player;
|
||||
|
||||
/**
|
||||
* Constructs a CardListRenderer object.
|
||||
*
|
||||
* @param player The player associated with the renderer.
|
||||
*/
|
||||
CardListRenderer(Player player) {
|
||||
this.player = player;
|
||||
}
|
||||
|
||||
/**
|
||||
* Custom rendering of list cell components.
|
||||
*
|
||||
* @param list The JList being rendered.
|
||||
* @param value The value to be rendered.
|
||||
* @param index The cell index.
|
||||
* @param isSelected True if the cell is selected.
|
||||
* @param cellHasFocus True if the cell has focus.
|
||||
* @return The rendered component.
|
||||
*/
|
||||
@Override
|
||||
public Component getListCellRendererComponent(JList<?> list, Object value, int index, boolean isSelected, boolean cellHasFocus) {
|
||||
final Component c = super.getListCellRendererComponent(list, value, index, isSelected, cellHasFocus);
|
||||
if (value instanceof Card card) {
|
||||
setText(card.toString());
|
||||
setBackground(player.canPlay(card) ? GREEN : RED);
|
||||
if (isSelected)
|
||||
setBackground(getBackground().darker());
|
||||
}
|
||||
return c;
|
||||
}
|
||||
}
|
||||
67
src/cards/maumau/gui/GameTableModel.java
Normal file
67
src/cards/maumau/gui/GameTableModel.java
Normal file
@@ -0,0 +1,67 @@
|
||||
package cards.maumau.gui;
|
||||
|
||||
import cards.maumau.model.MauMau;
|
||||
|
||||
import javax.swing.table.AbstractTableModel;
|
||||
|
||||
import static cards.maumau.model.GameState.GAME_CANCELED;
|
||||
import static cards.maumau.model.GameState.GAME_OVER;
|
||||
|
||||
/**
|
||||
* Represents the table model for displaying player information in the Mau-Mau game.
|
||||
*/
|
||||
class GameTableModel extends AbstractTableModel {
|
||||
private final transient MauMau game;
|
||||
|
||||
/**
|
||||
* Constructs a GameTableModel object.
|
||||
*
|
||||
* @param game The Mau-Mau game.
|
||||
*/
|
||||
public GameTableModel(MauMau game) {
|
||||
this.game = game;
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the table data.
|
||||
*/
|
||||
void update() {
|
||||
fireTableDataChanged();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getRowCount() {
|
||||
if (showRanking())
|
||||
return game.getRanking().size();
|
||||
return game.getPlayers().size();
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getColumnCount() {
|
||||
return 2;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Object getValueAt(int rowIndex, int columnIndex) {
|
||||
if (showRanking())
|
||||
return switch (columnIndex) {
|
||||
case 0 -> game.getRanking().get(rowIndex).getName();
|
||||
case 1 -> rowIndex + 1;
|
||||
default -> null;
|
||||
};
|
||||
return switch (columnIndex) {
|
||||
case 0 -> game.getPlayers().get(rowIndex).getName();
|
||||
case 1 -> game.getPlayers().get(rowIndex).getCards().size();
|
||||
default -> null;
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the ranking should be shown based on the current game state.
|
||||
*
|
||||
* @return True if the ranking should be shown, false otherwise.
|
||||
*/
|
||||
private boolean showRanking() {
|
||||
return game.getGameState() == GAME_CANCELED || game.getGameState() == GAME_OVER;
|
||||
}
|
||||
}
|
||||
231
src/cards/maumau/gui/PlayerFrame.java
Normal file
231
src/cards/maumau/gui/PlayerFrame.java
Normal file
@@ -0,0 +1,231 @@
|
||||
package cards.maumau.gui;
|
||||
|
||||
import cards.Card;
|
||||
import cards.Suit;
|
||||
import cards.maumau.model.IObserver;
|
||||
import cards.maumau.model.MauMau;
|
||||
import cards.maumau.model.Player;
|
||||
|
||||
import javax.swing.BorderFactory;
|
||||
import javax.swing.DefaultListModel;
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JLabel;
|
||||
import javax.swing.JList;
|
||||
import javax.swing.JOptionPane;
|
||||
import javax.swing.JPanel;
|
||||
import javax.swing.JTable;
|
||||
import java.awt.BorderLayout;
|
||||
import java.awt.Color;
|
||||
import java.awt.GridBagConstraints;
|
||||
import java.awt.GridBagLayout;
|
||||
import java.awt.Insets;
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.util.List;
|
||||
|
||||
import static cards.maumau.model.GameState.CHOOSE_SUIT;
|
||||
import static cards.maumau.model.GameState.GAME_CANCELED;
|
||||
import static cards.maumau.model.GameState.GAME_OVER;
|
||||
import static java.awt.BorderLayout.CENTER;
|
||||
import static java.awt.Color.GREEN;
|
||||
import static java.awt.Color.RED;
|
||||
|
||||
/**
|
||||
* Represents the graphical user interface for a player in the Mau-Mau card game.
|
||||
*/
|
||||
public class PlayerFrame extends JFrame implements IObserver {
|
||||
private final transient Player player;
|
||||
private final GameTableModel playerTableModel;
|
||||
private final DefaultListModel<Card> cardListModel = new DefaultListModel<>();
|
||||
private final JList<Card> cardList = new JList<>(cardListModel);
|
||||
private final JLabel topCardLabel = new JLabel("top card:");
|
||||
private final JLabel stateLabel = new JLabel("game starts");
|
||||
private final JPanel pane = new JPanel(new GridBagLayout());
|
||||
|
||||
/**
|
||||
* Constructs a new PlayerFrame object.
|
||||
*
|
||||
* @param player The player associated with this frame.
|
||||
*/
|
||||
public PlayerFrame(Player player) {
|
||||
super(player.getName());
|
||||
this.player = player;
|
||||
final MauMau game = player.getGame();
|
||||
playerTableModel = new GameTableModel(game);
|
||||
update();
|
||||
initializeUI();
|
||||
game.addObserver(this);
|
||||
}
|
||||
|
||||
/**
|
||||
* Initializes the user interface components.
|
||||
*/
|
||||
private void initializeUI() {
|
||||
getContentPane().setLayout(new BorderLayout());
|
||||
getContentPane().add(pane, CENTER);
|
||||
|
||||
final GridBagConstraints constraints = new GridBagConstraints();
|
||||
constraints.gridx = 0;
|
||||
constraints.gridy = 0;
|
||||
constraints.gridheight = 9;
|
||||
constraints.weightx = 1;
|
||||
constraints.weighty = 1;
|
||||
constraints.fill = GridBagConstraints.BOTH;
|
||||
constraints.ipadx = 15;
|
||||
constraints.ipady = 15;
|
||||
constraints.insets = new Insets(5, 5, 5, 5);
|
||||
|
||||
cardList.setBorder(BorderFactory.createLoweredBevelBorder());
|
||||
cardList.setCellRenderer(new CardListRenderer(player));
|
||||
pane.add(cardList, constraints);
|
||||
|
||||
constraints.gridx = 1;
|
||||
constraints.gridheight = 1;
|
||||
constraints.gridwidth = 4;
|
||||
constraints.weightx = 0;
|
||||
constraints.weighty = 0;
|
||||
constraints.fill = GridBagConstraints.HORIZONTAL;
|
||||
final JTable table = new JTable(playerTableModel);
|
||||
table.setBorder(BorderFactory.createLoweredBevelBorder());
|
||||
pane.add(table, constraints);
|
||||
|
||||
constraints.gridy++;
|
||||
constraints.ipadx = 0;
|
||||
constraints.ipady = 5;
|
||||
constraints.insets = new Insets(0, 0, 0, 0);
|
||||
pane.add(topCardLabel, constraints);
|
||||
|
||||
constraints.gridy++;
|
||||
pane.add(stateLabel, constraints);
|
||||
|
||||
constraints.gridy++;
|
||||
pane.add(makeButton("choose card", this::chooseCard), constraints);
|
||||
|
||||
constraints.gridy++;
|
||||
pane.add(makeButton("skip", e -> player.skip()), constraints);
|
||||
|
||||
constraints.gridy++;
|
||||
constraints.gridwidth = 1;
|
||||
for (Suit suit : Suit.values()) {
|
||||
pane.add(makeButton(suit.toString(), e -> player.chooseSuit(suit)), constraints);
|
||||
constraints.gridx++;
|
||||
}
|
||||
|
||||
constraints.gridy++;
|
||||
constraints.gridx = 1;
|
||||
constraints.gridwidth = 4;
|
||||
pane.add(makeButton("have no 7", e -> player.no7()), constraints);
|
||||
|
||||
constraints.gridy++;
|
||||
constraints.gridwidth = 2;
|
||||
pane.add(makeButton("\"Mau\"", e -> player.mau()), constraints);
|
||||
|
||||
constraints.gridx = 3;
|
||||
pane.add(makeButton("\"Mau-Mau\"", e -> player.maumau()), constraints);
|
||||
|
||||
pack();
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a JButton with the specified text and action listener.
|
||||
*
|
||||
* @param text The text displayed on the button.
|
||||
* @param listener The action listener for the button.
|
||||
* @return The created JButton.
|
||||
*/
|
||||
private JButton makeButton(String text, ActionListener listener) {
|
||||
final JButton button = new JButton(text);
|
||||
button.addActionListener(listener);
|
||||
return button;
|
||||
}
|
||||
|
||||
/**
|
||||
* Action performed when the "choose card" button is clicked.
|
||||
*
|
||||
* @param e The ActionEvent object.
|
||||
*/
|
||||
private void chooseCard(ActionEvent e) {
|
||||
final List<Card> selected = cardList.getSelectedValuesList();
|
||||
if (selected.size() == 1)
|
||||
player.chooseCard(selected.getFirst());
|
||||
else if (selected.isEmpty())
|
||||
error("You must select a card first!");
|
||||
else
|
||||
error("You must not select more than a single card!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the user interface.
|
||||
*/
|
||||
@Override
|
||||
public void update() {
|
||||
playerTableModel.update();
|
||||
updateCardList();
|
||||
updateTopCardLabel();
|
||||
updateStateLabel();
|
||||
updateBorderColor();
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the card list displayed in the UI.
|
||||
*/
|
||||
private void updateCardList() {
|
||||
cardListModel.clear();
|
||||
player.getCards().stream().sorted().forEach(cardListModel::addElement);
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the label displaying the top card on the discard pile.
|
||||
*/
|
||||
private void updateTopCardLabel() {
|
||||
final List<Card> pile = player.getGame().getDiscardPile();
|
||||
topCardLabel.setText(pile.isEmpty() ? "no top card" : "top card: " + pile.getFirst());
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the label displaying the current game state.
|
||||
*/
|
||||
private void updateStateLabel() {
|
||||
final MauMau game = player.getGame();
|
||||
if (game.getGameState() == GAME_CANCELED)
|
||||
stateLabel.setText("Game canceled");
|
||||
else if (game.getGameState() == GAME_OVER)
|
||||
stateLabel.setText("Game over");
|
||||
else if (game.get7Counter() > 0)
|
||||
stateLabel.setText(game.get7Counter() + " seven on discard pile");
|
||||
else if (game.getChosenSuit() != null)
|
||||
stateLabel.setText(game.getChosenSuit() + " chosen");
|
||||
else if (game.getGameState() == CHOOSE_SUIT && game.getCurrentPlayer() == player)
|
||||
stateLabel.setText("Choose a suit");
|
||||
else
|
||||
stateLabel.setText(" ");
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates the border color of the frame based on the current player's turn.
|
||||
*/
|
||||
private void updateBorderColor() {
|
||||
final Color borderColor = player.getGame().getCurrentPlayer() == player ? GREEN : RED;
|
||||
pane.setBorder(BorderFactory.createLineBorder(borderColor, 5));
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays an error message dialog.
|
||||
*
|
||||
* @param msg The error message to display.
|
||||
*/
|
||||
private void error(String msg) {
|
||||
JOptionPane.showMessageDialog(this, msg, "Error", JOptionPane.ERROR_MESSAGE);
|
||||
}
|
||||
|
||||
/**
|
||||
* Displays a message dialog.
|
||||
*
|
||||
* @param msg The message to display.
|
||||
*/
|
||||
@Override
|
||||
public void message(String msg) {
|
||||
JOptionPane.showMessageDialog(this, msg, "Message", JOptionPane.INFORMATION_MESSAGE);
|
||||
}
|
||||
}
|
||||
153
src/cards/maumau/model/ActionHandler.java
Normal file
153
src/cards/maumau/model/ActionHandler.java
Normal file
@@ -0,0 +1,153 @@
|
||||
package cards.maumau.model;
|
||||
|
||||
import cards.Card;
|
||||
import cards.Suit;
|
||||
|
||||
/**
|
||||
* Manages the actions and state transitions within a MauMau game.
|
||||
*/
|
||||
class ActionHandler {
|
||||
private final MauMau game;
|
||||
private Suit chosenSuit;
|
||||
private int ctr7 = 0;
|
||||
|
||||
/**
|
||||
* Constructs an ActionHandler for the specified MauMau game.
|
||||
*
|
||||
* @param game The MauMau game instance.
|
||||
*/
|
||||
ActionHandler(MauMau game) {
|
||||
this.game = game;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the specified player to the game.
|
||||
*
|
||||
* @param player The player to be added to the game.
|
||||
*/
|
||||
void addPlayer(Player player) {
|
||||
//TODO implement
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the game.
|
||||
*/
|
||||
void startGame() {
|
||||
//TODO implement
|
||||
}
|
||||
|
||||
/**
|
||||
* Transitions the game state to GAME_OVER.
|
||||
*/
|
||||
void finishGame() {
|
||||
//TODO implement
|
||||
}
|
||||
|
||||
/**
|
||||
* Transitions the game state to GAME_CANCELED.
|
||||
*/
|
||||
void cancelGame() {
|
||||
//TODO implement
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the player's choice of a card in the current state.
|
||||
*
|
||||
* @param c The card chosen by the player.
|
||||
*/
|
||||
void chooseCard(Card c) {
|
||||
//TODO implement
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the player's choice of a suit in the current state.
|
||||
*
|
||||
* @param suit The suit chosen by the player.
|
||||
*/
|
||||
void chooseSuit(Suit suit) {
|
||||
//TODO implement
|
||||
}
|
||||
|
||||
/**
|
||||
* Lets the player skip a round.
|
||||
**/
|
||||
void skip() {
|
||||
//TODO implement
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the player saying "no 7" in the current state.
|
||||
*/
|
||||
void no7() {
|
||||
//TODO implement
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the MauMau game instance associated with this action handler.
|
||||
*
|
||||
* @return The MauMau game instance.
|
||||
*/
|
||||
MauMau getGame() {
|
||||
return game;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the suit chosen by a player after playing a Jack card.
|
||||
*
|
||||
* @return The chosen suit.
|
||||
*/
|
||||
Suit getChosenSuit() {
|
||||
return chosenSuit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the suit chosen by a player after playing a Jack card.
|
||||
*
|
||||
* @param chosenSuit The suit chosen by the player.
|
||||
*/
|
||||
void setChosenSuit(Suit chosenSuit) {
|
||||
this.chosenSuit = chosenSuit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of number 7 cards played recently.
|
||||
*
|
||||
* @return The number of number 7 cards played recently.
|
||||
*/
|
||||
int get7Counter() {
|
||||
return ctr7;
|
||||
}
|
||||
|
||||
/**
|
||||
* Resets the counter of number 7 cards played to zero.
|
||||
*/
|
||||
void reset7Counter() {
|
||||
ctr7 = 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Increments the counter of number 7 cards played by one.
|
||||
*/
|
||||
void increment7Counter() {
|
||||
ctr7++;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current state of the game.
|
||||
*/
|
||||
GameState getGameState() {
|
||||
//TODO implement
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a card can be played by the current player in the current state.
|
||||
*
|
||||
* @param c The card being played.
|
||||
* @return True if the card can be played, false otherwise.
|
||||
*/
|
||||
boolean canPlay(Card c) {
|
||||
//TODO implement
|
||||
return false;
|
||||
}
|
||||
}
|
||||
96
src/cards/maumau/model/CardHandler.java
Normal file
96
src/cards/maumau/model/CardHandler.java
Normal file
@@ -0,0 +1,96 @@
|
||||
package cards.maumau.model;
|
||||
|
||||
import cards.Card;
|
||||
|
||||
import java.util.Collections;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Manages the draw pile and discard pile in a MauMau game.
|
||||
*/
|
||||
class CardHandler {
|
||||
private final MauMau game;
|
||||
private final int numCardsPerPlayer;
|
||||
private final List<Card> drawPile = new LinkedList<>();
|
||||
private final List<Card> discardPile = new LinkedList<>();
|
||||
|
||||
/**
|
||||
* Constructs a CardHandler for the specified MauMau game, deck, and number of cards per player.
|
||||
*
|
||||
* @param game The MauMau game instance.
|
||||
* @param deck The deck of cards.
|
||||
* @param numCardsPerPlayer The number of cards per player.
|
||||
*/
|
||||
CardHandler(MauMau game, List<Card> deck, int numCardsPerPlayer) {
|
||||
this.game = game;
|
||||
this.numCardsPerPlayer = numCardsPerPlayer;
|
||||
drawPile.addAll(deck);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the draw pile containing remaining cards.
|
||||
*
|
||||
* @return The draw pile.
|
||||
*/
|
||||
List<Card> getDrawPile() {
|
||||
return drawPile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the discard pile containing played cards.
|
||||
*
|
||||
* @return The discard pile.
|
||||
*/
|
||||
List<Card> getDiscardPile() {
|
||||
return discardPile;
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws a card from the draw pile.
|
||||
*
|
||||
* @return The drawn card, or null if the draw pile is empty.
|
||||
*/
|
||||
Card drawCard() {
|
||||
if (drawPile.isEmpty())
|
||||
reuseDiscardedCards();
|
||||
if (!drawPile.isEmpty())
|
||||
return drawPile.removeFirst();
|
||||
game.getActionHandler().cancelGame();
|
||||
return null;
|
||||
}
|
||||
|
||||
private void reuseDiscardedCards() {
|
||||
if (discardPile.isEmpty()) return;
|
||||
final Card top = discardPile.removeFirst();
|
||||
Collections.shuffle(discardPile);
|
||||
drawPile.addAll(discardPile);
|
||||
discardPile.clear();
|
||||
discardPile.addFirst(top);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deals cards to all players.
|
||||
*/
|
||||
void dealCards() {
|
||||
//TODO implement
|
||||
}
|
||||
|
||||
/**
|
||||
* Discards a card onto the discard pile.
|
||||
*
|
||||
* @param c The card to discard.
|
||||
*/
|
||||
void discard(Card c) {
|
||||
discardPile.addFirst(c);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the top card of the discard pile.
|
||||
*
|
||||
* @return The top card of the discard pile.
|
||||
*/
|
||||
Card top() {
|
||||
return discardPile.getFirst();
|
||||
}
|
||||
}
|
||||
29
src/cards/maumau/model/GameState.java
Normal file
29
src/cards/maumau/model/GameState.java
Normal file
@@ -0,0 +1,29 @@
|
||||
package cards.maumau.model;
|
||||
|
||||
/**
|
||||
* 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
|
||||
}
|
||||
18
src/cards/maumau/model/IObserver.java
Normal file
18
src/cards/maumau/model/IObserver.java
Normal file
@@ -0,0 +1,18 @@
|
||||
package cards.maumau.model;
|
||||
|
||||
/**
|
||||
* Interface for observing changes in the Mau-Mau game.
|
||||
*/
|
||||
public interface IObserver {
|
||||
/**
|
||||
* Method called to notify the observer of a general update in the game.
|
||||
*/
|
||||
void update();
|
||||
|
||||
/**
|
||||
* Method called to send a message to the observer.
|
||||
*
|
||||
* @param msg The message to be sent.
|
||||
*/
|
||||
void message(String msg);
|
||||
}
|
||||
227
src/cards/maumau/model/MauMau.java
Normal file
227
src/cards/maumau/model/MauMau.java
Normal file
@@ -0,0 +1,227 @@
|
||||
package cards.maumau.model;
|
||||
|
||||
import cards.Card;
|
||||
import cards.Suit;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.Objects;
|
||||
import java.util.function.Function;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Represents a Mau-Mau card game.
|
||||
*/
|
||||
public class MauMau {
|
||||
private final ActionHandler actionHandler = new ActionHandler(this);
|
||||
private final PlayerHandler playerHandler = new PlayerHandler(this);
|
||||
private final CardHandler cardHandler;
|
||||
private final List<IObserver> observers = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Constructs a MauMau game with the specified parameters.
|
||||
*
|
||||
* @param numCardsPerPlayer The number of cards each player should have initially.
|
||||
* @param deck The deck of cards to be used in the game.
|
||||
*/
|
||||
public MauMau(int numCardsPerPlayer, List<Card> deck) {
|
||||
cardHandler = new CardHandler(this, deck, numCardsPerPlayer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds an observer to the game.
|
||||
*
|
||||
* @param observer The observer to add.
|
||||
*/
|
||||
public void addObserver(IObserver observer) {
|
||||
observers.add(observer);
|
||||
}
|
||||
|
||||
/**
|
||||
* Notifies all observers of the game.
|
||||
*/
|
||||
void notifyObservers() {
|
||||
for (IObserver o : observers)
|
||||
o.update();
|
||||
}
|
||||
|
||||
/**
|
||||
* Sends a message to all observers.
|
||||
*
|
||||
* @param msg The message to send.
|
||||
*/
|
||||
void sendMessage(String msg) {
|
||||
for (IObserver o : observers)
|
||||
o.message(msg);
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the player handler for the game.
|
||||
*
|
||||
* @return The player handler.
|
||||
*/
|
||||
PlayerHandler getPlayerHandler() {
|
||||
return playerHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the card handler for the game.
|
||||
*
|
||||
* @return The card handler.
|
||||
*/
|
||||
CardHandler getCardHandler() {
|
||||
return cardHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the action handler for the game.
|
||||
*
|
||||
* @return The action handler.
|
||||
*/
|
||||
ActionHandler getActionHandler() {
|
||||
return actionHandler;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the suit chosen by a player after playing a Jack.
|
||||
*
|
||||
* @return The chosen suit.
|
||||
*/
|
||||
public Suit getChosenSuit() {
|
||||
return actionHandler.getChosenSuit();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the number of number 7 cards played recently.
|
||||
*
|
||||
* @return The number of number 7 cards played recently.
|
||||
*/
|
||||
public int get7Counter() {
|
||||
return actionHandler.get7Counter();
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts the Mau-Mau game.
|
||||
*/
|
||||
public void startGame() {
|
||||
actionHandler.startGame();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of the game.
|
||||
*
|
||||
* @return String representation of the game.
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
final StringBuilder sb = new StringBuilder();
|
||||
sb.append("Game state: ").append(getGameState()).append("\n");
|
||||
sb.append("State: ").append(actionHandler).append("\n");
|
||||
sb.append("Players:\n");
|
||||
sb.append(mkString(getPlayers(), " ", "\n "));
|
||||
sb.append(mkString(getDrawPile(), "draw pile: ", " "));
|
||||
sb.append(mkString(getDiscardPile(), "discard pile: ", " "));
|
||||
sb.append(mkString(getRanking(), Player::getName, "ranking: ", ", ", "\n"));
|
||||
sb.append("chosen suit: ").append(getChosenSuit()).append("\n");
|
||||
sb.append("7 count: ").append(get7Counter()).append("\n");
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* Joins the elements of the list into a single string with the specified prefix and delimiter.
|
||||
*
|
||||
* @param list The list to join.
|
||||
* @param func Function to convert list elements to strings.
|
||||
* @param prefix Prefix for the resulting string.
|
||||
* @param delimiter Delimiter to separate list elements.
|
||||
* @param suffix Suffix for the resulting string.
|
||||
* @param <A> Type of elements in the list.
|
||||
* @return Joined string representation of the list.
|
||||
*/
|
||||
static <A> String mkString(List<A> list, Function<? super A, String> func, String prefix, String delimiter, String suffix) {
|
||||
return list.stream()
|
||||
.map(func)
|
||||
.collect(Collectors.joining(delimiter, prefix, suffix));
|
||||
}
|
||||
|
||||
/**
|
||||
* Joins the elements of the list into a single string with the specified prefix and delimiter.
|
||||
*
|
||||
* @param list The list to join.
|
||||
* @param prefix Prefix for the resulting string.
|
||||
* @param delimiter Delimiter to separate list elements.
|
||||
* @param <A> Type of elements in the list.
|
||||
* @return Joined string representation of the list.
|
||||
*/
|
||||
static <A> String mkString(List<A> list, String prefix, String delimiter) {
|
||||
return mkString(list, Objects::toString, prefix, delimiter, "\n");
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the list of players still participating in the game.
|
||||
* The first player in the list is the current player.
|
||||
*
|
||||
* @return The list of players.
|
||||
*/
|
||||
public List<Player> getPlayers() {
|
||||
return playerHandler.getPlayers();
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a player to the game.
|
||||
*
|
||||
* @param name The name of the player.
|
||||
* @return The player added to the game.
|
||||
*/
|
||||
public Player addPlayer(String name) {
|
||||
final Player player = new Player(name, this);
|
||||
actionHandler.addPlayer(player);
|
||||
return player;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the ranking of players that have already discarded all of their cards
|
||||
* and, when the game is over, also the loser.
|
||||
*
|
||||
* @return The ranking of players.
|
||||
*/
|
||||
public List<Player> getRanking() {
|
||||
return playerHandler.getRanking();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current player whose turn it is.
|
||||
*
|
||||
* @return The current player.
|
||||
*/
|
||||
public Player getCurrentPlayer() {
|
||||
return playerHandler.getCurrentPlayer();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the draw pile containing remaining cards.
|
||||
*
|
||||
* @return The draw pile.
|
||||
*/
|
||||
public List<Card> getDrawPile() {
|
||||
return cardHandler.getDrawPile();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the discard pile containing played cards.
|
||||
*
|
||||
* @return The discard pile.
|
||||
*/
|
||||
public List<Card> getDiscardPile() {
|
||||
return cardHandler.getDiscardPile();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current state of the game.
|
||||
*
|
||||
* @return The state of the game.
|
||||
*/
|
||||
public GameState getGameState() {
|
||||
return actionHandler.getGameState();
|
||||
}
|
||||
}
|
||||
174
src/cards/maumau/model/Player.java
Normal file
174
src/cards/maumau/model/Player.java
Normal file
@@ -0,0 +1,174 @@
|
||||
package cards.maumau.model;
|
||||
|
||||
import cards.Card;
|
||||
import cards.Suit;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.List;
|
||||
import java.util.stream.Collectors;
|
||||
|
||||
/**
|
||||
* Represents a player in a Mau-Mau card game.
|
||||
*/
|
||||
public class Player {
|
||||
private final MauMau game;
|
||||
private final String name;
|
||||
private final List<Card> cards = new ArrayList<>();
|
||||
|
||||
/**
|
||||
* Constructor for Player class.
|
||||
*
|
||||
* @param name The name of the player.
|
||||
* @param game The MauMau game instance the player is part of.
|
||||
*/
|
||||
Player(String name, MauMau game) {
|
||||
this.name = name;
|
||||
this.game = game;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a string representation of the player, including their name and their cards.
|
||||
*
|
||||
* @return String representation of the player.
|
||||
*/
|
||||
@Override
|
||||
public String toString() {
|
||||
return name + cards.stream()
|
||||
.map(Object::toString)
|
||||
.collect(Collectors.joining(" ", " (cards ", ")"));
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the name of the player.
|
||||
*
|
||||
* @return The name of the player.
|
||||
*/
|
||||
public String getName() {
|
||||
return name;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the MauMau game instance the player is part of.
|
||||
*
|
||||
* @return The MauMau game instance.
|
||||
*/
|
||||
public MauMau getGame() {
|
||||
return game;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the list of cards held by the player.
|
||||
*
|
||||
* @return List of cards held by the player.
|
||||
*/
|
||||
public List<Card> getCards() {
|
||||
return cards;
|
||||
}
|
||||
|
||||
/**
|
||||
* Draws a specified number of cards from the draw pile.
|
||||
*
|
||||
* @param n The number of cards to draw.
|
||||
*/
|
||||
void drawCards(int n) {
|
||||
for (int i = 0; i < n; i++) {
|
||||
final Card c = game.getCardHandler().drawCard();
|
||||
if (c == null) return;
|
||||
cards.add(c);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Plays a card from the player's hand onto the discard pile.
|
||||
*
|
||||
* @param c The card to be played.
|
||||
*/
|
||||
void playCard(Card c) {
|
||||
if (!cards.remove(c))
|
||||
throw new IllegalArgumentException(this + " doesn't have " + c);
|
||||
game.getCardHandler().discard(c);
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the player can legally play a specific card.
|
||||
*
|
||||
* @param c The card to check.
|
||||
* @return True if the player can legally play the card, false otherwise.
|
||||
*/
|
||||
public boolean canPlay(Card c) {
|
||||
return game.getCurrentPlayer() == this && game.getActionHandler().canPlay(c);
|
||||
}
|
||||
|
||||
/**
|
||||
* Selects a card to play from the player's hand.
|
||||
*
|
||||
* @param c The card to play.
|
||||
*/
|
||||
public void chooseCard(Card c) {
|
||||
if (isCurrentPlayer()) {
|
||||
game.getActionHandler().chooseCard(c);
|
||||
game.notifyObservers();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Chooses a suit after playing a Jack card.
|
||||
*
|
||||
* @param s The suit chosen by the player.
|
||||
*/
|
||||
public void chooseSuit(Suit s) {
|
||||
if (isCurrentPlayer()) {
|
||||
game.getActionHandler().chooseSuit(s);
|
||||
game.notifyObservers();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Skips the player's turn.
|
||||
*/
|
||||
public void skip() {
|
||||
if (isCurrentPlayer()) {
|
||||
game.getActionHandler().skip();
|
||||
game.notifyObservers();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes special action when the player cannot play a seven card.
|
||||
*/
|
||||
public void no7() {
|
||||
if (isCurrentPlayer()) {
|
||||
game.getActionHandler().no7();
|
||||
game.notifyObservers();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes special action when the player calls "Mau".
|
||||
*/
|
||||
public void mau() {
|
||||
game.getPlayerHandler().mau(this);
|
||||
game.notifyObservers();
|
||||
}
|
||||
|
||||
/**
|
||||
* Executes special action when the player calls "Mau-Mau".
|
||||
*/
|
||||
public void maumau() {
|
||||
game.getPlayerHandler().maumau(this);
|
||||
game.notifyObservers();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if the current player is this player instance.
|
||||
* If not, sends a message indicating it's not their turn.
|
||||
*
|
||||
* @return True if it's this player's turn, false otherwise.
|
||||
*/
|
||||
private boolean isCurrentPlayer() {
|
||||
if (this == game.getCurrentPlayer())
|
||||
return true;
|
||||
game.sendMessage(String.format("It's %s's turn, not %s's.", game.getCurrentPlayer().getName(), name));
|
||||
return false;
|
||||
}
|
||||
}
|
||||
106
src/cards/maumau/model/PlayerHandler.java
Normal file
106
src/cards/maumau/model/PlayerHandler.java
Normal file
@@ -0,0 +1,106 @@
|
||||
package cards.maumau.model;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.LinkedList;
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* Handles players in a MauMau game.
|
||||
*/
|
||||
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) {
|
||||
this.game = game;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initiates the next turn in the game.
|
||||
*
|
||||
* @param n The number of turns to proceed.
|
||||
*/
|
||||
void nextTurn(int n) {
|
||||
//TODO implement
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles a player calling "Mau".
|
||||
*
|
||||
* @param p The player calling "Mau".
|
||||
*/
|
||||
void mau(Player p) {
|
||||
//TODO implement
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles a player calling "Mau-Mau".
|
||||
*
|
||||
* @param p The player calling "Mau-Mau".
|
||||
*/
|
||||
void maumau(Player p) {
|
||||
//TODO implement
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the list of players participating in the game.
|
||||
*
|
||||
* @return The list of players.
|
||||
*/
|
||||
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() {
|
||||
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
|
||||
}
|
||||
|
||||
/**
|
||||
* 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
|
||||
}
|
||||
|
||||
/**
|
||||
* Finishes a player's participation in the game.
|
||||
*
|
||||
* @param p The player to finish.
|
||||
*/
|
||||
private void finishPlayer(Player p) {
|
||||
//TODO implement
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current player whose turn it is.
|
||||
*
|
||||
* @return The current player.
|
||||
*/
|
||||
Player getCurrentPlayer() {
|
||||
return players.isEmpty() ? null : players.getFirst();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user