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
*/
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
*/
public Deck (ArrayList<T> cardList){
this.cardDeck = cardList;
public Deck (ArrayList<T> cardList) {
this.cardList = 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 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);
Random randomGenerator = new Random();
int deckSize = cardList.size();
int halfDeckSize = deckSize / 2;
int timesToShuffle = 30 * deckSize;
for (int i = 0; i < timesToShuffle; i++) {
int oldPosition = randomGenerator.nextInt(halfDeckSize);
int newPosition = randomGenerator.nextInt(deckSize - halfDeckSize) + halfDeckSize;
cardList.add(newPosition, cardList.remove(oldPosition));
}
}
/**
* draws a card form the other deck and adds it to the current deck
* @param other The deck to draw the card from
*/
@Override
public void draw(IDeck<T> other){
Deck<T> otherDeck = (Deck) other;
cardDeck.add(otherDeck.cardDeck.get(0));
otherDeck.cardDeck.remove(0);
public void draw(IDeck<T> other) {
Deck<T> otherDeck = (Deck<T>) other;
cardList.add(otherDeck.cardList.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
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");
Deck<T> otherDeck = (Deck<T>) other;
if (n < 1 || n > otherDeck.size()) {
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
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);
}
Deck<T> otherDeck = (Deck<T>) other;
otherDeck.draw(this, this.size());
}
/**
* checks if the deck is empty
* @return true if deck is empty false otherwise
*/
@Override
public boolean isEmpty() {
return cardDeck.isEmpty();
return cardList.isEmpty();
}
/**
* gets the size of the current deck
* @return size of the deck
*/
@Override
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
public List<T> getCards() {
ArrayList<T> returnDeck = new ArrayList<>();
for (T card:cardDeck){
returnDeck.add(card);
}
return returnDeck;
return new ArrayList<>(cardList);
}
}

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
*/
public class ProgrammingCard implements ICardWithoutSuit<Integer, Action> {
private Integer cardValue;
private Action cardAction;
private final Integer cardValue;
private final Action cardAction;
/**
* Initializes the value and the action of the card
* @param cardValue the value of the card
* @param cardAction the action of the card
*/
public ProgrammingCard(int cardValue,Action cardAction){
this.cardValue=cardValue;
this.cardAction=cardAction;
public ProgrammingCard(int cardValue, Action cardAction){
this.cardValue = cardValue;
this.cardAction = cardAction;
}
/**
* Returns the value of the card
* @return the value of the card
*/
@Override
public Integer getValue() {
return cardValue;
}
/**
* Returns the action the card should perform
* @return the action of the card
*/
@Override
public Action getSymbol() {
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.Test;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;
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;
public class TestProgrammingCardDeck {
private final ProgrammingCard programmingCard1 = new ProgrammingCard(5, Action.MOVE_1);
private final ProgrammingCard programmingCard2 = new ProgrammingCard(6, Action.MOVE_2);
private final ProgrammingCard programmingCard3 = new ProgrammingCard(7, Action.MOVE_3);
private final ProgrammingCard programmingCard4 = new ProgrammingCard(55, Action.MOVE_1);
private final ProgrammingCard programmingCard5 = new ProgrammingCard(66, Action.MOVE_2);
private final ProgrammingCard programmingCard6 = new ProgrammingCard(756, Action.MOVE_3);
private final ArrayList<ProgrammingCard> cardlist = new ArrayList<>();
private final ArrayList<ProgrammingCard> cardlist2 = new ArrayList<>();
private ProgrammingCardDeck testDeck;
private ProgrammingCardDeck testDeck2;
@Before
public void setUp() {
cardlist.add(programmingCard1);
@ -26,8 +28,8 @@ public class TestPlayerDeck {
cardlist2.add(programmingCard4);
cardlist2.add(programmingCard5);
cardlist2.add(programmingCard6);
testDeck = new PlayerDeck(cardlist);
testDeck2 = new PlayerDeck(cardlist2);
testDeck = new ProgrammingCardDeck(cardlist);
testDeck2 = new ProgrammingCardDeck(cardlist2);
}
@Test
public void testSize(){
@ -61,9 +63,9 @@ public class TestPlayerDeck {
}
@Test
public void testIsEmpty(){
assertEquals(false,testDeck.isEmpty());
assertFalse(testDeck.isEmpty());
testDeck.emptyInto(testDeck2);
assertEquals(true,testDeck.isEmpty());
assertTrue(testDeck.isEmpty());
}
@Test
@ -76,11 +78,11 @@ public class TestPlayerDeck {
@Test
public void testshuffle(){
ProgrammingCard card1 =(ProgrammingCard) testDeck.getCards().get(0);
ProgrammingCard card1 = testDeck.getCards().get(0);
int noe = card1.getValue();
ProgrammingCard card2 =(ProgrammingCard) testDeck.getCards().get(1);
ProgrammingCard card2 = testDeck.getCards().get(1);
int noe2 = card2.getValue();
ProgrammingCard card3 =(ProgrammingCard) testDeck.getCards().get(2);
ProgrammingCard card3 = testDeck.getCards().get(2);
int noe3 = card3.getValue();
System.out.println(noe);
@ -89,21 +91,15 @@ public class TestPlayerDeck {
testDeck.shuffle();
ProgrammingCard scard1 =(ProgrammingCard) testDeck.getCards().get(0);
ProgrammingCard scard1 = testDeck.getCards().get(0);
int snoe = scard1.getValue();
ProgrammingCard scard2 =(ProgrammingCard) testDeck.getCards().get(1);
ProgrammingCard scard2 = testDeck.getCards().get(1);
int snoe2 = scard2.getValue();
ProgrammingCard scard3 =(ProgrammingCard) testDeck.getCards().get(2);
ProgrammingCard scard3 = testDeck.getCards().get(2);
int snoe3 = scard3.getValue();
System.out.println(snoe);
System.out.println(snoe2);
System.out.println(snoe3);
}
}