5 Card Poker Main JAVA-Analyser et catégoriser


Invite: Écrivez un programme qui lit cinq cartes de l'utilisateur, puis analyse les cartes et imprime la catégorie de main qu'elles représentent.

Les mains de Poker sont classés selon les étiquettes suivantes: Quinte flush, carré, full, couleur, quinte, brelan, deux paires, la paire, la carte la plus haute.

J'ai actuellement mon programme défini comme suit, invitant d'abord l'utilisateur pour 5 cartes, 2-9, puis triant les cartes dans l'ordre croissant. J'ai mis en place mon programme invitez l'utilisateur, puis passez par plusieurs instructions if else appelant des méthodes. J'ai des problèmes cependant où il n'identifie pas trois ou quatre d'une sorte.

Exemple, si j'entre 1, 3, 2, 1, 1, il l'identifie comme DEUX PAIRES au lieu de Trois d'une sorte. Même chose pour entrer 1, 1,1, 1, 4, il s'identifie comme trois de type au lieu de 4.

Des suggestions à mon code?

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;
 }
}
Author: user8974164, 2018-02-14

1 answers

Quelques conseils.

Commencez avec la main la plus haute. Cela élimine beaucoup de logique.

I. e si vous vérifiez pour les paires d'abord, que vous devez également vérifier que votre paire est la seule paire, et non pas trois d'une sorte.

Mais si vous avez déjà exclu tous ceux-ci, votre code serait check card 1and2 23 34 et 45.

 0
Author: Joel Libby, 2018-02-14 03:43:41