Skip to content
Snippets Groups Projects

Created DeckOfCards class, created list of cards, created method for shuffling...

Merged Steven Ha requested to merge master into main
1 file
+ 49
0
Compare changes
  • Side-by-side
  • Inline
package edu.ntnu.idatt2003;
import java.util.Collections;
import java.util.ArrayList;
import java.util.List;
/**
* En kortstokk bestående av 52 kort.
*/
public class DeckOfCards {
private final List<PlayingCard> cards = new ArrayList<>();
public DeckOfCards() {
char[] suits = { 'S', 'H', 'D', 'C' };
for (char suit : suits) {
for (int face = 1; face <= 13; face++) {
cards.add(new PlayingCard(suit, face));
}
}
}
/**
* Blander kortene i kortstokken.
*/
public void shuffle() {
Collections.shuffle(cards);
}
/**
* Deler ut en hånd med tilfeldige kort.
*
* @param n Antall kort som skal deles ut.
* @return en samling av PlayingCard, som representerer hånden som ble delt ut.
* @throws IllegalArgumentException hvis det ikke er nok kort til å dele ut ønsket antall.
*/
public List<PlayingCard> dealHand(int n) {
if (n > cards.size()) {
throw new IllegalArgumentException("Not enough cards in the deck to deal the hand.");
}
List<PlayingCard> hand = new ArrayList<>();
for (int i = 0; i < n; i++) {
hand.add(cards.remove(cards.size() - 1));
}
return hand;
}
public int cardsLeft() {
return cards.size();
}
}
Loading