forked from matt-l-morgan/WhistCardGame
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPlayer.java
168 lines (145 loc) · 3.38 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
162
163
164
165
166
167
168
import java.util.*;
/**
* Created by mattmorgan on 2/3/16.
*/
/**
* Represents a player in a game
*/
public class Player {
/**
* represents a list of cards in a player's hand
*/
public List<Card> hand;
/**
* the number of wins the player currently has
*/
private int wins;
/**
* adds a card to the player's hand
* @param card the card that will be added.
*/
public void addCard(Card card){
this.hand.add(card);
}
/**
* the constructor for a player
* @param hand the cards a player has
*/
public Player(List<Card> hand){
this.hand = hand;
this.wins = 0;
}
/**
* a zero constructor for a player
*/
public Player(){
this.hand = new ArrayList<Card>();
this.wins = 0;
}
/**
* sorts a hand using the alphabetical card comparator
*/
public void sortHand(){
Collections.sort(hand, new CardComparator());
}
/**
* prints a player's hand
* @return a string is the cards in a player's hand.
*/
public String printHand(){
String stringhand = "";
for(int i = 0; i < hand.size(); i++) {
if ((i == hand.size() - 1) && (i == 0)) {
stringhand = stringhand + hand.get(i).toString();
}
else if (i == hand.size() - 1){
stringhand = stringhand + " " + hand.get(i).toString();
}
else if (i == 0) {
stringhand = stringhand + hand.get(i).toString() + ",";
}
else {
stringhand = stringhand + " " + hand.get(i).toString() + ",";
}
}
return stringhand;
}
/**
* returns the number of wins a player has
* @return wins a player has
*/
public int getWins() {
return this.wins;
}
/**
* adds a win to a player who won
*/
public void addWin() {
this.wins = this.wins + 1;
}
/**
* gets the card from the player's have and the given index
* @param cardIndex the index of the card in the player's hand
* @return the card at this index
*/
public Card getPlayersCard(int cardIndex) {
return this.hand.get(cardIndex);
}
/**
* give's a players hand
* @return the List</Card> the player has
*/
public List<Card> giveHand() {
return this.hand;
}
/**
* removes a player's card from his hand
* @param card the card to be removed
*/
public void removePlayersCard(Card card) {
this.hand.remove(card);
}
/**
* decides who had more wins
* @param other_p the other person we are comaparing
* @return the player iwth more wins
*/
public Player hasMoreWins(Player other_p){
if (this.wins > other_p.wins) {
return this;
}
else {
return other_p;
}
}
/**
* decides if a player has cards
* @return true if the play has at least one card in hand, false otherwise.
*/
public boolean hasCards(){
return this.hand.size() > 0;
}
/**
* decides if this player has the given card
* @param card the card that is being searched for
* @return true if the player has this card in their hand, false otherwise
*/
public boolean hasCard(Card card) {
if (this.hand.contains(card)) {
return true;
}
else {
return false;
}
}
public boolean hasCardOfSuit(Card.Suit suit) {
boolean has_suit = false;
for (int i = 0; i < hand.size(); i++) {
Card curr_card = hand.get(i);
if (curr_card.getSuit() == suit) {
has_suit = true;
}
}
return has_suit;
}
}