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;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
/**
* This class represents a deck of cards
*/
public class Deck {
private ArrayList<ProgrammingCard> cardDeck;
public abstract class Deck<T> implements IDeck<T> {
private ArrayList<T> cardDeck;
/**
* Initalizes the card deck.
* @param cardDeck a list of starting cards.
* Initilazes the deck with cards
* @param cardList list of cards
*/
public Deck(ArrayList<ProgrammingCard> cardDeck){
this.cardDeck=cardDeck;
public Deck (ArrayList<T> cardList){
this.cardDeck = cardList;
}
/**
* This method shuffles the cards in the deck so that they are in a random order
*/
@Override
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);
for (int i = cardDeck.size() - 1; i > 0; i--) {
int randomIndex = randomNumber.nextInt(i);
T CardRandomIndex = cardDeck.get(randomIndex);
cardDeck.add(randomIndex, cardDeck.get(i));
cardDeck.remove(randomIndex+1);
cardDeck.add(i, CardRandomIndex);
cardDeck.remove(i+1);
}
}
/**
* draws the first card in the card deck
* @return first card in the card deck list
* draws a card form the other deck and adds it to the current deck
* @param other The deck to draw the card from
*/
public ProgrammingCard drawCard(){
ProgrammingCard draw = cardDeck.get(0);
cardDeck.remove(0);
return draw;
@Override
public void draw(IDeck<T> other){
Deck<T> otherDeck = (Deck) other;
cardDeck.add(otherDeck.cardDeck.get(0));
otherDeck.cardDeck.remove(0);
}
/**
* 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
* 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
*/
public void drawNCardsFromOtherDeck(int n, Deck otherDeck){
if (n<1 || n>otherDeck.getCard().size()){
throw new IllegalArgumentException("cant draw negativ cards or more cards then are in the other deck");
@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");
}
else{
for (int i=0;i<n;i++){
cardDeck.add(otherDeck.drawCard());
else {
for (int i=0; i<n;i++){
cardDeck.add(otherDeck.cardDeck.get(0));
otherDeck.cardDeck.remove(0);
}
}
}
/**
* draws the first card from the other deck and adds it to this deck
* @param otherCardDeck the other card deck
* 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
*/
public void drawCardOtherDeck(Deck otherCardDeck){
cardDeck.add(otherCardDeck.drawCard());
@Override
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
* @return returns a list of all the cards from this deck
* checks if the deck is empty
* @return true if deck is empty false otherwise
*/
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
* @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(){
@Override
public boolean isEmpty() {
return cardDeck.isEmpty();
}
/**
* gets the size of this deck
* gets the size of the current deck
* @return size of the deck
*/
public int size(){
@Override
public int 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);
}
}