So, I have to create a poker hand program using functions/methods and arrays.
Here's a sample output I need to have:
Enter five numeric cards, no face cards. Use 2 - 9.Card 1: 8 Card 2: 7 Card 3: 8 Card 4: 2 Card 5: 7 Two Pair! Enter five numeric cards, no face cards. Use 2 - 9. Card 1: 4 Card 2: 5 Card 3: 6 Card 4: 8 Card 5: 7 Straight! Enter five numeric cards, no face cards. Use 2 - 9. Card 1: 9 Card 2: 2 Card 3: 3 Card 4: 4 Card 5: 5 High Card!
And here's my code (I'm having issues with logic in determining if one gets pair, 3 of a kind, etc.). They have be methods/functions. So, if I can figure out how to do 1 or 2 of them, it should be hopefully be a breeze from there:
import java.util.Scanner;
public class Assignment4
{
public static void main(String args[])
{
final int LEN = 5;
int[] hand = new int[LEN];
Scanner input = new Scanner(System.in);
//input the hand
System.out.println("Enter five numeric cards, no face cards. Use 2-9.");
for (int index = 0; index < hand.length; index++) {
System.out.print("Card " + (index + 1) + ": ");
hand[index] = input.nextInt();
}
//sort the collection
bubbleSortCards(hand);
//determine players hand type
//flow of evaluation -> checking complex hands first
if (containsFullHouse(hand)) {
System.out.println("Full House!");
} else if (containsStraight(hand)) {
System.out.println("Straight!");
} else if (containsFourOfaKind(hand)) {
System.out.println("Four of a Kind!");
} else if (containsThreeOfaKind(hand)) {
System.out.println("Three of a Kind!");
} else if (containsTwoPair(hand)) {
System.out.println("Two Pair!");
} else if (containsPair(hand)) {
System.out.println("Pair!");
} else
System.out.println("High Card!");
}
And this is the recommended way from the assignment's instructions:
public class PokerHand
{
public static void main(String args[])
{
int hand[] = {5, 2, 2, 3, 8};
if (containsAPair(hand)) {
System.out.println("Pair!");
} else {
System.out.println("Not a pair!");
}
}
public static boolean containsAPair(int hand[]) {
// Your code here... don’t return true every time...
return true;
}
}
If more information is needed, I'll be more than happy to supply that. THANKS!
Instead of sorting the hand, I would recommend that you tally the contents of a hand and generate an array of counts, where the i
th element of the array has the number of cards with value i
. You should then be able to figure out how to use that array to decide whether it is a particular type of hand.
Since this is homework, I'll point you in a direction to get started to help you think about a solution.
In your post, you need to create the code for the containsFullHouse()
, containsStraight()
and so on. So...
c
has c + 1
as the next card's value, except the last, I have a straight.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.