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: 3664299 • Letter: C

Question

Could someone write detail comments or decriptions on the code that can help me to study the code? 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

/*importing java.util.Scanner interface for developing our playing cards program*/

import java.util.Scanner;

/*Playcards program starts now to implement Playing cards game giving input values player1 and player2 cards*/

public class PlayCards {
  

/*we have create object for Scanner class named “in” for calling CONSTRUCTORS or functions Scanner class*/
   public static Scanner in;

/*Create the following string objects for our playing cards game,public means we have used every where,static means their no need to create instance of class,Final means the value can not be changed and String means creates String object*/

   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";

/* here we have create an String array named handArray note that this is not final i.e we can change the array values*/

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

/*here we have create String array named getCardValue for reading the cards of players,we know that in cards we have A,2,3,4,5,6,7,8,9,j,Q,K cards.Here we have reading integer values from key board and converted into string values using toString() method   */

   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 "";
       }
   }

/* here getSuitValue(char suit) method created for reading which type of suit i.e spades or Diamonds or Hearts or Clubs and it returns String value*/

   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 "";
       }
   }

/*here we have hasAPair(Card[] cards) method created for in order to check players has got a pair or not and it returns boolean value i.e True or False*/

   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;
   }

/*here we have hasThreeOfAKind(Card[] cards) method created for in order to check players has got a three cards are equal or not and it returns boolean value i.e True or 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;
   }

/*here we have hasFourOfAKind(Card[] cards) method created for in order to check players has got a four cards are equal or not and it returns boolean value i.e True or 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;
   }

/*here we have hasTwoPair(Card[] cards) method created for in order to check players has got two pairs or not and it returns boolean value i.e True or 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;
   }

/*here we have hasAFlush(Card[] cards) method created for in order to check players has got flush or not and it returns boolean value i.e True or 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;
   }

/*here we have hasStraight(Card[] cards) method created for in order to check players has got Straight or not and it returns boolean value i.e True or False*/

   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;
   }

/*here we have hasAFullHouse(Card[] cards) method created for in order to check players has got Full house or not and it returns boolean value i.e True or False*/

   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;
   }

/*here we have   hasStraightFlush(Card[] cards) method created for in order to calucate players have got Straight Flush or not using hasAFlush(), hasStraight() functions and it returns boolean value i.e True or False*/

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

/* here we have created getHand() Function to read cards of palyer,remember that it takes integer values and returns String values using Character.toString() method */

   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;
   }

/* here we have bestHand() method in order to calculate which player got best hand in priority wise*/

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;
       }
   }
  /* here we have findHand() using java.lang.String.equalsIgnoreCase() method */
   private static int findHand(String hand){
       for(int i = 0; i < handArray.length; ++i){
           if(handArray[i].equalsIgnoreCase(hand))
               return i;
       }
       return -1;
   }
  /* here whoWon() function returns integer value to know who won in the game*/
   public static int whoWon(String player1, String player2){
       int ind1 = findHand(player1);
       int ind2 = findHand(player2);
       return ind1 - ind2;
   }
  /*Here we have readCards() in order to read card vale and card type using Value and suit objects and this method returns card array.*/
   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;
   }

/*here we have our main program starts and it displays Enter Player 1's cards and we have enter the card values,it shows card values of player1

Enter Player 2's cards and we have enter the card values,it shows card values of player2 and it shows Best hand is either player1or player2 or Tie using Whowon()*/

   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(i"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!");
       }
   }

}

/* created Card class with two variables are value Integer type and suit character type*/

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

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