Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Could someone write detail comments or decriptions on the code that can help me

ID: 3664346 • Letter: C

Question

Could someone write detail comments or decriptions on the code that can help me to study the code? what is cards.length;  Card[] player2 = readCards();Integer.toString(card)? And give me some concrete example on some method calculations. I am trying to learn java language. Thank you.

import java.util.Scanner;

public class PlayCards {
  
   public static Scanner in;

   public static final String STRAIGHT_FLUSH = "STRAIGHT FLUSH";
   public static final String FOUR_OF_KIND = "FOUR OF A KIND";
   public static final String FULL_HOUSE = "FULL HOUSE";
   public static final String FLUSH = "FLUSH";
   public static final String STRAIGHT = "STRAIGHT";
   public static final String THREE_OF_KIND = "THREE OF A KIND";
   public static final String TWO_PAIR = "TWO PAIR";
   public static final String;
   public static final String HIGH_CARD = "HIGH CARD";

   public static String[] handArray = { STRAIGHT_FLUSH, FOUR_OF_KIND,
           FULL_HOUSE, FLUSH, STRAIGHT, THREE_OF_KIND, TWO_PAIR, ONE_PAIR,
           HIGH_CARD };

   public static String getCardValue(int card) {
       if (card == 1) {
           return "A";
       } else if (card >= 2 && card <= 10) {
           return Integer.toString(card);
       } else if (card == 11) {
           return "J";
       } else if (card == 12) {
           return "Q";
       } else if (card == 13) {
           return "K";
       } else {
           return "";
       }
   }

   public static String getSuitValue(char suit) {
       if (suit == 'S') {
           return "Spades";
       } else if (suit == 'D') {
           return "Diamonds";
       } else if (suit == 'H') {
           return "Hearts";
       } else if (suit == 'C') {
           return "Clubs";
       } else {
           return "";
       }
   }

   public static boolean hasAPair(Card[] cards) {
       for (int i = 0; i < cards.length - 1; ++i) {
           if (cards[i].value == cards[i + 1].value) {
               return true;
           }
       }
       return false;
   }

   public static boolean hasThreeOfAKind(Card[] cards) {
       for (int i = 0; i < cards.length - 2; ++i) {
           if (cards[i].value == cards[i + 1].value
                   && cards[i].value == cards[i + 2].value)
               return true;
       }
       return false;
   }

   public static boolean hasFourOfAKind(Card[] cards) {
       for (int i = 0; i < cards.length - 3; ++i) {
           if (cards[i].value == cards[i + 1].value
                   && cards[i].value == cards[i + 2].value
                   && cards[i].value == cards[i + 3].value)
               return true;
       }
       return false;
   }

   public static boolean hasTwoPair(Card[] cards) {
       int val = -1;
       for (int i = 0; i < cards.length - 1; ++i) {
           if (cards[i].value == cards[i + 1].value && cards[i].value != val) {
               if (val == -1) {
                   val = cards[i].value;
               } else {
                   return true;
               }
           }
       }
       return false;
   }

   public static boolean hasAFlush(Card[] cards) {
       for (int i = 0; i < cards.length - 1; ++i) {
           if (cards[i].suit != cards[i + 1].suit) {
               return false;
           }
       }
       return true;
   }

   public static boolean hasStraight(Card[] cards) {
       if (cards[0].value == 1 && cards[1].value == 10 && cards[2].value == 11
               && cards[3].value == 12 && cards[4].value == 13) {
           return true;
       }
       for (int i = 0; i < cards.length - 1; ++i) {
           if (cards[i].value != cards[i + 1].value - 1) {
               return false;
           }
       }
       return true;
   }

   public static boolean hasAFullHouse(Card[] cards) {
       int pairValue = -1;
       for (int i = 0; i < cards.length - 1; ++i) {
           if (cards[i].value == cards[i + 1].value) {
               pairValue = cards[i].value;
           }
       }
       if (pairValue != -1) {
           for (int i = 0; i < cards.length - 2; ++i) {
               if (cards[i].value == cards[i + 1].value
                       && cards[i].value == cards[i + 2].value
                       && cards[i].value != pairValue)
                   return true;
           }
       }
       return false;
   }

   public static boolean hasStraightFlush(Card[] cards) {
       return hasAFlush(cards) && hasStraight(cards);
   }

   public static String getHand(Card[] cards) {
       String ret = "";
       for (int i = 0; i < cards.length; ++i) {
           ret += getCardValue(cards[i].value)
                   + Character.toString(cards[i].suit) + " ";
       }
       return ret;
   }

   public static String bestHand(Card[] cards){
       if(hasStraightFlush(cards)){
           return STRAIGHT_FLUSH;
       }
       else if(hasFourOfAKind(cards)){
           return FOUR_OF_KIND;
       }
       else if(hasAFullHouse(cards)){
           return FULL_HOUSE;
       }
       else if(hasAFlush(cards)){
           return FLUSH;
       }
       else if(hasStraight(cards)){
           return STRAIGHT;
       }
       else if(hasThreeOfAKind(cards)){
           return THREE_OF_KIND;
       }
       else if(hasTwoPair(cards)){
           return TWO_PAIR;
       }
       else if(hasAPair(cards)){
           return ONE_PAIR;
       }
       else{
           return HIGH_CARD;
       }
   }
  
   private static int findHand(String hand){
       for(int i = 0; i < handArray.length; ++i){
           if(handArray[i].equalsIgnoreCase(hand))
               return i;
       }
       return -1;
   }
  
   public static int whoWon(String player1, String player2){
       int ind1 = findHand(player1);
       int ind2 = findHand(player2);
       return ind1 - ind2;
   }
  
   public static Card[] readCards(){
       Card[] cards = new Card[5];
       for (int i = 0; i < 5; ++i) {
           cards[i] = new Card();
           cards[i].value = in.nextInt();
           cards[i].suit = in.next().charAt(0);
       }
       return cards;
   }

   public static void main(String args[]) {
       in = new Scanner(System.in);
      
       System.out.print("Enter Player 1's cards: ");
       Card[] player1 = readCards();
       System.out.println("Player 1's hand: " + getHand(player1));
       String hand1 = bestHand(player1);
       System.out.println("Best Hand: " + hand1);
      
       in.nextLine();
       System.out.print("Enter Player 2's cards: ");
       Card[] player2 = readCards();
       System.out.println("Player 2's hand: " + getHand(player2));
       String hand2 = bestHand(player2);
       System.out.println("Best Hand: " + hand2);

       if(whoWon(hand1, hand2) < 0){
           System.out.println("Player 1 Wins!");
       }
       else if(whoWon(hand1, hand2) > 0){
           System.out.println("Player 2 Wins!");
       }
       else{
           System.out.println("Tie!");
       }
   }

}

class Card {
   public int value;
   public char suit;
}

Explanation / Answer


//PlayCards.java
import java.util.Scanner;
public class PlayCards
{
   public static Scanner in;
   //Constant string values for the cards result
   public static final String STRAIGHT_FLUSH = "STRAIGHT FLUSH";
   public static final String FOUR_OF_KIND = "FOUR OF A KIND";
   public static final String FULL_HOUSE = "FULL HOUSE";
   public static final String FLUSH = "FLUSH";
   public static final String STRAIGHT = "STRAIGHT";
   public static final String THREE_OF_KIND = "THREE OF A KIND";
   public static final String TWO_PAIR = "TWO PAIR";
   public static final String;
   public static final String HIGH_CARD = "HIGH CARD";

   //an string array of constant values at hand of player
   public static String[] handArray =
              {
                      STRAIGHT_FLUSH,
                      FOUR_OF_KIND,
                      FULL_HOUSE,
                      FLUSH, STRAIGHT,
                      THREE_OF_KIND,
                      TWO_PAIR,
                      ONE_PAIR,
                      HIGH_CARD };

   /**The method getCardValue that takes an integer ,card and returns
    * the values of card*/
   public static String getCardValue(int card)
   {
       if (card == 1)
       {
           return "A";
       }
       else if (card >= 2 && card <= 10)
       {
           //Since card is integer, to convert integer to string
           //Integer.ToString method converts the int to string
           return Integer.toString(card);
       }
       else if (card == 11)
       {
           return "J";
       }
       else if (card == 12) {
           return "Q";
       }
       else if (card == 13)
       {
           return "K";
       }
       else {
           return "";
       }
   }

   /*The method getSuitValue that takes character suit and
    * returns the suit value of card S,D , H or C*/
   public static String getSuitValue(char suit)
   {
       if (suit == 'S')
       {
           return "Spades";
       }
       else if (suit == 'D')
       {
           return "Diamonds";
       }
       else if (suit == 'H')
       {
           return "Hearts";
       }
       else if (suit == 'C')
       {
           return "Clubs";
       } else {
           return "";
       }
   }

   /*The method hasAPair that checks for the pair of cards
    * in the cards array and returns true if any two pair
    * of vlaues in the cards array is same*/
   public static boolean hasAPair(Card[] cards)
   {
       for (int i = 0; i < cards.length - 1; ++i)
       {
           //Checking pair of values of consecutive cards
           if (cards[i].value == cards[i + 1].value)
           {
               return true;
           }
       }
       return false;
   }

   /**The metod that returns true for cards that contains
    * three of a kind of values in the hand,
    * example 2 of Hearts,2 of Hearts,2 of Hearts and two of other cards*/
   public static boolean hasThreeOfAKind(Card[] cards)
   {
       for (int i = 0; i < cards.length - 2; ++i)
       {
           if (cards[i].value == cards[i + 1].value
                   && cards[i].value == cards[i + 2].value)
               return true;
       }
       return false;
   }

   /**The metod that returns true for cards that contains
    * four of a kind of values in the hand,
    * example 2 of Hearts,2 of Hearts,2 of Hearts,2 of Hearts and other card*/
   public static boolean hasFourOfAKind(Card[] cards) {
       for (int i = 0; i < cards.length - 3; ++i) {
           if (cards[i].value == cards[i + 1].value
                   && cards[i].value == cards[i + 2].value
                   && cards[i].value == cards[i + 3].value)
               return true;
       }
       return false;
   }


   /**The metod that returns true for cards that contains
    * two of a kind of values in the hand,
    * example 2 of Hearts,2 of Hearts,and two of other card*/
   public static boolean hasTwoPair(Card[] cards) {
       int val = -1;
       for (int i = 0; i < cards.length - 1; ++i) {
           if (cards[i].value == cards[i + 1].value && cards[i].value != val) {
               if (val == -1) {
                   val = cards[i].value;
               } else {
                   return true;
               }
           }
       }
       return false;
   }

   /*The method that takes the cards array of type Card that returns
    * true if the suit of cards are same*/
   public static boolean hasAFlush(Card[] cards)
   {
       for (int i = 0; i < cards.length - 1; ++i) {
           if (cards[i].suit != cards[i + 1].suit) {
               return false;
           }
       }
       return true;
   }

   public static boolean hasStraight(Card[] cards)
   {
       if (cards[0].value == 1 && cards[1].value == 10 && cards[2].value == 11
               && cards[3].value == 12 && cards[4].value == 13) {
           return true;
       }
       for (int i = 0; i < cards.length - 1; ++i) {
           if (cards[i].value != cards[i + 1].value - 1) {
               return false;
           }
       }
       return true;
   }

   public static boolean hasAFullHouse(Card[] cards) {
       int pairValue = -1;
       for (int i = 0; i < cards.length - 1; ++i) {
           if (cards[i].value == cards[i + 1].value) {
               pairValue = cards[i].value;
           }
       }
       if (pairValue != -1) {
           for (int i = 0; i < cards.length - 2; ++i) {
               if (cards[i].value == cards[i + 1].value
                       && cards[i].value == cards[i + 2].value
                       && cards[i].value != pairValue)
                   return true;
           }
       }
       return false;
   }

   public static boolean hasStraightFlush(Card[] cards)
   {
       return hasAFlush(cards) && hasStraight(cards);
   }

   /**The method that takes an array of type Card
    * that returns the list of values and suit values
    * as string value.*/
   public static String getHand(Card[] cards)
   {
       String ret = "";
       for (int i = 0; i < cards.length; ++i)
       {
           //Call getCardValue to get value
           //Covert the integer to string of suit
           //using Character.toString
           ret += getCardValue(cards[i].value)
                   + Character.toString(cards[i].suit) + " ";
       }
       return ret;
   }

   /**The method bestHand that takes cards an input
    * argument and returns the result of cards in hand
    * by calling corresponding methods */
   public static String bestHand(Card[] cards)
   {
       if(hasStraightFlush(cards))
       {
           return STRAIGHT_FLUSH;
       }
       else if(hasFourOfAKind(cards))
       {
           return FOUR_OF_KIND;
       }
       else if(hasAFullHouse(cards))
       {
           return FULL_HOUSE;
       }
       else if(hasAFlush(cards))
       {
           return FLUSH;
       }
       else if(hasStraight(cards))
       {
           return STRAIGHT;
       }
       else if(hasThreeOfAKind(cards))
       {
           return THREE_OF_KIND;
       }
       else if(hasTwoPair(cards))
       {
           return TWO_PAIR;
       }
       else if(hasAPair(cards))
       {
           return ONE_PAIR;
       }
       else{
           return HIGH_CARD;
       }
   }

   private static int findHand(String hand)
   {
       for(int i = 0; i < handArray.length; ++i){
           if(handArray[i].equalsIgnoreCase(hand))
               return i;
       }
       return -1;
   }

   public static int whoWon(String player1, String player2)
   {
       int ind1 = findHand(player1);
       int ind2 = findHand(player2);
       return ind1 - ind2;
   }

   /**The method readCards that creates an array of type Card
    * of size =5 and prompts player to enter values for card
    * value and suit */
   public static Card[] readCards()
   {
       //An array of type card of size =5
       //It is like array but takes 5 Card type values
       Card[] cards = new Card[5];
       for (int i = 0; i < 5; ++i)
       {
           //Create an instance of type Card
           cards[i] = new Card();
           //read value
           cards[i].value = in.nextInt();
           //read suit value
           cards[i].suit = in.next().charAt(0);
       }
       //returns array object
       return cards;
   }

   public static void main(String args[])
   {
       in = new Scanner(System.in);
    
       System.out.print("Enter Player 1's cards: ");
       Card[] player1 = readCards();
       System.out.println("Player 1's hand: " + getHand(player1));
       String hand1 = bestHand(player1);
       System.out.println("Best Hand: " + hand1);
    
       in.nextLine();
       System.out.print("Enter Player 2's cards: ");
       Card[] player2 = readCards();
       System.out.println("Player 2's hand: " + getHand(player2));
       String hand2 = bestHand(player2);
       System.out.println("Best Hand: " + hand2);

       if(whoWon(hand1, hand2) < 0)
       {
           System.out.println("Player 1 Wins!");
       }
       else if(whoWon(hand1, hand2) > 0){
           System.out.println("Player 2 Wins!");
       }
       else{
           System.out.println("Tie!");
       }
   }

}

------------------------------------------------------------------------------------------------------------------------

//Card.java

class Card {
   public int value;
   public char suit;
}

----------------------------------------------------------------------------------------------------------------

Sample output:

Enter Player 1's cards: 2
H
2
H
2
H
1
A
2
Q
Player 1's hand: 2H 2H 2H AA 2Q
Best Hand: THREE OF A KIND
Enter Player 2's cards:

Note: To learn any programming language, work out simple programs and write simple prorams

with basics to have clear insight in basics. To know more about above program, study the game of poker

and game cards and methods of play.

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote