Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I have a program that prints 2 random cards (code included below for logic). How do I evaluate the hand to be either a pair,

I have a program that prints 2 random cards (code included below for logic). How do I evaluate the hand to be either a pair, straight, flush, or jack or higher using methods?

Only need help with isPair, thanks

----------------------------------------------------------------------------------------------------------

public static void main(String[] args) {

int[] hand = Cards.dealHand(2);

Cards.printHand(hand);

//System.out.println(isPair? + Cards.isPair(hand));

//System.out.println(isStraight? + Cards.isStraight(hand));

System.out.println("isFlush? " + Cards.isFlush(hand));

System.out.println("isJacks? " + Cards.isJackOrHigher(hand));

}

public static boolean isPair(int[] hand) {

}

public static boolean isStraight(int[] hand) {

boolean isStraight = false;

int rank1 = Cards.rank(hand[0]);

int rank2 = Cards.rank(hand[1]);

if((rank1 == 0 && rank2 == 12) || (rank2 == 0 && rank1 == 12)) {

isStraight = true;

}else if ((rank1 == (rank2+1)) || rank2 == (rank1+1)) {

isStraight = true;

}

return isStraight;

}

public static boolean isFlush(int[] hand) {

boolean isFlush;

char suit1 = Cards.suit(hand[0]);

char suit2 = Cards.suit(hand[1]);

if (suit1 == suit2) {

isFlush = true;

}else {

isFlush = false;

}

return isFlush;

}

public static boolean isJackOrHigher(int[] hand) {

boolean isJack = false;

int rank1 = Cards.rank(hand[0]);

int rank2 = Cards.rank(hand[1]);

if ((rank1 == 0 || rank2 == 0) && (rank1 >= 10 || rank2 >= 10)) {

isJack = true;

}else if (rank1 >= 10 && rank2 >= 10) {

isJack = true;

}else {

isJack=false;

}

return isJack;

}

public static void printHand(int[] hand) { //prints hand using printCard method

printCard(hand);

}

public static void printCard(int[] hand) {

String s = "A23456789TJQK";

int rank1 = Cards.rank(hand[0]);

int rank2 = Cards.rank(hand[1]);

char card1 = s.charAt(rank1);

char card2 = s.charAt(rank2);

char suit1 = Cards.suit(hand[0]);

char suit2 = Cards.suit(hand[1]);

System.out.println(card1 + "/" + suit1);

System.out.println(card2 + "/" + suit2);

}

public static int[] dealHand(int n) {

int[] hand = new int[n];

int rand;

int max = 51;

int min = 0;

int range = max - min + 1;

do{

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

rand = (int) (Math.random()* range) + min;

hand[i] = rand;

}

} while (hand[0] == hand[1]);

return hand;

}

public static char suit(int card) { //Assigns suit based on int val

char suit;

if (0 <= card && card<= 12) {

suit = 'H';

}else if (13<= card && card<= 25) {

suit = 'S';

}else if (26 <= card && card <= 38) {

suit = 'D';

}else if (39 <= card && card <= 51) {

suit ='C';

} else {

suit = 'x';

}

return suit;

}

public static int rank(int card) { //Assigns rank based on int val

int cardValue = 0;

if (card == 0 || card == 13 || card == 26 || card == 39) {

cardValue = 0;

}else if (card == 1 || card == 14 || card == 27 || card == 40) {

cardValue = 1;

}else if (card == 2 || card == 15 || card == 28 || card == 41) {

cardValue = 2;

}else if (card == 3 || card == 16 || card == 29 || card == 42) {

cardValue = 3;

}else if (card == 4 || card == 17 || card == 30 || card == 43) {

cardValue = 4;

}else if (card == 5 || card == 18 || card == 31 || card == 44) {

cardValue = 5;

}else if (card == 6 || card == 19 || card == 32 || card == 45) {

cardValue = 6;

}else if (card == 7 || card == 20 || card == 33 || card == 46) {

cardValue = 7;

}else if (card == 8 || card == 21 || card == 34 || card == 47) {

cardValue = 8;

}else if (card == 9 || card == 22 || card == 35 || card == 48) {

cardValue = 9;

}else if (card == 10 || card == 23 || card == 36 || card == 49) {

cardValue = 10;

}else if (card == 11 || card == 24 || card == 37 || card == 50) {

cardValue = 11;

}else if (card == 12 || card == 25 || card == 38 || card == 51) {

cardValue = 12;

}

return cardValue;

}

}

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

Database Application Development And Design

Authors: Michael V. Mannino

1st Edition

0072463678, 978-0072463675

More Books

Students also viewed these Databases questions

Question

5. Benchmark current training practices.

Answered: 1 week ago