-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hand.java
110 lines (87 loc) · 2.76 KB
/
Hand.java
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
import java.awt.Color;
import java.awt.Graphics;
import java.util.ArrayList;
public class Hand {
public ArrayList<Card> cards = new ArrayList<Card>();
public int total = 0;
public int softTotal = 0;
public boolean hasAce = false;
private Card card;
public void addCard(Deck deck, Boolean visible) {
card = deck.nextCard();
card.visible = visible;
if (visible) {
addTotal(card);
}
cards.add(card);
}
public void addTotal(Card card) {
if (card.rank == "A") {
this.hasAce = true;
this.softTotal++;
} else {
this.softTotal += card.value;
}
this.total += card.value;
}
public void drawHand(String type, Graphics pen) {
final int cardWidth = 150;
final int cardHeight = 175;
final int cardXSpacing = 165;
final int yDealer = 30;
final int yPlayer = 210 + yDealer;
final int xStart = 10;
int offsetX = 0;
int offsetY = 0;
int index = 0;
pen.setColor(Color.white);
String scoreText = "Total: " + this.total;
if (this.hasAce && this.total != 21) {
scoreText += " OR " + this.softTotal;
}
switch (type) {
case "Dealer":
offsetY = yDealer;
String dealerText = "Dealer " + scoreText;
pen.drawString(dealerText, xStart, yDealer - 5);
break;
case "Player":
offsetX = (cardXSpacing * index) + xStart;
offsetY = yPlayer;
String playerText = "Player " + scoreText;
pen.drawString(playerText, xStart, yPlayer - 5);
break;
default:
return;
}
for (Card card : cards) {
offsetX = (cardXSpacing * index) + xStart;
index++;
pen.setColor(Color.WHITE);
pen.fillRoundRect(offsetX, offsetY, cardWidth, cardHeight, 10, 10);
pen.setColor(Color.black);
pen.drawRoundRect(offsetX, offsetY, cardWidth, cardHeight, 10, 10);
// Check if card should show
if (!card.visible) {
return;
}
pen.setColor(card.color);
int cardCenterWidth = cardWidth / 2;
int cardCenterHeight = cardHeight / 2;
int rankOffsetX = (offsetX + cardCenterWidth) - 10;
int rankOffsetY = (offsetY + cardCenterHeight) + 10;
// Draw value
pen.drawString(card.rank, rankOffsetX, rankOffsetY);
int suitOffsetXLeft = offsetX + 10;
int suitOffsetXRight = offsetX + 125;
int suitOffsetYTop = offsetY + 20;
int suitOffsetYBottom = offsetY + 170;
// Draw suit on top side corners
pen.drawString(card.display, suitOffsetXLeft, suitOffsetYTop);
pen.drawString(card.display, suitOffsetXRight, suitOffsetYTop);
// Draw suit on bottom side corners
pen.drawString(card.display, suitOffsetXLeft, suitOffsetYBottom);
pen.drawString(card.display, suitOffsetXRight, suitOffsetYBottom);
}
}
}