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> {
|
|
|
|
private ArrayList<T> cardDeck;
|
|
|
|
|
2020-03-03 04:18:07 +01:00
|
|
|
|
2020-03-03 04:46:03 +01:00
|
|
|
/**
|
2020-03-03 14:13:11 +01:00
|
|
|
* Initilazes the deck with cards
|
|
|
|
* @param cardList list of cards
|
2020-03-03 04:46:03 +01:00
|
|
|
*/
|
2020-03-03 14:13:11 +01:00
|
|
|
public Deck (ArrayList<T> cardList){
|
|
|
|
this.cardDeck = cardList;
|
2020-03-03 04:18:07 +01:00
|
|
|
}
|
|
|
|
|
2020-03-03 14:13:11 +01:00
|
|
|
|
2020-03-03 04:46:03 +01:00
|
|
|
/**
|
|
|
|
* This method shuffles the cards in the deck so that they are in a random order
|
|
|
|
*/
|
2020-03-03 14:13:11 +01:00
|
|
|
|
|
|
|
@Override
|
2020-03-03 04:18:07 +01:00
|
|
|
public void shuffle() {
|
|
|
|
Random randomNumber = new Random();
|
2020-03-03 14:13:11 +01:00
|
|
|
|
2020-03-03 04:18:07 +01:00
|
|
|
for (int i = cardDeck.size() - 1; i > 0; i--) {
|
|
|
|
int index = randomNumber.nextInt(i);
|
|
|
|
|
2020-03-03 14:13:11 +01:00
|
|
|
T CardIndex = cardDeck.get(index);
|
2020-03-03 04:18:07 +01:00
|
|
|
cardDeck.add(index, cardDeck.get(i));
|
|
|
|
cardDeck.remove(index+1);
|
|
|
|
cardDeck.add(i, CardIndex);
|
|
|
|
cardDeck.remove(i+1);
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
2020-03-03 04:46:03 +01:00
|
|
|
|
|
|
|
/**
|
2020-03-03 14:13:11 +01:00
|
|
|
* draws a card form the other deck and adds it to the current deck
|
|
|
|
* @param other The deck to draw the card from
|
2020-03-03 04:46:03 +01:00
|
|
|
*/
|
2020-03-03 14:13:11 +01:00
|
|
|
@Override
|
|
|
|
public void draw(IDeck<T> other){
|
|
|
|
Deck<T> otherDeck = (Deck) other;
|
|
|
|
cardDeck.add(otherDeck.cardDeck.get(0));
|
|
|
|
otherDeck.cardDeck.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
|
|
|
* draws n cards from the other deck and adds them to the current deck
|
|
|
|
* @param other The other deck to draw from
|
|
|
|
* @param n The number of cards to draw
|
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) {
|
|
|
|
Deck<T> otherDeck = (Deck) other;
|
|
|
|
if(n<1||n>otherDeck.size()){
|
|
|
|
throw new IllegalArgumentException("n cant be below 1 or over the size of the other card deck");
|
2020-03-03 04:33:58 +01:00
|
|
|
}
|
2020-03-03 14:13:11 +01:00
|
|
|
else {
|
|
|
|
for (int i=0; i<n;i++){
|
|
|
|
cardDeck.add(otherDeck.cardDeck.get(0));
|
|
|
|
otherDeck.cardDeck.remove(0);
|
2020-03-03 04:33:58 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-03 04:18:07 +01:00
|
|
|
|
2020-03-03 04:46:03 +01:00
|
|
|
/**
|
2020-03-03 14:13:11 +01:00
|
|
|
* emptys the current deck of cards and adds the cards into the other card deck
|
|
|
|
* @param other The deck to move this deck's cards into
|
2020-03-03 04:46:03 +01:00
|
|
|
*/
|
2020-03-03 14:13:11 +01:00
|
|
|
@Override
|
|
|
|
public void emptyInto(IDeck<T> other) {
|
|
|
|
Deck<T> otherDeck = (Deck) other;
|
|
|
|
for (int i=0; i<otherDeck.size();i++){
|
|
|
|
otherDeck.draw(this);
|
2020-03-03 04:18:07 +01:00
|
|
|
}
|
|
|
|
|
2020-03-03 04:33:58 +01:00
|
|
|
}
|
2020-03-03 04:18:07 +01:00
|
|
|
|
2020-03-03 04:46:03 +01:00
|
|
|
/**
|
2020-03-03 14:13:11 +01:00
|
|
|
* checks if the deck is empty
|
|
|
|
* @return true if deck is empty false otherwise
|
2020-03-03 04:46:03 +01:00
|
|
|
*/
|
2020-03-03 14:13:11 +01:00
|
|
|
@Override
|
|
|
|
public boolean isEmpty() {
|
2020-03-03 04:33:58 +01:00
|
|
|
return cardDeck.isEmpty();
|
|
|
|
}
|
2020-03-03 04:46:03 +01:00
|
|
|
|
|
|
|
/**
|
2020-03-03 14:13:11 +01:00
|
|
|
* gets the size of the current deck
|
2020-03-03 04:46:03 +01:00
|
|
|
* @return size of the deck
|
|
|
|
*/
|
2020-03-03 14:13:11 +01:00
|
|
|
@Override
|
|
|
|
public int size() {
|
2020-03-03 04:33:58 +01:00
|
|
|
return cardDeck.size();
|
|
|
|
}
|
2020-03-03 04:18:07 +01:00
|
|
|
|
2020-03-03 14:13:11 +01:00
|
|
|
/**
|
|
|
|
* gets the list of cards inn this deck
|
|
|
|
* @return list of cards inn the deck
|
|
|
|
*/
|
|
|
|
@Override
|
|
|
|
public List<T> getCards() {
|
|
|
|
ArrayList<T> returnDeck = new ArrayList();
|
|
|
|
for (int i=0;i<cardDeck.size();i++){
|
|
|
|
returnDeck.add(cardDeck.get(i));
|
|
|
|
}
|
|
|
|
return returnDeck;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-03-03 04:18:07 +01:00
|
|
|
}
|
|
|
|
|