This is the first part of a project to deal 13 cards to 4 players from a shuffle
ID: 3813887 • Letter: T
Question
This is the first part of a project to deal 13 cards to 4 players from a shuffled deck of 52 cards. Each card in the deck will be represented by an integer from 0 to 51. We will see later how to translate the integer into a string representing a card. The deck and each hand will be represented by a vector of ints. The vectors should be declared in the main function and not declared globally. void unwrap(deck) # load the deck vector with integers in order from 0 to 51 void shuffle(deck) # shuffle the deck vector using random_shuffle method - see pages 1008 and 1014 in the text You can use the debugger to show these functions are working correctly. Future labs will have you write a "deal" function and "showHand" function.
Explanation / Answer
package myProject;
import java.util.Random;
//Class Definition
public class SuffelCard
{
//Creates Random class object
Random rand = new Random();
////Shuffle cards
void shuffle(int deck[])
{
//Loops till the length of deck
for (int i = 0; i < deck.length; i++)
{
int newCard;
int temp;
// Pick a random index between 0 and Deck - 1
newCard = rand.nextInt(deck.length);
// Swap deck[i] and deck[newCard]
temp = deck[i];
deck[i] = deck[newCard];
deck[newCard] = temp;
}//End of loop
}//End of method
//Distribute 13 card among 4 players
void cardToPlayer(int player[][], int deck[])
{
int t = 0, x , y, z;
//Loops 4 times for each player
for(x = 0; x < 4; x++)
{
//Loops 13 times for each player
for(y = t, z = 0; y < t + 13; y++, z++)
{
//Assign cards to players
player[x][z] = deck[y];
}//end of inner for loop
//Reset the t value for next player
t = y;
}//End of outer for loop
}//End of method
//Initializes deck
void unwrap(int deck[])
{
//Loops 52 times
for(int x = 0; x < 52; x++)
deck[x] = x;
}//end of method
//Displays each players card
void dispPlayerCard(int player[][])
{
//Loops 4 times for each player
for(int x = 0; x < 4; x++)
{
System.out.println(" Player " + (x + 1));
//Loops 13 times for each player cards
for(int y = 0; y < 13; y++)
System.out.print(player[x][y] + " ");
}//End of for loop
}//End of method
//Displays cards available in deck
void dispDeck(int deck[])
{
for(int x = 0; x < deck.length; x++)
System.out.print("["+x+"]"+deck[x] + " ");
}//End of method
//Main method
public static void main(String ss[])
{
//Creates a deck of type integer with size 52
int deck[] = new int[52];
//Creates 4 players each 13
int player[][] = new int[4][13];
SuffelCard sc = new SuffelCard();
System.out.println("Load the deck vector with integers in order from 0 to 51");
sc.unwrap(deck);
sc.dispDeck(deck);
System.out.println(" Shuffle the deck vector");
sc.shuffle(deck);
sc.dispDeck(deck);
System.out.println(" Each player card");
sc.cardToPlayer(player, deck);
sc.dispPlayerCard(player);
}//End of main method
}//End of class
Output:
Load the deck vector with integers in order from 0 to 51
[0]0 [1]1 [2]2 [3]3 [4]4 [5]5 [6]6 [7]7 [8]8 [9]9 [10]10 [11]11 [12]12 [13]13 [14]14 [15]15 [16]16 [17]17 [18]18 [19]19 [20]20 [21]21 [22]22 [23]23 [24]24 [25]25 [26]26 [27]27 [28]28 [29]29 [30]30 [31]31 [32]32 [33]33 [34]34 [35]35 [36]36 [37]37 [38]38 [39]39 [40]40 [41]41 [42]42 [43]43 [44]44 [45]45 [46]46 [47]47 [48]48 [49]49 [50]50 [51]51
Shuffle the deck vector
[0]17 [1]15 [2]24 [3]4 [4]1 [5]45 [6]23 [7]29 [8]26 [9]8 [10]43 [11]31 [12]40 [13]7 [14]20 [15]16 [16]32 [17]46 [18]6 [19]13 [20]51 [21]28 [22]5 [23]21 [24]48 [25]47 [26]44 [27]19 [28]36 [29]14 [30]9 [31]41 [32]10 [33]35 [34]39 [35]30 [36]50 [37]0 [38]11 [39]25 [40]3 [41]2 [42]18 [43]33 [44]42 [45]27 [46]49 [47]37 [48]22 [49]12 [50]34 [51]38
Each player card
13
26
39
52
Player 1
17 15 24 4 1 45 23 29 26 8 43 31 40
Player 2
7 20 16 32 46 6 13 51 28 5 21 48 47
Player 3
44 19 36 14 9 41 10 35 39 30 50 0 11
Player 4
25 3 2 18 33 42 27 49 37 22 12 34 38
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.