-
Notifications
You must be signed in to change notification settings - Fork 0
/
Hand.java
106 lines (96 loc) · 2.75 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
import java.awt.*;
import java.awt.image.*;
import javax.swing.*;
import java.io.*;
import javax.imageio.*;
import java.util.*;
import java.awt.event.*;
import javax.sound.sampled.*;
/*
Contains the cards in one party's hand (player or dealer)
*/
public class Hand{
private ArrayList<Card> cards = new ArrayList<Card>();
private int y;
private int x;
private File yourFile; //location of the sound file to be used
private AudioInputStream stream;
private AudioFormat format;
private DataLine.Info info;
private Clip clip;
private static final int startingX = 100;
//initializes the leftmost position for the hand.
public Hand(int y){
x = startingX;
this.y = y;
}
//takes a card and adds it to this hand
public void takeCard(Card c){
try { //play fanfare music https://stackoverflow.com/questions/2416935/how-to-play-wav-files-with-java
try{
clip.stop();
}
catch(Exception ex){
}
yourFile = new File("flipcard.wav");
stream = AudioSystem.getAudioInputStream(yourFile);
format = stream.getFormat();
info = new DataLine.Info(Clip.class, format);
clip = (Clip) AudioSystem.getLine(info);
clip.open(stream);
clip.start();
} catch (Exception exception) {
System.out.println(exception);
// whatevers
} //end play fanfare music
cards.add(c);
}
//returns true if cards contains at least 1 element
public boolean hasCards(){
return cards.size() > 0;
}
//removes the top card from this hand and returns it to the class calling for it
public Card returnCard(){
if (cards.get(0).getValue() == 1){
cards.get(0).increaseAce();
}
return cards.remove(0);
}
//if the hand contains an ace valued at 11, retuces its value to 1 and returns true
//returns false otherwise
private boolean hasAce(){
for (Card c : cards){
if (c.getValue() == 11){
c.decreaseAce();
return true;
}
}
return false;
}
//calculates and returns the sum of the value in cards. If sum exceeds 21 and there
//is at least one ace valued at 11, the ace value is reduced to 1.
public int getValue(){
int value = 0;
for (Card c : cards){
value += c.getValue();
}
while (value > 21 && hasAce()){
value -= 10;
}
return value;
}
//sets x back to starting point for repainting
public void resetX(){
x = startingX;
}
public int getX(){
return x;
}
//paints the cards in this hand
public void paint(Graphics g){
for (Card c : cards){
c.paint(g, x, y);
x += 100;
}
}
}