Skip to content
Snippets Groups Projects

Compare revisions

Changes are shown as if the source revision was being merged into the target revision. Learn more about comparing revisions.

Source

Select target project
No results found

Target

Select target project
  • twitch/live-coding-fr
1 result
Show changes
Commits on Source (2)
Showing
with 540 additions and 550 deletions
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;
}
private final Notifications notifications;
private final AnswerRandomSimulator answerRandom;
public Player answer(Player player) {
if (!answerRandom.isRight()) {
return wrongAnswer(player);
}
public Answer(Notifications notifications, AnswerRandomSimulator answerRandom) {
this.notifications = notifications;
this.answerRandom = answerRandom;
}
return goodAnswer(player);
public Player answer(Player player) {
if (!answerRandom.isRight()) {
return wrongAnswer(player);
}
private Player goodAnswer(Player player) {
if (!player.isInPenaltyBox()) {
notifications.correctAnswer();
player = player.addCoin();
notifications.actualGoldCoins(player.getName(), player.getPurse());
}
return player;
}
return goodAnswer(player);
}
private Player wrongAnswer(Player player) {
notifications.incorrectlyAnswered();
notifications.sendInPenaltyBox(player.getName());
return player.inPenaltyBox();
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();
}
}
......@@ -3,13 +3,13 @@ package com.adaptionsoft.games.uglytrivia;
import java.util.Random;
class AnswerRandomSimulator {
private final Random rand;
private final Random rand;
AnswerRandomSimulator(Random rand) {
this.rand = rand;
}
AnswerRandomSimulator(Random rand) {
this.rand = rand;
}
boolean isRight() {
return !(rand.nextInt(9) == 7);
}
boolean isRight() {
return !(rand.nextInt(9) == 7);
}
}
package com.adaptionsoft.games.uglytrivia;
class Board {
private final int limitBoardSize;
private final int limitBoardSize;
Board(int limitBoardSize) {
this.limitBoardSize = limitBoardSize;
}
Board(int limitBoardSize) {
this.limitBoardSize = limitBoardSize;
}
boolean isBeyondLimitBoard(int position) {
return position >= limitBoardSize;
}
boolean isBeyondLimitBoard(int position) {
return position >= limitBoardSize;
}
int getLimit() {
return limitBoardSize;
}
int getLimit() {
return limitBoardSize;
}
}
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;
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;
}
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);
}
static Category fromPosition(int position) {
return Arrays.stream(Category.values()).filter(category -> category.positions.contains(position)).findFirst().orElse(Category.ROCK);
}
String getCategory() {
return category;
}
String getCategory() {
return category;
}
}
......@@ -3,12 +3,13 @@ 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);
}
private final PrintStream print;
public ConsolePrinter(PrintStream print) {
this.print = print;
}
void sendMessage(String message) {
print.println(message);
}
}
......@@ -5,26 +5,25 @@ import java.util.LinkedList;
import java.util.Map;
class Deck {
Map<Category, LinkedList<Question>> deck;
Map<Category, LinkedList<Question>> deck;
Deck(int limitDeckSize) {
Deck(int limitDeckSize) {
deck = new HashMap<>();
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<>());
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));
}
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();
}
String removeFirstQuestion(Category category) {
return deck.get(category).removeFirst().getMessage();
}
}
......@@ -3,13 +3,13 @@ package com.adaptionsoft.games.uglytrivia;
import java.util.Random;
class Dice {
private final Random rand;
private final Random rand;
public Dice(Random rand) {
this.rand = rand;
}
public Dice(Random rand) {
this.rand = rand;
}
public int roll() {
return rand.nextInt(5) + 1;
}
public int roll() {
return rand.nextInt(5) + 1;
}
}
package com.adaptionsoft.games.uglytrivia;
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);
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;
}
boolean isPlayable() {
return minimumPlayer() && limitPlayersNotExceeded();
}
private boolean minimumPlayer() {
return howManyPlayers() >= MIN_PLAYER_PLAYABLE;
}
players = players.nextPlayer();
return false;
}
private boolean limitPlayersNotExceeded() {
return howManyPlayers() < MAX_PLAYER_PLAYABLE;
}
boolean add(String playerName) {
private boolean didPlayerWin() {
return currentPlayer().getPurse() == MAX_GOLD_COINS;
}
players = players.add(playerName);
private void updateCurrentPlayer(Player currentPlayer) {
players = players.updateCurrentPlayer(currentPlayer);
}
notifications.addPlayer(playerName);
notifications.playerPlace(howManyPlayers());
private void askQuestion() {
notifications.askQuestion(deck.removeFirstQuestion(currentCategory()));
}
return true;
}
private Category currentCategory() {
return Category.fromPosition(currentPlayer().getPosition());
}
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;
}
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();
}
private Player currentPlayer() {
return players.getCurrentPlayer();
}
}
package com.adaptionsoft.games.uglytrivia;
class Notifications {
private final ConsolePrinter printer;
private final ConsolePrinter printer;
public Notifications(ConsolePrinter printer) {
this.printer = printer;
}
public Notifications(ConsolePrinter printer) {
this.printer = printer;
}
void addPlayer(String player) {
printer.sendMessage(player + " was added");
}
void addPlayer(String player) {
printer.sendMessage(player + " was added");
}
void playerPlace(int place) {
printer.sendMessage("They are player number " + place);
}
void playerPlace(int place) {
printer.sendMessage("They are player number " + place);
}
void currentPlayer(String player) {
printer.sendMessage(player + " is the current player");
}
void currentPlayer(String player) {
printer.sendMessage(player + " is the current player");
}
void roll(int roll) {
printer.sendMessage("They have rolled a " + roll);
}
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 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 newLocation(String player, int location) {
printer.sendMessage(player + "'s new location is " + location);
}
void currentCategory(String category) {
printer.sendMessage("The category is " + category);
}
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 gettingOutOfPenaltyBox(String player) {
printer.sendMessage(player + " is getting out of the penalty box");
}
void correctAnswer() {
printer.sendMessage("Answer was correct!!!!");
}
void correctAnswer() {
printer.sendMessage("Answer was correct!!!!");
}
void actualGoldCoins(String player, int goldCoins) {
printer.sendMessage(player + " now has " + goldCoins + " Gold Coins.");
}
void actualGoldCoins(String player, int goldCoins) {
printer.sendMessage(player + " now has " + goldCoins + " Gold Coins.");
}
void incorrectlyAnswered() {
printer.sendMessage("Question was incorrectly answered");
}
void incorrectlyAnswered() {
printer.sendMessage("Question was incorrectly answered");
}
void sendInPenaltyBox(String player) {
printer.sendMessage(player + " was sent to the penalty box");
}
void sendInPenaltyBox(String player) {
printer.sendMessage(player + " was sent to the penalty box");
}
void askQuestion(String question) {
printer.sendMessage(question);
}
void askQuestion(String question) {
printer.sendMessage(question);
}
}
package com.adaptionsoft.games.uglytrivia;
import org.apache.maven.surefire.shade.booter.org.apache.commons.lang3.builder.HashCodeBuilder;
import java.util.Optional;
import org.apache.maven.surefire.shade.booter.org.apache.commons.lang3.builder.HashCodeBuilder;
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 final String name;
private final int position;
private final int purse;
private final boolean inPenaltyBox;
private final int order;
private Player(PlayerBuilder builder) {
name = builder.name;
position = builder.position;
purse = builder.purse;
inPenaltyBox = builder.isInPenaltyBox;
order = builder.order;
}
Player(String name, int order) {
this.name = name;
this.position = 0;
this.purse = 0;
this.inPenaltyBox = false;
this.order = order;
}
String getName() {
return name;
}
private Player(PlayerBuilder builder) {
name = builder.name;
position = builder.position;
purse = builder.purse;
inPenaltyBox = builder.isInPenaltyBox;
order = builder.order;
}
int getPosition() {
return position;
}
String getName() {
return name;
}
Player position(int position) {
int newPosition = this.position + position;
return buildNewPlayer(Optional.of(newPosition), Optional.empty(), Optional.empty());
}
int getPosition() {
return position;
}
Player reset(int borderBoard) {
Integer newPosition = position - borderBoard;
return buildNewPlayer(Optional.of(newPosition), Optional.empty(), Optional.empty());
}
Player position(int position) {
int newPosition = this.position + position;
return buildNewPlayer(Optional.of(newPosition), Optional.empty(), Optional.empty());
}
int getPurse() {
return purse;
}
Player reset(int borderBoard) {
Integer newPosition = position - borderBoard;
return buildNewPlayer(Optional.of(newPosition), Optional.empty(), Optional.empty());
}
Player addCoin() {
return buildNewPlayer(Optional.empty(), Optional.of(this.purse + 1), Optional.empty());
}
int getPurse() {
return purse;
}
boolean isInPenaltyBox() {
return inPenaltyBox;
}
Player addCoin() {
return buildNewPlayer(Optional.empty(), Optional.of(this.purse + 1), Optional.empty());
}
Player inPenaltyBox() {
return buildNewPlayer(Optional.empty(), Optional.empty(), Optional.of(true));
}
Player outPenaltyBox() {
return buildNewPlayer(Optional.empty(), Optional.empty(), Optional.of(false));
}
boolean isInPenaltyBox() {
return inPenaltyBox;
}
public int getOrder() {
return order;
}
Player inPenaltyBox() {
return buildNewPlayer(Optional.empty(), Optional.empty(), Optional.of(true));
}
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();
}
Player outPenaltyBox() {
return buildNewPlayer(Optional.empty(), Optional.empty(), Optional.of(false));
}
private static class PlayerBuilder {
public int getOrder() {
return order;
}
private String name;
private int position;
private int purse;
private boolean isInPenaltyBox;
private int 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();
}
PlayerBuilder name(String name) {
this.name = name;
private static class PlayerBuilder {
private String name;
private int position;
private int purse;
private boolean isInPenaltyBox;
private int order;
return this;
}
PlayerBuilder name(String name) {
this.name = name;
PlayerBuilder position(int position) {
this.position = position;
return this;
}
return this;
}
PlayerBuilder position(int position) {
this.position = position;
PlayerBuilder purse(int purse) {
this.purse = purse;
return this;
}
return this;
}
PlayerBuilder purse(int purse) {
this.purse = purse;
PlayerBuilder isInPenaltyBox(boolean isInPenaltyBox) {
this.isInPenaltyBox = isInPenaltyBox;
return this;
}
return this;
}
PlayerBuilder isInPenaltyBox(boolean isInPenaltyBox) {
this.isInPenaltyBox = isInPenaltyBox;
PlayerBuilder order(int order) {
this.order = order;
return this;
}
return this;
}
PlayerBuilder order(int order) {
this.order = order;
Player build() {
return new Player(this);
}
return 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();
Player build() {
return new Player(this);
}
}
@Override
public int hashCode() {
return new HashCodeBuilder().append(name).append(order).build();
}
@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();
}
}
......@@ -6,54 +6,54 @@ import java.util.List;
import java.util.stream.Collectors;
class Players {
private final List<Player> players;
private int currentPlayer;
private final List<Player> players;
private int currentPlayer;
Players() {
this.players = new ArrayList<>();
}
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(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()));
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);
}
return new Players(newPlayers, currentPlayer);
}
int howManyPlayers() {
return players.size();
}
Player get(int index) {
return players.get(index);
}
Player getCurrentPlayer() {
return players.get(currentPlayer);
}
int howManyPlayers() {
return players.size();
}
Players nextPlayer() {
currentPlayer++;
if (isExceededLimitPlayers(currentPlayer)) {
currentPlayer = 0;
}
Player getCurrentPlayer() {
return players.get(currentPlayer);
}
return new Players(players, currentPlayer);
Players nextPlayer() {
currentPlayer++;
if (isExceededLimitPlayers(currentPlayer)) {
currentPlayer = 0;
}
private boolean isExceededLimitPlayers(int index) {
return index >= players.size();
}
return new Players(players, currentPlayer);
}
Players updateCurrentPlayer(Player player) {
List<Player> newPlayers = new ArrayList<>(players);
newPlayers.remove(currentPlayer);
newPlayers.add(player);
return new Players(newPlayers, 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);
}
}
package com.adaptionsoft.games.uglytrivia;
class Question {
private final String message;
private final String message;
Question(Category category, int index) {
message = category.getCategory() + " Question " + index;
}
Question(Category category, int index) {
message = category.getCategory() + " Question " + index;
}
String getMessage() {
return message;
}
String getMessage() {
return message;
}
}
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;
}
private final Notifications notifications;
private final Board board;
private final Dice dice;
Player tryToPlay(Player player) {
int roll = dice.roll();
Turn(Notifications notifications, Board board, Dice dice) {
this.notifications = notifications;
this.board = board;
this.dice = dice;
}
notifications.currentPlayer(player.getName());
notifications.roll(roll);
Player tryToPlay(Player player) {
int roll = dice.roll();
if (canNotGettingOutOfPenaltyBox(player, roll)) {
notifications.notGettingOutOfPenaltyBox(player.getName());
return player;
}
notifications.currentPlayer(player.getName());
notifications.roll(roll);
return playTurn(player, roll);
if (canNotGettingOutOfPenaltyBox(player, roll)) {
notifications.notGettingOutOfPenaltyBox(player.getName());
return player;
}
private Player playTurn(Player player, int roll) {
player = inPenaltyBox(player);
return playTurn(player, roll);
}
player = move(player, roll);
private Player playTurn(Player player, int roll) {
player = inPenaltyBox(player);
notifications.newLocation(player.getName(), player.getPosition());
notifications.currentCategory(currentCategory(player).getCategory());
player = move(player, roll);
return player;
}
notifications.newLocation(player.getName(), player.getPosition());
notifications.currentCategory(currentCategory(player).getCategory());
private Player inPenaltyBox(Player player) {
if (player.isInPenaltyBox()) {
player = player.outPenaltyBox();
notifications.gettingOutOfPenaltyBox(player.getName());
}
return player;
}
return player;
}
private Player move(Player player, int roll) {
return resetPlayerPosition(player.position(roll));
private Player inPenaltyBox(Player player) {
if (player.isInPenaltyBox()) {
player = player.outPenaltyBox();
notifications.gettingOutOfPenaltyBox(player.getName());
}
return player;
}
private Player resetPlayerPosition(Player player) {
if (board.isBeyondLimitBoard(player.getPosition())) {
return player.reset(board.getLimit());
}
private Player move(Player player, int roll) {
return resetPlayerPosition(player.position(roll));
}
return player;
private Player resetPlayerPosition(Player player) {
if (board.isBeyondLimitBoard(player.getPosition())) {
return player.reset(board.getLimit());
}
private Category currentCategory(Player player) {
return Category.fromPosition(player.getPosition());
}
return player;
}
private boolean canNotGettingOutOfPenaltyBox(Player player, int roll) {
return player.isInPenaltyBox() && isPairRoll(roll);
}
private Category currentCategory(Player player) {
return Category.fromPosition(player.getPosition());
}
private boolean isPairRoll(int roll) {
return roll % 2 == 0;
}
private boolean canNotGettingOutOfPenaltyBox(Player player, int roll) {
return player.isInPenaltyBox() && isPairRoll(roll);
}
private boolean isPairRoll(int roll) {
return roll % 2 == 0;
}
}
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.*;
import java.util.Random;
import org.junit.jupiter.api.Test;
public class AnswerRandomSimulatorUnitTest {
Random rand = mock(Random.class);
Random rand = mock(Random.class);
@Test
public void shouldCheckIsWrongAnswerForSeven() {
AnswerRandomSimulator randomSimulator = new AnswerRandomSimulator(rand);
@Test
public void shouldCheckIsWrongAnswerForSeven() {
AnswerRandomSimulator randomSimulator = new AnswerRandomSimulator(rand);
when(rand.nextInt(9)).thenReturn(7);
assertThat(randomSimulator.isRight()).isFalse();
}
when(rand.nextInt(9)).thenReturn(7);
assertThat(randomSimulator.isRight()).isFalse();
}
@Test
public void shouldCheckIsRightAnswerForMinSeven() {
AnswerRandomSimulator randomSimulator = new AnswerRandomSimulator(rand);
@Test
public void shouldCheckIsRightAnswerForMinSeven() {
AnswerRandomSimulator randomSimulator = new AnswerRandomSimulator(rand);
when(rand.nextInt(9)).thenReturn(6);
assertThat(randomSimulator.isRight()).isTrue();
}
when(rand.nextInt(9)).thenReturn(6);
assertThat(randomSimulator.isRight()).isTrue();
}
@Test
public void shouldCheckIsRightAnswerForMoreSeven() {
AnswerRandomSimulator randomSimulator = new AnswerRandomSimulator(rand);
@Test
public void shouldCheckIsRightAnswerForMoreSeven() {
AnswerRandomSimulator randomSimulator = new AnswerRandomSimulator(rand);
when(rand.nextInt(9)).thenReturn(8);
assertThat(randomSimulator.isRight()).isTrue();
}
when(rand.nextInt(9)).thenReturn(8);
assertThat(randomSimulator.isRight()).isTrue();
}
}
package com.adaptionsoft.games.uglytrivia;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.*;
import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;
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);
}
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);
}
}
package com.adaptionsoft.games.uglytrivia;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
class BoardUnitTest {
static final int BOARD_LIMIT = 10;
private final Board board = new Board(BOARD_LIMIT);
static final int BOARD_LIMIT = 10;
private final Board board = new Board(BOARD_LIMIT);
@Test
void shouldNotBeBeyondLimit() {
assertThat(board.isBeyondLimitBoard(9)).isFalse();
}
@Test
void shouldNotBeBeyondLimit() {
assertThat(board.isBeyondLimitBoard(9)).isFalse();
}
@Test
void shouldBeBeyondLimit() {
assertThat(board.isBeyondLimitBoard(12)).isTrue();
}
@Test
void shouldBeBeyondLimit() {
assertThat(board.isBeyondLimitBoard(12)).isTrue();
}
@Test
void shouldGetLimit() {
assertThat(board.getLimit()).isEqualTo(BOARD_LIMIT);
}
@Test
void shouldGetLimit() {
assertThat(board.getLimit()).isEqualTo(BOARD_LIMIT);
}
}
package com.adaptionsoft.games.uglytrivia;
import static org.assertj.core.api.Assertions.*;
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 shouldGetPopCategory() {
Assertions.assertThat(Category.POP.getCategory()).isEqualTo("Pop");
}
@Test
void shouldGetNullCategoryForUnknownPosition() {
assertThat(Category.fromPosition(50).getCategory()).isEqualTo("Rock");
}
@Test
void shouldGetNullCategoryForUnknownPosition() {
assertThat(Category.fromPosition(50).getCategory()).isEqualTo("Rock");
}
@Test
void shouldGetCategoryWithPositionZero() {
assertThat(Category.fromPosition(0).getCategory()).isEqualTo("Pop");
}
@Test
void shouldGetCategoryWithPositionZero() {
assertThat(Category.fromPosition(0).getCategory()).isEqualTo("Pop");
}
@Test
void shouldGetCategoryWithPositionOne() {
assertThat(Category.fromPosition(1).getCategory()).isEqualTo("Science");
}
@Test
void shouldGetCategoryWithPositionOne() {
assertThat(Category.fromPosition(1).getCategory()).isEqualTo("Science");
}
}
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.*;
import java.io.PrintStream;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentCaptor;
class ConsolePrinterUnitTest {
PrintStream print = mock(PrintStream.class);
PrintStream print = mock(PrintStream.class);
@Test
void shouldPrintMessage() {
ConsolePrinter printer = new ConsolePrinter(print);
@Test
void shouldPrintMessage() {
ConsolePrinter printer = new ConsolePrinter(print);
ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass(String.class);
ArgumentCaptor<String> messageCaptor = ArgumentCaptor.forClass(String.class);
String message = "IS THIS MESSAGE";
printer.sendMessage(message);
String message = "IS THIS MESSAGE";
printer.sendMessage(message);
verify(print).println(messageCaptor.capture());
assertThat(messageCaptor.getValue()).isEqualTo(message);
}
verify(print).println(messageCaptor.capture());
assertThat(messageCaptor.getValue()).isEqualTo(message);
}
}
package com.adaptionsoft.games.uglytrivia;
import org.junit.jupiter.api.Test;
import static org.assertj.core.api.Assertions.*;
import org.junit.jupiter.api.Test;
class DeckUnitTest {
@Test
void shouldRemoveOnceFirstPopQuestion() {
assertThat(new Deck(1).removeFirstQuestion(Category.POP)).isEqualTo("Pop Question 0");
}
@Test
void shouldRemoveOnceFirstPopQuestion() {
assertThat(new Deck(1).removeFirstQuestion(Category.POP)).isEqualTo("Pop Question 0");
}
@Test
void shouldRemoveTwiceFirstPopQuestion() {
Deck deck = new Deck(2);
@Test
void shouldRemoveTwiceFirstPopQuestion() {
Deck deck = new Deck(2);
deck.removeFirstQuestion(Category.POP);
deck.removeFirstQuestion(Category.POP);
assertThat(deck.removeFirstQuestion(Category.POP)).isEqualTo("Pop Question 1");
}
assertThat(deck.removeFirstQuestion(Category.POP)).isEqualTo("Pop Question 1");
}
@Test
void shouldRemoveTwiceFirstScienceQuestion() {
Deck deck = new Deck(2);
@Test
void shouldRemoveTwiceFirstScienceQuestion() {
Deck deck = new Deck(2);
deck.removeFirstQuestion(Category.SCIENCE);
deck.removeFirstQuestion(Category.SCIENCE);
assertThat(deck.removeFirstQuestion(Category.SCIENCE)).isEqualTo("Science Question 1");
}
assertThat(deck.removeFirstQuestion(Category.SCIENCE)).isEqualTo("Science Question 1");
}
@Test
void shouldRemoveSportsFirstScienceQuestion() {
Deck deck = new Deck(2);
@Test
void shouldRemoveSportsFirstScienceQuestion() {
Deck deck = new Deck(2);
deck.removeFirstQuestion(Category.SPORTS);
deck.removeFirstQuestion(Category.SPORTS);
assertThat(deck.removeFirstQuestion(Category.SPORTS)).isEqualTo("Sports Question 1");
}
assertThat(deck.removeFirstQuestion(Category.SPORTS)).isEqualTo("Sports Question 1");
}
@Test
void shouldRemoveRockFirstScienceQuestion() {
Deck deck = new Deck(2);
@Test
void shouldRemoveRockFirstScienceQuestion() {
Deck deck = new Deck(2);
deck.removeFirstQuestion(Category.ROCK);
deck.removeFirstQuestion(Category.ROCK);
assertThat(deck.removeFirstQuestion(Category.ROCK)).isEqualTo("Rock Question 1");
}
assertThat(deck.removeFirstQuestion(Category.ROCK)).isEqualTo("Rock Question 1");
}
}
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.*;
import java.util.Random;
import org.junit.jupiter.api.Test;
class DiceUnitTest {
Random rand = mock(Random.class);
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);
}
@Test
public void shouldRollDice() {
Dice dice = new Dice(rand);
when(rand.nextInt(5)).thenReturn(2);
assertThat(dice.roll()).isEqualTo(3);
}
}