Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Your job is to write implementations for several Java methods to support a game of poker. (You may read online for more information about the

Your job is to write implementations for several Java methods to support a game of poker. (You may read online for more information about the game, for example, at Wikipedia.) There are several variants of poker, but the cards in the game and poker hands (flush, straight, etc.) are standard across games.

Each card value in a hand is stored as an integer value in an array. For a card of a given suit (spade, club, heart, or diamond) and rank (2 through 10, Jack, Queen, King, Ace), you can look up its integer value in the table below.

S\R

A

2

3

4

5

6

7

8

9

10

J

Q

K

1

5

9

13

17

21

25

29

33

37

41

45

49

2

6

10

14

18

22

26

30

34

38

42

46

50

3

7

11

15

19

23

27

31

35

39

43

47

51

4

8

12

16

20

24

28

32

36

40

44

48

52

Notice that there is a pattern to how the values are assigned. The use of division and remainder operators / and % can help you figure out information about the location of a given card value in this table! Most people keep their hands sorted by rank, and suit acts as a kind of tiebreaker. For this assignment, you can assume that every array is sorted in integer value order. Because there is only one of each card in the deck, that means you can assume for each index i, hand[i] < hand[i+1].

Please implement the following methods. Card value is the integer value for a specific card as seen in the table above. Rank value is the column of the table (Ace is in column 1, 2-10 is in column 2-10, Jack in 11, Queen in 12, King in 13) and Suit value is the row of the table (Spades is in row 1, Clubs is in row 2, Hearts in row 3, Diamonds in row 4).

/**

8 points: Is the given card of the given suit?

*

@param card Card value (see assignment description)

@param suit Suit value (Spade=1, Club=2, Heart=3, Diamond=4)

@return true if card is of the given suit, false otherwise (including if suit * or card is not in the specified range)

*/

public static boolean isSuit(int card, int suit)

/**

8 points: Is the given card of the given rank?

*

@param card Card value

@param rank Rank value (A=1, 2-10=2-10, J=11, Q=12, K=13)

@return true if card is of the given rank, false otherwise (including if rank

or card is not in the specified range)

*/

public static boolean isRank(int card, int rank)

/**

10 points: Is the given hand a flush? All cards must be the same suit. This

method should work for a hand of any size but you may assume all card values * are valid and there are no duplicates. HINT: Use isSuit() on each card.

*

@param hand Array of card values representing the hand

@return True if all cards in the hand are the same suit, false otherwise

*/

public static boolean isFlush(int[] hand)

/**

14 points: Is the given hand a straight? All cards must be in ascending rank.

Ace may count as either a high or low card, but not both. (A-2-3-4-5 or

10-J-Q-K-A are five card straights, but not K-A-2-3-4.) The cards will be

sorted by value, meaning that cards of a given rank will be grouped together,

but Aces will be grouped at the beginning. This method should work for a hand * of any size but you may assume all card values are valid and there are no

duplicates.

HINT: Use isRank() on each card in turn. Consider how the rank changes from * one card to the next.

*

@param hand Array of card values representing the hand

@return True if all cards in the hand are in ascending rank, false otherwise

*/

public static boolean isStraight(int[] hand)

/**

10 points: Count how many cards of a given rank appear in the hand.

*

@param hand Array of card values representing the hand

@param rank Rank value

@return Count of cards in the hand with the given rank (0 if no cards)

*/

public static int countCardsOfRank(int[] hand, int rank)

/**

10 points: Does the hand contain a run of cards of the same rank for the

given length? For example, the hand [A 2 2 2 10 K] contains a run of three

2s. It also contains a run of two 2s. HINT: Use countCardsOfRank() and try * all possible ranks.

*

@param hand Array of card values representing the hand

@param length Length of run to check for

@return True if the hand contains at least "length" cards of any one rank

*/

public boolean containsRun(int[] hand, int length) /**

20 points: Replace selected cards in your hand by drawing from the deck. For

each location in "hand," if the same location in "selected" is true, replace

the array location's value with a value (card) starting from the "top" index

in the "deck" array. You may assume that the selected array is the same

length as the hand array and there are enough cards in the deck to replace

your hand. As an example, the hand [1, 2, 3] with selected [true, false,

true], deck [5, 6, 7, 8] and topOfDeck 1 would cause the hand to become [6, * 2, 7] and 2 would be returned because two cards were replaced.

*

@param hand Array of card values representing the hand

@param replace Boolean array indicating which cards to replace

@param deck Deck of cards used to replace hand cards

@param top Location of the top of the deck within the array (allows multiple

people to use the deck)

@return Number of cards replaced

*/

public static int drawCards(int[] hand, boolean[] selected, int[] deck, int top)

/**

20 points: Return true if myHand is a winner over otherHand according to the

rules of poker. For full credit, you only need to correctly pick a winner if

each hand is in a separate one of these categories: Straight flush > four of

a kind > full house (pair and three of a kind) > flush > straight > three of * a kind > two pair > one pair. 2 points of EXTRA CREDIT: Use high card as a * tiebreaker if both are in the same category.

*

It is possible for neither hand to be a winner if they both have the same * thing, e.g. both hands have a pair of twos.

*

@param myHand Array of card values representing my hand

@param otherHand Array of card values representing the other hand * @return True if myHand is a winner over otherHand, false if it is not.

*/

public static boolean isWinner(int[] myHand, int[] otherHand)

Start code:

public class PokerFace {

/**

* 8 points: Is the given card of the given suit?

*

* @param card

* Card value (see assignment description)

* @param suit

* Suit value (Spade=1, Club=2, Heart=3, Diamond=4)

* @return true if card is of the given suit, false otherwise (including if suit

* or card is not in the specified range)

*/

public static boolean isSuit(int card, int suit) {

return false; // Instead of returning false, calculate according to the above description!

}

/**

* 8 points: Is the given card of the given rank?

*

* @param card

* Card value

* @param rank

* Rank value (A=1, 2-10=2-10, J=11, Q=12, K=13)

* @return true if card is of the given rank, false otherwise (including if rank

* or card is not in the specified range)

*/

public static boolean isRank(int card, int rank) {

return false; // Instead of returning false, calculate according to the above description!

}

/**

* 10 points: Is the given hand a flush? All cards must be the same suit. This

* method should work for a hand of any size but you may assume all card values

* are valid and there are no duplicates. HINT: Use isSuit() on each card.

*

* @param hand

* Array of card values representing the hand

* @return True if all cards in the hand are the same suit, false otherwise

*/

public static boolean isFlush(int[] hand) {

return false; // Instead of returning false, calculate according to the above description!

}

/**

* 14 points: Is the given hand a straight? All cards must be in ascending rank.

* Ace may count as either a high or low card, but not both. (A-2-3-4-5 or

* 10-J-Q-K-A are five card straights, but not K-A-2-3-4.) The cards will be

* sorted by value, meaning that cards of a given rank will be grouped together,

* but Aces will be grouped at the beginning. This method should work for a hand

* of any size but you may assume all card values are valid and there are no

* duplicates.

*

* HINT: Use isRank() on each card in turn. Consider how the rank changes from

* one card to the next.

*

* @param hand

* Array of card values representing the hand

* @return True if all cards in the hand are in ascending rank, false otherwise

*/

public static boolean isStraight(int[] hand) {

return false; // Instead of returning false, calculate according to the above description!

}

/**

* 10 points: Count how many cards of a given rank appear in the hand.

*

* @param hand

* Array of card values representing the hand

* @param rank

* Rank value

* @return

*/

public static int countCardsOfRank(int[] hand, int rank) {

return 0; // Instead of returning 0, calculate according to the above description!

}

/**

* 10 points: Does the hand contain a run of cards of the same rank for the

* given length? For example, the hand [A 2 2 2 10 K] contains a run of three

* 2s. It also contains a run of two 2s. HINT: Use countCardsOfRank() and try

* all possible ranks.

*

* @param hand

* @param length

* @return

*/

public boolean containsRun(int[] hand, int length) {

return false; // Instead of returning false, calculate according to the above description!

}

/**

* 20 points: Replace selected cards in your hand by drawing from the deck. For

* each location in "hand," if the same location in "selected" is true, replace

* the array location's value with a value (card) starting from the "top" index

* in the "deck" array. You may assume that the selected array is the same

* length as the hand array and there are enough cards in the deck to replace

* your hand. As an example, the hand [1, 2, 3] with selected [true, false,

* true], deck [5, 6, 7, 8] and top 1 would cause the hand to become [6, 2, 7]

* and 2 would be returned because two cards were replaced.

*

* @param hand

* Array of card values representing the hand

* @param replace

* Boolean array indicating which cards to replace

* @param deck

* Deck of cards used to replace hand cards

* @param topOfDeck

* Location of the top of the deck within the array (allows multiple

* people to use the deck)

* @return Number of cards replaced

*/

public static int drawCards(int[] hand, boolean[] selected, int[] deck, int top) {

return 0; // Instead of returning 0, calculate according to the above description!

}

/**

* 20 points: Return true if myHand is a winner over otherHand according to the

* rules of poker. For full credit, you only need to correctly pick a winner if

* each hand is in a separate one of these categories: Straight flush > four of

* a kind > full house (pair and three of a kind) > flush > straight > three of

* a kind > two pair > one pair.

*

* 2 points of EXTRA CREDIT: Use the high card of the category as a tiebreaker

* if both are in the same category. Ace is the highest but otherwise numerical

* designation of rank designates "height".

*

* It is possible for neither hand to be a winner if they both have the same

* thing, e.g. both hands have a pair of twos.

*

* @param myHand

* @param otherHand

* @return True if myHand is a winner, false if it is not.

*/

public static boolean isWinner(int[] myHand, int[] otherHand) {

return false; // Instead of returning false, calculate according to the above description!

}

}

Tester

import java.lang.reflect.Method;

import java.lang.reflect.Modifier;

import java.util.Arrays;

public class PokerFaceTest {

public static void main(String[] args) {

// The following code should not be used (copied and pasted or adapted in any

// way) to solve your assignment. It is designed to be difficult to understand

// and use (for pedagogical reasons, as a test program).

// Instead, you should just pay attention to the error messages to fix YOUR

// solution and make it work. Any direct use of the test code in your solution

// would be a violation of the CS 142 academic dishonesty policy.

System.out.println(

"If you don't see TOTAL SCORE at the end, your program is in an infinite loop or crashing! Use the debugger.");

int rankPoints = 0, suitPoints = 0;

int badSuit = 0, badRank = 0;

long[][] a = {

{ 0, 0, -8608481154137325568L, 4919131459786113024L, 2459565729893056512L, 1229782864946528256L, 0, 0 },

{ 0, 0, 150120014422562L, 75059940102416L, 37529970051208L, 18765052134469L, 0, 0 } };

for (int x = 0, i = -10; i <= -2; i++) {

for (int j = -4; j <= 8; j++, x++) {

for (int k = -1; k <= 6; k++) {

boolean issuit = PokerFace.isSuit(i * i - j, k);

if ((issuit ? 1L : 0L) << (x & 63) != (a[x <= 63 ? 0 : 1][k + 1] & (1L << (x & 63)))) {

if (badSuit < 10)

System.out.println("isSuit method gave unexpected result for (" + (i * i - j) + ", " + k

+ ") (expected " + !issuit + " got " + issuit + ")");

badSuit++;

if (badSuit == 10)

System.out.println("Further isSuit errors will not be printed.");

}

}

}

}

rankPoints = Math.max(0, 20 - badRank);

long[][] b = {

{ 0, 0, 0, 0, 0, 0, 0, 0, 0, -1152921504606846976L, 1080863910568919040L, 67553994410557440L,

4222124650659840L, 263882790666240L, 16492674416640L, 0, 0 },

{ 0, 0, 264913582817280L, 16557166034944L, 4089446400L, 138149888, 261120, 960, 61, 2, 0, 0, 0, 0, 0, 0,

0 } };

for (int x = 0, i = -10; i <= -2; i++) {

for (int j = -4; j <= 8; j++, x++) {

for (int k = -1; k <= 15; k++) {

boolean isrank = PokerFace.isRank(i * i - j, k);

if ((isrank ? 1L : 0L) << (x & 63) != (b[x <= 63 ? 0 : 1][k + 1] & (1L << (x & 63)))) {

if (badRank < 10)

System.out.println("isRank method gave unexpected result for (" + (i * i - j) + ", " + k

+ ") (expected " + !isrank + " got " + isrank + ")");

badRank++;

if (badRank == 10)

System.out.println("Further isRank errors will not be printed.");

}

}

}

}

suitPoints = Math.max(0, 20 - badSuit);

int[][] hands = { { 1, 5, 9, 13, 17 }, { 7, 15, 23, 27 }, { 3, 38, 43, 45, 52 }, { 6 }, { 5, 14 }, { 1, 5 },

{ 25, 26, 27, 28 }, { 1, 6, 11, 16, 19, 22, 25, 30 }, { 1, 5, 9, 13, 21 }, { 4, 5, 9, 13, 21 } };

int[][] countHands = { { 1, 2, 3, 4 }, { 5, 7, 8, 9 }, { 13, 15, 16, 17, 37, 38 } };

int[][] countCounts = { { 4, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 }, { 0, 3, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0 },

{ 0, 0, 0, 3, 1, 0, 0, 0, 0, 2, 0, 0, 0 } };

boolean[][] fs = { { true, true, false, true, false, true, false, false, true, false },

{ true, false, true, true, false, true, false, true, false, false } };

int straightPoints = 0;

int flushPoints = 0;

int countPoints = 0;

int drawPoints = 0;

for (int i = 0; i < hands.length; i++) {

if (PokerFace.isFlush(hands[i]) == fs[0][i])

flushPoints++;

else

System.out.println("For the hand " + Arrays.toString(hands[i]) + ", isFlush returned " + !fs[0][i]

+ " but should have returned " + fs[0][i]);

if (PokerFace.isStraight(hands[i]) == fs[1][i])

straightPoints++;

else

System.out.println("For the hand " + Arrays.toString(hands[i]) + ", isStraight returned " + !fs[1][i]

+ " but should have returned " + fs[1][i]);

}

for (int i = 0; i < countHands.length; i++) {

int[] cc = countCounts[i];

int max = 0;

for (int j = 0; j < cc.length; j++) {

int c = PokerFace.countCardsOfRank(countHands[i], j + 1);

if (c != cc[j]) {

System.out.println("countCardsOfRank for " + Arrays.toString(countHands[i]) + ", " + (j + 1)

+ " should be " + cc[j] + " but was " + c);

} else

countPoints++;

max = Math.max(max, cc[j]);

}

}

System.out.println("Point totals will be revised with the next version of the tester!");

System.out.println("isSuit " + suitPoints + " / 20");

System.out.println("isRank " + rankPoints + " / 20");

System.out.println("isFlush " + flushPoints + " / 10");

System.out.println("isStraight " + (3 * straightPoints / 2) + " / 15");

System.out.println("countCardsOfRank " + countPoints / 3 + " / 13");

System.out.println("containsRun is now extra credit and is not tested by this tester.");

try {

Method cr = PokerFace.class.getMethod("containsRun", int[].class, int.class);

if (cr != null) {

if (!Modifier.isStatic(cr.getModifiers())) {

System.out.println(

"If you would like to get credit for containsRun, please add the word \"static\" to the containsRun method declaration, so it says: public static boolean containsRun(int[] hand, int length)");

} else {

System.out.println("You have declared containsRun correctly, but this tester doesn't test it.");

}

}

} catch (Exception e) {

System.out.println(

"I don't see your containsRun method. It should be declared as: public static boolean containsRun(int[] hand, int length)");

}

{

int[][] dhands = { { 1, 2, 3, 4, 5 }, { 5 }, { 13, 14, 15, 16 } };

int[][] dafter = { { 2, 4, 6, 7, 8 }, { 5 }, { 9, 10, 11, 12 } };

int[] deck = { 6, 7, 8, 9, 10, 11, 12 };

boolean[][] dselected = { { true, false, true, false, true }, { false }, { true, true, true, true } };

int[] dcounts = { 3, 0, 4 };

int[] dtops = { 0, 100, 3 };

for (int i = 0; i < dhands.length; i++) {

int[] hand = dhands[i];

int[] after = dafter[i];

boolean[] selected = dselected[i];

int[] deckopy = deck.clone();

int top = dtops[i];

System.out.println("Testing drawCards(" + Arrays.toString(hand) + ", " + Arrays.toString(selected)

+ ", " + Arrays.toString(deck) + ", " + top + ")...");

int drew = PokerFace.drawCards(hand, selected, deck, top);

Arrays.sort(hand);

if (!Arrays.equals(hand, after)) {

System.out.println("After drawCards, the hand contained these cards: " + Arrays.toString(hand));

System.out.println("I expected the hand to contain these cards: " + Arrays.toString(after));

} else

drawPoints += 5;

if (drew != dcounts[i]) {

System.out.println("I expected the value " + dcounts[i] + " to be returned. Instead, " + drew

+ " was returned.");

} else

drawPoints += 2;

if (!Arrays.equals(deckopy, deck)) {

System.out.println(

"The deck shouldn't change! Only the hand changes. The return value indicates how many cards were drawn.");

if (drawPoints > 0)

drawPoints--;

}

}

if (drawPoints > 11)

drawPoints++; // bonus!

}

System.out.println("drawCards " + drawPoints + " / 22");

System.out.println("isWinner is now extra credit and is not tested by this tester.");

System.out.println("TOTAL SCORE: "

+ (suitPoints + rankPoints + flushPoints + 3 * straightPoints / 2 + countPoints / 3 + drawPoints)

+ " / 100");

}

}

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

Formal SQL Tuning For Oracle Databases Practical Efficiency Efficient Practice

Authors: Leonid Nossov ,Hanno Ernst ,Victor Chupis

1st Edition

3662570564, 978-3662570562

More Books

Students also viewed these Databases questions

Question

Write a program to check an input year is leap or not.

Answered: 1 week ago

Question

Write short notes on departmentation.

Answered: 1 week ago

Question

What are the factors affecting organisation structure?

Answered: 1 week ago

Question

What are the features of Management?

Answered: 1 week ago

Question

Briefly explain the advantages of 'Management by Objectives'

Answered: 1 week ago