Poker Game To represent a 5card hand use a twodimensional ar

Poker Game

To represent a 5-card hand, use a two-dimensional array with 5 rows and 2 columns. The 5 rows represent the 5 cards. You can think of each row as a one-dimensional array representing one card. That is, the first column represents each card’s kind. The second column represents each card’s suit.

Write a method with the following header which generates a random 5-card hand. It should make one or more calls to drawCard()to generate each card in the hand. Before adding a card to the hand, it must check whether a card with the same kind and suit already appears in the hand. If so, it must not add the card to the hand but instead continue calling drawCard()until a card is generated whose combination of kind and suit does not yet appear in the hand. Once a hand consisting of 5 distinct cards has been generated, the method should return a reference to the two dimensional array representing the hand.

public static int[][] drawHand()

Solution

Here is the code for you:

import java.util.*;
class DrawHandOfPoker
{
public static int[] drawCard()
{
Random rnd = new Random();
int kind = rnd.nextInt(13) + 1;   //Assuming values from 1 to 13.
int suit = rnd.nextInt(4);       //Assuming values from 0 to 3.
int[] array = new int[2];
array[0] = kind;
array[1] = suit;
return array;
}
public static int[][] drawHand()
{
int[][] hand = new int[5][2];
int count = 0;
while(count < 5)
{
int[] temp = new int[2];
temp = drawCard();
boolean duplicate = false;
for(int i = count - 1; i >= 0; i--)
if(temp[0] == hand[i][0] && temp[1] == hand[i][1])
duplicate = true;
if(!duplicate)
{
hand[count][0] = temp[0];
hand[count][1] = temp[1];
count++;
}
}
return hand;
}
}

Poker Game To represent a 5-card hand, use a two-dimensional array with 5 rows and 2 columns. The 5 rows represent the 5 cards. You can think of each row as a o

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site