]>
wirehaze git hosting - solitaire.git/blob - src/solitaire/Depot.java
3 import java
.util
.LinkedList
;
5 import java
.util
.Optional
;
6 import java
.util
.stream
.Collectors
;
9 private static boolean isValidNextCard(Card lastCard
, Card nextCard
) {
10 if (nextCard
.getSuit().isRed() == lastCard
.getSuit().isRed())
13 return lastCard
.getRank().isSuccessorOf(nextCard
.getRank());
16 private static boolean isValidRun(List
<Card
> run
) {
19 for (Card next
: run
) {
20 if (next
.isHidden() || (prev
!= null && !isValidNextCard(prev
, next
)))
29 private final List
<Card
> cards
;
31 Depot(List
<Card
> cards
) {
33 throw new IllegalArgumentException("Depot should be initialized with at least one card");
35 this.cards
= new LinkedList
<>(cards
);
36 this.cards
.getLast().reveal();
40 public String
toString() {
44 return cards
.stream().map(String
::valueOf
).collect(Collectors
.joining("\n"));
47 private boolean isValidPlacement(Card card
) {
49 return card
.getRank() == Rank
.KING
;
51 return isValidNextCard(cards
.getLast(), card
);
54 boolean add(Card card
) {
55 return add(List
.of(card
));
58 boolean add(List
<Card
> run
) {
60 * no invalid run can be generated by the game, so we only need to check
63 if (!isValidPlacement(run
.getFirst()))
70 Optional
<List
<Card
>> remove(int size
) {
72 throw new IllegalArgumentException("Size must be a positive integer");
74 if (cards
.size() < size
)
75 return Optional
.empty();
77 List
<Card
> slice
= cards
.subList(cards
.size() - size
, cards
.size());
79 if (!isValidRun(slice
))
80 return Optional
.empty();
82 List
<Card
> run
= List
.copyOf(slice
);
86 cards
.getLast().reveal();
88 return Optional
.of(run
);