Repository navigation
Expand file tree
/
Copy pathPlayer.java
More file actions
114 lines (87 loc) · 3.36 KB
/
Copy pathPlayer.java
File metadata and controls
114 lines (87 loc) · 3.36 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
package model.player;
import java.util.ArrayList;
import exception.GameException;
import exception.InvalidCardException;
import exception.InvalidMarbleException;
import model.Colour;
import model.card.Card;
@SuppressWarnings("unused")
public class Player {
private final String name;
private final Colour colour;
private ArrayList<Card> hand;
private final ArrayList<Marble> marbles;
private Card selectedCard;
private final ArrayList<Marble> selectedMarbles;
public Player(String name, Colour colour) {
this.name = name;
this.colour = colour;
this.hand = new ArrayList<>();
this.selectedMarbles = new ArrayList<>();
this.marbles = new ArrayList<>();
for (int i = 0; i < 4; i++) {
this.marbles.add(new Marble(colour));
}
//default value
this.selectedCard = null;
}
public String getName() {
return name;
}
public Colour getColour() {
return colour;
}
public ArrayList<Card> getHand() {
return hand;
}
public void setHand(ArrayList<Card> hand) {
this.hand = hand;
}
public ArrayList<Marble> getMarbles() {
return marbles;
}
public Card getSelectedCard() {
return selectedCard;
}
public void regainMarble(Marble marble) {
this.marbles.add(marble);
}
public Marble getOneMarble() {
if(marbles.isEmpty())
return null;
return this.marbles.get(0);
}
public void selectCard(Card card) throws InvalidCardException {
if (!this.hand.contains(card))
throw new InvalidCardException("Card not in hand.");
this.selectedCard = card;
}
public void selectMarble(Marble marble) throws InvalidMarbleException {
if (!this.selectedMarbles.contains(marble)) {
if(this.selectedMarbles.size() > 1)
throw new InvalidMarbleException("Cannot select more than 2 marbles.");
selectedMarbles.add(marble);
}
}
public void deselectAll() {
this.selectedCard = null;
this.selectedMarbles.clear();
}
public void play() throws GameException {
if(selectedCard == null)
throw new InvalidCardException("Must select a card to play.");
// Special case for Queen and Ten cards - they can be played without marbles
boolean isQueenOrTen = selectedCard.getName().contains("Queen") || selectedCard.getName().contains("Ten");
// Only validate marble selection if not a Queen/Ten card being played without marbles
if(!(isQueenOrTen && selectedMarbles.isEmpty())) {
if(!this.selectedCard.validateMarbleSize(this.selectedMarbles))
throw new InvalidMarbleException("Invalid number of marbles selected for " + selectedCard.getName() + ".");
if(!this.selectedCard.validateMarbleColours(this.selectedMarbles))
throw new InvalidMarbleException("Invalid marble colours selected for " + selectedCard.getName() + ".");
}
this.selectedCard.act(this.selectedMarbles);
}
public ArrayList<Marble> getSelectedMarbles() {
return selectedMarbles;
}
}