如何添加新播放器?

时间:2019-10-28 13:37:43

标签: java javafx model-view-controller poker

我分配了一个用Java进行Pokergame的任务,并且在建立Button来添加新Player时遇到了一些麻烦。 我有一个常数NUM_PLAYERS

public class PokerGame extends Application {
    public static final int NUM_PLAYERS = 2;

视图类

public class PokerGameView {
private HBox players;
private ControlArea controls;

private PokerGameModel model;

public PokerGameView(Stage stage, PokerGameModel model) {
    this.model = model;

    // Create all of the player panes we need, and put them into an HBox
    players = new HBox();
    for (int i = 0; i < PokerGame.NUM_PLAYERS; i++) {
        PlayerPane pp = new PlayerPane();
        pp.setPlayer(model.getPlayer(i)); // link to player object in the logic
        players.getChildren().add(pp);
    }

    // Creation of the Setup scene

    Label lbs = new Label("Welcome to the SE Poker Miniproject!");
    Label lbs2 = new Label("Four players max!");
    Label lbs3 = new Label("How many players are you?");

    Button chng = new Button ("Start!");

    BorderPane boot = new BorderPane();
    boot.setTop(lbs);
    boot.setCenter(chng);

    Scene scene1 = new Scene(boot, 400, 400);
    stage.setTitle("Poker");
    stage.setScene(scene1);
    stage.show();



    // Create the control area
    controls = new ControlArea();
    controls.linkDeck(model.getDeck()); // link DeckLabel to DeckOfCards in the logic

    // Put players and controls into a BorderPane
    BorderPane root = new BorderPane();
    root.setCenter(players);
    root.setBottom(controls);

    // Disallow resizing - which is difficult to get right with images
    stage.setResizable(false);

    // Create the scene using our layout; then display it
    Scene sce = new Scene(root);
    sce.getStylesheets().add(
            getClass().getResource("poker.css").toExternalForm());
    stage.setTitle("Poker Miniproject");
    stage.setScene(scene1);
    stage.show();   

    // Switching to the main Scene
    chng.setOnAction(e-> stage.setScene(sce));
}

控制器类

public class PokerGameController {
private PokerGameModel model;
private PokerGameView view;

public PokerGameController(PokerGameModel model, PokerGameView view) {
    this.model = model;
    this.view = view;

    view.getShuffleButton().setOnAction( e -> shuffle() );
    view.getDealButton().setOnAction( e -> deal() );
    view.getBackButton().setOnAction(e -> setBack() );
    view.getAddButton().setOnAction(e -> AddnewPlayer());
}



/**
 * Remove all cards from players hands, and shuffle the deck
 */
private void shuffle() {
    for (int i = 0; i < PokerGame.NUM_PLAYERS; i++) {
        Player p = model.getPlayer(i);
        p.discardHand();
        PlayerPane pp = view.getPlayerPane(i);
        pp.updatePlayerDisplay();
    }

    model.getDeck().shuffle();
}

/**
 * Deal each player five cards, then evaluate the two hands
 */
private void deal() {
    int cardsRequired = PokerGame.NUM_PLAYERS * Player.HAND_SIZE;
    DeckOfCards deck = model.getDeck();
    if (cardsRequired <= deck.getCardsRemaining()) {
        for (int i = 0; i < PokerGame.NUM_PLAYERS; i++) {
            Player p = model.getPlayer(i);
            p.discardHand();
            for (int j = 0; j < Player.HAND_SIZE; j++) {
                Card card = deck.dealCard();
                p.addCard(card);
            }
            p.evaluateHand();
            PlayerPane pp = view.getPlayerPane(i);
            pp.updatePlayerDisplay();
        }
    } else {
        Alert alert = new Alert(AlertType.ERROR, "Not enough cards - shuffle first");
        alert.showAndWait();
    }
}

private void addnewPlayer() { 
    for(int i = 0 ; i < 4; i++) {
        PokerGame.NUM_PLAYERS++;
        //Update your frame and or label

ControlArea类

public class ControlArea extends HBox{
private DeckLabel lblDeck = new DeckLabel();
private Region spacer = new Region(); // Empty spacer
Button btnShuffle = new Button("Shuffle");
Button btnDeal = new Button("Deal");
Button btnBack = new Button("Back to Menu");
Button btnAdd = new Button("New Player");

public ControlArea() {
    super(); // Always call super-constructor first !!

    this.getChildren().addAll(lblDeck, spacer, btnBack, btnAdd, btnShuffle, btnDeal);

    HBox.setHgrow(spacer, Priority.ALWAYS); // Use region to absorb resizing
    this.setId("controlArea"); // Unique ID in the CSS
}

public void linkDeck(DeckOfCards deck) {
    lblDeck.setDeck(deck);

PlayerPane类

public class PlayerPane extends VBox {
private Label lblName = new Label();
private HBox hboxCards = new HBox();
private Label lblDeter = new Label("--");
private Label lblEvaluation = new Label("--");

// Link to player object
private Player player;

public PlayerPane() {
    super(); // Always call super-constructor first !!
    this.getStyleClass().add("player"); // CSS style class

    // Add child nodes
    this.getChildren().addAll(lblName, hboxCards, lblEvaluation, lblDeter);

    // Add CardLabels for the cards
    for (int i = 0; i < 5; i++) {
        Label lblCard = new CardLabel();
        hboxCards.getChildren().add(lblCard);
    }
}

public void setPlayer(Player player) {
    this.player = player;
    updatePlayerDisplay(); // Immediately display the player information
}

public void updatePlayerDisplay() {
    lblName.setText(player.getPlayerName());
    for (int i = 0; i < Player.HAND_SIZE; i++) {
        Card card = null;
        if (player.getCards().size() > i) card = player.getCards().get(i);
        CardLabel cl = (CardLabel) hboxCards.getChildren().get(i);
        cl.setCard(card);
        HandType evaluation = player.evaluateHand();
        if (evaluation != null)
            lblEvaluation.setText(evaluation.toString());
        else
            lblEvaluation.setText("--");

还有播放器类

public class Player implements Comparable<Player> {
public static final int HAND_SIZE = 5;

private final String playerName; // This is the ID
private final ArrayList<Card> cards = new ArrayList<>();
private HandType handType;

public Player(String playerName) {
    this.playerName = playerName;
}

public String getPlayerName() {
    return playerName;
}

public ArrayList<Card> getCards() {
    return cards;
}

public void addCard(Card card) {
    if (cards.size() < HAND_SIZE) cards.add(card);

我想我必须更改控制器类中的方法,但是由于我是编程的新手,所以我不知道如何进行编程。

我希望所提供的代码现在足够了,并感谢所有到目前为止的帮助!

The goal of this Button / Method should be that an additional Player is shown. see picture.

1 个答案:

答案 0 :(得分:0)

当您使用按钮时,我假设您正在框架上显示NUM_VALUES。

如果是正确的话,您可能只需要重新粉刷框架/标签以使更改可视化。

更改以下内容:

 public static int NUM_PLAYERS = 2;

private void AddnewPlayer() { 
for(int i = 0 ; i < 4; i++) {
    PokerGame.NUM_PLAYERS++;
    //Update your frame and or label
    yourJFrame.repaint();
}