Fikser opp i kode, kommentarer og testing for kort og kortstokk

Fjerner overflødige kommentarer i Deck
Forenkler en del kode i Deck
Fikser uparameteriserte typer i Deck
Bytter navn på PlayerDeck til ProgrammingCardDeck
Fjerner overflødige kommentarer i ProgrammingCard
Legger final til ting som ikke skal endres
Fikser mellomrom noen steder
Legger til en toString() metode til ProgrammingCard for enklere debugging
Fikser uparameteriserte lister i TestProgrammingCardDeck
Fjerner unødvendige mellomrom i TestProgrammingCardDeck
This commit is contained in:
Kristian Knarvik 2020-03-03 17:25:18 +01:00
parent 985676c6ea
commit f1bb6ae34b
5 changed files with 75 additions and 131 deletions

View File

@ -8,115 +8,65 @@ 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 ArrayList<T> cardDeck; private final ArrayList<T> cardList;
/** /**
* Initilazes 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 (ArrayList<T> cardList) {
this.cardDeck = cardList; this.cardList = cardList;
} }
/**
* This method shuffles the cards in the deck so that they are in a random order
*/
@Override @Override
public void shuffle() { public void shuffle() {
Random randomNumber = new Random(); Random randomGenerator = new Random();
int deckSize = cardList.size();
for (int i = cardDeck.size() - 1; i > 0; i--) { int halfDeckSize = deckSize / 2;
int randomIndex = randomNumber.nextInt(i); int timesToShuffle = 30 * deckSize;
for (int i = 0; i < timesToShuffle; i++) {
T CardRandomIndex = cardDeck.get(randomIndex); int oldPosition = randomGenerator.nextInt(halfDeckSize);
cardDeck.add(randomIndex, cardDeck.get(i)); int newPosition = randomGenerator.nextInt(deckSize - halfDeckSize) + halfDeckSize;
cardDeck.remove(randomIndex+1); cardList.add(newPosition, cardList.remove(oldPosition));
cardDeck.add(i, CardRandomIndex);
cardDeck.remove(i+1);
} }
} }
/**
* draws a card form the other deck and adds it to the current deck
* @param other The deck to draw the card from
*/
@Override @Override
public void draw(IDeck<T> other){ public void draw(IDeck<T> other) {
Deck<T> otherDeck = (Deck) other; Deck<T> otherDeck = (Deck<T>) other;
cardDeck.add(otherDeck.cardDeck.get(0)); cardList.add(otherDeck.cardList.remove(0));
otherDeck.cardDeck.remove(0);
} }
/**
* 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
*/
@Override @Override
public void draw(IDeck<T> other, int n) { public void draw(IDeck<T> other, int n) {
Deck<T> otherDeck = (Deck) other; Deck<T> otherDeck = (Deck<T>) other;
if(n<1||n>otherDeck.size()){ if (n < 1 || n > otherDeck.size()) {
throw new IllegalArgumentException("n cant be below 1 or over the size of the other card deck"); throw new IllegalArgumentException("n can't be below 1 or over the size of the other card deck");
}
else {
for (int i=0; i<n;i++){
cardDeck.add(otherDeck.cardDeck.get(0));
otherDeck.cardDeck.remove(0);
} }
for (int i = 0; i < n; i++) {
draw(other);
} }
} }
/**
* 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
*/
@Override @Override
public void emptyInto(IDeck<T> other) { public void emptyInto(IDeck<T> other) {
Deck<T> otherDeck = (Deck) other; Deck<T> otherDeck = (Deck<T>) other;
int size = otherDeck.size(); otherDeck.draw(this, this.size());
for (int i=0; i<size;i++){
otherDeck.draw(this);
} }
}
/**
* checks if the deck is empty
* @return true if deck is empty false otherwise
*/
@Override @Override
public boolean isEmpty() { public boolean isEmpty() {
return cardDeck.isEmpty(); return cardList.isEmpty();
} }
/**
* gets the size of the current deck
* @return size of the deck
*/
@Override @Override
public int size() { public int size() {
return cardDeck.size(); return cardList.size();
} }
/**
* gets the list of cards inn this deck
* @return list of cards inn the deck
*/
@Override @Override
public List<T> getCards() { public List<T> getCards() {
return new ArrayList<>(cardList);
ArrayList<T> returnDeck = new ArrayList<>();
for (T card:cardDeck){
returnDeck.add(card);
} }
return returnDeck;
}
} }

View File

@ -1,15 +0,0 @@
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

@ -5,37 +5,33 @@ import inf112.fiasko.roborally.element_properties.Action;
/** /**
* This class represents a programming card * This class represents a programming card
*/ */
public class ProgrammingCard implements ICardWithoutSuit<Integer, Action> { public class ProgrammingCard implements ICardWithoutSuit<Integer, Action> {
private Integer cardValue; private final Integer cardValue;
private Action cardAction; private final Action cardAction;
/** /**
* Initializes the value and the action of the card * Initializes the value and the action of the card
* @param cardValue the value of the card * @param cardValue the value of the card
* @param cardAction the action of the card * @param cardAction the action of the card
*/ */
public ProgrammingCard(int cardValue,Action cardAction){ public ProgrammingCard(int cardValue, Action cardAction){
this.cardValue=cardValue; this.cardValue = cardValue;
this.cardAction=cardAction; this.cardAction = cardAction;
} }
/**
* Returns the value of the card
* @return the value of the card
*/
@Override @Override
public Integer getValue() { public Integer getValue() {
return cardValue; return cardValue;
} }
/**
* Returns the action the card should perform
* @return the action of the card
*/
@Override @Override
public Action getSymbol() { public Action getSymbol() {
return cardAction; return cardAction;
} }
@Override
public String toString() {
return this.getValue() + " " + this.cardAction.toString();
}
} }

View File

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

View File

@ -4,20 +4,22 @@ import inf112.fiasko.roborally.element_properties.Action;
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.assertTrue;
import java.util.ArrayList; import java.util.ArrayList;
public class TestPlayerDeck { public class TestProgrammingCardDeck {
private ProgrammingCard programmingCard1 = new ProgrammingCard(5, Action.MOVE_1); private final ProgrammingCard programmingCard1 = new ProgrammingCard(5, Action.MOVE_1);
private ProgrammingCard programmingCard2 = new ProgrammingCard(6, Action.MOVE_2); private final ProgrammingCard programmingCard2 = new ProgrammingCard(6, Action.MOVE_2);
private ProgrammingCard programmingCard3 = new ProgrammingCard(7, Action.MOVE_3); private final ProgrammingCard programmingCard3 = new ProgrammingCard(7, Action.MOVE_3);
private ProgrammingCard programmingCard4 = new ProgrammingCard(55, Action.MOVE_1); private final ProgrammingCard programmingCard4 = new ProgrammingCard(55, Action.MOVE_1);
private ProgrammingCard programmingCard5 = new ProgrammingCard(66, Action.MOVE_2); private final ProgrammingCard programmingCard5 = new ProgrammingCard(66, Action.MOVE_2);
private ProgrammingCard programmingCard6 = new ProgrammingCard(756, Action.MOVE_3); private final ProgrammingCard programmingCard6 = new ProgrammingCard(756, Action.MOVE_3);
private ArrayList<ProgrammingCard> cardlist = new ArrayList(); private final ArrayList<ProgrammingCard> cardlist = new ArrayList<>();
private ArrayList<ProgrammingCard> cardlist2 = new ArrayList(); private final ArrayList<ProgrammingCard> cardlist2 = new ArrayList<>();
private PlayerDeck testDeck; private ProgrammingCardDeck testDeck;
private PlayerDeck testDeck2; private ProgrammingCardDeck testDeck2;
@Before @Before
public void setUp() { public void setUp() {
cardlist.add(programmingCard1); cardlist.add(programmingCard1);
@ -26,8 +28,8 @@ public class TestPlayerDeck {
cardlist2.add(programmingCard4); cardlist2.add(programmingCard4);
cardlist2.add(programmingCard5); cardlist2.add(programmingCard5);
cardlist2.add(programmingCard6); cardlist2.add(programmingCard6);
testDeck = new PlayerDeck(cardlist); testDeck = new ProgrammingCardDeck(cardlist);
testDeck2 = new PlayerDeck(cardlist2); testDeck2 = new ProgrammingCardDeck(cardlist2);
} }
@Test @Test
public void testSize(){ public void testSize(){
@ -61,9 +63,9 @@ public class TestPlayerDeck {
} }
@Test @Test
public void testIsEmpty(){ public void testIsEmpty(){
assertEquals(false,testDeck.isEmpty()); assertFalse(testDeck.isEmpty());
testDeck.emptyInto(testDeck2); testDeck.emptyInto(testDeck2);
assertEquals(true,testDeck.isEmpty()); assertTrue(testDeck.isEmpty());
} }
@Test @Test
@ -76,11 +78,11 @@ public class TestPlayerDeck {
@Test @Test
public void testshuffle(){ public void testshuffle(){
ProgrammingCard card1 =(ProgrammingCard) testDeck.getCards().get(0); ProgrammingCard card1 = testDeck.getCards().get(0);
int noe = card1.getValue(); int noe = card1.getValue();
ProgrammingCard card2 =(ProgrammingCard) testDeck.getCards().get(1); ProgrammingCard card2 = testDeck.getCards().get(1);
int noe2 = card2.getValue(); int noe2 = card2.getValue();
ProgrammingCard card3 =(ProgrammingCard) testDeck.getCards().get(2); ProgrammingCard card3 = testDeck.getCards().get(2);
int noe3 = card3.getValue(); int noe3 = card3.getValue();
System.out.println(noe); System.out.println(noe);
@ -89,21 +91,15 @@ public class TestPlayerDeck {
testDeck.shuffle(); testDeck.shuffle();
ProgrammingCard scard1 =(ProgrammingCard) testDeck.getCards().get(0); ProgrammingCard scard1 = testDeck.getCards().get(0);
int snoe = scard1.getValue(); int snoe = scard1.getValue();
ProgrammingCard scard2 =(ProgrammingCard) testDeck.getCards().get(1); ProgrammingCard scard2 = testDeck.getCards().get(1);
int snoe2 = scard2.getValue(); int snoe2 = scard2.getValue();
ProgrammingCard scard3 =(ProgrammingCard) testDeck.getCards().get(2); ProgrammingCard scard3 = testDeck.getCards().get(2);
int snoe3 = scard3.getValue(); int snoe3 = scard3.getValue();
System.out.println(snoe); System.out.println(snoe);
System.out.println(snoe2); System.out.println(snoe2);
System.out.println(snoe3); System.out.println(snoe3);
} }
} }