Conflicts:
	src/test/java/inf112/fiasko/roborally/objects/TestPlayerDeck.java
This commit is contained in:
GabrielMagnus 2020-03-03 19:11:22 +01:00
commit d1d4ec1fe1
9 changed files with 206 additions and 212 deletions

View File

@ -18,6 +18,8 @@ Det er blitt jobbet med deck-klassene og diskutert hvordan disse skal implemente
Faser og runder er blitt gjennomgått step-by-step. Vi diskuterer forskjellige måter vi vil implementere
de forskjellige aspektene som kommer med disse fasene og rundene.
Grafisk design er blitt grovt skissert
#### Fase
- Kjør programmeringskort etter høyest verdi
@ -63,3 +65,17 @@ de forskjellige aspektene som kommer med disse fasene og rundene.
- Program
#### Initialisering
- Last inn og stokke kort
- Bestem robot ID
### Brukerhistorier
- Som spiller må jeg vite hvilken robot som er min for å riktig kunne
programmere roboten min
- Som spiller må jeg ha et unikt navn for å kunne identifiseres
- Som spiller må jeg kunne annonsere power down runden før jeg skal ta power down for å få
ta en gyldig power down

View File

@ -8,115 +8,74 @@ 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 List<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 (List<T> cardList) {
this.cardList = new ArrayList<>(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);
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;
Deck<T> otherDeck = (Deck<T>) other;
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);
}
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);
}
@Override
public String toString() {
StringBuilder builder = new StringBuilder();
for (T card : cardList) {
builder.append(card.toString()).append("\n");
}
return builder.toString();
}
}

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,11 +5,10 @@ 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
@ -21,21 +20,18 @@ public class ProgrammingCard implements ICardWithoutSuit<Integer, Action> {
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.List;
/**
* 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(List<ProgrammingCard> cardList) {
super(cardList);
}
}

View File

@ -0,0 +1,37 @@
package inf112.fiasko.roborally.utility;
import inf112.fiasko.roborally.element_properties.Action;
import inf112.fiasko.roborally.objects.ProgrammingCard;
import inf112.fiasko.roborally.objects.ProgrammingCardDeck;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
/**
* Helper class for loading card decks
*/
public final class DeckLoaderUtil {
/**
* Returns a programming card deck containing all official programming cards
* @return A programming card deck with programming cards
* @throws IOException If the programming cards file is invalid
*/
public static ProgrammingCardDeck loadProgrammingCardsDeck() throws IOException {
BufferedReader reader = new BufferedReader(new InputStreamReader(
ResourceUtil.getResourceAsInputStream("programming_cards.txt")));
int numberOfCards = Integer.parseInt(reader.readLine());
List<ProgrammingCard> programmingCardList = new ArrayList<>();
for (int i = 0; i < numberOfCards; i++) {
String cardLine = reader.readLine();
String[] parts = cardLine.split(" ");
int cardPriority = Integer.parseInt(parts[0]);
Action cardAction = Action.valueOf(parts[1]);
programmingCardList.add(new ProgrammingCard(cardPriority, cardAction));
}
return new ProgrammingCardDeck(programmingCardList);
}
}

View File

@ -0,0 +1,93 @@
package inf112.fiasko.roborally.objects;
import inf112.fiasko.roborally.element_properties.Action;
import inf112.fiasko.roborally.utility.DeckLoaderUtil;
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.io.IOException;
import java.util.ArrayList;
public class ProgrammingCardDeckTest {
private ProgrammingCardDeck fullDeck;
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() {
try {
fullDeck = DeckLoaderUtil.loadProgrammingCardsDeck();
} catch (IOException e) {
e.printStackTrace();
}
cardlist.add(programmingCard1);
cardlist.add(programmingCard2);
cardlist.add(programmingCard3);
cardlist2.add(programmingCard4);
cardlist2.add(programmingCard5);
cardlist2.add(programmingCard6);
testDeck = new ProgrammingCardDeck(cardlist);
testDeck2 = new ProgrammingCardDeck(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 testDrawMultipleCards() {
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() {
assertFalse(testDeck.isEmpty());
testDeck.emptyInto(testDeck2);
assertTrue(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() {
System.out.print(fullDeck.toString());
fullDeck.shuffle();
System.out.print(fullDeck.toString());
}
}

View File

@ -1,109 +0,0 @@
package inf112.fiasko.roborally.objects;
import inf112.fiasko.roborally.element_properties.Action;
import org.junit.Before;
import org.junit.Test;
import java.util.ArrayList;
import static org.junit.Assert.*;
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(){
Boolean atLeastOneShuffle = false;
ArrayList<Boolean> resultList = new ArrayList<>();
ArrayList<ProgrammingCard> beforeShuffle = (ArrayList<ProgrammingCard>)testDeck.getCards();
for (int i = 0; i < 10; i++){ //Saves result of ten shuffles
testDeck.shuffle();
ArrayList<ProgrammingCard> afterShuffle = (ArrayList<ProgrammingCard>)testDeck.getCards();
if (beforeShuffle != afterShuffle) {
resultList.add(true);
}
else {
resultList.add(false);
}
}
//Looks to see if at least one shuffle is different from before
for (int i = 0; i < resultList.size(); i++) {
if (resultList.get(i)==true) {
atLeastOneShuffle = true;
}
}
assertTrue(atLeastOneShuffle);
}
}