From 1c54461cfa0507e10357e819a6375a1ce58c3d32 Mon Sep 17 00:00:00 2001 From: jconte <jconte@ippon.fr> Date: Mon, 17 Aug 2020 19:50:44 +0200 Subject: [PATCH] Ugly trivia resolution --- .gitlab-ci.yml | 1 + README.md | 1 + ugly-trivia/README.md | 2 +- ugly-trivia/pom.xml | 54 ++-- .../games/trivia/runner/GameRunner.java | 36 --- .../adaptionsoft/games/uglytrivia/Answer.java | 35 +++ .../uglytrivia/AnswerRandomSimulator.java | 15 ++ .../adaptionsoft/games/uglytrivia/Board.java | 17 ++ .../games/uglytrivia/Category.java | 28 ++ .../games/uglytrivia/ConsolePrinter.java | 14 + .../adaptionsoft/games/uglytrivia/Deck.java | 30 +++ .../adaptionsoft/games/uglytrivia/Dice.java | 15 ++ .../adaptionsoft/games/uglytrivia/Game.java | 240 ++++++------------ .../games/uglytrivia/Notifications.java | 61 +++++ .../adaptionsoft/games/uglytrivia/Player.java | 137 ++++++++++ .../games/uglytrivia/Players.java | 59 +++++ .../games/uglytrivia/Question.java | 13 + .../adaptionsoft/games/uglytrivia/Turn.java | 70 +++++ .../adaptionsoft/games/trivia/SomeTest.java | 13 - .../AnswerRandomSimulatorUnitTest.java | 36 +++ .../games/uglytrivia/AnswerUnitTest.java | 41 +++ .../games/uglytrivia/BoardUnitTest.java | 25 ++ .../games/uglytrivia/CategoryUnitTest.java | 29 +++ .../uglytrivia/ConsolePrinterUnitTest.java | 26 ++ .../games/uglytrivia/DeckUnitTest.java | 49 ++++ .../games/uglytrivia/DiceUnitTest.java | 20 ++ .../games/uglytrivia/GameUnitTest.java | 132 ++++++++++ .../uglytrivia/NotificationsUnitTest.java | 90 +++++++ .../games/uglytrivia/PlayerUnitTest.java | 104 ++++++++ .../games/uglytrivia/PlayersUnitTest.java | 52 ++++ .../games/uglytrivia/QuestionUnitTest.java | 23 ++ .../games/uglytrivia/TurnUnitTest.java | 77 ++++++ 32 files changed, 1302 insertions(+), 243 deletions(-) mode change 100644 => 100755 ugly-trivia/pom.xml delete mode 100644 ugly-trivia/src/main/java/com/adaptionsoft/games/trivia/runner/GameRunner.java create mode 100755 ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Answer.java create mode 100755 ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/AnswerRandomSimulator.java create mode 100755 ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Board.java create mode 100755 ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Category.java create mode 100755 ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/ConsolePrinter.java create mode 100755 ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Deck.java create mode 100755 ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Dice.java mode change 100644 => 100755 ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Game.java create mode 100755 ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Notifications.java create mode 100755 ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Player.java create mode 100755 ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Players.java create mode 100755 ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Question.java create mode 100755 ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Turn.java delete mode 100644 ugly-trivia/src/test/java/com/adaptionsoft/games/trivia/SomeTest.java create mode 100755 ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/AnswerRandomSimulatorUnitTest.java create mode 100755 ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/AnswerUnitTest.java create mode 100755 ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/BoardUnitTest.java create mode 100755 ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/CategoryUnitTest.java create mode 100644 ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/ConsolePrinterUnitTest.java create mode 100755 ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/DeckUnitTest.java create mode 100755 ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/DiceUnitTest.java create mode 100755 ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/GameUnitTest.java create mode 100755 ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/NotificationsUnitTest.java create mode 100755 ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/PlayerUnitTest.java create mode 100755 ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/PlayersUnitTest.java create mode 100755 ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/QuestionUnitTest.java create mode 100755 ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/TurnUnitTest.java diff --git a/.gitlab-ci.yml b/.gitlab-ci.yml index 1c3825ee..0ef78a9a 100644 --- a/.gitlab-ci.yml +++ b/.gitlab-ci.yml @@ -7,3 +7,4 @@ include: - local: "/roman-numerals/.gitlab-ci.yml" - local: "/string-calculator/.gitlab-ci.yml" - local: "/gilded-rose/.gitlab-ci.yml" + - local: "/ugly-trivia/.gitlab-ci.yml" diff --git a/README.md b/README.md index 7b32afec..beaaf239 100644 --- a/README.md +++ b/README.md @@ -22,3 +22,4 @@ Ce dépôt Git a pour but de partager les différents ateliers pouvant être ré 4. [C'est une bonne situation ça techlead ? (débat)](https://www.youtube.com/watch?v=9tOoXfOE12o) 5. [Pagination Seven](/pagination-seven) 6. [GildedRose](/gilded-rose) +7. [UglyTrivia](/ugly-trivia) diff --git a/ugly-trivia/README.md b/ugly-trivia/README.md index 7b33ed15..c55592fe 100644 --- a/ugly-trivia/README.md +++ b/ugly-trivia/README.md @@ -8,4 +8,4 @@ Dans le cadre d'un programme de mentoring interne (BlackBelt) Julie a fait une r - **Date** : 19/09/2020 - **Langage** : Java - **Niveau** : Moyen -- **Replay** : +- **Replay** : [REVUE KATA : Ugly Trivia avec Julie, Emmanuelle, Alexis & Colin](https://www.youtube.com/watch?v=IeAa8chmngI) diff --git a/ugly-trivia/pom.xml b/ugly-trivia/pom.xml old mode 100644 new mode 100755 index f9de2335..0105c637 --- a/ugly-trivia/pom.xml +++ b/ugly-trivia/pom.xml @@ -1,39 +1,35 @@ <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"> <modelVersion>4.0.0</modelVersion> - <groupId>com.adaptionsoft.games</groupId> - <artifactId>uglytrivia</artifactId> - <packaging>jar</packaging> - <version>1.0-SNAPSHOT</version> + + <parent> + <version>1.0.0</version> + <groupId>fr.ippon.kata</groupId> + <artifactId>java-parent</artifactId> + <relativePath>../java-parent</relativePath> + </parent> + + <version>1.0.0-SNAPSHOT</version> + <artifactId>ugly-trivia</artifactId> + + <name>UglyTrivia</name> <dependencies> <dependency> - <groupId>junit</groupId> - <artifactId>junit</artifactId> - <version>4.8.2</version> + <groupId>org.apache.maven.surefire</groupId> + <artifactId>surefire-booter</artifactId> + <version>3.0.0-M3</version> </dependency> </dependencies> - <build> - <plugins> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-resources-plugin</artifactId> - <version>2.5</version> - <configuration> - <encoding>UTF-8</encoding> - </configuration> - </plugin> - <plugin> - <groupId>org.apache.maven.plugins</groupId> - <artifactId>maven-compiler-plugin</artifactId> - <version>2.3.2</version> - <configuration> - <source>1.6</source> - <target>1.6</target> - <encoding>UTF-8</encoding> - </configuration> - </plugin> - </plugins> - </build> + <developers> + <developer> + <email>jconte@ippon.fr</email> + <name>Julie CONTE</name> + </developer> + <developer> + <email>cdamon@ippon.fr</email> + <name>Colin DAMON</name> + </developer> + </developers> </project> diff --git a/ugly-trivia/src/main/java/com/adaptionsoft/games/trivia/runner/GameRunner.java b/ugly-trivia/src/main/java/com/adaptionsoft/games/trivia/runner/GameRunner.java deleted file mode 100644 index ed99a9ae..00000000 --- a/ugly-trivia/src/main/java/com/adaptionsoft/games/trivia/runner/GameRunner.java +++ /dev/null @@ -1,36 +0,0 @@ - -package com.adaptionsoft.games.trivia.runner; -import java.util.Random; - -import com.adaptionsoft.games.uglytrivia.Game; - - -public class GameRunner { - - private static boolean notAWinner; - - public static void main(String[] args) { - Game aGame = new Game(); - - aGame.add("Chet"); - aGame.add("Pat"); - aGame.add("Sue"); - - Random rand = new Random(); - - do { - - aGame.roll(rand.nextInt(5) + 1); - - if (rand.nextInt(9) == 7) { - notAWinner = aGame.wrongAnswer(); - } else { - notAWinner = aGame.wasCorrectlyAnswered(); - } - - - - } while (notAWinner); - - } -} diff --git a/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Answer.java b/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Answer.java new file mode 100755 index 00000000..f4a25f33 --- /dev/null +++ b/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Answer.java @@ -0,0 +1,35 @@ +package com.adaptionsoft.games.uglytrivia; + + +public class Answer { + private final Notifications notifications; + private final AnswerRandomSimulator answerRandom; + + public Answer(Notifications notifications, AnswerRandomSimulator answerRandom) { + this.notifications = notifications; + this.answerRandom = answerRandom; + } + + public Player answer(Player player) { + if (!answerRandom.isRight()) { + return wrongAnswer(player); + } + + return goodAnswer(player); + } + + private Player goodAnswer(Player player) { + if (!player.isInPenaltyBox()) { + notifications.correctAnswer(); + player = player.addCoin(); + notifications.actualGoldCoins(player.getName(), player.getPurse()); + } + return player; + } + + private Player wrongAnswer(Player player) { + notifications.incorrectlyAnswered(); + notifications.sendInPenaltyBox(player.getName()); + return player.inPenaltyBox(); + } +} diff --git a/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/AnswerRandomSimulator.java b/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/AnswerRandomSimulator.java new file mode 100755 index 00000000..c6786613 --- /dev/null +++ b/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/AnswerRandomSimulator.java @@ -0,0 +1,15 @@ +package com.adaptionsoft.games.uglytrivia; + +import java.util.Random; + +class AnswerRandomSimulator { + private final Random rand; + + AnswerRandomSimulator(Random rand) { + this.rand = rand; + } + + boolean isRight() { + return !(rand.nextInt(9) == 7); + } +} diff --git a/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Board.java b/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Board.java new file mode 100755 index 00000000..8187277f --- /dev/null +++ b/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Board.java @@ -0,0 +1,17 @@ +package com.adaptionsoft.games.uglytrivia; + +class Board { + private final int limitBoardSize; + + Board(int limitBoardSize) { + this.limitBoardSize = limitBoardSize; + } + + boolean isBeyondLimitBoard(int position) { + return position >= limitBoardSize; + } + + int getLimit() { + return limitBoardSize; + } +} diff --git a/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Category.java b/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Category.java new file mode 100755 index 00000000..cad25d09 --- /dev/null +++ b/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Category.java @@ -0,0 +1,28 @@ +package com.adaptionsoft.games.uglytrivia; + + +import java.util.Arrays; +import java.util.List; + +enum Category { + POP("Pop", List.of(0,4,8)), + SCIENCE("Science", List.of(1,5,9)), + SPORTS("Sports", List.of(2,6,10)), + ROCK("Rock", List.of()); + + private final String category; + private final List<Integer> positions; + + Category(String category, List<Integer> positions) { + this.category = category; + this.positions = positions; + } + + static Category fromPosition(int position) { + return Arrays.stream(Category.values()).filter(category -> category.positions.contains(position)).findFirst().orElse(Category.ROCK); + } + + String getCategory() { + return category; + } +} diff --git a/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/ConsolePrinter.java b/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/ConsolePrinter.java new file mode 100755 index 00000000..f6f333bb --- /dev/null +++ b/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/ConsolePrinter.java @@ -0,0 +1,14 @@ +package com.adaptionsoft.games.uglytrivia; + +import java.io.PrintStream; + +class ConsolePrinter { + private final PrintStream print; + public ConsolePrinter(PrintStream print) { + this.print = print; + } + + void sendMessage(String message) { + print.println(message); + } +} diff --git a/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Deck.java b/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Deck.java new file mode 100755 index 00000000..efc0de14 --- /dev/null +++ b/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Deck.java @@ -0,0 +1,30 @@ +package com.adaptionsoft.games.uglytrivia; + +import java.util.HashMap; +import java.util.LinkedList; +import java.util.Map; + +class Deck { + Map<Category, LinkedList<Question>> deck; + + Deck(int limitDeckSize) { + + deck = new HashMap<>(); + + deck.put(Category.POP, new LinkedList<>()); + deck.put(Category.SCIENCE, new LinkedList<>()); + deck.put(Category.SPORTS, new LinkedList<>()); + deck.put(Category.ROCK, new LinkedList<>()); + + for (int i = 0; i < limitDeckSize; i++) { + deck.get(Category.POP).addLast(new Question(Category.POP, i)); + deck.get(Category.SCIENCE).addLast(new Question(Category.SCIENCE, i)); + deck.get(Category.SPORTS).addLast(new Question(Category.SPORTS, i)); + deck.get(Category.ROCK).addLast(new Question(Category.ROCK, i)); + } + } + + String removeFirstQuestion(Category category) { + return deck.get(category).removeFirst().getMessage(); + } +} diff --git a/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Dice.java b/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Dice.java new file mode 100755 index 00000000..7735479e --- /dev/null +++ b/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Dice.java @@ -0,0 +1,15 @@ +package com.adaptionsoft.games.uglytrivia; + +import java.util.Random; + +class Dice { + private final Random rand; + + public Dice(Random rand) { + this.rand = rand; + } + + public int roll() { + return rand.nextInt(5) + 1; + } +} diff --git a/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Game.java b/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Game.java old mode 100644 new mode 100755 index f4a3db5a..c32f47d8 --- a/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Game.java +++ b/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Game.java @@ -1,169 +1,81 @@ package com.adaptionsoft.games.uglytrivia; -import java.util.ArrayList; -import java.util.LinkedList; - -public class Game { - ArrayList players = new ArrayList(); - int[] places = new int[6]; - int[] purses = new int[6]; - boolean[] inPenaltyBox = new boolean[6]; - - LinkedList popQuestions = new LinkedList(); - LinkedList scienceQuestions = new LinkedList(); - LinkedList sportsQuestions = new LinkedList(); - LinkedList rockQuestions = new LinkedList(); - - int currentPlayer = 0; - boolean isGettingOutOfPenaltyBox; - - public Game(){ - for (int i = 0; i < 50; i++) { - popQuestions.addLast("Pop Question " + i); - scienceQuestions.addLast(("Science Question " + i)); - sportsQuestions.addLast(("Sports Question " + i)); - rockQuestions.addLast(createRockQuestion(i)); - } +class Game { + static final int MAX_PLAYER_PLAYABLE = 6; + static final int MAX_GOLD_COINS = 6; + static final int MIN_PLAYER_PLAYABLE = 2; + static final int LIMIT_DECK_SIZE = 50; + private final Turn turn; + private final Answer answer; + private final Notifications notifications; + private final Deck deck; + + private Players players; + + Game(Turn turn, Answer answer, Notifications notifications) { + this.notifications = notifications; + this.turn = turn; + this.answer = answer; + players = new Players(); + deck = new Deck(LIMIT_DECK_SIZE); + } + + boolean isPlayable() { + return minimumPlayer() && limitPlayersNotExceeded(); + } + + private boolean minimumPlayer() { + return howManyPlayers() >= MIN_PLAYER_PLAYABLE; + } + + private boolean limitPlayersNotExceeded() { + return howManyPlayers() < MAX_PLAYER_PLAYABLE; + } + + boolean add(String playerName) { + + players = players.add(playerName); + + notifications.addPlayer(playerName); + notifications.playerPlace(howManyPlayers()); + + return true; + } + + private int howManyPlayers() { + return players.howManyPlayers(); + } + + boolean playTurn() { + updateCurrentPlayer(turn.tryToPlay(currentPlayer())); + askQuestion(); + updateCurrentPlayer(answer.answer(currentPlayer())); + + if (didPlayerWin()) { + return true; + } + + players = players.nextPlayer(); + return false; + } + + private boolean didPlayerWin() { + return currentPlayer().getPurse() == MAX_GOLD_COINS; } - public String createRockQuestion(int index){ - return "Rock Question " + index; - } - - public boolean isPlayable() { - return (howManyPlayers() >= 2); - } - - public boolean add(String playerName) { - - - players.add(playerName); - places[howManyPlayers()] = 0; - purses[howManyPlayers()] = 0; - inPenaltyBox[howManyPlayers()] = false; - - System.out.println(playerName + " was added"); - System.out.println("They are player number " + players.size()); - return true; - } - - public int howManyPlayers() { - return players.size(); - } - - public void roll(int roll) { - System.out.println(players.get(currentPlayer) + " is the current player"); - System.out.println("They have rolled a " + roll); - - if (inPenaltyBox[currentPlayer]) { - if (roll % 2 != 0) { - isGettingOutOfPenaltyBox = true; - - System.out.println(players.get(currentPlayer) + " is getting out of the penalty box"); - places[currentPlayer] = places[currentPlayer] + roll; - if (places[currentPlayer] > 11) places[currentPlayer] = places[currentPlayer] - 12; - - System.out.println(players.get(currentPlayer) - + "'s new location is " - + places[currentPlayer]); - System.out.println("The category is " + currentCategory()); - askQuestion(); - } else { - System.out.println(players.get(currentPlayer) + " is not getting out of the penalty box"); - isGettingOutOfPenaltyBox = false; - } - - } else { - - places[currentPlayer] = places[currentPlayer] + roll; - if (places[currentPlayer] > 11) places[currentPlayer] = places[currentPlayer] - 12; - - System.out.println(players.get(currentPlayer) - + "'s new location is " - + places[currentPlayer]); - System.out.println("The category is " + currentCategory()); - askQuestion(); - } - - } - - private void askQuestion() { - if (currentCategory() == "Pop") - System.out.println(popQuestions.removeFirst()); - if (currentCategory() == "Science") - System.out.println(scienceQuestions.removeFirst()); - if (currentCategory() == "Sports") - System.out.println(sportsQuestions.removeFirst()); - if (currentCategory() == "Rock") - System.out.println(rockQuestions.removeFirst()); - } - - - private String currentCategory() { - if (places[currentPlayer] == 0) return "Pop"; - if (places[currentPlayer] == 4) return "Pop"; - if (places[currentPlayer] == 8) return "Pop"; - if (places[currentPlayer] == 1) return "Science"; - if (places[currentPlayer] == 5) return "Science"; - if (places[currentPlayer] == 9) return "Science"; - if (places[currentPlayer] == 2) return "Sports"; - if (places[currentPlayer] == 6) return "Sports"; - if (places[currentPlayer] == 10) return "Sports"; - return "Rock"; - } - - public boolean wasCorrectlyAnswered() { - if (inPenaltyBox[currentPlayer]){ - if (isGettingOutOfPenaltyBox) { - System.out.println("Answer was correct!!!!"); - purses[currentPlayer]++; - System.out.println(players.get(currentPlayer) - + " now has " - + purses[currentPlayer] - + " Gold Coins."); - - boolean winner = didPlayerWin(); - currentPlayer++; - if (currentPlayer == players.size()) currentPlayer = 0; - - return winner; - } else { - currentPlayer++; - if (currentPlayer == players.size()) currentPlayer = 0; - return true; - } - - - - } else { - - System.out.println("Answer was corrent!!!!"); - purses[currentPlayer]++; - System.out.println(players.get(currentPlayer) - + " now has " - + purses[currentPlayer] - + " Gold Coins."); - - boolean winner = didPlayerWin(); - currentPlayer++; - if (currentPlayer == players.size()) currentPlayer = 0; - - return winner; - } - } - - public boolean wrongAnswer(){ - System.out.println("Question was incorrectly answered"); - System.out.println(players.get(currentPlayer)+ " was sent to the penalty box"); - inPenaltyBox[currentPlayer] = true; - - currentPlayer++; - if (currentPlayer == players.size()) currentPlayer = 0; - return true; - } - - - private boolean didPlayerWin() { - return !(purses[currentPlayer] == 6); - } + private void updateCurrentPlayer(Player currentPlayer) { + players = players.updateCurrentPlayer(currentPlayer); + } + + private void askQuestion() { + notifications.askQuestion(deck.removeFirstQuestion(currentCategory())); + } + + private Category currentCategory() { + return Category.fromPosition(currentPlayer().getPosition()); + } + + private Player currentPlayer() { + return players.getCurrentPlayer(); + } } diff --git a/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Notifications.java b/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Notifications.java new file mode 100755 index 00000000..589b0d3d --- /dev/null +++ b/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Notifications.java @@ -0,0 +1,61 @@ +package com.adaptionsoft.games.uglytrivia; + +class Notifications { + private final ConsolePrinter printer; + + public Notifications(ConsolePrinter printer) { + this.printer = printer; + } + + void addPlayer(String player) { + printer.sendMessage(player + " was added"); + } + + void playerPlace(int place) { + printer.sendMessage("They are player number " + place); + } + + void currentPlayer(String player) { + printer.sendMessage(player + " is the current player"); + } + + void roll(int roll) { + printer.sendMessage("They have rolled a " + roll); + } + + void notGettingOutOfPenaltyBox(String player) { + printer.sendMessage(player + " is not getting out of the penalty box"); + } + + void newLocation(String player, int location) { + printer.sendMessage(player + "'s new location is " + location); + } + + void currentCategory(String category) { + printer.sendMessage("The category is " + category); + } + + void gettingOutOfPenaltyBox(String player) { + printer.sendMessage(player + " is getting out of the penalty box"); + } + + void correctAnswer() { + printer.sendMessage("Answer was correct!!!!"); + } + + void actualGoldCoins(String player, int goldCoins) { + printer.sendMessage(player + " now has " + goldCoins + " Gold Coins."); + } + + void incorrectlyAnswered() { + printer.sendMessage("Question was incorrectly answered"); + } + + void sendInPenaltyBox(String player) { + printer.sendMessage(player + " was sent to the penalty box"); + } + + void askQuestion(String question) { + printer.sendMessage(question); + } +} diff --git a/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Player.java b/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Player.java new file mode 100755 index 00000000..485a86da --- /dev/null +++ b/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Player.java @@ -0,0 +1,137 @@ +package com.adaptionsoft.games.uglytrivia; + +import org.apache.maven.surefire.shade.booter.org.apache.commons.lang3.builder.HashCodeBuilder; + +import java.util.Optional; + +class Player { + private final String name; + private final int position; + private final int purse; + private final boolean inPenaltyBox; + private final int order; + + Player(String name, int order) { + this.name = name; + this.position = 0; + this.purse = 0; + this.inPenaltyBox = false; + this.order = order; + } + + private Player(PlayerBuilder builder) { + name = builder.name; + position = builder.position; + purse = builder.purse; + inPenaltyBox = builder.isInPenaltyBox; + order = builder.order; + } + + String getName() { + return name; + } + + int getPosition() { + return position; + } + + Player position(int position) { + int newPosition = this.position + position; + return buildNewPlayer(Optional.of(newPosition), Optional.empty(), Optional.empty()); + } + + Player reset(int borderBoard) { + Integer newPosition = position - borderBoard; + return buildNewPlayer(Optional.of(newPosition), Optional.empty(), Optional.empty()); + } + + int getPurse() { + return purse; + } + + Player addCoin() { + return buildNewPlayer(Optional.empty(), Optional.of(this.purse + 1), Optional.empty()); + } + + boolean isInPenaltyBox() { + return inPenaltyBox; + } + + Player inPenaltyBox() { + return buildNewPlayer(Optional.empty(), Optional.empty(), Optional.of(true)); + } + + Player outPenaltyBox() { + return buildNewPlayer(Optional.empty(), Optional.empty(), Optional.of(false)); + } + + public int getOrder() { + return order; + } + + private Player buildNewPlayer(Optional<Integer> position, Optional<Integer> purse, Optional<Boolean> inPenaltyBox) { + return new PlayerBuilder() + .name(name) + .position(position.orElse(this.position)) + .purse(purse.orElse(this.purse)) + .isInPenaltyBox(inPenaltyBox.orElse(this.inPenaltyBox)) + .order(order) + .build(); + } + + private static class PlayerBuilder { + + private String name; + private int position; + private int purse; + private boolean isInPenaltyBox; + private int order; + + PlayerBuilder name(String name) { + this.name = name; + + return this; + } + + PlayerBuilder position(int position) { + this.position = position; + + return this; + } + + PlayerBuilder purse(int purse) { + this.purse = purse; + + return this; + } + + PlayerBuilder isInPenaltyBox(boolean isInPenaltyBox) { + this.isInPenaltyBox = isInPenaltyBox; + + return this; + } + + PlayerBuilder order(int order) { + this.order = order; + + return this; + } + + Player build() { + return new Player(this); + } + } + + @Override + public boolean equals(Object o) { + if (this == o) return true; + if (o == null || getClass() != o.getClass()) return false; + Player player = (Player) o; + return name.equals(player.name) && order == player.getOrder(); + } + + @Override + public int hashCode() { + return new HashCodeBuilder().append(name).append(order).build(); + } +} diff --git a/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Players.java b/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Players.java new file mode 100755 index 00000000..ff848b09 --- /dev/null +++ b/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Players.java @@ -0,0 +1,59 @@ +package com.adaptionsoft.games.uglytrivia; + +import java.util.ArrayList; +import java.util.Comparator; +import java.util.List; +import java.util.stream.Collectors; + +class Players { + private final List<Player> players; + private int currentPlayer; + + Players() { + this.players = new ArrayList<>(); + } + + Players(List<Player> players, int currentPlayer) { + this.players = players.stream().sorted(Comparator.comparing(Player::getOrder)).collect(Collectors.toUnmodifiableList()); + this.currentPlayer = currentPlayer; + } + + Players add(String playerName) { + List<Player> newPlayers = new ArrayList<>(players); + newPlayers.add(new Player(playerName, players.size())); + + return new Players(newPlayers, currentPlayer); + } + + Player get(int index) { + return players.get(index); + } + + int howManyPlayers() { + return players.size(); + } + + Player getCurrentPlayer() { + return players.get(currentPlayer); + } + + Players nextPlayer() { + currentPlayer++; + if (isExceededLimitPlayers(currentPlayer)) { + currentPlayer = 0; + } + + return new Players(players, currentPlayer); + } + + private boolean isExceededLimitPlayers(int index) { + return index >= players.size(); + } + + Players updateCurrentPlayer(Player player) { + List<Player> newPlayers = new ArrayList<>(players); + newPlayers.remove(currentPlayer); + newPlayers.add(player); + return new Players(newPlayers, currentPlayer); + } +} diff --git a/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Question.java b/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Question.java new file mode 100755 index 00000000..cef34468 --- /dev/null +++ b/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Question.java @@ -0,0 +1,13 @@ +package com.adaptionsoft.games.uglytrivia; + +class Question { + private final String message; + + Question(Category category, int index) { + message = category.getCategory() + " Question " + index; + } + + String getMessage() { + return message; + } +} diff --git a/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Turn.java b/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Turn.java new file mode 100755 index 00000000..b3b29417 --- /dev/null +++ b/ugly-trivia/src/main/java/com/adaptionsoft/games/uglytrivia/Turn.java @@ -0,0 +1,70 @@ +package com.adaptionsoft.games.uglytrivia; + +class Turn { + private final Notifications notifications; + private final Board board; + private final Dice dice; + + Turn(Notifications notifications, Board board, Dice dice) { + this.notifications = notifications; + this.board = board; + this.dice = dice; + } + + Player tryToPlay(Player player) { + int roll = dice.roll(); + + notifications.currentPlayer(player.getName()); + notifications.roll(roll); + + if (canNotGettingOutOfPenaltyBox(player, roll)) { + notifications.notGettingOutOfPenaltyBox(player.getName()); + return player; + } + + return playTurn(player, roll); + } + + private Player playTurn(Player player, int roll) { + player = inPenaltyBox(player); + + player = move(player, roll); + + notifications.newLocation(player.getName(), player.getPosition()); + notifications.currentCategory(currentCategory(player).getCategory()); + + return player; + } + + private Player inPenaltyBox(Player player) { + if (player.isInPenaltyBox()) { + player = player.outPenaltyBox(); + notifications.gettingOutOfPenaltyBox(player.getName()); + } + return player; + } + + private Player move(Player player, int roll) { + return resetPlayerPosition(player.position(roll)); + } + + private Player resetPlayerPosition(Player player) { + if (board.isBeyondLimitBoard(player.getPosition())) { + return player.reset(board.getLimit()); + } + + return player; + } + + private Category currentCategory(Player player) { + return Category.fromPosition(player.getPosition()); + } + + private boolean canNotGettingOutOfPenaltyBox(Player player, int roll) { + return player.isInPenaltyBox() && isPairRoll(roll); + } + + private boolean isPairRoll(int roll) { + return roll % 2 == 0; + } +} diff --git a/ugly-trivia/src/test/java/com/adaptionsoft/games/trivia/SomeTest.java b/ugly-trivia/src/test/java/com/adaptionsoft/games/trivia/SomeTest.java deleted file mode 100644 index 6c8c4149..00000000 --- a/ugly-trivia/src/test/java/com/adaptionsoft/games/trivia/SomeTest.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.adaptionsoft.games.trivia; - -import static org.junit.Assert.*; - -import org.junit.Test; - -public class SomeTest { - - @Test - public void true_is_true() throws Exception { - assertTrue(false); - } -} diff --git a/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/AnswerRandomSimulatorUnitTest.java b/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/AnswerRandomSimulatorUnitTest.java new file mode 100755 index 00000000..c2abe12e --- /dev/null +++ b/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/AnswerRandomSimulatorUnitTest.java @@ -0,0 +1,36 @@ +package com.adaptionsoft.games.uglytrivia; + +import org.junit.jupiter.api.Test; + +import java.util.Random; + +import static org.assertj.core.api.Assertions.*; +import static org.mockito.Mockito.*; + +public class AnswerRandomSimulatorUnitTest { + Random rand = mock(Random.class); + + @Test + public void shouldCheckIsWrongAnswerForSeven() { + AnswerRandomSimulator randomSimulator = new AnswerRandomSimulator(rand); + + when(rand.nextInt(9)).thenReturn(7); + assertThat(randomSimulator.isRight()).isFalse(); + } + + @Test + public void shouldCheckIsRightAnswerForMinSeven() { + AnswerRandomSimulator randomSimulator = new AnswerRandomSimulator(rand); + + when(rand.nextInt(9)).thenReturn(6); + assertThat(randomSimulator.isRight()).isTrue(); + } + + @Test + public void shouldCheckIsRightAnswerForMoreSeven() { + AnswerRandomSimulator randomSimulator = new AnswerRandomSimulator(rand); + + when(rand.nextInt(9)).thenReturn(8); + assertThat(randomSimulator.isRight()).isTrue(); + } +} diff --git a/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/AnswerUnitTest.java b/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/AnswerUnitTest.java new file mode 100755 index 00000000..54b747ef --- /dev/null +++ b/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/AnswerUnitTest.java @@ -0,0 +1,41 @@ +package com.adaptionsoft.games.uglytrivia; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.*; +import static org.mockito.Mockito.*; + +class AnswerUnitTest { + private final Notifications notifications = mock(Notifications.class); + private final AnswerRandomSimulator randomSimulator = mock(AnswerRandomSimulator.class); + + @Test + void shouldWrongAnswer() { + Answer answer = new Answer(notifications, randomSimulator); + when(randomSimulator.isRight()).thenReturn(false); + Player player = player(); + + assertThat(answer.answer(player).isInPenaltyBox()).isTrue(); + + verify(notifications).incorrectlyAnswered(); + verify(notifications).sendInPenaltyBox(player.getName()); + } + + @Test + void shouldCorrectAnswer() { + Answer answer = new Answer(notifications, randomSimulator); + when(randomSimulator.isRight()).thenReturn(true); + Player player = player(); + + player = answer.answer(player); + assertThat(player.isInPenaltyBox()).isFalse(); + assertThat(player.getPurse()).isEqualTo(1); + + verify(notifications).correctAnswer(); + verify(notifications).actualGoldCoins(player.getName(), player.getPurse()); + } + + private Player player() { + return new Player("Julie", 0); + } +} diff --git a/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/BoardUnitTest.java b/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/BoardUnitTest.java new file mode 100755 index 00000000..0ed09a12 --- /dev/null +++ b/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/BoardUnitTest.java @@ -0,0 +1,25 @@ +package com.adaptionsoft.games.uglytrivia; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.*; + +class BoardUnitTest { + static final int BOARD_LIMIT = 10; + private final Board board = new Board(BOARD_LIMIT); + + @Test + void shouldNotBeBeyondLimit() { + assertThat(board.isBeyondLimitBoard(9)).isFalse(); + } + + @Test + void shouldBeBeyondLimit() { + assertThat(board.isBeyondLimitBoard(12)).isTrue(); + } + + @Test + void shouldGetLimit() { + assertThat(board.getLimit()).isEqualTo(BOARD_LIMIT); + } +} diff --git a/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/CategoryUnitTest.java b/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/CategoryUnitTest.java new file mode 100755 index 00000000..5f7a9f0b --- /dev/null +++ b/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/CategoryUnitTest.java @@ -0,0 +1,29 @@ +package com.adaptionsoft.games.uglytrivia; + +import org.assertj.core.api.Assertions; +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.*; + +class CategoryUnitTest { + + @Test + void shouldGetPopCategory() { + Assertions.assertThat(Category.POP.getCategory()).isEqualTo("Pop"); + } + + @Test + void shouldGetNullCategoryForUnknownPosition() { + assertThat(Category.fromPosition(50).getCategory()).isEqualTo("Rock"); + } + + @Test + void shouldGetCategoryWithPositionZero() { + assertThat(Category.fromPosition(0).getCategory()).isEqualTo("Pop"); + } + + @Test + void shouldGetCategoryWithPositionOne() { + assertThat(Category.fromPosition(1).getCategory()).isEqualTo("Science"); + } +} diff --git a/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/ConsolePrinterUnitTest.java b/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/ConsolePrinterUnitTest.java new file mode 100644 index 00000000..a5a2cd27 --- /dev/null +++ b/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/ConsolePrinterUnitTest.java @@ -0,0 +1,26 @@ +package com.adaptionsoft.games.uglytrivia; + +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; + +import java.io.PrintStream; + +import static org.assertj.core.api.Assertions.*; +import static org.mockito.Mockito.*; + +class ConsolePrinterUnitTest { + PrintStream print = mock(PrintStream.class); + + @Test + void shouldPrintMessage() { + ConsolePrinter printer = new ConsolePrinter(print); + + ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass(String.class); + + String message = "IS THIS MESSAGE"; + printer.sendMessage(message); + + verify(print).println(messageCaptor.capture()); + assertThat(messageCaptor.getValue()).isEqualTo(message); + } +} diff --git a/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/DeckUnitTest.java b/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/DeckUnitTest.java new file mode 100755 index 00000000..bd883c30 --- /dev/null +++ b/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/DeckUnitTest.java @@ -0,0 +1,49 @@ +package com.adaptionsoft.games.uglytrivia; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.*; + +class DeckUnitTest { + + @Test + void shouldRemoveOnceFirstPopQuestion() { + assertThat(new Deck(1).removeFirstQuestion(Category.POP)).isEqualTo("Pop Question 0"); + } + + @Test + void shouldRemoveTwiceFirstPopQuestion() { + Deck deck = new Deck(2); + + deck.removeFirstQuestion(Category.POP); + + assertThat(deck.removeFirstQuestion(Category.POP)).isEqualTo("Pop Question 1"); + } + + @Test + void shouldRemoveTwiceFirstScienceQuestion() { + Deck deck = new Deck(2); + + deck.removeFirstQuestion(Category.SCIENCE); + + assertThat(deck.removeFirstQuestion(Category.SCIENCE)).isEqualTo("Science Question 1"); + } + + @Test + void shouldRemoveSportsFirstScienceQuestion() { + Deck deck = new Deck(2); + + deck.removeFirstQuestion(Category.SPORTS); + + assertThat(deck.removeFirstQuestion(Category.SPORTS)).isEqualTo("Sports Question 1"); + } + + @Test + void shouldRemoveRockFirstScienceQuestion() { + Deck deck = new Deck(2); + + deck.removeFirstQuestion(Category.ROCK); + + assertThat(deck.removeFirstQuestion(Category.ROCK)).isEqualTo("Rock Question 1"); + } +} diff --git a/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/DiceUnitTest.java b/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/DiceUnitTest.java new file mode 100755 index 00000000..b9799cdd --- /dev/null +++ b/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/DiceUnitTest.java @@ -0,0 +1,20 @@ +package com.adaptionsoft.games.uglytrivia; + + +import org.junit.jupiter.api.Test; + +import java.util.Random; + +import static org.assertj.core.api.Assertions.*; +import static org.mockito.Mockito.*; + +class DiceUnitTest { + Random rand = mock(Random.class); + + @Test + public void shouldRollDice() { + Dice dice = new Dice(rand); + when(rand.nextInt(5)).thenReturn(2); + assertThat(dice.roll()).isEqualTo(3); + } +} diff --git a/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/GameUnitTest.java b/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/GameUnitTest.java new file mode 100755 index 00000000..4cd3e0bd --- /dev/null +++ b/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/GameUnitTest.java @@ -0,0 +1,132 @@ +package com.adaptionsoft.games.uglytrivia; + +import org.junit.jupiter.api.Test; +import org.mockito.ArgumentCaptor; + +import java.util.List; + +import static org.assertj.core.api.Assertions.*; +import static org.mockito.Mockito.*; + +public class GameUnitTest { + public static final String JULIE = "JULIE"; + private static final String JADE = "JADE"; + private static final String MORGANA = "Morgana"; + private static final String XENOLUNE = "Xenolune"; + private static final String LILITH = "Lilith"; + private static final String ELSA = "Elsa"; + private final Notifications notifications = mock(Notifications.class); + private final Turn turn = mock(Turn.class); + private final Answer answer = mock(Answer.class); + + private final Game game = new Game(turn, answer, notifications); + + @Test + public void shouldAddPlayer() { + assertThat(game.add(JULIE)).isTrue(); + } + + @Test + public void shouldGetOnePlayer() { + game.add(JULIE); + + verify(notifications).addPlayer(JULIE); + } + + @Test + public void shouldNotPlayableGameForOnePlayer() { + game.add(JULIE); + + assertThat(game.isPlayable()).isFalse(); + } + + @Test + public void shouldNotPlayableGameForSixPlayer() { + gamePlayable(); + game.add(MORGANA); + game.add(XENOLUNE); + game.add(LILITH); + game.add(ELSA); + + assertThat(game.isPlayable()).isFalse(); + } + + @Test + public void shouldPlayableGameForFivePlayer() { + gamePlayable(); + game.add(MORGANA); + game.add(XENOLUNE); + game.add(LILITH); + + assertThat(game.isPlayable()).isTrue(); + } + + @Test + public void shouldPlayableGameForTwoPlayers() { + gamePlayable(); + + assertThat(game.isPlayable()).isTrue(); + } + + @Test + public void shouldPlayableGameForThreePlayers() { + gamePlayable(); + game.add(MORGANA); + + assertThat(game.isPlayable()).isTrue(); + } + + @Test + public void shouldCheckNotifyAddPlayer() { + game.add(JULIE); + + verify(notifications).addPlayer(JULIE); + verify(notifications).playerPlace(1); + } + + @Test + public void shouldPlayTurnWithoutWinner() { + Player julie = new Player(JULIE, 0); + Player jade = new Player(JADE, 1); + when(turn.tryToPlay(julie)).thenReturn(julie); + when(turn.tryToPlay(jade)).thenReturn(jade); + when(answer.answer(julie)).thenReturn(julie); + when(answer.answer(jade)).thenReturn(jade); + + gamePlayable(); + assertThat(game.playTurn()).isFalse(); + assertThat(game.playTurn()).isFalse(); + + ArgumentCaptor<Player> captor = ArgumentCaptor.forClass(Player.class); + verify(turn, times(2)).tryToPlay(captor.capture()); + + List<Player> players = captor.getAllValues(); + assertThat(players).extracting("name").containsExactly(JULIE, JADE); + + verify(notifications).askQuestion("Pop Question 0"); + verify(notifications).askQuestion("Pop Question 1"); + verify(answer, times(2)).answer(any(Player.class)); + } + + @Test + public void shouldPlayTurnWithWinner() { + Player julie = new Player(JULIE, 0); + Player jade = new Player(JADE, 1); + for (int i = 0; i < 6; i++) { + jade = jade.addCoin(); + } + when(turn.tryToPlay(julie)).thenReturn(julie); + when(turn.tryToPlay(new Player(JADE, 1))).thenReturn(jade); + when(answer.answer(julie)).thenReturn(julie); + when(answer.answer(jade)).thenReturn(jade); + + gamePlayable(); + assertThat(game.playTurn()).isFalse(); + assertThat(game.playTurn()).isTrue(); + } + + private void gamePlayable() { + game.add(JULIE); + game.add(JADE); + } +} diff --git a/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/NotificationsUnitTest.java b/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/NotificationsUnitTest.java new file mode 100755 index 00000000..c59d6e69 --- /dev/null +++ b/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/NotificationsUnitTest.java @@ -0,0 +1,90 @@ +package com.adaptionsoft.games.uglytrivia; + +import org.junit.jupiter.api.Test; + +import static org.mockito.Mockito.*; + +class NotificationsUnitTest { + private static final String JULIE = "Julie"; + private final ConsolePrinter printer = mock(ConsolePrinter.class); + private final Notifications notifications = new Notifications(printer); + + @Test + void shouldNotifyAddPlayer() { + notifications.addPlayer(JULIE); + verify(printer).sendMessage("Julie was added"); + ; + } + + @Test + void shouldNotifyPlayerPlace() { + notifications.playerPlace(1); + verify(printer).sendMessage("They are player number 1"); + } + + @Test + void shouldNotifyCurrentPlayer() { + notifications.currentPlayer(JULIE); + verify(printer).sendMessage("Julie is the current player"); + } + + @Test + void shouldNotifyRoll() { + notifications.roll(1); + verify(printer).sendMessage("They have rolled a 1"); + } + + @Test + void shouldNotifyNotGettingOutPenaltyBox() { + notifications.notGettingOutOfPenaltyBox(JULIE); + verify(printer).sendMessage("Julie is not getting out of the penalty box"); + } + + @Test + void shouldNotifyNewLocation() { + notifications.newLocation(JULIE, 1); + verify(printer).sendMessage("Julie's new location is 1"); + } + + @Test + void shouldNotifyCurrentCategory() { + notifications.currentCategory("Pop"); + verify(printer).sendMessage("The category is Pop"); + } + + @Test + void shouldNotifyGettingOutPenaltyBox() { + notifications.gettingOutOfPenaltyBox(JULIE); + verify(printer).sendMessage("Julie is getting out of the penalty box"); + } + + @Test + void shouldNotifyCorrectAnswer() { + notifications.correctAnswer(); + verify(printer).sendMessage("Answer was correct!!!!"); + } + + @Test + void shouldNotifyActualGoldCoins() { + notifications.actualGoldCoins(JULIE, 1); + verify(printer).sendMessage("Julie now has 1 Gold Coins."); + } + + @Test + void shouldNotifyIncorrectlyAnswered() { + notifications.incorrectlyAnswered(); + verify(printer).sendMessage("Question was incorrectly answered"); + } + + @Test + void shouldNotifySendInPenaltyBox() { + notifications.sendInPenaltyBox(JULIE); + verify(printer).sendMessage("Julie was sent to the penalty box"); + } + + @Test + void shouldAskQuestion() { + notifications.askQuestion("question"); + verify(printer).sendMessage("question"); + } +} diff --git a/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/PlayerUnitTest.java b/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/PlayerUnitTest.java new file mode 100755 index 00000000..76ffe84b --- /dev/null +++ b/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/PlayerUnitTest.java @@ -0,0 +1,104 @@ +package com.adaptionsoft.games.uglytrivia; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.*; + +public class PlayerUnitTest { + + private static final String JULIE = "Julie"; + private static final String PLAYER = "Player"; + private static final String JADE = "Jade"; + + @Test + public void shouldCreatePlayer() { + assertThat(player().getPosition()).isEqualTo(0); + assertThat(player().getPurse()).isEqualTo(0); + assertThat(player().getOrder()).isEqualTo(0); + } + + @Test + public void shouldGetPlayerName() { + assertThat(player().getName()).isEqualTo(JULIE); + } + + @Test + public void shouldGetNewPlayerPosition() { + Player player = player().position(1); + + assertThat(player.getPosition()).isEqualTo(1); + } + + @Test + public void shouldGetSecondNewPlayerPosition() { + Player player = player().position(1).position(1); + + assertThat(player.getPosition()).isEqualTo(2); + } + + @Test + public void shouldResetPlayerPosition() { + Player player = player().position(1).reset(1); + + assertThat(player.getPosition()).isEqualTo(0); + } + + @Test + public void shouldAddGoldCoin() { + Player player = player().addCoin(); + + assertThat(player.getPurse()).isEqualTo(1); + } + + @Test + public void shouldInPenalTyBox() { + Player player = player().inPenaltyBox(); + assertThat(player.isInPenaltyBox()).isTrue(); + } + + @Test + public void shouldOutPenalTyBox() { + Player player = player().inPenaltyBox().outPenaltyBox(); + assertThat(player.isInPenaltyBox()).isFalse(); + } + + @Test + public void shouldBeEqual() { + assertThat(player().addCoin().equals(player())).isTrue(); + } + + @Test + public void shouldNotBeEqualWhenNameIsDifferent() { + assertThat(new Player(JADE, 0).equals(player())).isFalse(); + } + + @Test + public void shouldNotBeEqualWhenOrderIsDifferent() { + assertThat(new Player(JULIE, 1).equals(player())).isFalse(); + } + + @Test + public void shouldGetSameHashCodeWithSamePlayer() { + assertThat(player().hashCode()).isEqualTo(player().hashCode()); + } + + @Test + public void shouldGetDifferentHashCodeWithDifferentPlayer() { + assertThat(player().hashCode()).isNotEqualTo(new Player(PLAYER, 1).hashCode()); + } + + @Test + public void shouldGetDifferentHashCodeWithDifferentName() { + assertThat(player().hashCode()).isNotEqualTo(new Player(PLAYER, 0).hashCode()); + } + + @Test + public void shouldGetDifferentHashCodeWithDifferentOrder() { + assertThat(player().hashCode()).isNotEqualTo(new Player(JULIE, 1).hashCode()); + } + + + private Player player() { + return new Player(JULIE, 0); + } +} diff --git a/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/PlayersUnitTest.java b/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/PlayersUnitTest.java new file mode 100755 index 00000000..1df0c209 --- /dev/null +++ b/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/PlayersUnitTest.java @@ -0,0 +1,52 @@ +package com.adaptionsoft.games.uglytrivia; + + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.*; + +class PlayersUnitTest { + + static final String PLAYER_NAME = "Julie"; + private static final String JADE = "Jade"; + + @Test + void shouldAddPlayer() { + Player player = new Players().add(PLAYER_NAME).get(0); + assertThat(player.getName()).isEqualTo(PLAYER_NAME); + assertThat(player.getOrder()).isEqualTo(0); + } + + @Test + void shouldGetHowManyPlayers() { + Players players = new Players().add(PLAYER_NAME); + + assertThat(players.howManyPlayers()).isEqualTo(1); + } + + @Test + void shouldGetCurrentPlayer() { + Players players = new Players().add(PLAYER_NAME).add(JADE).nextPlayer(); + assertThat(players.getCurrentPlayer().getName()).isEqualTo(JADE); + } + + @Test + void shouldGetFirstPlayerWhenNoNextPlayer() { + Players players = new Players().add(PLAYER_NAME).add(JADE).nextPlayer().nextPlayer(); + assertThat(players.getCurrentPlayer().getName()).isEqualTo(PLAYER_NAME); + } + + @Test + void shouldUpdateCurrentPlayer() { + Players players = new Players().add(PLAYER_NAME); + Player currentPlayer = player().addCoin().inPenaltyBox(); + + currentPlayer = players.updateCurrentPlayer(currentPlayer).getCurrentPlayer(); + assertThat(currentPlayer.getPurse()).isEqualTo(1); + assertThat(currentPlayer.isInPenaltyBox()).isTrue(); + } + + private Player player() { + return new Player(PLAYER_NAME, 0); + } +} diff --git a/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/QuestionUnitTest.java b/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/QuestionUnitTest.java new file mode 100755 index 00000000..7a2d8f58 --- /dev/null +++ b/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/QuestionUnitTest.java @@ -0,0 +1,23 @@ +package com.adaptionsoft.games.uglytrivia; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.*; + +public class QuestionUnitTest { + @Test + public void shouldCreateQuestion() { + assertThat(new Question(Category.POP, 0)).isNotNull(); + } + + @Test + public void shouldGetMessageForPopCategory() { + Question question = new Question(Category.POP, 0); + assertThat(question.getMessage()).isEqualTo("Pop Question 0"); + } + + @Test + public void shouldGetMessageScienceCategory() { + assertThat(new Question(Category.SCIENCE, 1).getMessage()).isEqualTo("Science Question 1"); + } +} diff --git a/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/TurnUnitTest.java b/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/TurnUnitTest.java new file mode 100755 index 00000000..88d009c6 --- /dev/null +++ b/ugly-trivia/src/test/java/com/adaptionsoft/games/uglytrivia/TurnUnitTest.java @@ -0,0 +1,77 @@ +package com.adaptionsoft.games.uglytrivia; + +import org.junit.jupiter.api.Test; + +import static org.assertj.core.api.Assertions.*; +import static org.mockito.Mockito.*; + +public class TurnUnitTest { + static final int BOARD_LIMIT = 12; + + public static final String PLAYER_NAME = "Julie"; + private final Notifications notifications = mock(Notifications.class); + private final Dice dice = mock(Dice.class); + private final Turn turn = turn(); + + @Test + public void shouldPlayClassicTurn() { + int roll = 1; + when(dice.roll()).thenReturn(roll); + Player player = turn.tryToPlay(player()); + + assertThat(player.getPosition()).isEqualTo(1); + assertThat(player.isInPenaltyBox()).isFalse(); + + verify(notifications).roll(roll); + verify(notifications).currentPlayer(PLAYER_NAME); + verify(notifications).newLocation(PLAYER_NAME, roll); + verify(notifications).currentCategory(Category.SCIENCE.getCategory()); + + verify(notifications, never()).gettingOutOfPenaltyBox(PLAYER_NAME); + } + + @Test + public void shouldGettingOutOfPenaltyBox() { + int roll = 1; + when(dice.roll()).thenReturn(roll); + Player player = player(); + player = player.inPenaltyBox(); + + player = turn.tryToPlay(player); + + assertThat(player.isInPenaltyBox()).isFalse(); + + verify(notifications).gettingOutOfPenaltyBox(PLAYER_NAME); + verify(notifications).newLocation(PLAYER_NAME, roll); + verify(notifications).currentCategory(Category.SCIENCE.getCategory()); + } + + @Test + public void shouldNotGettingOutOfPenaltyBox() { + int roll = 2; + when(dice.roll()).thenReturn(roll); + Player player = player(); + player = player.inPenaltyBox(); + + player = turn.tryToPlay(player); + assertThat(player.isInPenaltyBox()).isTrue(); + verify(notifications).notGettingOutOfPenaltyBox(PLAYER_NAME); + } + + @Test + public void shouldResetPosition() { + when(dice.roll()).thenReturn(13); + Player player = turn.tryToPlay(player()); + + assertThat(player.getPosition()).isEqualTo(1); + verify(notifications).newLocation(PLAYER_NAME, 1); + } + + private Turn turn() { + return new Turn(notifications, new Board(BOARD_LIMIT), dice); + } + + private Player player() { + return new Player(PLAYER_NAME, 0); + } +} -- GitLab