-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathCard.java
More file actions
119 lines (94 loc) · 3.17 KB
/
Card.java
File metadata and controls
119 lines (94 loc) · 3.17 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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
package com.codecool.klondike;
import javafx.scene.effect.DropShadow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.paint.Color;
import java.util.*;
import static java.util.Collections.*;
public class Card extends ImageView {
private Suit suit;
private Rank rank;
private boolean faceDown;
private Image backFace;
private Image frontFace;
private Pile containingPile;
private DropShadow dropShadow;
static Image cardBackImage;
private static final Map<String, Image> cardFaceImages = new HashMap<>();
public static final int WIDTH = 150;
public static final int HEIGHT = 215;
public Card(Suit suit, Rank rank, boolean faceDown) {
this.suit = suit;
this.rank = rank;
this.faceDown = faceDown;
this.dropShadow = new DropShadow(2, Color.gray(0, 0.75));
backFace = cardBackImage;
frontFace = cardFaceImages.get(getShortName());
setImage(faceDown ? backFace : frontFace);
setEffect(dropShadow);
}
public Suit getSuit() {
return suit;
}
public Rank getRank() {
return rank;
}
public boolean isFaceDown() {
return faceDown;
}
public String getShortName() {
return "S" + suit.getValue() + "R" + rank.getValue();
}
public DropShadow getDropShadow() {
return dropShadow;
}
public Pile getContainingPile() {
return containingPile;
}
public void setContainingPile(Pile containingPile) {
this.containingPile = containingPile;
}
public void moveToPile(Pile destPile) {
this.getContainingPile().getCards().remove(this);
destPile.addCard(this);
}
public void flip() {
faceDown = !faceDown;
setImage(faceDown ? backFace : frontFace);
}
@Override
public String toString() {
return "The " + "Rank" + rank + " of " + "Suit" + suit;
}
public static boolean isOppositeColor(Card card1, Card card2) {
if (card1.suit.getValue() <= 2 && card2.suit.getValue() > 2)
return true;
return false;
}
public static boolean isSameSuit(Card card1, Card card2) {
return card1.getSuit() == card2.getSuit();
}
public static List<Card> createNewDeck() {
List<Card> result = new ArrayList<>();
//Zamienic for na for generyczny
for (Suit suit : Suit.values()) {
for (Rank cardRank: Rank.values()){
result.add(new Card(suit, cardRank, true));
}
}
Collections.shuffle(result);
return result;
}
public static void loadCardImages() {
cardBackImage = new Image("card_images/card_back.png");
for (Suit suit : Suit.values()) {
for (Rank cardRank: Rank.values()){
String cardName = suit.getTextName() + cardRank.getValue();
String cardId = "S" + suit.getValue() + "R" + cardRank.getValue();
String imageFileName = "card_images/" + cardName + ".png";
System.out.println(imageFileName);
cardFaceImages.put(cardId, new Image(imageFileName));
}
}
}
}