Add shuffle test

master
Saša Kocić 6 years ago
parent fd9990e99e
commit 4e0e714851

@ -8,7 +8,6 @@ public class Card {
DIAMONDS DIAMONDS
}; };
public Card(int number, Suit suit) { public Card(int number, Suit suit) {
this.number = number; this.number = number;
this.suit = suit; this.suit = suit;

@ -27,13 +27,17 @@ public class Pile extends ArrayList<Card> {
* Fisher-Yates shuffle() * Fisher-Yates shuffle()
* https://stackoverflow.com/questions/1519736/random-shuffling-of-an-array * https://stackoverflow.com/questions/1519736/random-shuffling-of-an-array
*/ */
public void shuffle() { public void shuffle(Random random) {
Random random = new Random();
for (int i = this.size() - 1; i > 0; i--) { for (int i = this.size() - 1; i > 0; i--) {
Collections.swap(this, i, random.nextInt(i + 1)); Collections.swap(this, i, random.nextInt(i + 1));
} }
} }
public void shuffle() {
Random random = new Random();
shuffle(random);
}
public boolean uniqueMaximum(Card max, int maxIndex) { public boolean uniqueMaximum(Card max, int maxIndex) {
for (int i = 0; i < this.size(); i++) { for (int i = 0; i < this.size(); i++) {
if (maxIndex != i && max.number == this.get(i).number) { if (maxIndex != i && max.number == this.get(i).number) {

@ -0,0 +1,44 @@
import org.junit.jupiter.api.Test;
import java.util.Random;
import static org.junit.jupiter.api.Assertions.*;
class PileTest {
@Test
void shuffleEmptyPile() {
Pile pile = new Pile();
pile.shuffle();
assertEquals(0, pile.size());
}
@Test
void shuffleOneCard() {
Pile pile = new Pile();
pile.add(new Card(1, Card.Suit.CLUBS));
pile.shuffle();
assertEquals(1, pile.size());
assertEquals(1, pile.size());
}
@Test
void shuffleCards() {
Pile pile = new Pile();
pile.add(new Card(1, Card.Suit.CLUBS));
pile.add(new Card(2, Card.Suit.DIAMONDS));
pile.add(new Card(3, Card.Suit.HEARTS));
pile.add(new Card(4, Card.Suit.SPADES));
Random random = new Random(1);
pile.shuffle(random);
assertEquals(4, pile.get(0).number);
assertEquals(Card.Suit.SPADES, pile.get(0).suit);
assertEquals(1, pile.get(1).number);
assertEquals(Card.Suit.CLUBS, pile.get(1).suit);
assertEquals(2, pile.get(2).number);
assertEquals(Card.Suit.DIAMONDS, pile.get(2).suit);
assertEquals(3, pile.get(3).number);
assertEquals(Card.Suit.HEARTS, pile.get(3).suit);
assertEquals(4, pile.size());
}
}
Loading…
Cancel
Save