5 Card Poker Hand JAVA-Analyze and Categorize

时间:2018-02-14 03:23:20

标签: java arrays methods

提示:编写一个程序,从用户那里读取五张牌,然后分析这些牌并打印出他们所代表的牌的类别。

扑克手牌根据以下标签分类:同花顺,四种,满屋,同花顺,三种,两种,一对,高牌。

我目前的程序设置如下,首先提示用户输入5张卡,2-9,然后按升序排序。我设置我的程序来提示用户,然后通过几个if else语句调用方法。我有问题,但它没有确定三种或四种。

例如,如果我输入1,3,2,1,1,则将其标识为TWO PAIRS而不是Three of a Kind。 对于输入1,1,1,1,4相同,它标识为三种而不是4.

对我的代码有任何建议吗?

public static void main(String[] args)
  {
  Scanner input = new Scanner(System.in);
  final int HAND_SIZE = 5;    
  int[] hand = new int[HAND_SIZE];

  getHand(hand); //Prompt user for hand

  sortHand(hand);//Sort hand in ascending order

  if(containsFullHouse(hand))
  {
         System.out.print("FULL HOUSE!");
  }
  else if(containsStraight(hand))
  {
         System.out.print("STRAIGHT!");
  }
  else if(containsFourOfAKind(hand))
  {
         System.out.print("FOUR OF A KIND!");
  }
  else if(containsThreeOfAKind(hand))
  {
         System.out.println("THREE OF A KIND!");
  }
  else if(containsTwoPair(hand))
  {
         System.out.println("TWO PAIRS!");
  }
  else if(containsPair(hand))
  {
         System.out.println("PAIR!");
  }
  else
         System.out.println("High Card!");
}


public static void getHand(int[] hand)
{
  Scanner input = new Scanner(System.in); 
  System.out.println("Enter five numeric cards, 2-9, no face cards please");

      for(int index = 0; index < hand.length; index++)
      {
         System.out.print("Card " + (index + 1) + ": ");
         hand[index] = input.nextInt();
      }   
}

public static void sortHand(int[] hand)
{
  int startScan, index, minIndex, minValue;

  for(startScan = 0; startScan < (hand.length-1); startScan++)
  {
         minIndex = startScan;
         minValue = hand[startScan];
         for(index = startScan + 1; index <hand.length; index++)
         {
                if(hand[index] < minValue)
                {
                       minValue = hand[index];
                       minIndex = index;
                }
         }
         hand[minIndex] = hand[startScan];
         hand[startScan] = minValue;
   }
}

public static boolean containsPair(int hand[])
{
  boolean pairFound = false;
  int pairCount = 0;
  int startCheck = hand[0];

  for(int index = 1; index < hand.length; index++)
  {
         if((hand[index] - startCheck) == 0)
         {
                pairCount++;
         }
         startCheck = hand[index];
  }

  if (pairCount == 1)
  {
         pairFound = true;
  }
  else if(pairCount !=1)
  {
         pairFound = false;
  }
  return pairFound;             
}


public static boolean containsTwoPair(int hand[])
{
  boolean twoPairFound = false;
  int twoPairCount = 0;
  int startCheck = hand[0];

  for(int index = 1; index < hand.length; index++)
  {
     if((hand[index] - startCheck) == 0)
         {
            twoPairCount++;
         }       
     startCheck = hand[index];
  }  

  if (twoPairCount == 2)
  {
         twoPairFound = true;
  }
  else if(twoPairCount != 2)
  {
     twoPairFound = false;
  }

  return twoPairFound; 
}

public static boolean containsThreeOfAKind(int hand[])
{
  boolean threeFound = false;
  int threeKind = 0;
  int startCheck = hand[0];

  for(int index = 1; index < hand.length; index++)
  {
         if((hand[index] - startCheck) == 0)
         {
                threeKind++;
         }
         startCheck = hand[index];
  }   

      if(threeKind == 3)
      {
             threeFound = true;
      }
      else if(threeKind !=3)
      {
             threeFound = false;
      }
     return threeFound;
}

public static boolean containsStraight(int hand[])
{
   boolean straightFound = false;
   int straight = 0;
   int startCheck = hand[0];

   for(int index = 1; index < hand.length; index++)
   {
      if((hand[index] - startCheck) == 1)  
          {
                 straight++;
          }
          startCheck = hand[index];
   }   

   if(straight == 4)
   {
      straightFound = true;
   }       
   return straightFound;
}

public static boolean containsFullHouse(int hand[])
{
  boolean fullHouseFound = false;
  int pairCheck = 0;
  int startPairCheck = hand[0];

  for(int index = 1; index < hand.length; index++)
  {
         if((hand[index] - startPairCheck) == 0)
         {
               pairCheck++;
         }
         startPairCheck = hand[index];

  }

  int threeOfKindCheck = 0;
  int startThreeKindCheck = hand[0];

  for(int index = 1; index < hand.length; index++)
  {
         if((hand[index] - startThreeKindCheck) == 0)
         {
               threeOfKindCheck++;
         }
         startThreeKindCheck = hand[index];
  }

      if(pairCheck == 1 && startThreeKindCheck == 3)
      {
         fullHouseFound = true;       
      }       
  return fullHouseFound;
}


public static boolean containsFourOfAKind(int hand[])
{
   boolean fourFound = false;
   int fourKind = 0;
   int startCheck = hand[0];

   for(int index = 1; index < hand.length; index++)
   {
          if((hand[index] - startCheck) == 0)
          {
             fourKind++;
          }
          startCheck = hand[index];
   }   

   if(fourKind == 1)
   {
          fourFound = true;
   }
   else if(fourKind !=4)
   {
          fourFound = false;
   }
      return fourFound;
 }
}

1 个答案:

答案 0 :(得分:0)

一些提示。

从最高牌开始。这消除了许多逻辑。

即如果你先检查配对,你还必须检查以确保你的配对是唯一配对,而不是三种配对。

但是如果你已经排除了所有这些,你的代码将是支票卡1和2 23 34和45。