Implemented double or nothing game.
This commit is contained in:
parent
021886be4d
commit
9b5952b8cd
@ -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<Card> 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(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<Card> 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();
|
||||
}
|
||||
}
|
||||
|
@ -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<Card> 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<Card> 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;
|
||||
}
|
||||
|
||||
}
|
24
src/main/java/uk/co/neviyn/pokergame/model/DONResult.java
Normal file
24
src/main/java/uk/co/neviyn/pokergame/model/DONResult.java
Normal file
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user