In: Computer Science
Dealing Cards
Write a program that deals a deck of card into 4 hands – Each hand having 13 cards.
Hints – Import below java utility to use the random function.
import java.util.*;
Random rand = new Random();
int r = rand.nextInt(52); // this will generate random numbers between 0-52
to get a value between 1-13, use the mod function and get the remainder
if your number is 15, 15%13 has a remainder of 2. 2 is your face card.
---------------------------------------------------------------------------
11 = jack
12= Queen
13= King
Card is a CLUB - If your generated number is between (1-13)
Card is a Diamond - If your generated number is between (14-26)
Card is a Spade - If your generated number is between (27-39)
Card is a Heart - If your generated number is between (40-52)
Your program output should look like this below
----------------------------------------------------------------------------------------------------------------------------------
Deck of cards shuffled into 4 hands
8 1 7 18 43 26 31 2 12 3 40 22 4
29 9 10 11 5 35 47 36 25 14 17 39 23
21 52 6 46 38 48 24 16 27 32 13 45 42
49 44 50 20 37 34 51 15 28 30 19 33 41
Face of cards in each Hand
Hand 1:
Clubs: 8 1 7 2 Q 3 4
Diamonds: 5 K 9
Spades: 5
Heart: 4 1
Hand 2:
Clubs: 4 9 10 J 5
Diamonds: Q 1 4
Spades: 3 9 10 K
Heart: 8
Hand 3:
Clubs: 6 K
Diamonds: 10 8 J 3
Spades: K Q 1 6
Heart: K 7 9
Hand 4:
Clubs: K
Diamonds: 7 2
Spades: J 8 2 4
Heart: 6 3 10 5 J Q
In case of any query do comment. Please rate answer. Thanks
Please use below code:
=======================Card.java====================
import java.util.*;
public class Card
{
private int[] deck = new int[52];
//string array for Ranks, 13 index would be 0 when take modulus so It is K.
String[] Ranks = {"K", "1","2","3","4","5","6","7","8","9","10","J","Q"};
//constructor to initialize the Card Deck with it's index value
public Card()
{
for (int index=0; index <52 ;index++ )
{
deck[index] = index +1;
}
}
//shuffle cards
public void ShuffleCards()
{
//shuffle the cards, by generating random number and then shuffle the indexes
for (int index=0; index <52 ;index++ )
{
Random rand = new Random();
int newIndex = rand.nextInt(52);
int temp = deck[index];
deck[index] = deck[newIndex];
deck[newIndex] = temp;
}
}
//To print the hands as desired output
public void PrintHands()
{
System.out.println("Deck of cards shuffled into 4 hands");
for (int index =0; index <52 ; index++ )
{
System.out.print(deck[index] + " ");
if ((index+1) % 13 ==0)
{
System.out.println();
}
}
System.out.println("Face of cards in each Hand");
//Print the informartion for each hand group by suites
for (int index =0; index < 4 ; index++ )
{
System.out.println("Hand " + (index+1) + ":");
int startIndex = index *13;
int endIndex = startIndex+13;
PrintHandsGroupBySuites(startIndex, endIndex);
}
}
//for each hand, create the information group by suites and then print
public void PrintHandsGroupBySuites(int startIndex,int endIndex)
{
String[] cardHandInformation = {"Clubs:" , "Diamonds:","Spades:","Heart:"};
for (int index =startIndex ; index<endIndex ; index++ )
{
//card suit would be 1-13 (club), 14-26 (Diamond), 27-39(Spades), 40-52(Heart)
int cardSuit = deck[index]/14;
int cardRank = deck[index]% 13;
cardHandInformation[cardSuit] += " " + Ranks[cardRank];
}
//print the information
for (int index=0; index < cardHandInformation.length ; index ++ )
{
System.out.println("\t" + cardHandInformation[index]);
}
}
}
============================main.java (driver program)================
public class Main
{
public static void main(String[] args) {
//create an object of Card class
Card deckCard = new Card();
//shuffle the deck
deckCard.ShuffleCards();
//print Hands
deckCard.PrintHands();
}
}
=======================screen shot of the code===================
Output: