2020-03-03 04:18:07 +01:00
|
|
|
package inf112.fiasko.roborally.objects;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
2020-03-03 14:13:11 +01:00
|
|
|
import java.util.List;
|
2020-03-03 04:18:07 +01:00
|
|
|
import java.util.Random;
|
|
|
|
|
2020-03-03 04:46:03 +01:00
|
|
|
/**
|
|
|
|
* This class represents a deck of cards
|
|
|
|
*/
|
2020-03-03 14:13:11 +01:00
|
|
|
public abstract class Deck<T> implements IDeck<T> {
|
2020-03-03 17:25:18 +01:00
|
|
|
private final ArrayList<T> cardList;
|
2020-03-03 04:18:07 +01:00
|
|
|
|
2020-03-03 04:46:03 +01:00
|
|
|
/**
|
2020-03-03 17:25:18 +01:00
|
|
|
* Initializes the deck with cards
|
2020-03-03 14:13:11 +01:00
|
|
|
* @param cardList list of cards
|
2020-03-03 04:46:03 +01:00
|
|
|
*/
|
2020-03-03 17:25:18 +01:00
|
|
|
public Deck (ArrayList<T> cardList) {
|
|
|
|
this.cardList = cardList;
|
2020-03-03 04:18:07 +01:00
|
|
|
}
|
|
|
|
|
2020-03-03 14:13:11 +01:00
|
|
|
@Override
|
2020-03-03 04:18:07 +01:00
|
|
|
public void shuffle() {
|
2020-03-03 17:25:18 +01:00
|
|
|
Random randomGenerator = new Random();
|
|
|
|
int deckSize = cardList.size();
|
|
|
|
int halfDeckSize = deckSize / 2;
|
|
|
|
int timesToShuffle = 30 * deckSize;
|
|
|
|
for (int i = 0; i < timesToShuffle; i++) {
|
|
|
|
int oldPosition = randomGenerator.nextInt(halfDeckSize);
|
|
|
|
int newPosition = randomGenerator.nextInt(deckSize - halfDeckSize) + halfDeckSize;
|
|
|
|
cardList.add(newPosition, cardList.remove(oldPosition));
|
2020-03-03 04:18:07 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-03 04:46:03 +01:00
|
|
|
|
2020-03-03 14:13:11 +01:00
|
|
|
@Override
|
2020-03-03 17:25:18 +01:00
|
|
|
public void draw(IDeck<T> other) {
|
|
|
|
Deck<T> otherDeck = (Deck<T>) other;
|
|
|
|
cardList.add(otherDeck.cardList.remove(0));
|
2020-03-03 04:18:07 +01:00
|
|
|
}
|
2020-03-03 04:46:03 +01:00
|
|
|
|
2020-03-03 14:13:11 +01:00
|
|
|
@Override
|
|
|
|
public void draw(IDeck<T> other, int n) {
|
2020-03-03 17:25:18 +01:00
|
|
|
Deck<T> otherDeck = (Deck<T>) other;
|
|
|
|
if (n < 1 || n > otherDeck.size()) {
|
|
|
|
throw new IllegalArgumentException("n can't be below 1 or over the size of the other card deck");
|
2020-03-03 04:33:58 +01:00
|
|
|
}
|
2020-03-03 17:25:18 +01:00
|
|
|
for (int i = 0; i < n; i++) {
|
|
|
|
draw(other);
|
2020-03-03 04:33:58 +01:00
|
|
|
}
|
|
|
|
}
|
2020-03-03 04:18:07 +01:00
|
|
|
|
2020-03-03 14:13:11 +01:00
|
|
|
@Override
|
|
|
|
public void emptyInto(IDeck<T> other) {
|
2020-03-03 17:25:18 +01:00
|
|
|
Deck<T> otherDeck = (Deck<T>) other;
|
|
|
|
otherDeck.draw(this, this.size());
|
2020-03-03 04:33:58 +01:00
|
|
|
}
|
2020-03-03 04:18:07 +01:00
|
|
|
|
2020-03-03 14:13:11 +01:00
|
|
|
@Override
|
|
|
|
public boolean isEmpty() {
|
2020-03-03 17:25:18 +01:00
|
|
|
return cardList.isEmpty();
|
2020-03-03 04:33:58 +01:00
|
|
|
}
|
2020-03-03 04:46:03 +01:00
|
|
|
|
2020-03-03 14:13:11 +01:00
|
|
|
@Override
|
|
|
|
public int size() {
|
2020-03-03 17:25:18 +01:00
|
|
|
return cardList.size();
|
2020-03-03 04:33:58 +01:00
|
|
|
}
|
2020-03-03 04:18:07 +01:00
|
|
|
|
2020-03-03 14:13:11 +01:00
|
|
|
@Override
|
|
|
|
public List<T> getCards() {
|
2020-03-03 17:25:18 +01:00
|
|
|
return new ArrayList<>(cardList);
|
2020-03-03 14:13:11 +01:00
|
|
|
}
|
2020-03-03 04:18:07 +01:00
|
|
|
}
|
|
|
|
|