Repository navigation
Expand file tree
/
Copy pathCardView.java
More file actions
88 lines (76 loc) · 2.92 KB
/
Copy pathCardView.java
File metadata and controls
88 lines (76 loc) · 2.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
package application;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import model.card.Card;
import model.player.Player;
import java.util.ArrayList;
import java.util.List;
/**
* Displays player's hand as clickable cards.
*/
public class CardView {
private final Player player;
private final List<Button> cardButtons;
private Button selectedButton = null;
// Add a callback for card selection
private Runnable cardSelectionCallback;
public void setCardSelectionCallback(Runnable callback) {
this.cardSelectionCallback = callback;
}
public CardView(engine.Game game) {
this.player = game.getPlayers().get(game.getCurrentPlayerIndex());
this.cardButtons = new ArrayList<>();
buildCardButtons(game);
}
private void buildCardButtons(engine.Game game) {
for (Card card : player.getHand()) {
// Skip null cards
if (card == null) {
continue;
}
String label = card.getName();
try {
// Try to get suit if available
java.lang.reflect.Method getSuit = card.getClass().getMethod("getSuit");
Object suitObj = getSuit.invoke(card);
if (suitObj != null) {
String suitSymbol = suitToSymbol(suitObj.toString());
label += " " + suitSymbol;
}
} catch (Exception ignored) {}
Button cardButton = new Button(label);
cardButton.setPrefWidth(100);
if (player.getSelectedCard() == card) {
cardButton.setStyle("-fx-border-color: #0078D7; -fx-border-width: 3px;");
selectedButton = cardButton;
}
cardButton.setOnAction(e -> {
try {
game.selectCard(card);
if (selectedButton != null) selectedButton.setStyle("");
cardButton.setStyle("-fx-border-color: #0078D7; -fx-border-width: 3px;");
selectedButton = cardButton;
// Notify the GameScreen that a card has been selected
if (cardSelectionCallback != null) {
cardSelectionCallback.run();
}
} catch (Exception ex) {
ErrorDialog.show("Card selection error: " + ex.getMessage());
}
});
cardButtons.add(cardButton);
}
}
private String suitToSymbol(String suit) {
return switch (suit.toUpperCase()) {
case "HEART" -> "♥";
case "DIAMOND" -> "♦";
case "CLUB" -> "♣";
case "SPADE" -> "♠";
default -> suit;
};
}
public List<Button> getNodes() {
return cardButtons;
}
}