Commit 535bce1b authored by Colin DAMON's avatar Colin DAMON

Merge branch '26-format-trivia' into 'master'

Resolve "format trivia"

Closes #26

See merge request !23
parents 7306f26f 0915e0b9
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;
}