You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

50 lines
1.3 KiB

import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;
public class Pile extends ArrayList<Card> {
public static Pile createDeck(int numberOfCards) {
Pile deck = new Pile();
for (int number = 1; number <= numberOfCards; number++) {
for (Card.Suit suit : Card.Suit.values()) {
deck.add(new Card(number, suit));
}
}
deck.shuffle();
return deck;
}
public Pile getCardsForPlayer(int number) {
Pile cardsForPlayer = new Pile();
for (int i = 0; i < number; i++) {
cardsForPlayer.add(get(0));
remove(0);
}
return cardsForPlayer;
}
/**
* Fisher-Yates shuffle()
* https://stackoverflow.com/questions/1519736/random-shuffling-of-an-array
*/
public void shuffle(Random random) {
for (int i = this.size() - 1; i > 0; i--) {
Collections.swap(this, i, random.nextInt(i + 1));
}
}
public void shuffle() {
Random random = new Random();
shuffle(random);
}
public boolean uniqueMaximum(Card max, int maxIndex) {
for (int i = 0; i < this.size(); i++) {
if (maxIndex != i && max.number == this.get(i).number) {
return false;
}
}
return true;
}
}