Fikser en del småfeil i Deck, ProgrammingCardDeck og tester

This commit is contained in:
Kristian Knarvik 2020-03-03 17:57:27 +01:00
parent 9f7ebb59b1
commit 315cbb2590
3 changed files with 43 additions and 46 deletions

View File

@ -8,13 +8,13 @@ import java.util.Random;
* This class represents a deck of cards * This class represents a deck of cards
*/ */
public abstract class Deck<T> implements IDeck<T> { public abstract class Deck<T> implements IDeck<T> {
private final ArrayList<T> cardList; private final List<T> cardList;
/** /**
* Initializes the deck with cards * Initializes the deck with cards
* @param cardList list of cards * @param cardList list of cards
*/ */
public Deck (ArrayList<T> cardList) { public Deck (List<T> cardList) {
this.cardList = new ArrayList<>(cardList); this.cardList = new ArrayList<>(cardList);
} }
@ -68,5 +68,14 @@ public abstract class Deck<T> implements IDeck<T> {
public List<T> getCards() { public List<T> getCards() {
return new ArrayList<>(cardList); return new ArrayList<>(cardList);
} }
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
for (T card : cardList) {
builder.append(card.toString()).append("\n");
}
return builder.toString();
}
} }

View File

@ -1,6 +1,6 @@
package inf112.fiasko.roborally.objects; package inf112.fiasko.roborally.objects;
import java.util.ArrayList; import java.util.List;
/** /**
* This class represents a deck containing programming cards * This class represents a deck containing programming cards
@ -11,7 +11,7 @@ public class ProgrammingCardDeck extends Deck<ProgrammingCard> {
* Initializes the PlayerDeck with a list of cards * Initializes the PlayerDeck with a list of cards
* @param cardList list of programing cards * @param cardList list of programing cards
*/ */
public ProgrammingCardDeck(ArrayList<ProgrammingCard> cardList) { public ProgrammingCardDeck(List<ProgrammingCard> cardList) {
super(cardList); super(cardList);
} }
} }

View File

@ -1,15 +1,18 @@
package inf112.fiasko.roborally.objects; package inf112.fiasko.roborally.objects;
import inf112.fiasko.roborally.element_properties.Action; import inf112.fiasko.roborally.element_properties.Action;
import inf112.fiasko.roborally.utility.DeckLoaderUtil;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue; import static org.junit.Assert.assertTrue;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
public class TestProgrammingCardDeck { public class ProgrammingCardDeckTest {
private ProgrammingCardDeck fullDeck;
private final ProgrammingCard programmingCard1 = new ProgrammingCard(5, Action.MOVE_1); private final ProgrammingCard programmingCard1 = new ProgrammingCard(5, Action.MOVE_1);
private final ProgrammingCard programmingCard2 = new ProgrammingCard(6, Action.MOVE_2); private final ProgrammingCard programmingCard2 = new ProgrammingCard(6, Action.MOVE_2);
private final ProgrammingCard programmingCard3 = new ProgrammingCard(7, Action.MOVE_3); private final ProgrammingCard programmingCard3 = new ProgrammingCard(7, Action.MOVE_3);
@ -22,6 +25,11 @@ public class TestProgrammingCardDeck {
private ProgrammingCardDeck testDeck2; private ProgrammingCardDeck testDeck2;
@Before @Before
public void setUp() { public void setUp() {
try {
fullDeck = DeckLoaderUtil.loadProgrammingCardsDeck();
} catch (IOException e) {
e.printStackTrace();
}
cardlist.add(programmingCard1); cardlist.add(programmingCard1);
cardlist.add(programmingCard2); cardlist.add(programmingCard2);
cardlist.add(programmingCard3); cardlist.add(programmingCard3);
@ -32,13 +40,13 @@ public class TestProgrammingCardDeck {
testDeck2 = new ProgrammingCardDeck(cardlist2); testDeck2 = new ProgrammingCardDeck(cardlist2);
} }
@Test @Test
public void testSize(){ public void testSize() {
assertEquals(3,testDeck.size()); assertEquals(3,testDeck.size());
testDeck.emptyInto(testDeck2); testDeck.emptyInto(testDeck2);
assertEquals(0,testDeck.size()); assertEquals(0,testDeck.size());
} }
@Test @Test
public void testDrawCard(){ public void testDrawCard() {
assertEquals(3,testDeck.size()); assertEquals(3,testDeck.size());
assertEquals(3,testDeck2.size()); assertEquals(3,testDeck2.size());
testDeck.draw(testDeck2); testDeck.draw(testDeck2);
@ -46,60 +54,40 @@ public class TestProgrammingCardDeck {
assertEquals(2,testDeck2.size()); assertEquals(2,testDeck2.size());
} }
@Test @Test
public void testDrawMultipulCards(){ public void testDrawMultipleCards() {
assertEquals(3,testDeck.size()); assertEquals(3, testDeck.size());
assertEquals(3,testDeck2.size()); assertEquals(3, testDeck2.size());
testDeck.draw(testDeck2, 3); testDeck.draw(testDeck2, 3);
assertEquals(6,testDeck.size()); assertEquals(6, testDeck.size());
assertEquals(0,testDeck2.size()); assertEquals(0, testDeck2.size());
} }
@Test @Test
public void testEmptyInto(){ public void testEmptyInto() {
assertEquals(3,testDeck.size()); assertEquals(3, testDeck.size());
assertEquals(3,testDeck2.size()); assertEquals(3, testDeck2.size());
testDeck.emptyInto(testDeck2); testDeck.emptyInto(testDeck2);
assertEquals(0,testDeck.size()); assertEquals(0, testDeck.size());
assertEquals(6,testDeck2.size()); assertEquals(6, testDeck2.size());
} }
@Test @Test
public void testIsEmpty(){ public void testIsEmpty() {
assertFalse(testDeck.isEmpty()); assertFalse(testDeck.isEmpty());
testDeck.emptyInto(testDeck2); testDeck.emptyInto(testDeck2);
assertTrue(testDeck.isEmpty()); assertTrue(testDeck.isEmpty());
} }
@Test @Test
public void testGetCards(){ public void testGetCards() {
testDeck2.emptyInto(testDeck); testDeck2.emptyInto(testDeck);
assertEquals(programmingCard1,testDeck.getCards().get(0)); assertEquals(programmingCard1, testDeck.getCards().get(0));
assertEquals(programmingCard3,testDeck.getCards().get(2)); assertEquals(programmingCard3, testDeck.getCards().get(2));
assertEquals(programmingCard6,testDeck.getCards().get(5)); assertEquals(programmingCard6, testDeck.getCards().get(5));
} }
@Test @Test
public void testshuffle(){ public void testShuffle() {
ProgrammingCard card1 = testDeck.getCards().get(0); System.out.print(fullDeck.toString());
int noe = card1.getValue(); fullDeck.shuffle();
ProgrammingCard card2 = testDeck.getCards().get(1); System.out.print(fullDeck.toString());
int noe2 = card2.getValue();
ProgrammingCard card3 = testDeck.getCards().get(2);
int noe3 = card3.getValue();
System.out.println(noe);
System.out.println(noe2);
System.out.println(noe3);
testDeck.shuffle();
ProgrammingCard scard1 = testDeck.getCards().get(0);
int snoe = scard1.getValue();
ProgrammingCard scard2 = testDeck.getCards().get(1);
int snoe2 = scard2.getValue();
ProgrammingCard scard3 = testDeck.getCards().get(2);
int snoe3 = scard3.getValue();
System.out.println(snoe);
System.out.println(snoe2);
System.out.println(snoe3);
} }
} }