This commit is contained in:
Steinar Aalstad Lillesund 2020-03-03 16:02:04 +01:00
commit 985676c6ea
3 changed files with 193 additions and 59 deletions

View File

@ -1,112 +1,122 @@
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--) {
int index = randomNumber.nextInt(i);
ProgrammingCard CardIndex = cardDeck.get(index); for (int i = cardDeck.size() - 1; i > 0; i--) {
cardDeck.add(index, cardDeck.get(i)); int randomIndex = randomNumber.nextInt(i);
cardDeck.remove(index+1);
cardDeck.add(i, CardIndex); T CardRandomIndex = cardDeck.get(randomIndex);
cardDeck.add(randomIndex, cardDeck.get(i));
cardDeck.remove(randomIndex+1);
cardDeck.add(i, CardRandomIndex);
cardDeck.remove(i+1); cardDeck.remove(i+1);
} }
} }
/** /**
* 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;
int size = otherDeck.size();
for (int i=0; i<size;i++){
otherDeck.draw(this);
}
} }
/** /**
* draws all the cards from this card deck * checks if the deck is empty
* @return returns a list of all the cards from this deck * @return true if deck is empty false otherwise
*/ */
public ArrayList<ProgrammingCard> drawAllCard(){ @Override
ArrayList<ProgrammingCard> allCards= new ArrayList<>(); public boolean isEmpty() {
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
* @return a list of all the cards inn this deck
*/
public ArrayList<ProgrammingCard> getCard(){
return cardDeck;
}
/**
* 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
*/ */
@Override
public int size() { 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 (T card:cardDeck){
returnDeck.add(card);
}
return returnDeck;
}
} }

View File

@ -0,0 +1,15 @@
package inf112.fiasko.roborally.objects;
import java.util.ArrayList;
public class PlayerDeck<ProgrammingCard> extends Deck<ProgrammingCard> {
/**
* initalizes the PlayerDeck with a list of cards
* @param cardlist list of programing cards
*/
public PlayerDeck(ArrayList<ProgrammingCard> cardlist) {
super(cardlist);
}
}

View File

@ -0,0 +1,109 @@
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 testSize(){
assertEquals(3,testDeck.size());
testDeck.emptyInto(testDeck2);
assertEquals(0,testDeck.size());
}
@Test
public void testDrawCard(){
assertEquals(3,testDeck.size());
assertEquals(3,testDeck2.size());
testDeck.draw(testDeck2);
assertEquals(4,testDeck.size());
assertEquals(2,testDeck2.size());
}
@Test
public void testDrawMultipulCards(){
assertEquals(3,testDeck.size());
assertEquals(3,testDeck2.size());
testDeck.draw(testDeck2, 3);
assertEquals(6,testDeck.size());
assertEquals(0,testDeck2.size());
}
@Test
public void testEmptyInto(){
assertEquals(3,testDeck.size());
assertEquals(3,testDeck2.size());
testDeck.emptyInto(testDeck2);
assertEquals(0,testDeck.size());
assertEquals(6,testDeck2.size());
}
@Test
public void testIsEmpty(){
assertEquals(false,testDeck.isEmpty());
testDeck.emptyInto(testDeck2);
assertEquals(true,testDeck.isEmpty());
}
@Test
public void testGetCards(){
testDeck2.emptyInto(testDeck);
assertEquals(programmingCard1,testDeck.getCards().get(0));
assertEquals(programmingCard3,testDeck.getCards().get(2));
assertEquals(programmingCard6,testDeck.getCards().get(5));
}
@Test
public void testshuffle(){
ProgrammingCard card1 =(ProgrammingCard) testDeck.getCards().get(0);
int noe = card1.getValue();
ProgrammingCard card2 =(ProgrammingCard) testDeck.getCards().get(1);
int noe2 = card2.getValue();
ProgrammingCard card3 =(ProgrammingCard) testDeck.getCards().get(2);
int noe3 = card3.getValue();
System.out.println(noe);
System.out.println(noe2);
System.out.println(noe3);
testDeck.shuffle();
ProgrammingCard scard1 =(ProgrammingCard) testDeck.getCards().get(0);
int snoe = scard1.getValue();
ProgrammingCard scard2 =(ProgrammingCard) testDeck.getCards().get(1);
int snoe2 = scard2.getValue();
ProgrammingCard scard3 =(ProgrammingCard) testDeck.getCards().get(2);
int snoe3 = scard3.getValue();
System.out.println(snoe);
System.out.println(snoe2);
System.out.println(snoe3);
}
}