-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.java
161 lines (160 loc) · 4.42 KB
/
Player.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
/***The Player class should be able to represent the set of cards a player has in hand.
* The player class must allow to take a card from the deck and put it in the hand,
* count the points of the cards in hand...
**/
import java.util.ArrayList;
public class Player {
private ArrayList<Card> hand; // The cards in the plyaer's hand.
/*Constructor*/
Player(){
hand=new ArrayList<Card>();
}
/*Accessor*/
public ArrayList<Card> getHand() {
return hand;
}/*** Remove all cards from the hand, leaving it empty. */
public void clear() {
getHand().clear();
}
/*** Add a card to the hand. It is added at the end of the current hand.
* @param card the non-null card to be added.
* @throws NullPointerException if the parameter card is null.
*/
public void add(Card card) {
if(card!=null) {
getHand().add(card);
}
else {
throw new NullPointerException();
}
}
/** * Remove a card from the hand, if present.
* @param card the card to be removed. If card is null or if the card is not in
* the hand, then nothing is done.
*/
public void remove(Card card) {
if(getHand().size()!=0) {
for(Card i:getHand()) {
if(i==card && i!=null) getHand().remove(card);
}
}
}
/*** Remove the card in a specified position from the hand.
* @param position the position of the card that is to be removed, where
* positions are numbered starting from zero.
* @throws IllegalArgumentException if the position does not exist in
* the hand, that is if the position is less than 0 or greater than
* or equal to the number of cards in the hand.
*/
public void remove(int position) {
if(position<0 || position>=getHand().size()) {
throw new IllegalArgumentException();
}
else {
getHand().remove(position);
}
}
/*** Returns the number of cards in the hand. */
public int totalCards() {
return getHand().size();
}
/*** Gets the card in a specified position in the hand. (Note that this card
* is not removed from the hand!)
* @param position the position of the card that is to be returned
* @throws IllegalArgumentException if position does not exist in the hand
*/
public Card getCard(int position) {
Card a=null;
if(position<0 || position>=getHand().size()) {
throw new IllegalArgumentException();
}
else {
a= getHand().get(position);
}
return a;
}
/** * Computes and returns the total value of this hand in the game */
public int getHandTotal(){
int a=0;
for(int i=0;i<getHand().size();i++) {
Card get=getCard(i);
if(get!=null) {
int valu=get.getValue();
if(valu>10) {
valu=10;
}
a+=valu;
}
}
for(int i=0;i<this.hand.size();i++) {
Card get=getCard(i);
if(get!=null) {
int valu=get.getValue();
if(valu==1 && (a+10)<=21){
a=a+10;
}
}
}
return a;
}
/** * Sorts the cards in the hand so that cards of the same color are
* grouped together, and within a color the cards are sorted by value.
* Note that aces are considered to have the lowest value, 1.
*/
public void sortByColor() {
ArrayList<Card> hand2=new ArrayList<Card>();
for(int j=0;j<4;j++) {
for(int i=0;i<getHand().size();i++) {
Card get=getCard(i);
int color=get.getColor();
if(color==j) {
hand2.add(get);
}
}
}
ArrayList<Card> hand3=new ArrayList<Card>();
for(int i=0;i<4;i++){
for(int j=1;j<14;j++) {
for(int k=0;k<getHand().size();k++) {
Card get=hand2.get(k);
int valu=get.getValue();
int color=get.getColor();
if(valu==j && color==i) {
hand3.add(get);
}
}
}
}
hand=hand3;
}
/*** Sorts the cards in the hand so that cards of the same value are
* grouped together. Cards with the same value are sorted by color.
* Note that aces are considered to have the lowest value, 1.
*/
public void sortByValue() {
ArrayList<Card> hand2=new ArrayList<Card>();
for(int j=1;j<14;j++) {
for(int i=0;i<getHand().size();i++) {
Card get=getCard(i);
int valu=get.getValue();
if(valu==j) {
hand2.add(get);
}
}
}
ArrayList<Card> hand3=new ArrayList<Card>();
for(int i=1;i<14;i++){
for(int j=0;j<4;j++) {
for(int k=0;k<getHand().size();k++) {
Card get=hand2.get(k);
int valu=get.getValue();
int color=get.getColor();
if(valu==i && color==j) {
hand3.add(get);
}
}
}
}
hand=hand3;
}
}// end class Player