扑克游戏(迄今为止最好的手牌)

时间:2015-10-03 22:34:29

标签: java poker

大家好我这个扑克游戏代码有问题我需要弄清楚到目前为止如何展示最好的牌"迄今为止最好的牌:AH,AD,2H,7S,KC"。例如,如果我在第一场比赛中有一对,那么在第二场比赛中它的两对或齐平替换最后一手只有在它是更高牌时。

'
import java.util.*;
//****main****
 public class Player  
 {
     private Scanner  input;
     Bankroll bankroll;
     PokerGame pokerGame;
     Bet bet;
     Hand hand;


 Player()
 {
      input = new Scanner(System.in);
 }

 void getInitialBankroll()
 {
      int numCoins;
      do
      {
           System.out.print("How many coins do you wish to insert into the      machine: ");
           numCoins = input.nextInt();
      }while (numCoins <= 0);
      System.out.println();
      bankroll = new Bankroll(numCoins);
 }

 void addCoins()
 {
      int numCoins;
      do
      {
           System.out.print("How many coins do you wish to insert into the machine: ");
           numCoins = input.nextInt();
      } while (numCoins <= 0);

      bankroll.alterBankroll(numCoins);
      System.out.println("Currently you have "+ bankroll.getBankroll()+ "   coins");
      System.out.println();
 }

 public void betAndPlay()
 {
      int coins;
      do
      {
           System.out.print("Enter a bet: 1 to 5 coins: ");
           coins = input.nextInt();
      } while (coins <=0 || coins > 5 || coins > bankroll.getBankroll());

      bet = new Bet(coins);
      pokerGame = new PokerGame(bet, bankroll, this);
      pokerGame.viewInitialHand();
      pokerGame.discardOrHoldCards();

 }

 public void displayHand(String[] handString)
 {
      System.out.println("********** Your Hand **********");
      for(int i = 0; i < 5; i++)
           System.out.println((i+1) +".  "+handString[i]);
      System.out.println("*******************************");
      System.out.println();
 }

 public void getDiscard(boolean[] x)
 {
      String ans;
      System.out.println("Hold or discard? ");
      for (int i = 0; i < 5; i++)
      {
           do
           {
                System.out.print("Hold (h) or Discard (d) card number "+(i+1)+": ");
                ans = input.next();
                if (ans.equals("h") )
                     x[i] = true; // hold
                else if (ans.equals("h") )
                     x[i] = false; // discard
           }while (!(ans.equals("h") || ans.equals("d")));
      }
      System.out.println();
 }

 public void displayResults(int payoff, int winnings)
 {
      String nameOfHand = "Lose";
      if (payoff == 250)
           nameOfHand = "Royal Flush";
      else if (payoff == 50)
           nameOfHand = "Straight Flush";
      else if (payoff == 25)
           nameOfHand = "Four of a Kind";
      else if (payoff == 9)
           nameOfHand = "Full House";
      else if (payoff == 6)
           nameOfHand = " Flush";
      else if (payoff == 4)
           nameOfHand = "Straight ";
      else if (payoff == 3)
           nameOfHand = "Three of a Kind";
      else if (payoff == 2)
           nameOfHand = "Two Pair";
      else if (payoff == 1)
           nameOfHand  = " Pair of Jacks or Better";

      if (winnings >0 )
      {
           System.out.println("Winner: "+ nameOfHand);
           System.out.println("Payoff is "+ winnings + " coins.");

      }
      else
           System.out.println("You lost your bet of  "+ bet.getBet());
           System.out.println("Current Bankroll is " +   bankroll.getBankroll());
           System.out.println();

 }

 public void quit()
 {
      int br = bankroll.getBankroll();
      System.out.println("\n******Game Over****** \n");
      if (br > 0)
           System.out.println("Returned: "+br+" coin(s)");
      else
           System.out.println("No coins remain");
      System.out.println("\n*********************");
 }


 public void menu()
 {
      String choice;
      do
      { 
       //System.out.println("Best Hand: " + something); //Best Hand so far

       System.out.println("Choose");
           System.out.println("1: Make a bet and play poker");
           System.out.println("2: Add coins to the machine ");
           System.out.println("3: Cash out and quit");
           System.out.print("Your choice: ");
           choice = input.next();
           if (choice.equals("1"))
                betAndPlay();
           else if (choice.equals("2"))
                addCoins();
      }while ((!(choice.equals("3") ) && bankroll.getBankroll() >0));
 }

 public static void main(String[] args)
 {
      Player player= new Player();
      player.getInitialBankroll();
      player.menu();
      player.quit();
 }

}     `

public class PokerGame
 {
 private Bankroll bankroll;
 private Bet bet;
 private Hand hand;
 private Player player;
 private boolean[] holdCards;
 private static Hand bestHand;

 public PokerGame(Bet coinsBet, Bankroll br, Player pl)
 {
      bankroll = br;
      bet = coinsBet;
      player = pl;
      hand = new Hand();
      holdCards = new boolean[5];

 }

 int updateBankroll(int payoff)
 {
      int winnings = payoff*(bet.getBet());   // negative for a loss
      bankroll.alterBankroll(winnings);
      return winnings;
 }

 public void viewInitialHand()
  {
      hand.newHand();
      player.displayHand(hand.getHand());
   }
   public void discardOrHoldCards()
   { 
      player.getDiscard(holdCards);
      hand.updateHand(holdCards);
      player.displayHand(hand.getHand());
  bestHand = player.evaluateHand();
    if(bestHand > count ){
        bestHand = 
    }
      int payoff = hand.evaluateHand();
      int winnings = updateBankroll(payoff);
      player.displayResults(payoff, winnings); // the hand & the number of   coins won(lost)
      }
 }

  ///////////////////Hand.java///////////////////

 public class Hand
 {
 private Card[] cards;
 private Deck deck;
 private int suits[];  // holds the number of each suit in a hand
 private int values[]; // holds the number of each type card (A,2,3,4,...K)

 public Hand()
 {
      cards = new Card[5];
      suits = new int[5];      // uses indices 1..4
      values = new int[14];   // uses indices 1..13
      deck = new Deck();
 }

 public void newHand()
 {
      deck.shuffle();
      for (int i = 0; i < 5; i++)
      {
           cards[i] = deck.deal();
           suits[cards[i].getSuit()]++ ;
           values[cards[i].getValue()]++;
           }
           sort();
 }

 public void  updateHand(boolean[] x)
 {
      for (int i = 0; i < 5; i++)
      if ( !x[i])
      {
           // remove card data for card i
           suits[cards[i].getSuit()]-- ;
           values[cards[i].getValue()]--;
           // get a new card
           cards[i] = deck.deal();
           // update data for card i
           suits[cards[i].getSuit()]++ ;
           values[cards[i].getValue()]++;
      }
      sort();
 }

 public String[] getHand()
 {

      String[] cardsInHand = new String[5];
      for (int i = 0; i < 5; i++)
           cardsInHand[i] = cards[i].getName();
      return cardsInHand;
 }

 private void sort()  // orders cards by value field; a helper function
 {
      int max; // holds the position of the highest valued card
      for (int place = 4; place > 0; place--)
      {
           max = 0;
           // find the position of the highest valued card between 0 ans place
           // the position of the high card is stored in max
           for (int i = 1; i <= place; i++)
           if ( cards[i].getValue() > cards[max].getValue())
                max = i;
           // swap the highest wlaued card with the card in position place
           Card temp = cards[place];
           cards[place] = cards[max];
           cards[max] = temp;
      }
 }

 public int  evaluateHand()
 {
      if (royalFlush())  // royal flush pays 250:1
           return 250;
      else if (straightFlush()) // straight flush pays 50:1
           return 50;
      else if (fourOfAKind()) // four of a kind
           return 25; // four of a kind pays 25:1
      else if (fullHouse()) // full house
           return 9;
      else if (flush())
           return 6;
      else if (straight())
           return 4;
      else if (threeOfAKind()) // three of a kind
           return 3;
      else if (twoPair())
           return 2;
      else if (pair())  // Jacks or better
           return 1;
      return -1;   // losing hand
 }

 private boolean royalFlush()
 {
      //10, J,Q,K,A of the same suit
      boolean sameSuit= false;  // true if all same suit
      boolean isRoyalty= false; //  true if cards are 10,J,K,Q,A
      for(int i = 1; i <=4; i++)
           if (suits[i] == 5)  // all five cards of one suit?
                sameSuit = true;
      isRoyalty = (values[1] == 1 &&
                          values[10] ==1 &&
                          values[11] ==1 &&
                          values[12] == 1 &&
                          values[13] == 1); //one Ace && one 10 && one J &&one Q&&one K
      return (sameSuit && isRoyalty); // true if both conditions are true
 }

 private boolean straightFlush()
 {
      boolean sameSuit = false;
      boolean ranksInOrder = false;
      for(int i = 1; i <=4; i++)
           if (suits[i] == 5)
                sameSuit = true;  // same suit?
      // cards in sequence?
      ranksInOrder =
      cards[1].getValue() == (cards[0].getValue() + 1) &&
      cards[2].getValue() == (cards[0].getValue() + 2) &&
      cards[3].getValue() == (cards[0].getValue() + 3) &&
      cards[4].getValue() == (cards[0].getValue() + 4);
      return (sameSuit && ranksInOrder);
 }

 private boolean flush()
 {
      for(int i = 1; i <=4; i++)
           if (suits[i] == 5)  // all the same suit?
                return true;
      return false;
 }

 private boolean fourOfAKind()
 {
      for(int i =1 ; i <= 13; i++)
           if (values[i] == 4)
                return true;
      return false;
 }

 private boolean fullHouse()
 {
      boolean three= false;
      boolean two= false;
      for(int i =1 ; i <= 13; i++)
           if (values[i] == 3)  // three of one kind
                three= true;
           else if (values[i] ==2) // two of another kind
                two = true;
      return  two && three; // both conditions
 }

 private boolean straight()
 {
      // cards in sequence?
      return
      // Ace precedes 2
       (cards[1].getValue() == (cards[0].getValue() + 1) &&
      cards[2].getValue() == (cards[0].getValue() + 2) &&
      cards[3].getValue() == (cards[0].getValue() + 3) &&
      cards[4].getValue() == (cards[0].getValue() + 4))  ||
      //Ace follows King
       (values[1] == 1 && //Ace
      values[10] ==1 &&  //Ten
      values[11]==1 &&  //Jack
      values[12] == 1 && //Queen
      values[13] == 1); //King
 }

 private boolean threeOfAKind()
 {
      for(int i =1 ; i <= 13; i++)
           if (values[i] == 3)
                return true;
      return false;
 }

 private boolean twoPair()
 {
      int count = 0;
      for( int i = 1; i <= 13; i++)
           if(values[i] == 2)   // count the number of pairs
                count++;
      return (count == 2);
 }

 private boolean pair() // Jacks or Higher
 {
      if (values[1] == 2) //pair of aces
           return true;
      for( int i = 11; i <= 13; i++) // pair of Jacks or higher
           if(values[i] ==2)
                return true;
      return false;
      }
 }

1 个答案:

答案 0 :(得分:0)

提示:你需要一种方法来比较两只手并找出这两只手中的哪一只更好。鉴于这种方法,问题的其余部分非常简单。