/*****Version Changes************************************************************** 1.1 - sorting in GameLogic.initialize(Hand h) is now done with Arrays.sort(array) SinglePlayerPoker.showScoreSystem() "long line" problem fixed (more readable) score system altered slightly: no win is now worth 0 point instead of -5 **********************************************************************************/ import jpb.*; import java.util.*; /////////////////// //SinglePlayerPoker /////////////////// public class SinglePlayerPoker { public static void main(String[] args) { System.out.println("Version 1.1"); showScoreSystem(); SimpleIO.prompt("Please enter your name: "); String name = SimpleIO.readLine().trim(); Player player = new Player(name); System.out.println(name+", your score is: "+player.getScore()); Hand hand = new Hand(); int[] discard; while(true) { player.setScore(-5); System.out.println("\nYour hand is: "+hand); SimpleIO.prompt("\nWhich cards would you like to discard? "); String cardsToDiscard = SimpleIO.readLine().trim(); switch(cardsToDiscard.length()) { case 9: discard = new int[5]; discard[0] = Integer.parseInt(cardsToDiscard.substring(0,1)); discard[1] = Integer.parseInt(cardsToDiscard.substring(2,3)); discard[2] = Integer.parseInt(cardsToDiscard.substring(4,5)); discard[3] = Integer.parseInt(cardsToDiscard.substring(6,7)); discard[4] = Integer.parseInt(cardsToDiscard.substring(8)); break; case 7: discard = new int[4]; discard[0] = Integer.parseInt(cardsToDiscard.substring(0,1)); discard[1] = Integer.parseInt(cardsToDiscard.substring(2,3)); discard[2] = Integer.parseInt(cardsToDiscard.substring(4,5)); discard[3] = Integer.parseInt(cardsToDiscard.substring(6)); break; case 5: discard = new int[3]; discard[0] = Integer.parseInt(cardsToDiscard.substring(0,1)); discard[1] = Integer.parseInt(cardsToDiscard.substring(2,3)); discard[2] = Integer.parseInt(cardsToDiscard.substring(4)); break; case 3: discard = new int[2]; discard[0] = Integer.parseInt(cardsToDiscard.substring(0,1)); discard[1] = Integer.parseInt(cardsToDiscard.substring(2)); break; case 1: discard = new int[1]; discard[0] = Integer.parseInt(cardsToDiscard); break; default: discard = new int[1]; discard[0] = 10; } hand.newCard(discard); SimpleIO.prompt("\nYour new hand is: "+hand); String garbage = SimpleIO.readLine(); //allows for a pause in the game System.out.print("\n"+GameLogic.checkWin(hand)); int points = 0; if(GameLogic.checkWin(hand).equals("Pair")) points = 5; else if(GameLogic.checkWin(hand).equals("Two Pairs")) points = 10; else if(GameLogic.checkWin(hand).equals("Three of a Kind")) points = 15; else if(GameLogic.checkWin(hand).equals("Flush")) points = 25; else if(GameLogic.checkWin(hand).equals("Four of a Kind")) points = 50; else if(GameLogic.checkWin(hand).equals("Full House")) points = 30; else if(GameLogic.checkWin(hand).equals("Straight")) points = 20; else if(GameLogic.checkWin(hand).equals("Straight Flush")) points = 75; else if(GameLogic.checkWin(hand).equals("Royal Flush")) points = 100; else if(GameLogic.checkWin(hand).equals("Not a winning hand")) points = (0); player.setScore(points); System.out.println(", "+points+" points."); if(player.getScore()<0) { System.err.println("\nSorry, "+player.getName()+", you lost please play again."); System.exit(0); } System.out.println("\n"+player.getName()+", you have "+player.getScore()+" points."); SimpleIO.prompt("(P)lay again, (S)core system(also continues the game), (R)eset score, (Q)uit: "); String decision = SimpleIO.readLine().trim(); if(decision.equalsIgnoreCase("q")) return; else if(decision.equalsIgnoreCase("r")) { player.reset(); SimpleIO.prompt("\n"+player.getName()+", your score is now "+player.getScore()); garbage = SimpleIO.readLine(); } else if(decision.equalsIgnoreCase("s")) showScoreSystem(); hand.newHand(); } } private static void showScoreSystem() { System.out.println("\nEach hand costs 5 pts. to play."+ "\n\nThe score system is shown below:"+ "\nNo win--------------------0 pts."+ "\nPair----------------------5 pts."+ "\nTwo Pairs----------------10 pts."+ "\nThree of a Kind----------15 pts."+ "\nStraight-----------------20 pts."+ "\nFlush--------------------25 pts."+ "\nFull House---------------30 pts."+ "\nFour of a Kind-----------50 pts."+ "\nStraight Flush-----------75 pts."+ "\nRoyal Flush-------------100 pts.\n"); } } ////////// //Player ////////// class Player { private String name; private int score = 50; public Player(String name) { this.name = name; } public Player() { this.name = "Anonymous"; } public String getName() { return this.name; } public int getScore() { return this.score; } public void setScore(int score) { this.score += score; } public void reset() { this.score = 50; } } ////////// //Card ////////// class Card { // Instance variables private int rank; private int suit; // Names for ranks public static final int TWO = 0; public static final int THREE = 1; public static final int FOUR = 2; public static final int FIVE = 3; public static final int SIX = 4; public static final int SEVEN = 5; public static final int EIGHT = 6; public static final int NINE = 7; public static final int TEN = 8; public static final int JACK = 9; public static final int QUEEN = 10; public static final int KING = 11; public static final int ACE = 12; // Names for suits public static final int CLUBS = 0; public static final int DIAMONDS = 1; public static final int HEARTS = 2; public static final int SPADES = 3; // Constants for use within the class private static final String RANKS = "23456789TJQKA"; private static final String SUITS = "CDHS"; public Card(int rank, int suit) { this.rank = rank; this.suit = suit; } public int getRank() { return this.rank; } public int getSuit() { return this.suit; } public String toString() { return RANKS.charAt(rank) + "" + SUITS.charAt(suit); } public static Card pickRandom() { return new Card((int) (Math.random() * 13), (int) (Math.random() * 4)); } } ////////// //Deck ////////// class Deck { private Vector orderedDeck; private Vector deck; public Deck() { deck = new Vector(); orderedDeck = new Vector(); for(int rank=0; rank<=12; rank++) for(int suit=0; suit<=3; suit++) this.orderedDeck.addElement(new Card(rank,suit)); for(int i=0; i<52; i++) { int randomNum = (int) (Math.random()*(this.orderedDeck.size())); Card c = (Card)this.orderedDeck.elementAt(randomNum); this.deck.addElement(c); this.orderedDeck.removeElementAt(randomNum); } } public void shuffle() { deck = new Vector(); orderedDeck = new Vector(); for(int rank=0; rank<=12; rank++) for(int suit=0; suit<=3; suit++) this.orderedDeck.addElement(new Card(rank,suit)); for(int i=0; i<52; i++) { int randomNum = (int) (Math.random()*(this.orderedDeck.size())); Card c = (Card)this.orderedDeck.elementAt(randomNum); this.deck.addElement(c); this.orderedDeck.removeElementAt(randomNum); } } public Card getCard(int index) { Card c = (Card) deck.elementAt(index); return c; } } ////////// //Hand ////////// class Hand { private Card card1; private Card card2; private Card card3; private Card card4; private Card card5; private static Deck d = new Deck(); private static int currentCard = 0; public Hand(Card c1,Card c2,Card c3,Card c4,Card c5) { card1 = c1; card2 = c2; card3 = c3; card4 = c4; card5 = c5; } public Hand() { this.card1 = d.getCard(currentCard); incrementCard(); this.card2 = d.getCard(currentCard); incrementCard(); this.card3 = d.getCard(currentCard); incrementCard(); this.card4 = d.getCard(currentCard); incrementCard(); this.card5 = d.getCard(currentCard); incrementCard(); } public void newHand() { this.card1 = d.getCard(currentCard); incrementCard(); this.card2 = d.getCard(currentCard); incrementCard(); this.card3 = d.getCard(currentCard); incrementCard(); this.card4 = d.getCard(currentCard); incrementCard(); this.card5 = d.getCard(currentCard); incrementCard(); } public void newCard(int[] i) { for(int j=0; j