mirror of
https://github.com/inf112-v20/Fiasko.git
synced 2025-02-01 07:39:35 +01:00
fixet masse ting i Deck classen
This commit is contained in:
parent
58c15db1b1
commit
b8c9c87b39
@ -1,31 +1,37 @@
|
|||||||
package inf112.fiasko.roborally.objects;
|
package inf112.fiasko.roborally.objects;
|
||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
import java.util.Random;
|
import java.util.Random;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This class represents a deck of cards
|
* This class represents a deck of cards
|
||||||
*/
|
*/
|
||||||
public class Deck {
|
public abstract class Deck<T> implements IDeck<T> {
|
||||||
private ArrayList<ProgrammingCard> cardDeck;
|
private ArrayList<T> cardDeck;
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Initalizes the card deck.
|
* Initilazes the deck with cards
|
||||||
* @param cardDeck a list of starting cards.
|
* @param cardList list of cards
|
||||||
*/
|
*/
|
||||||
public Deck(ArrayList<ProgrammingCard> cardDeck){
|
public Deck (ArrayList<T> cardList){
|
||||||
this.cardDeck=cardDeck;
|
this.cardDeck = cardList;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* This method shuffles the cards in the deck so that they are in a random order
|
* This method shuffles the cards in the deck so that they are in a random order
|
||||||
*/
|
*/
|
||||||
|
|
||||||
|
@Override
|
||||||
public void shuffle() {
|
public void shuffle() {
|
||||||
Random randomNumber = new Random();
|
Random randomNumber = new Random();
|
||||||
|
|
||||||
for (int i = cardDeck.size() - 1; i > 0; i--) {
|
for (int i = cardDeck.size() - 1; i > 0; i--) {
|
||||||
int index = randomNumber.nextInt(i);
|
int index = randomNumber.nextInt(i);
|
||||||
|
|
||||||
ProgrammingCard CardIndex = cardDeck.get(index);
|
T CardIndex = cardDeck.get(index);
|
||||||
cardDeck.add(index, cardDeck.get(i));
|
cardDeck.add(index, cardDeck.get(i));
|
||||||
cardDeck.remove(index+1);
|
cardDeck.remove(index+1);
|
||||||
cardDeck.add(i, CardIndex);
|
cardDeck.add(i, CardIndex);
|
||||||
@ -35,78 +41,79 @@ public class Deck {
|
|||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* draws the first card in the card deck
|
* draws a card form the other deck and adds it to the current deck
|
||||||
* @return first card in the card deck list
|
* @param other The deck to draw the card from
|
||||||
*/
|
*/
|
||||||
public ProgrammingCard drawCard(){
|
@Override
|
||||||
ProgrammingCard draw = cardDeck.get(0);
|
public void draw(IDeck<T> other){
|
||||||
cardDeck.remove(0);
|
Deck<T> otherDeck = (Deck) other;
|
||||||
return draw;
|
cardDeck.add(otherDeck.cardDeck.get(0));
|
||||||
|
otherDeck.cardDeck.remove(0);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* draws n cards for another card deck and adds it to this card deck
|
* draws n cards from the other deck and adds them to the current deck
|
||||||
* @param n number of cards you want to draw from the other deck
|
* @param other The other deck to draw from
|
||||||
* @param otherDeck the other card deck
|
* @param n The number of cards to draw
|
||||||
*/
|
*/
|
||||||
public void drawNCardsFromOtherDeck(int n, Deck otherDeck){
|
@Override
|
||||||
if (n<1 || n>otherDeck.getCard().size()){
|
public void draw(IDeck<T> other, int n) {
|
||||||
throw new IllegalArgumentException("cant draw negativ cards or more cards then are in the other deck");
|
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");
|
||||||
}
|
}
|
||||||
else{
|
else {
|
||||||
for (int i=0;i<n;i++){
|
for (int i=0; i<n;i++){
|
||||||
cardDeck.add(otherDeck.drawCard());
|
cardDeck.add(otherDeck.cardDeck.get(0));
|
||||||
|
otherDeck.cardDeck.remove(0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* draws the first card from the other deck and adds it to this deck
|
* emptys the current deck of cards and adds the cards into the other card deck
|
||||||
* @param otherCardDeck the other card deck
|
* @param other The deck to move this deck's cards into
|
||||||
*/
|
*/
|
||||||
public void drawCardOtherDeck(Deck otherCardDeck){
|
@Override
|
||||||
cardDeck.add(otherCardDeck.drawCard());
|
public void emptyInto(IDeck<T> other) {
|
||||||
}
|
Deck<T> otherDeck = (Deck) other;
|
||||||
|
for (int i=0; i<otherDeck.size();i++){
|
||||||
/**
|
otherDeck.draw(this);
|
||||||
* draws all the cards from this card deck
|
|
||||||
* @return returns a list of all the cards from this deck
|
|
||||||
*/
|
|
||||||
public ArrayList<ProgrammingCard> drawAllCard(){
|
|
||||||
ArrayList<ProgrammingCard> allCards= new ArrayList<>();
|
|
||||||
int cardDeckSize = cardDeck.size();
|
|
||||||
for (int i=0;i<cardDeckSize;i++){
|
|
||||||
allCards.add(cardDeck.get((cardDeckSize-1)-i));
|
|
||||||
}
|
}
|
||||||
for (int i=0; i<cardDeckSize;i++){
|
|
||||||
cardDeck.remove(0);
|
|
||||||
}
|
|
||||||
return allCards;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gives a list of all the cards inn this deck
|
* checks if the deck is empty
|
||||||
* @return a list of all the cards inn this deck
|
* @return true if deck is empty false otherwise
|
||||||
*/
|
*/
|
||||||
public ArrayList<ProgrammingCard> getCard(){
|
@Override
|
||||||
return cardDeck;
|
public boolean isEmpty() {
|
||||||
}
|
|
||||||
|
|
||||||
/**
|
|
||||||
* checks if this deck is empty
|
|
||||||
* @return true if empty false otherwise
|
|
||||||
*/
|
|
||||||
public Boolean isEmpty(){
|
|
||||||
return cardDeck.isEmpty();
|
return cardDeck.isEmpty();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* gets the size of this deck
|
* gets the size of the current deck
|
||||||
* @return size of the deck
|
* @return size of the deck
|
||||||
*/
|
*/
|
||||||
public int size(){
|
@Override
|
||||||
|
public int size() {
|
||||||
return cardDeck.size();
|
return cardDeck.size();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 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;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -0,0 +1,10 @@
|
|||||||
|
package inf112.fiasko.roborally.objects;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class PlayerDeck<ProgrammingCard> extends Deck<ProgrammingCard> {
|
||||||
|
|
||||||
|
public PlayerDeck(ArrayList<ProgrammingCard> cardlist) {
|
||||||
|
super(cardlist);
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,42 @@
|
|||||||
|
package inf112.fiasko.roborally.objects;
|
||||||
|
|
||||||
|
import inf112.fiasko.roborally.element_properties.Action;
|
||||||
|
import org.junit.Before;
|
||||||
|
import org.junit.Test;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
|
||||||
|
public class TestPlayerDeck {
|
||||||
|
private ProgrammingCard programmingCard1 = new ProgrammingCard(5, Action.MOVE_1);
|
||||||
|
private ProgrammingCard programmingCard2 = new ProgrammingCard(6, Action.MOVE_2);
|
||||||
|
private ProgrammingCard programmingCard3 = new ProgrammingCard(7, Action.MOVE_3);
|
||||||
|
private ProgrammingCard programmingCard4 = new ProgrammingCard(55, Action.MOVE_1);
|
||||||
|
private ProgrammingCard programmingCard5 = new ProgrammingCard(66, Action.MOVE_2);
|
||||||
|
private ProgrammingCard programmingCard6 = new ProgrammingCard(756, Action.MOVE_3);
|
||||||
|
private ArrayList<ProgrammingCard> cardlist = new ArrayList();
|
||||||
|
private ArrayList<ProgrammingCard> cardlist2 = new ArrayList();
|
||||||
|
private PlayerDeck testDeck;
|
||||||
|
private PlayerDeck testDeck2;
|
||||||
|
@Before
|
||||||
|
public void setUp() {
|
||||||
|
cardlist.add(programmingCard1);
|
||||||
|
cardlist.add(programmingCard2);
|
||||||
|
cardlist.add(programmingCard3);
|
||||||
|
cardlist2.add(programmingCard4);
|
||||||
|
cardlist2.add(programmingCard5);
|
||||||
|
cardlist2.add(programmingCard6);
|
||||||
|
testDeck = new PlayerDeck(cardlist);
|
||||||
|
testDeck2 = new PlayerDeck(cardlist2);
|
||||||
|
}
|
||||||
|
@Test
|
||||||
|
public void testDrawCard(){
|
||||||
|
assertEquals(3,testDeck.size());
|
||||||
|
assertEquals(3,testDeck2.size());
|
||||||
|
testDeck.draw(testDeck2);
|
||||||
|
assertEquals(4,testDeck.size());
|
||||||
|
assertEquals(2,testDeck2.size());
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user