2020-03-03 04:18:07 +01:00
|
|
|
package inf112.fiasko.roborally.objects;
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
import java.util.Random;
|
|
|
|
|
2020-03-03 04:46:03 +01:00
|
|
|
/**
|
|
|
|
* This class represents a deck of cards
|
|
|
|
*/
|
2020-03-03 04:18:07 +01:00
|
|
|
public class Deck {
|
|
|
|
private ArrayList<ProgrammingCard> cardDeck;
|
|
|
|
|
2020-03-03 04:46:03 +01:00
|
|
|
/**
|
|
|
|
* Initalizes the card deck.
|
|
|
|
* @param cardDeck a list of starting cards.
|
|
|
|
*/
|
2020-03-03 04:18:07 +01:00
|
|
|
public Deck(ArrayList<ProgrammingCard> cardDeck){
|
|
|
|
this.cardDeck=cardDeck;
|
|
|
|
}
|
|
|
|
|
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 04:18:07 +01:00
|
|
|
public void shuffle() {
|
|
|
|
Random randomNumber = new Random();
|
|
|
|
for (int i = cardDeck.size() - 1; i > 0; i--) {
|
|
|
|
int index = randomNumber.nextInt(i);
|
|
|
|
|
|
|
|
ProgrammingCard CardIndex = cardDeck.get(index);
|
|
|
|
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
|
|
|
|
|
|
|
/**
|
|
|
|
* draws the first card in the card deck
|
|
|
|
* @return first card in the card deck list
|
|
|
|
*/
|
2020-03-03 04:18:07 +01:00
|
|
|
public ProgrammingCard drawCard(){
|
|
|
|
ProgrammingCard draw = cardDeck.get(0);
|
|
|
|
cardDeck.remove(0);
|
|
|
|
return draw;
|
|
|
|
}
|
2020-03-03 04:46:03 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* draws n cards for another card deck and adds it to this card deck
|
|
|
|
* @param n number of cards you want to draw from the other deck
|
|
|
|
* @param otherDeck the other card deck
|
|
|
|
*/
|
2020-03-03 04:33:58 +01:00
|
|
|
public void drawNCardsFromOtherDeck(int n, Deck otherDeck){
|
2020-03-03 04:46:03 +01:00
|
|
|
if (n<1 || n>otherDeck.getCard().size()){
|
|
|
|
throw new IllegalArgumentException("cant draw negativ cards or more cards then are in the other deck");
|
2020-03-03 04:33:58 +01:00
|
|
|
}
|
|
|
|
else{
|
|
|
|
for (int i=0;i<n;i++){
|
|
|
|
cardDeck.add(otherDeck.drawCard());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-03-03 04:18:07 +01:00
|
|
|
|
2020-03-03 04:46:03 +01:00
|
|
|
/**
|
|
|
|
* draws the first card from the other deck and adds it to this deck
|
|
|
|
* @param otherCardDeck the other card deck
|
|
|
|
*/
|
2020-03-03 04:18:07 +01:00
|
|
|
public void drawCardOtherDeck(Deck otherCardDeck){
|
|
|
|
cardDeck.add(otherCardDeck.drawCard());
|
|
|
|
}
|
|
|
|
|
2020-03-03 04:46:03 +01:00
|
|
|
/**
|
|
|
|
* draws all the cards from this card deck
|
|
|
|
* @return returns a list of all the cards from this deck
|
|
|
|
*/
|
2020-03-03 04:18:07 +01:00
|
|
|
public ArrayList<ProgrammingCard> drawAllCard(){
|
|
|
|
ArrayList<ProgrammingCard> allCards= new ArrayList<>();
|
2020-03-03 04:33:58 +01:00
|
|
|
int cardDeckSize = cardDeck.size();
|
|
|
|
for (int i=0;i<cardDeckSize;i++){
|
|
|
|
allCards.add(cardDeck.get((cardDeckSize-1)-i));
|
2020-03-03 04:18:07 +01:00
|
|
|
}
|
2020-03-03 04:33:58 +01:00
|
|
|
for (int i=0; i<cardDeckSize;i++){
|
2020-03-03 04:18:07 +01:00
|
|
|
cardDeck.remove(0);
|
|
|
|
}
|
|
|
|
return allCards;
|
|
|
|
}
|
|
|
|
|
2020-03-03 04:46:03 +01:00
|
|
|
/**
|
|
|
|
* gives a list of all the cards inn this deck
|
|
|
|
* @return a list of all the cards inn this deck
|
|
|
|
*/
|
|
|
|
public ArrayList<ProgrammingCard> getCard(){
|
2020-03-03 04:33:58 +01:00
|
|
|
return cardDeck;
|
|
|
|
}
|
2020-03-03 04:18:07 +01:00
|
|
|
|
2020-03-03 04:46:03 +01:00
|
|
|
/**
|
|
|
|
* checks if this deck is empty
|
|
|
|
* @return true if empty false otherwise
|
|
|
|
*/
|
2020-03-03 04:33:58 +01:00
|
|
|
public Boolean isEmpty(){
|
|
|
|
return cardDeck.isEmpty();
|
|
|
|
}
|
2020-03-03 04:46:03 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* gets the size of this deck
|
|
|
|
* @return size of the deck
|
|
|
|
*/
|
2020-03-03 04:33:58 +01:00
|
|
|
public int size(){
|
|
|
|
return cardDeck.size();
|
|
|
|
}
|
2020-03-03 04:18:07 +01:00
|
|
|
|
|
|
|
}
|
|
|
|
|