Skip to content

Add game class and player interface #34

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 24 commits into
base: epic/play-against-computer
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions core/src/main/java/com/coderanch/blackjack/Game.java
Original file line number Diff line number Diff line change
@@ -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<Player> players;

/**
* Constructs a new game of Blackjack.
*
* @param players players in the game.
*/
public Game(List<Player> 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<Player> players() {
throw new UnsupportedOperationException();
}

/**
* Getter for isFinished.
*
* @return if the game is finished.
*/
public boolean isFinished() {
return isFinished;
}

}
38 changes: 38 additions & 0 deletions core/src/main/java/com/coderanch/blackjack/HumanPlayer.java
Original file line number Diff line number Diff line change
@@ -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();
}
}
51 changes: 51 additions & 0 deletions core/src/main/java/com/coderanch/blackjack/Player.java
Original file line number Diff line number Diff line change
@@ -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();
}
127 changes: 127 additions & 0 deletions core/src/test/java/com/coderanch/blackjack/GameTest.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
}