diff --git a/core/src/main/java/com/coderanch/blackjack/Game.java b/core/src/main/java/com/coderanch/blackjack/Game.java new file mode 100644 index 0000000..3c2210d --- /dev/null +++ b/core/src/main/java/com/coderanch/blackjack/Game.java @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2018 Coderanch. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package com.coderanch.blackjack; + +import java.util.List; + +/** + * A game of Blackjack. + */ +public final class Game { + + + /** + * Possible Blackjack game choices. + */ + public enum CHOICE { + HIT, PASS + } + + /** + * Represents whether the game has finished or not. + */ + private boolean isFinished; + + /** + * Players in the game. + */ + private final List players; + + /** + * Constructs a new game of Blackjack. + * + * @param players players in the game. + */ + public Game(List players) { + throw new UnsupportedOperationException(); + } + + /** + * Run the game. + * Deal cards to the players + */ + public void run() { + throw new UnsupportedOperationException(); + } + + /** + * Start the next turn in the game. + * Ask each player their next move. + */ + public void nextTurn() { + throw new UnsupportedOperationException(); + } + + private CHOICE getChoice(Player player) { + throw new UnsupportedOperationException(); + } + + /** + * Getter for players. + * + * @return players + */ + public List players() { + throw new UnsupportedOperationException(); + } + + /** + * Getter for isFinished. + * + * @return if the game is finished. + */ + public boolean isFinished() { + return isFinished; + } + +} diff --git a/core/src/main/java/com/coderanch/blackjack/HumanPlayer.java b/core/src/main/java/com/coderanch/blackjack/HumanPlayer.java new file mode 100644 index 0000000..3f088a4 --- /dev/null +++ b/core/src/main/java/com/coderanch/blackjack/HumanPlayer.java @@ -0,0 +1,38 @@ +/* + * Copyright (C) 2018 Coderanch. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package com.coderanch.blackjack; + +/** + * A player that is controlled by human input. + */ +public final class HumanPlayer implements Player { + @Override + public Hand hand() { + throw new UnsupportedOperationException(); + } + + @Override + public void hand(Hand hand) { + throw new UnsupportedOperationException(); + } + + @Override + public boolean isFixed() { + throw new UnsupportedOperationException(); + } + + @Override + public void isFixed(boolean isFixed) { + throw new UnsupportedOperationException(); + } + + @Override + public Game.CHOICE askChoice() { + throw new UnsupportedOperationException(); + } +} diff --git a/core/src/main/java/com/coderanch/blackjack/Player.java b/core/src/main/java/com/coderanch/blackjack/Player.java new file mode 100644 index 0000000..c3949f7 --- /dev/null +++ b/core/src/main/java/com/coderanch/blackjack/Player.java @@ -0,0 +1,51 @@ +/* + * Copyright (C) 2018 Coderanch. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package com.coderanch.blackjack; + +/** + * Represents a player in the game of Blackjack. + * Has a hand and can give choices for the game. + */ +public interface Player { + + /** + * Get the hand of the player. + * + * @return the hand of the player. + */ + Hand hand(); + + /** + * Set the hand of the player. + * + * @param hand the new hand of the player. + */ + void hand(Hand hand); + + /** + * Gets whether the player has passed or gone bust. + * + * @return whether the player can play or not. + */ + boolean isFixed(); + + /** + * Fix the player. + * The player cannot hit or pass anymore. + * + * @param isFixed whether the player is fixed or not. + */ + void isFixed(boolean isFixed); + + /** + * Asks the player for their response and returns it. + * + * @return the players choice. + */ + Game.CHOICE askChoice(); +} diff --git a/core/src/test/java/com/coderanch/blackjack/GameTest.java b/core/src/test/java/com/coderanch/blackjack/GameTest.java new file mode 100644 index 0000000..7826fe6 --- /dev/null +++ b/core/src/test/java/com/coderanch/blackjack/GameTest.java @@ -0,0 +1,127 @@ +/* + * Copyright (C) 2018 Coderanch. + * + * This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. + */ +package com.coderanch.blackjack; + +import org.junit.Test; + +import java.util.List; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.*; + + +public final class GameTest { + + /** + * The minimum value for a Blackjack hand. (2 {@link com.coderanch.blackjack.Card.Rank#TWO}) + */ + private static final int LOWEST_POSSIBLE_START_SCORE = 4; + + /** + * Tests whether the players get a hand of cards. + */ + @Test + public void run() { + var game = new Game(List.of(new HumanPlayer())); + game.run(); + game.players().forEach(p -> { + assertThat("The player must have a hand.", p.hand(), is(notNullValue())); + assertThat( + "The player must have cards in the hand.", + p.hand().bestScore(), + is(greaterThanOrEqualTo(LOWEST_POSSIBLE_START_SCORE))); + }); + } + + /** + * Tests whether the game gets new choices from the players. + */ + @Test + public void nextTurn() { + var testPlayer = new TestPlayer(); + var game = new Game(List.of(testPlayer)); + game.run(); + game.nextTurn(); + assertThat( + "The player must have chosen more than once.", + ((TestPlayer) game.players().get(0)).choiceCount, + is(greaterThan(0)) + ); + } + + /** + * Tests whether the game finishes. + */ + @Test + public void isFinished() { + var testPlayer = new TestPlayer(); + var game = new Game(List.of(testPlayer)); + assertThat("The game must not be finished.", game.isFinished(), is(equalTo(false))); + game.run(); + while (!game.players().stream().allMatch(Player::isFixed)) { + game.nextTurn(); + } + assertThat("The game must be finished.", game.isFinished(), is(equalTo(true))); + } + + /** + * Tests if the game will return the players in the game. + */ + @Test + public void players() { + var game = new Game(List.of(new HumanPlayer())); + assertThat("The player list must not be null", game.players(), is(notNullValue())); + assertThat("The player list must not be empty", game.players().size(), is(greaterThan(0))); + } + + private static final class TestPlayer implements Player { + + /** + * How many times the player chose. + */ + private int choiceCount; + + /** + * The hand of the player. + */ + private Hand hand; + + /** + * Represents whether the player is fixed or not. + */ + private boolean isFixed; + + @Override + public Hand hand() { + return null; + } + + @Override + @SuppressWarnings("checkstyle:hiddenfield") + public void hand(Hand hand) { + this.hand = hand; + } + + @Override + public boolean isFixed() { + return false; + } + + @Override + public Game.CHOICE askChoice() { + choiceCount++; + return Game.CHOICE.HIT; + } + + @Override + @SuppressWarnings("checkstyle:hiddenfield") + public void isFixed(boolean isFixed) { + this.isFixed = isFixed; + } + } +}