From 9b5952b8cd59113c38e7da3ad47996ef7073b4d2 Mon Sep 17 00:00:00 2001 From: Nathan Cannon Date: Sun, 22 Jan 2017 15:17:18 +0000 Subject: [PATCH] Implemented double or nothing game. --- src/main/java/uk/co/neviyn/pokergame/App.java | 86 +++++++++++++++---- .../pokergame/game/DoubleOrNothing.java | 54 ++++++++++++ .../co/neviyn/pokergame/model/DONResult.java | 24 ++++++ 3 files changed, 148 insertions(+), 16 deletions(-) create mode 100644 src/main/java/uk/co/neviyn/pokergame/game/DoubleOrNothing.java create mode 100644 src/main/java/uk/co/neviyn/pokergame/model/DONResult.java diff --git a/src/main/java/uk/co/neviyn/pokergame/App.java b/src/main/java/uk/co/neviyn/pokergame/App.java index 18042c5..98f9539 100644 --- a/src/main/java/uk/co/neviyn/pokergame/App.java +++ b/src/main/java/uk/co/neviyn/pokergame/App.java @@ -1,8 +1,10 @@ package uk.co.neviyn.pokergame; +import uk.co.neviyn.pokergame.game.DoubleOrNothing; import uk.co.neviyn.pokergame.model.Card; import uk.co.neviyn.pokergame.game.Deck; import uk.co.neviyn.pokergame.game.Hand; +import uk.co.neviyn.pokergame.model.DONResult; import uk.co.neviyn.pokergame.model.Result; import java.io.BufferedReader; @@ -13,8 +15,7 @@ import java.util.List; public class App { - public static void main( String[] args ) - { + public App(){ Deck deck = new Deck(); Hand hand = new Hand(deck); boolean running = true; @@ -27,29 +28,82 @@ public class App List newCards = hand.newRound(); newCards.forEach(x -> System.out.print(x.getShortName() + " ")); System.out.println(); - System.out.print("Toggle keeping a card (1-5)"); - while(true){ - String input = reader.readLine(); - try{ - int selection = Integer.parseInt(input); - hand.toggleKeepCard(selection - 1); - newCards.forEach(x -> System.out.print(x.getShortName() + (hand.cardKept(x) ? "*" : "") + " ")); - } catch (NumberFormatException ex){ - break; - } - } + System.out.println("----------"); + askToKeep(hand, newCards, reader); System.out.println("----------"); newCards = hand.drawHand(); newCards.forEach(x -> System.out.print(x.getShortName() + " ")); System.out.println(); Result result = hand.checkForWin(); - System.out.println((result.isWin() ? "Win! " : "Lose! ") + result.toString()); - if(reader.readLine().equals("q")){ - running = false; + if(result.isWin()){ + System.out.println("Win! " + result.toString()); + System.out.println("Double or Nothing? (y/n/q)"); + switch (reader.readLine()){ + case "y": + int streak = playDoubleOrNothing(reader); + System.out.println("Your streak was " + streak); + break; + case "q": + running = false; + break; + default: + break; + } + } + else{ + System.out.println("Lose! " + result.toString()); + System.out.println("Play again? (enter q to quit)"); + if(reader.readLine().equals("q")){ + running = false; + } } } } catch (IOException e) { e.printStackTrace(); } } + + private void askToKeep(Hand hand, List cards, BufferedReader reader) throws IOException{ + System.out.print("Toggle keeping a card (1-5)"); + while(true){ + String input = reader.readLine(); + try{ + int selection = Integer.parseInt(input); + hand.toggleKeepCard(selection - 1); + cards.forEach(x -> System.out.print(x.getShortName() + (hand.cardKept(x) ? "*" : "") + " ")); + } catch (NumberFormatException ex){ + break; + } + } + } + + private int playDoubleOrNothing(BufferedReader reader) throws IOException{ + int streak = 0; + DoubleOrNothing doubleOrNothing = new DoubleOrNothing(); + System.out.println("--Double or Nothing--"); + boolean playing = true; + while(playing && doubleOrNothing.isPlayable()) { + System.out.println(doubleOrNothing.getCurrentCard().getShortName() + " Higher or Lower?(h/l/q)"); + String input = reader.readLine(); + if(input.equals("q")) + playing = false; + else { + boolean higher = input.equals("h"); + DONResult result = doubleOrNothing.play(higher); + if (result.isWin()) { + System.out.println("Win, " + result.getCard().getShortName()); + streak += 1; + } else { + System.out.println("Lose, " + result.getCard().getShortName()); + playing = false; + } + } + } + return streak; + } + + public static void main( String[] args ) + { + new App(); + } } diff --git a/src/main/java/uk/co/neviyn/pokergame/game/DoubleOrNothing.java b/src/main/java/uk/co/neviyn/pokergame/game/DoubleOrNothing.java new file mode 100644 index 0000000..4cd3ed6 --- /dev/null +++ b/src/main/java/uk/co/neviyn/pokergame/game/DoubleOrNothing.java @@ -0,0 +1,54 @@ +package uk.co.neviyn.pokergame.game; + +import uk.co.neviyn.pokergame.model.Card; +import uk.co.neviyn.pokergame.model.DONResult; +import uk.co.neviyn.pokergame.model.Suit; +import uk.co.neviyn.pokergame.model.Value; + +import java.util.*; + +public class DoubleOrNothing { + + private LinkedList cards; + private Card currentCard; + + public DoubleOrNothing() { + generateCards(); + currentCard = randomCard(); + } + + public DONResult play(final boolean higher) { + Card newCard = randomCard(); + boolean win = higher ? newCard.compareTo(currentCard) >= 0 : newCard.compareTo(currentCard) <= 0; + currentCard = newCard; + return new DONResult(newCard, win); + } + + private void generateCards(){ + List cardList = new ArrayList<>(52); + for (Suit suit : Suit.standardSuits()){ + for(Value value : Value.standardValues()){ + cardList.add(new Card(suit, value)); + } + } + Collections.shuffle(cardList); + cards = new LinkedList<>(cardList); + } + + public Card getCurrentCard() { + return currentCard; + } + + public Card cheat(){ + return cards.peek(); + } + + private Card randomCard(){ + return cards.pop(); + } + + public boolean isPlayable(){ + return cards.size() > 0; + } + +} diff --git a/src/main/java/uk/co/neviyn/pokergame/model/DONResult.java b/src/main/java/uk/co/neviyn/pokergame/model/DONResult.java new file mode 100644 index 0000000..422bc84 --- /dev/null +++ b/src/main/java/uk/co/neviyn/pokergame/model/DONResult.java @@ -0,0 +1,24 @@ +package uk.co.neviyn.pokergame.model; + + +/** + * A result for the Double or Nothing mode. + */ +public class DONResult { + + private final Card card; + private final boolean win; + + public DONResult(Card card, boolean win) { + this.card = card; + this.win = win; + } + + public Card getCard() { + return card; + } + + public boolean isWin() { + return win; + } +}