tests and generated Javadoc

master
Saša Kocić 6 years ago
parent 64f8a4c738
commit bd79d99394

@ -0,0 +1,14 @@
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class CardTest {
@Test
void constructorWorks() {
Card card = new Card(1, Card.Suit.CLUBS);
assertEquals(1, card.number);
assertEquals(Card.Suit.CLUBS, card.suit);
}
}

@ -1,18 +1,18 @@
public class Game { public class Game {
final Players players = new Players(); final Players players = new Players();
private Pile deck;
/** /**
* @param numberOfCards Total number of cards * @param numberOfCards Total number of cards
* @param numberOfPlayers Total number of players * @param numberOfPlayers Total number of players
*/ */
public Game(int numberOfCards, int numberOfPlayers) { public Game(int numberOfCards, int numberOfPlayers) {
Pile deck = Pile.createDeck(numberOfCards); deck = Pile.createDeck(numberOfCards);
int cardsPerPlayer = deck.size() / numberOfPlayers; int cardsPerPlayer = deck.size() / numberOfPlayers;
for (int i = 0; i < numberOfPlayers; i++) { for (int i = 0; i < numberOfPlayers; i++) {
Player player = new Player(String.valueOf(i), deck.take(cardsPerPlayer)); Player player = new Player(String.valueOf(i), deck.take(cardsPerPlayer));
players.add(player); players.add(player);
} }
players.play();
} }

@ -16,8 +16,8 @@ class GameTest {
@Test @Test
void createDeck() { void createDeck() {
assertEquals(2, game.players.size()); assertEquals(2, game.players.size());
for (int i = 0; i < 2; i++) { for (Player player : game.players) {
assertEquals(20, game.players.get(i).drawPile.size()); assertEquals(40 / 2, player.drawPile.size());
} }
} }

@ -3,8 +3,14 @@
*/ */
public class Main { public class Main {
/**
* The entry point of application.
*
* @param args the input arguments
*/
public static void main(String[] args) { public static void main(String[] args) {
new Game(10, 2); Game game = new Game(10, 2);
game.players.play();
System.out.print(Out.get()); System.out.print(Out.get());
} }
} }

@ -1,18 +1,49 @@
import java.util.ArrayList; import java.util.ArrayList;
/**
* The type Out.
*/
public class Out { public class Out {
/**
* The Output.
*/
static final ArrayList<String> output = new ArrayList<>(); static final ArrayList<String> output = new ArrayList<>();
private Out() { throw new IllegalStateException("Utility class"); } /**
* Instantiates a new Out.
*/
protected Out() { throw new IllegalStateException("Utility class"); }
/**
* Gets output.
*
* @return the output
*/
public static ArrayList<String> getOutput() {
return output;
}
/**
* Println.
*
* @param string the string
*/
public static void println(String string) { public static void println(String string) {
output.add(string); output.add(string);
} }
/**
* Println.
*/
public static void println() { public static void println() {
println(""); println("");
} }
/**
* Get string.
*
* @return the string
*/
public static String get() { public static String get() {
StringBuilder string = new StringBuilder(); StringBuilder string = new StringBuilder();
for (String s : output) { for (String s : output) {

@ -0,0 +1,20 @@
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class OutTest {
@Test
void printlnStoresString() {
Out.getOutput().clear();
Out.println();
assertEquals("\n", Out.get());
Out.println("Hello");
assertEquals("\nHello\n", Out.get());
}
@Test
void throwsException() {
assertThrows(IllegalStateException.class, Out::new);
}
}

@ -3,7 +3,16 @@ import java.util.Collections;
import java.util.Random; import java.util.Random;
import java.util.stream.IntStream; import java.util.stream.IntStream;
/**
* The type Pile.
*/
public class Pile extends ArrayList<Card> { public class Pile extends ArrayList<Card> {
/**
* Create deck pile.
*
* @param numberOfCards the number of cards
* @return the pile
*/
public static Pile createDeck(int numberOfCards) { public static Pile createDeck(int numberOfCards) {
Pile deck = new Pile(); Pile deck = new Pile();
for (int number = 1; number <= numberOfCards; number++) { for (int number = 1; number <= numberOfCards; number++) {
@ -15,6 +24,12 @@ public class Pile extends ArrayList<Card> {
return deck; return deck;
} }
/**
* Take pile.
*
* @param number the number
* @return the pile
*/
public Pile take(int number) { public Pile take(int number) {
Pile taken = new Pile(); Pile taken = new Pile();
for (int i = 0; i < number; i++) { for (int i = 0; i < number; i++) {
@ -27,6 +42,8 @@ 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
*
* @param random the random
*/ */
public void shuffle(Random random) { public void shuffle(Random random) {
for (int i = this.size() - 1; i > 0; i--) { for (int i = this.size() - 1; i > 0; i--) {
@ -34,15 +51,29 @@ public class Pile extends ArrayList<Card> {
} }
} }
/**
* Shuffle.
*/
public void shuffle() { public void shuffle() {
Random random = new Random(); Random random = new Random();
shuffle(random); shuffle(random);
} }
/**
* Occurrences long.
*
* @param max the max
* @return the long
*/
public long occurrences(int max) { public long occurrences(int max) {
return IntStream.range(0, size()).filter(i -> get(i).number == max).count(); return IntStream.range(0, size()).filter(i -> get(i).number == max).count();
} }
/**
* Gets max.
*
* @return the max
*/
public Card getMax() { public Card getMax() {
Card max = get(0); Card max = get(0);
for (int i = 1; i < this.size(); i++) { for (int i = 1; i < this.size(); i++) {

@ -1,3 +1,4 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import java.util.Random; import java.util.Random;
@ -5,17 +6,16 @@ import java.util.Random;
import static org.junit.jupiter.api.Assertions.*; import static org.junit.jupiter.api.Assertions.*;
class PileTest { class PileTest {
Pile pile = new Pile();
@Test @Test
void shuffleEmptyPile() { void shuffleEmptyPile() {
Pile pile = new Pile();
pile.shuffle(); pile.shuffle();
assertEquals(0, pile.size()); assertEquals(0, pile.size());
} }
@Test @Test
void shuffleOneCard() { void shuffleOneCard() {
Pile pile = new Pile();
pile.add(new Card(1, Card.Suit.CLUBS)); pile.add(new Card(1, Card.Suit.CLUBS));
pile.shuffle(); pile.shuffle();
assertEquals(1, pile.size()); assertEquals(1, pile.size());
@ -24,7 +24,6 @@ class PileTest {
@Test @Test
void shuffleCards() { void shuffleCards() {
Pile pile = new Pile();
pile.add(new Card(1, Card.Suit.CLUBS)); pile.add(new Card(1, Card.Suit.CLUBS));
pile.add(new Card(2, Card.Suit.DIAMONDS)); pile.add(new Card(2, Card.Suit.DIAMONDS));
pile.add(new Card(3, Card.Suit.HEARTS)); pile.add(new Card(3, Card.Suit.HEARTS));
@ -41,4 +40,47 @@ class PileTest {
assertEquals(Card.Suit.HEARTS, pile.get(3).suit); assertEquals(Card.Suit.HEARTS, pile.get(3).suit);
assertEquals(4, pile.size()); assertEquals(4, pile.size());
} }
@Test
void createDeck() {
pile = Pile.createDeck(1);
assertEquals(1, pile.get(0).number);
assertEquals(1, pile.get(1).number);
assertEquals(1, pile.get(2).number);
assertEquals(1, pile.get(3).number);
assertEquals(4, pile.size());
}
@Test
void createDeckWith40Cards() {
pile = Pile.createDeck(10);
assertEquals(40, pile.size());
}
@Test
void take() {
Pile taken;
pile = Pile.createDeck(2);
assertEquals(2 * 4, pile.size());
taken = pile.take(0);
assertEquals(0, taken.size());
assertEquals(2 * 4, pile.size());
taken = pile.take(3);
assertEquals(3, taken.size());
assertEquals(2 * 4 - 3, pile.size());
}
@Test
void occurrences() {
pile = Pile.createDeck(2);
assertEquals(4, pile.occurrences(1));
assertEquals(4, pile.occurrences(2));
assertEquals(0, pile.occurrences(3));
}
@Test
void getMax() {
pile = Pile.createDeck(2);
assertEquals(2, pile.getMax().number);
}
} }

@ -1,14 +1,37 @@
/**
* The type Player.
*/
public class Player { public class Player {
/**
* The Name.
*/
final String name; final String name;
/**
* The Draw pile.
*/
final Pile drawPile; final Pile drawPile;
/**
* The Discard pile.
*/
final Pile discardPile; final Pile discardPile;
/**
* Instantiates a new Player.
*
* @param name the name
* @param drawPile the draw pile
*/
public Player(String name, Pile drawPile) { public Player(String name, Pile drawPile) {
this.name = name; this.name = name;
this.drawPile = drawPile; this.drawPile = drawPile;
this.discardPile = new Pile(); this.discardPile = new Pile();
} }
/**
* Draw card.
*
* @return the card
*/
public Card draw() { public Card draw() {
if (drawPile.isEmpty()) { if (drawPile.isEmpty()) {
discardPile.shuffle(); discardPile.shuffle();
@ -20,6 +43,11 @@ public class Player {
return draw; return draw;
} }
/**
* Cards count int.
*
* @return the int
*/
public int cardsCount() { public int cardsCount() {
return drawPile.size() + discardPile.size(); return drawPile.size() + discardPile.size();
} }

@ -0,0 +1,37 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class PlayerTest {
Player player;
Pile pile;
@BeforeEach
void setUp() {
pile = Pile.createDeck(1);
player = new Player("0", pile);
}
@Test
void drawWithEmptyDrawPile() {
int n = player.drawPile.size();
player.discardPile.addAll(player.drawPile.take(n));
assertEquals(0, player.drawPile.size());
assertEquals(n, player.discardPile.size());
player.draw();
assertEquals(n - 1, player.drawPile.size());
assertEquals(0, player.discardPile.size());
}
@Test
void draw() {
Card card = player.draw();
assertEquals(1, card.number);
}
@Test
void cardsCount() {
assertEquals(4, player.cardsCount());
}
}

@ -1,22 +1,34 @@
import java.util.ArrayList; import java.util.ArrayList;
/**
* The type Players.
*/
public class Players extends ArrayList<Player> { public class Players extends ArrayList<Player> {
private final Pile pot = new Pile(); private final Pile pot = new Pile();
private final Pile draws = new Pile(); private final Pile draws = new Pile();
/**
* Play.
*/
public void play() { public void play() {
while (playersLeft() != 1) { if (size() > 0) {
draws.clear(); while (playersLeft() > 1) {
for (Player player : this) { draws.clear();
String output = String.format("Player %s (%d cards)", player.name, player.cardsCount()); for (Player player : this) {
Card card = player.draw(); String output = String.format("Player %s (%d cards)", player.name, player.cardsCount());
draws.add(card); Card card = player.draw();
Out.println(String.format("%s: %d - %s", output, card.number, card.suit)); draws.add(card);
Out.println(String.format("%s: %d - %s", output, card.number, card.suit));
}
pot.addAll(draws);
givePotOnWin();
}
if (size() == 0) {
Out.println("No winner in the game!");
} else {
Out.println(String.format("Player %s wins the game!", get(0).name));
} }
pot.addAll(draws);
givePotOnWin();
} }
Out.println(String.format("Player %s wins the game!", get(0).name));
} }
private int playersLeft() { private int playersLeft() {

@ -0,0 +1,73 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.*;
class PlayersTest {
Players players = new Players();
@BeforeEach
void setUp() {
Out.getOutput().clear();
}
@Test
void playForZeroPlayers() {
players.play();
assertEquals("", Out.get());
}
@Test
void playForOnePlayerMakesHimAWinner() {
Player player = new Player("0", Pile.createDeck(1));
players.add(player);
players.play();
assertEquals("Player 0 wins the game!\n", Out.get());
}
@Test
void playWithSameCardsHasNoWinner() {
Player player = new Player("0", Pile.createDeck(1));
players.add(player);
player = new Player("1", Pile.createDeck(1));
players.add(player);
players.play();
assertTrue(Out.get().contains("No winner in this round"));
assertTrue(Out.get().contains("No winner in the game!"));
}
@Test
void playWithTwoPlayers() {
Pile deck = new Pile();
deck.add(new Card(1, Card.Suit.CLUBS));
deck.add(new Card(2, Card.Suit.DIAMONDS));
deck.add(new Card(3, Card.Suit.HEARTS));
deck.add(new Card(4, Card.Suit.SPADES));
Player player = new Player("0", deck.take(2));
players.add(player);
player = new Player("1", deck.take(2));
players.add(player);
players.play();
assertTrue(Out.get().contains("Player 1 wins this round"));
assertTrue(Out.get().contains("Player 1 wins the game!"));
}
@Test
void playerOneWinningFourCards() {
Pile deck = new Pile();
deck.add(new Card(1, Card.Suit.CLUBS));
deck.add(new Card(2, Card.Suit.DIAMONDS));
deck.add(new Card(1, Card.Suit.HEARTS));
deck.add(new Card(4, Card.Suit.SPADES));
Player player = new Player("0", deck.take(2));
players.add(player);
player = new Player("1", deck.take(2));
players.add(player);
players.play();
assertTrue(Out.get().contains("No winner in this round"));
assertTrue(Out.get().contains("Player 1 wins this round"));
assertTrue(Out.get().contains("Player 1 wins the game!"));
assertEquals("1", players.get(0).name);
assertEquals(4, players.get(0).cardsCount());
}
}
Loading…
Cancel
Save