<> thinking :

Before implementation , Think about the steps first , Clear thinking will not make mistakes .
To achieve a deck of playing cards , First create this class , Then there is a deck of playing cards , Then shuffle , Grab cards .
1. Define a Card class
2. Realize this game :
① Construct a deck of playing cards (52 Zhang ) The king of size is not included here
② shuffle the cards
③ Three people each draw five cards

<> Code implementation and details :

<>1. Define a Card class

A card contains two attributes : Decor and face value
public class MyCard { public char suit; // Decor public String rank; // Face value of card public
MyCard(String rank, char suit) { this.suit = suit; this.rank = rank; } public
StringtoString(){ return String.format("[%s %c]",rank,suit); } }
<>2. Realize this game

Write one CardGame To realize this poker game , It is divided into many small branches .
1. Buy a deck of cards
It's written in the front Card To represent a card , Here we buy a deck of cards , namely 52 individual Card Put it together , Just form a List, So the type of a deck of cards here is List<Card>.
public List<MyCard> buyDeck(){ // First, display the designs, colors and face values // Because the face value of the card is A,J,Q,K, String array is used here
// You can also A,J,Q,K individualization , The rest is written as an integer array char[] suits={'♦','♥','♠','♣'}; String[] ranks={
"A","2","3","4","5","6","7","8","9","10","J","Q","K"}; // establish 52 Card List<MyCard>
deck=new ArrayList<>(52); for(int i=0;i<4;i++){ for(int j=0;j<13;j++){ deck.add(
newMyCard(ranks[j],suits[i])); } } return deck; }
2. shuffle the cards :
Use random numbers
// Write an exchange function to facilitate the implementation of shuffle method public void swap(List<MyCard> deck,int i,int j){ MyCard temp=
deck.get(i); deck.set(i,deck.get(j)); deck.set(j,temp); } public void shuffle(
List<MyCard> deck){ // Set random number Random r=new Random();// item base Random class for(int j=deck.
size()-1;j>0;j--){ int i=r.nextInt(j); // Randomly generate a greater than or equal to 0 less than 51 Integer of swap(deck,i,j);
// To be generated 0-51 Shuffle by exchanging random numbers with the last number } }
3. Main function :

Licensing is realized here , Five cards per person , Three people take turns , Put three people in one List among , The card information of three people is List<Card>, therefore hands The type of is List<List<Card>>.
public static void main(String[] args){ CardGame cg=new CardGame(); List<
MyCard> deck=cg.buyDeck(); System.out.println(" Just bought cards :"); System.out.println(
deck); cg.shuffle(deck); System.out.println(" Washed cards :"); System.out.println(deck);
// Three people take turns to catch cards , Five for each person // Put three players into one List among List<List<MyCard>> hands=new ArrayList<>(3);
// Five cards in each of the three hands hands.add(new ArrayList<>(5)); hands.add(new ArrayList<>(5));
hands.add(new ArrayList<>(5)); //ABC Three people grab cards for(int i=0;i<5;i++){ for(int j=0;j<3;
j++){ // from deck Inside // Grab the top when you grab it hands.get(j).add(deck.remove(0));; } } System.
out.println(" The remaining cards :"); System.out.println("A Cards in hand :"); System.out.println(hands.get
(0)); System.out.println("B Cards in hand :"); System.out.println(hands.get(1)); System.out
.println("C Cards in hand :"); System.out.println(hands.get(2)); }
<> Output results :

The back doesn't show all

Technology
©2019-2020 Toolsou All rights reserved,
C++ of string of compare usage Python Study notes ( one )evo Tool usage problems ——Degenerate covariance rank, Umeyama alignment is not possibleRISC-V_GD32VF103-TIMER0 timer interrupt java Array subscript variable _Java Basic grammar : array be based on stm32 Control four-wheel trolley motor drive ( one ) be based on redis Design of liking function Software engineering career planning mysql Query random data by conditions _MySQL Random query of several qualified records centos7 install RabbitMq