- Details
- Created on Thursday, 19 February 2015 20:28
- Last Updated on Thursday, 19 February 2015 20:28
- Written by Amory KC Wong
- Hits: 5099
Please use these hints only if you really need them. As a bonus, you can check for full house, straight flush and royal flush.
Change the card to use type "int" instead of strings, this makes it easier to compare.
DeckOfCards will now need to initialize Card with "int".
Move the strings from DeckOfCards to Card because that's were it really belongs.
Add face sort routine to make checking for matches easier:
- public static void sortByFace(Card[] hand) {
- for (int i = hand.length-1; i > 0; i--)
- for (int j = 0; j < i; j++)
- if (hand[j].face > hand[j+1].face) {
- Card temp = hand[j];
- hand[j] = hand[j+1];
- hand[j+1] = temp;
- }
- }
Your exercise program will be easier if you add this routine to check cards:
- static boolean isMatch(Card[] hand, int i1, int i2) {
- if (i1 < 0 || i1 >= hand.length)
- return false;
- if (i2 < 0 || i2 >= hand.length)
- return false;
- if (hand[i1].getFace() == hand[i2].getFace())
- return true;
- return false;
- }
Last hint, here is the code to check for 2 pairs:
- static boolean isTwoPair(Card[] hand) {
- int i;
- for (i = 1; i < hand.length-2; i++)
- if (isMatch(hand, i, i-1) && !isMatch(hand, i, i-2) && !isMatch(hand, i, i+1))
- break;
- if (i+1 >= hand.length)
- return false;
- for (i += 2; i < hand.length; i++)
- if (isMatch(hand, i, i-1) && !isMatch(hand, i, i-2) && !isMatch(hand, i, i+1))
- return true;
- return false;
- }