Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is an assignment for my cs141 class. I already finished, but there is a lot of bug. I dont know how to fix it.

This is an assignment for my cs141 class. I already finished, but there is a lot of bug. I dont know how to fix it.

This is question:

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).

This is my work:

// Your name here!

// Fill in these methods according to the descriptions given and the assignment handout!

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) {

int cardSuit = card % 4;

if (cardSuit == 0)

cardSuit = 4;

if (cardSuit == suit)

return true;

else

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) {

int cardRank = (rank - 1) * 4 + 1;

if (card >= cardRank && card <= cardRank + 3)

return true;

else

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) {

int len = hand.length;

for (int i = 1; i < 5; i++) {

int j = 0;

for (j = 0; j < len;) {

if (isSuit(hand[j], i))

j++;

else

break;

}

if (j == len - 1)

return true;

}

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) {

int len = hand.length;

boolean aceisFirst = false;

if (hand[0] >= 1 && hand[0] <= 4)

aceisFirst = true;

if (hand[1] >= 1 && hand[1] <= 4)

return false;

if (aceisFirst) {

if (isRank(hand[1], 10)) {

int j = 2;

for (j = 2; j < len;) {

if (isRank(hand[1], 9 + j))

j++;

else

return false;

}

if (j == len - 1)

return true;

}

else if (isRank(hand[1], 2)) {

int j = 2;

for (j = 2; j < len;) {

if (isRank(hand[1], 1 + j))

j++;

else

return false;

}

if (j == len - 1)

return true;

}

else

return false;

}

else {

int rankFirst = 1;

for (int i = 1; i < 10; i++) {

if (isRank(hand[0], rankFirst)) {

rankFirst = i;

}

}

int j = 1;

for (j = 1; j < len;) {

if (isRank(hand[1], rankFirst + j))

j++;

else

return false;

}

if (j == len - 1)

return true;

}

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) {

int count = 0;

int len = hand.length;

for (int i = 0; i < len; i++) {

if (isRank(hand[i], rank))

count++;

}

return count; // 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 static boolean containsRun(int[] hand, int length) {

for (int i = 1; i <= 13; i++) {

if (countCardsOfRank(hand, i) == length)

return true;

}

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) {

int replaced = 0;

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

if (selected[i] == true) {

hand[i] = deck[top];

replaced++;

}

}

return replaced; // 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) {

if (isFlush(myHand) && isStraight(myHand)) {

if (isFlush(otherHand) && isStraight(otherHand)) {

if (myHand[myHand.length - 1] > otherHand[otherHand.length - 1])

return true;

else

return false;

}

else

return true;

}

else if (isFlush(otherHand) && isStraight(otherHand)) {

return false;

}

else if (containsRun(myHand, 4)) {

if (containsRun(otherHand, 4)) {

if (myHand[myHand.length - 2] > otherHand[otherHand.length - 2])

return true;

else

return false;

}

else

return true;

}

else if (containsRun(otherHand, 4)) {

return false;

}

else if (containsRun(myHand, 3) && containsRun(myHand, 2)) {

if (containsRun(otherHand, 3) && containsRun(otherHand, 2)) {

if (myHand[myHand.length - 2] > otherHand[otherHand.length - 2])

return true;

else

return false;

}

else

return true;

}

else if (containsRun(myHand, 3) && containsRun(myHand, 2)) {

return false;

}

else if (isFlush(myHand)) {

if (isFlush(otherHand)) {

if (myHand[myHand.length - 1] > otherHand[otherHand.length - 1])

return true;

else

return false;

} else

return true;

}

else if (isFlush(otherHand)) {

return false;

}

else if (isStraight(myHand)) {

if (isStraight(otherHand)) {

if (myHand[myHand.length - 1] > otherHand[otherHand.length - 1])

return true;

else

return false;

} else

return true;

}

else if (isStraight(otherHand)) {

return false;

}

else if (containsRun(myHand, 3)) {

if (containsRun(otherHand, 3)) {

if (myHand[myHand.length - 2] > otherHand[otherHand.length - 2])

return true;

else

return false;

} else

return true;

}

else if (containsRun(otherHand, 3)) {

return false;

}

else if (containsRun(myHand, 2)) {

if (containsRun(otherHand, 2)) {

if (myHand[myHand.length - 2] > otherHand[otherHand.length - 2])

return true;

else

return false;

} else

return true;

}

else if (containsRun(otherHand, 2)) {

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

}

return false;

}

}

This is feedback from tester:

If you don't see TOTAL SCORE at the end, your program is in an infinite loop or crashing! Use the debugger. isSuit method gave unexpected result for (104, 4) (expected false got true) isSuit method gave unexpected result for (103, 3) (expected false got true) isSuit method gave unexpected result for (102, 2) (expected false got true) isSuit method gave unexpected result for (101, 1) (expected false got true) isSuit method gave unexpected result for (100, 4) (expected false got true) isSuit method gave unexpected result for (99, 3) (expected false got true) isSuit method gave unexpected result for (98, 2) (expected false got true) isSuit method gave unexpected result for (97, 1) (expected false got true) isSuit method gave unexpected result for (96, 4) (expected false got true) isSuit method gave unexpected result for (95, 3) (expected false got true) Further isSuit errors will not be printed. isRank method gave unexpected result for (60, 15) (expected false got true) isRank method gave unexpected result for (59, 15) (expected false got true) isRank method gave unexpected result for (58, 15) (expected false got true) isRank method gave unexpected result for (57, 15) (expected false got true) isRank method gave unexpected result for (56, 14) (expected false got true) isRank method gave unexpected result for (53, 14) (expected false got true) isRank method gave unexpected result for (0, 0) (expected false got true) isRank method gave unexpected result for (-1, 0) (expected false got true) isRank method gave unexpected result for (-2, 0) (expected false got true) isRank method gave unexpected result for (-3, 0) (expected false got true) Further isRank errors will not be printed. For the hand [1, 5, 9, 13, 17], isFlush returned false but should have returned true For the hand [1, 5, 9, 13, 17], isStraight returned false but should have returned true For the hand [7, 15, 23, 27], isFlush returned false but should have returned true For the hand [3, 38, 43, 45, 52], isStraight returned false but should have returned true

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

Transactions On Large Scale Data And Knowledge Centered Systems X Special Issue On Database And Expert Systems Applications Lncs 8220

Authors: Abdelkader Hameurlain ,Josef Kung ,Roland Wagner ,Stephen W. Liddle ,Klaus-Dieter Schewe ,Xiaofang Zhou

2013th Edition

3642412203, 978-3642412202

More Books

Students also viewed these Databases questions

Question

c. What were you expected to do when you grew up?

Answered: 1 week ago

Question

d. How were you expected to contribute to family life?

Answered: 1 week ago

Question

e. What do you know about your ethnic background?

Answered: 1 week ago