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