mirror of
https://github.com/inf112-v20/Fiasko.git
synced 2025-06-27 11:44:42 +02:00
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:
@ -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);
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
@ -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);
|
||||
}
|
||||
|
||||
}
|
@ -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();
|
||||
}
|
||||
}
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user