java - 如何添加新玩家?
问题描述
我有一个任务是用 Java 做一个Pokergame
,我在建立一个按钮来添加一个新的播放器时遇到了一些麻烦。我有一个常数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
控制区域类
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("--");
和 Player 类
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);
我想我必须更改控制器类中的方法,但由于我对编程很陌生,所以我不知道如何去做。
我希望现在提供的代码已经足够了,并感谢到目前为止所有人的帮助!
解决方案
当您使用按钮时,我假设您在框架上显示 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();
}
推荐阅读
- angular - ng build 找不到来自 Git Bash 的打字稿模块
- node.js - Fetch API 在身份验证期间无法获取,同时出现 CORS 错误
- javascript - 选项按钮不会在 PHP 中自动重新加载页面,我在代码中找不到错误
- javascript - 将 this.props 传递给嵌套函数
- android - 将最大高度应用于 BottomSheetDialogFragment 并应用高度变化过渡
- sql - 判断 A 列中的 ID 是否存在 B 列中的特定条目
- javascript - 如何调用方法 myFunction 以返回(名称)我尝试了很多方法但它不起作用
- powershell - 是否有写入提示缓冲区的命令/程序/Cmdlet?
- sql - 在 SQL Server 中加载具有相同值的 XML 数据
- python - Interpolating Month rows to Day rows