Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Problem Statement: By the end of this assignment, you will have created a program that can compare two poker hands and declare a winner. If

image text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribedimage text in transcribed

Problem Statement: By the end of this assignment, you will have created a program that can compare two poker hands and declare a winner. If you've never played poker, review Wikipedia for the list of winning hands (in order, starting with the highest rank): e straight flush e four of a kind full house . flush * straight o three of a kind two pair one pai high card We are not using any wild cards, so Five of a kind doesn't apply to this assignment. Whenever asked to return the best possible hand, use the following String mapping to return the result "STRAIGHT FLUSH", "FOUR OF A KIND", "FULL HOUSE" , " FLUSH" , "STRAIGHT", "THREE OF A KIND", "TWO PAIR", "ONE PAIR", "HIGH CARD" We'll be comparing your results against Strings in this given array. If you return "Three" instead of "THREE OF A KIND", your answer will be counted as incorrect Also, we will be storing both the card values and suits in arrays. Card values/ranks will be stored as a String. The conversion between the two are listed below: Number Representation Card Value/Rank 2 5 6 6 10 10 12 13 Suits will be stored as a String: Spades - "S", Clubs "C", Hearts - "H", Diamond-"D" Ps.-Although not required, storing ranks in int values as well might be helpful. This might make comparing two cards easier. It's completely up to you whether you want to do it or not. IPokerHand Interface: You will notice that the PokerHand.java file implements an interface named IPokerHand. Use the functions provided by this interface to implement your functionality. IPokerHand Interface: You will notice that the PokerHand.java file implements an interface named IPokerHand. Use the functions provided by this interface to implement your functionality. formattedHand returns a Stringcontaining the given cards in a formatted manner. See examples below. bestHand returns the best possible outcome of any given hand. Returns a String. Again, plenty of examples below. compareHands can be used to compare two given hands to decide which player wins. Returns an int value DO NOT CHANGE the lPokerHand interface or any of the method signatures (the method visibility, name, and parameters) as we will be using this interface to grade your submission Card Interface: Just like IPoker, you will notice that the Card.java file implements an interface named ICard. Use the functions provided by this interface to implement the required functionality in Card.java. getRank returns a String containing the rank of a given card. Ranks are the values mentioned in the second column above. . IPokerHand Interface: You will notice that the PokerHand.java file implements an interface named IPokerHand. Use the functions provided by this interface to implement your functionality. formattedHand returns a Stringcontaining the given cards in a formatted manner. See examples below. bestHand returns the best possible outcome of any given hand. Returns a String. Again, plenty of examples below. compareHands can be used to compare two given hands to decide which player wins. Returns an int value DO NOT CHANGE the lPokerHand interface or any of the method signatures (the method visibility, name, and parameters) as we will be using this interface to grade your submission Card Interface: Just like IPoker, you will notice that the Card.java file implements an interface named ICard. Use the functions provided by this interface to implement the required functionality in Card.java. getRank returns a String containing the rank of a given card. Ranks are the values mentioned in the second column above. . Problem 1: Enter hand of cards (25 points) Allow the user to input the hand of a single player. Using Java's built-in Scanner class in the main method of PokerHand.java to achieve this functionality. You can check the tutorial here as to how you can achieve user input. You need to use the nextLine() function to read a line in one go Enter Player 1's cards: 1 S 2 C 10 D 12 H 13 D This hand represents: Ace of spades, 2 of clubs, 10 of diamonds, Queen of hearts, and King of diamonds After breaking up the string, you need to make Objects of the Card class. The method .split()might come in handy here (look it up on the Javadoc) A Poker hand is made up of 5 cards. For imitating this functionality in code, we have created an array of Card objects inside the PokerHand Class (You'll need to uncomment that code) This is just like any other array used in Java where each element of the Array is an object of type Card. Since Card is an Object, you have to create it using the new keyword, which will call Card's "constructor method". Remember to create and set the proper class/instance variables (this was called global variables in the lecture) when writing the constructor, as the instance variables are not created for you. Just to help you out a bit more with constructors, we have provided an example of creating PokerHand object inside the main method by using the new keyword and passing correct argument to the PokerHand Constructor. The array syntax has been included in the starter code to help you out. Still, if you get stuck feel free to have a look at link. Don't forget to initialize all the objects using the new keyword. Note: it is safe to assume that we will only enter numbers between 1- 13 and we will only enterlconsider strings S, D, H, and C. When we test your code, we will follow the rules of input, so it is not necessary to check for bad input. BUT one thing that you need to check for: If the input string entered doesn't have 5 BUT one thing that you need to check for If the input string entered doesn't have 5 cards or has some other garbage/invalid values, you should return an empty string in the formattedHand method. Return the cards of the player. This should happen in the formattedHand method where you should return the formatted string. Your code should now result in exactly the following: Enter Player 1's cards: 1 S2 C 10 D 12 H 13 D Player 1's hand: AS_2C_10D_QH_KD PS - Underscore represents space in the above string. Your output should contain spaces instead of underscores. This is to highlight the fact that there should not be any trailing spaces in your returned string. Problem 2: Discover player's poker hand (50 points) Print out the hand of the player, e.g. FULL HOUSE, FOUR OF A KIND, etc. You should select the best hand. That is, if a player has 3 queens and 2 jacks, you would say they have a FULL HOUSE, not three of a kind. We suggest you create methods to test for each possible hand. This will result in 8 methods (high card does not require a method). Don't forget the Ace can be high or low in a run, e.g. A, 2,3, 4, 5 is a straight, and 10, J, Q, K, A also counts as a straight. Critical at this point: cards will be entered in numerical order, e.g. 1, 2, 3, 11, 12, 13. This is HUGE and will be a key assumption that you can follow in your program, and will make this program much easier. Problem 2: Discover player's poker hand (50 points) Print out the hand of the player, e.g. FULL HOUSE, FOUR OF A KIND, etc. You should select the best hand. That is, if a player has 3 queens and 2 jacks, you would say they have a FULL HOUSE, not three of a kind. We suggest you create methods to test for each possible hand. This will result in 8 methods (high card does not require a method). Don't forget the Ace can be high or low in a run, e.g. A, 2, 3, 4, 5 is a straight, and 10, J, Q, K, A also counts as a straight. Critical at this point: cards will be entered in numerical order, e.g. 1, 2, 3, 11, 12, 13. This is HUGE and will be a key assumption that you can follow in your program, and will make this program much easier. 10, J, Q, K, A is also a straight. Straight hands of such types will still be entered as- "1 D 10 S 11 S 12 H 13 C" (Because the input needs to be sorted ALWAYS) Here's some example runs. Make sure your code prints out these exact hands, this is how we'll be testing your code. Enter Player 1's cards: 1 S 2 C 10 D 12 H 13 D Player 1's hand: AS 2C 10D QH KD Best hand: HIGH CARD Enter Player 1's cards:1S2S 3 S4 S5S Player 1's hand: AS 2S 3S 4S 5S Best hand: STRAIGHT FLUSH Enter Player 1's cards: 1 D 10 C 11 S 12 H 13 H Player 1's hand: AD 10C JS QH KH Best hand: STRAIGHT Enter Player 1's cards: 1 S 2 S 3 S4S5 S Player 1's hand: AS 2S 3S 4S 5S Best hand: STRAIGHT FLUSH Enter Player 2's cards: 2 C2 H 5 D 11 D 11 C Player 2's hand: 2C 2H 5D JD JC Best hand: TWO PAIR Problem 4: Print the winner or tie (10 points) Print the player that has the winning hand. If both players have the same hand, e.g. both players have a full house, print there's a tie. You don't need to consider which full house is higher than the other. This isn't what happens in poker but we will assume this simplification so as to make the assignment a little easier. You need to implement the compareHands method inside the PokerHand class to achieve this functionality Return 0 for a tie Return 1 if player 1 has the winning hand Return 2 if player 2 has the winning hand Enter Player 1's cards: 1 S2S 3 54S5S Player 1's hand: AS 2S 3S 4S 5S Best hand: STRAIGHT FLUSH Enter Player 2's cards: Player 1's hand: AS 2S 3S 4S 5S Best hand: STRAIGHT FLUSH Enter Player 2's cards: 2C2H5D11 D 11 Player 2's hand: 2C 2H 5D JD JC Best hand: TWO PAIR For this scenario, Player 1 is the winner since STRAIGHT FLUSH has higher precedence over TWO PAIR. Your method compareHands should return 1 for this case. Again, don't forget to use these exact keywords "STRAIGHT FLUSH", "FOUR OF A KIND", "FULL HOUSE" , "FLUSH", "STRAIGHT", "THREE OF A KIND", "TWO PAIR", "ONE PAIR", "HIGH CARD" It would be wise to associate integer priorities to the elements of an array so that you can easily compare two hands. Can you somehow use the index numbers of the above string array to achieve a comparison of two hands? You can have a method like bestHandlndex that returns the index of the type of the hand. Then you can compare the returned indices for hand1 and hand2. Class to store a hand of cards public class PokerHand implements IPokerHand private final int SIZE 5; I/This is how you declare an array of type Card ,, Card [] cards = new Card [SIZE];- /Add as many instance variables as you require Constructor for the class @param hand, String entered by the user *Use the constructor to initialze the card array ek PokerHand(String hand) //initialize your instance methods here public String formattedHand(O return null; public String bestHand ) return null; public int compareHands (PokerHand p) return public static void main(String[] args)t //Take user input and store it in a String, say input //Instantiating a new object by calling the constructor of the Pokerhand class LRokerHand band Class that stores the information related to one card of the deck public class Card implements ICard t private String rank; private String suit; //You can add more instance variables if you want Constructor for the class @param cardNumber,suit From the PokerHand's class constructor *call the constructor to the card class by passing the cardNumber kand the suit of the Card Card(int cardNumber, String suit)f //set the instance variables here public String getRank)f return null; public String getSuit) return null; public String cardInfo)f return null; Problem Statement: By the end of this assignment, you will have created a program that can compare two poker hands and declare a winner. If you've never played poker, review Wikipedia for the list of winning hands (in order, starting with the highest rank): e straight flush e four of a kind full house . flush * straight o three of a kind two pair one pai high card We are not using any wild cards, so Five of a kind doesn't apply to this assignment. Whenever asked to return the best possible hand, use the following String mapping to return the result "STRAIGHT FLUSH", "FOUR OF A KIND", "FULL HOUSE" , " FLUSH" , "STRAIGHT", "THREE OF A KIND", "TWO PAIR", "ONE PAIR", "HIGH CARD" We'll be comparing your results against Strings in this given array. If you return "Three" instead of "THREE OF A KIND", your answer will be counted as incorrect Also, we will be storing both the card values and suits in arrays. Card values/ranks will be stored as a String. The conversion between the two are listed below: Number Representation Card Value/Rank 2 5 6 6 10 10 12 13 Suits will be stored as a String: Spades - "S", Clubs "C", Hearts - "H", Diamond-"D" Ps.-Although not required, storing ranks in int values as well might be helpful. This might make comparing two cards easier. It's completely up to you whether you want to do it or not. IPokerHand Interface: You will notice that the PokerHand.java file implements an interface named IPokerHand. Use the functions provided by this interface to implement your functionality. IPokerHand Interface: You will notice that the PokerHand.java file implements an interface named IPokerHand. Use the functions provided by this interface to implement your functionality. formattedHand returns a Stringcontaining the given cards in a formatted manner. See examples below. bestHand returns the best possible outcome of any given hand. Returns a String. Again, plenty of examples below. compareHands can be used to compare two given hands to decide which player wins. Returns an int value DO NOT CHANGE the lPokerHand interface or any of the method signatures (the method visibility, name, and parameters) as we will be using this interface to grade your submission Card Interface: Just like IPoker, you will notice that the Card.java file implements an interface named ICard. Use the functions provided by this interface to implement the required functionality in Card.java. getRank returns a String containing the rank of a given card. Ranks are the values mentioned in the second column above. . IPokerHand Interface: You will notice that the PokerHand.java file implements an interface named IPokerHand. Use the functions provided by this interface to implement your functionality. formattedHand returns a Stringcontaining the given cards in a formatted manner. See examples below. bestHand returns the best possible outcome of any given hand. Returns a String. Again, plenty of examples below. compareHands can be used to compare two given hands to decide which player wins. Returns an int value DO NOT CHANGE the lPokerHand interface or any of the method signatures (the method visibility, name, and parameters) as we will be using this interface to grade your submission Card Interface: Just like IPoker, you will notice that the Card.java file implements an interface named ICard. Use the functions provided by this interface to implement the required functionality in Card.java. getRank returns a String containing the rank of a given card. Ranks are the values mentioned in the second column above. . Problem 1: Enter hand of cards (25 points) Allow the user to input the hand of a single player. Using Java's built-in Scanner class in the main method of PokerHand.java to achieve this functionality. You can check the tutorial here as to how you can achieve user input. You need to use the nextLine() function to read a line in one go Enter Player 1's cards: 1 S 2 C 10 D 12 H 13 D This hand represents: Ace of spades, 2 of clubs, 10 of diamonds, Queen of hearts, and King of diamonds After breaking up the string, you need to make Objects of the Card class. The method .split()might come in handy here (look it up on the Javadoc) A Poker hand is made up of 5 cards. For imitating this functionality in code, we have created an array of Card objects inside the PokerHand Class (You'll need to uncomment that code) This is just like any other array used in Java where each element of the Array is an object of type Card. Since Card is an Object, you have to create it using the new keyword, which will call Card's "constructor method". Remember to create and set the proper class/instance variables (this was called global variables in the lecture) when writing the constructor, as the instance variables are not created for you. Just to help you out a bit more with constructors, we have provided an example of creating PokerHand object inside the main method by using the new keyword and passing correct argument to the PokerHand Constructor. The array syntax has been included in the starter code to help you out. Still, if you get stuck feel free to have a look at link. Don't forget to initialize all the objects using the new keyword. Note: it is safe to assume that we will only enter numbers between 1- 13 and we will only enterlconsider strings S, D, H, and C. When we test your code, we will follow the rules of input, so it is not necessary to check for bad input. BUT one thing that you need to check for: If the input string entered doesn't have 5 BUT one thing that you need to check for If the input string entered doesn't have 5 cards or has some other garbage/invalid values, you should return an empty string in the formattedHand method. Return the cards of the player. This should happen in the formattedHand method where you should return the formatted string. Your code should now result in exactly the following: Enter Player 1's cards: 1 S2 C 10 D 12 H 13 D Player 1's hand: AS_2C_10D_QH_KD PS - Underscore represents space in the above string. Your output should contain spaces instead of underscores. This is to highlight the fact that there should not be any trailing spaces in your returned string. Problem 2: Discover player's poker hand (50 points) Print out the hand of the player, e.g. FULL HOUSE, FOUR OF A KIND, etc. You should select the best hand. That is, if a player has 3 queens and 2 jacks, you would say they have a FULL HOUSE, not three of a kind. We suggest you create methods to test for each possible hand. This will result in 8 methods (high card does not require a method). Don't forget the Ace can be high or low in a run, e.g. A, 2,3, 4, 5 is a straight, and 10, J, Q, K, A also counts as a straight. Critical at this point: cards will be entered in numerical order, e.g. 1, 2, 3, 11, 12, 13. This is HUGE and will be a key assumption that you can follow in your program, and will make this program much easier. Problem 2: Discover player's poker hand (50 points) Print out the hand of the player, e.g. FULL HOUSE, FOUR OF A KIND, etc. You should select the best hand. That is, if a player has 3 queens and 2 jacks, you would say they have a FULL HOUSE, not three of a kind. We suggest you create methods to test for each possible hand. This will result in 8 methods (high card does not require a method). Don't forget the Ace can be high or low in a run, e.g. A, 2, 3, 4, 5 is a straight, and 10, J, Q, K, A also counts as a straight. Critical at this point: cards will be entered in numerical order, e.g. 1, 2, 3, 11, 12, 13. This is HUGE and will be a key assumption that you can follow in your program, and will make this program much easier. 10, J, Q, K, A is also a straight. Straight hands of such types will still be entered as- "1 D 10 S 11 S 12 H 13 C" (Because the input needs to be sorted ALWAYS) Here's some example runs. Make sure your code prints out these exact hands, this is how we'll be testing your code. Enter Player 1's cards: 1 S 2 C 10 D 12 H 13 D Player 1's hand: AS 2C 10D QH KD Best hand: HIGH CARD Enter Player 1's cards:1S2S 3 S4 S5S Player 1's hand: AS 2S 3S 4S 5S Best hand: STRAIGHT FLUSH Enter Player 1's cards: 1 D 10 C 11 S 12 H 13 H Player 1's hand: AD 10C JS QH KH Best hand: STRAIGHT Enter Player 1's cards: 1 S 2 S 3 S4S5 S Player 1's hand: AS 2S 3S 4S 5S Best hand: STRAIGHT FLUSH Enter Player 2's cards: 2 C2 H 5 D 11 D 11 C Player 2's hand: 2C 2H 5D JD JC Best hand: TWO PAIR Problem 4: Print the winner or tie (10 points) Print the player that has the winning hand. If both players have the same hand, e.g. both players have a full house, print there's a tie. You don't need to consider which full house is higher than the other. This isn't what happens in poker but we will assume this simplification so as to make the assignment a little easier. You need to implement the compareHands method inside the PokerHand class to achieve this functionality Return 0 for a tie Return 1 if player 1 has the winning hand Return 2 if player 2 has the winning hand Enter Player 1's cards: 1 S2S 3 54S5S Player 1's hand: AS 2S 3S 4S 5S Best hand: STRAIGHT FLUSH Enter Player 2's cards: Player 1's hand: AS 2S 3S 4S 5S Best hand: STRAIGHT FLUSH Enter Player 2's cards: 2C2H5D11 D 11 Player 2's hand: 2C 2H 5D JD JC Best hand: TWO PAIR For this scenario, Player 1 is the winner since STRAIGHT FLUSH has higher precedence over TWO PAIR. Your method compareHands should return 1 for this case. Again, don't forget to use these exact keywords "STRAIGHT FLUSH", "FOUR OF A KIND", "FULL HOUSE" , "FLUSH", "STRAIGHT", "THREE OF A KIND", "TWO PAIR", "ONE PAIR", "HIGH CARD" It would be wise to associate integer priorities to the elements of an array so that you can easily compare two hands. Can you somehow use the index numbers of the above string array to achieve a comparison of two hands? You can have a method like bestHandlndex that returns the index of the type of the hand. Then you can compare the returned indices for hand1 and hand2. Class to store a hand of cards public class PokerHand implements IPokerHand private final int SIZE 5; I/This is how you declare an array of type Card ,, Card [] cards = new Card [SIZE];- /Add as many instance variables as you require Constructor for the class @param hand, String entered by the user *Use the constructor to initialze the card array ek PokerHand(String hand) //initialize your instance methods here public String formattedHand(O return null; public String bestHand ) return null; public int compareHands (PokerHand p) return public static void main(String[] args)t //Take user input and store it in a String, say input //Instantiating a new object by calling the constructor of the Pokerhand class LRokerHand band Class that stores the information related to one card of the deck public class Card implements ICard t private String rank; private String suit; //You can add more instance variables if you want Constructor for the class @param cardNumber,suit From the PokerHand's class constructor *call the constructor to the card class by passing the cardNumber kand the suit of the Card Card(int cardNumber, String suit)f //set the instance variables here public String getRank)f return null; public String getSuit) return null; public String cardInfo)f return null

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions