Table of contents
1. Data structure of poker cards
2. Buy cards (initialization of poker cards)
1. Data structure of poker cards
First of all, the playing cards are arranged one by one. There are 52 in total except the big and small kings. We can consider using an array to store them. , each element of the array is a card, which is a card in the card library; in addition to considering the cards in the card library, you also need to consider the cards in the player's hand. The player's hand cards are part of the card library. We use the sequence Table to store playing cards. For a card, we have two parameters: Suit and Point< /span>
public class Card {
private int rank;//点数
private String suit;//花色
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
this.suit = suit;
}
@Override
public String toString() {
return suit+rank+" ";
}
}
We store the suits in the total poker set and use a string array to store them.
public class CardList {
public static final String[] suits = {"♠","♥","♣","♦"};
}
2. Buy cards (initialization of poker cards)
We want to specifically abstract a set of cards in the process of buying cards. We use an array to store it. Each element of the array is a card. We use generics to facilitate our definition of the deck.
There are a total of 52 playing cards except the king and king, so we define a playing card array< /span>for loops, and it passes through two 52The size isfor loop is used to realize the suit of the card, and the inner for loop is used to realize the number of cards. In the middle of the loop, the variables at time zero are used to generate a poker card at time zero. After we get a complete card, we use ArrayListInsertion methodaddPut our newly generated cards into the card In the group, finally return the generated deck consisting of 52 cards, and we have completed the initialization of playing cards
//买一副牌,也就是对牌进行初始化
public static final List<Card> buyCard(){
List<Card> cards = new ArrayList<Card>(52);
for (int i = 0; i < 4; i++) {
for (int j = 0; j <= 13; j++) {
String tempSuit = suits[i];
int tempRank = j;
Card newCard = new Card();
newCard.setSuit(suits[i]);
newCard.setRank(j);
cards.add(newCard);
}
}
return cards;
}
3. Shuffle
The so-called shuffling is to randomly disrupt the order of the cards. Thinking back to our real life, our shuffling action is actually to continuously insert some cards into another part, and then exchange the cards. Order.
So we encapsulate a method hereswap to exchange cards with each other, and then we shuffle the cards in the method< Use random numbers in a i=3>shuffle to ensure that the positions we swap are random, and use for loop It is guaranteed that we shuffle each position at least once
//洗牌
public void swap(List<Card> cards,int i,int j){
Card tempCard = cards.get(i);
cards.set(j,tempCard);
cards.set(i,cards.get(j));
}
//洗牌
public void shuffle(List<Card> cards){
Random random = new Random();
for (int i = cards.size()-1; i > 0; i--) {
int index = random.nextInt(i);
swap(cards,index,i);
}
}
4. Dealing cards
Dealing cards is actually to distribute some cards to players in equal amounts. We define the hand groups of three players here and use three arrays to implement them respectively. Then we put these three arrays into one A large array is used to record all the cards to be dealt to the players. We use two nested for loops to ensure that we deal 5 cards to each of the 3 players. We can understand the card dealing process like this:hands.get(j) is used to get the hand decks of 3 people, add(tempCard) is used to put the cards into these three arrays
//发牌
public List<List<Card>> dealCard(List<Card> cardList){
List<Card> hand1 = new ArrayList<>();
List<Card> hand2 = new ArrayList<>();
List<Card> hand3 = new ArrayList<>();
List<List<Card>> hands = new ArrayList<>();
hands.add(hand1);
hands.add(hand2);
hands.add(hand3);
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
Card tempCard = cardList.remove(0);
hands.get(j).add(tempCard);
}
}
return hands;
}
5. Complete code
Card.java
public class Card {
private int rank;
private String suit;
public int getRank() {
return rank;
}
public void setRank(int rank) {
this.rank = rank;
}
public String getSuit() {
return suit;
}
public void setSuit(String suit) {
this.suit = suit;
}
@Override
public String toString() {
return suit+rank+" ";
}
}
CardList.java
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class CardList {
public static final String[] suits = {"♠","♥","♣","♦"};
//买一副牌,也就是对牌进行初始化
public static final List<Card> buyCard(){
List<Card> cards = new ArrayList<Card>(52);
for (int i = 0; i < 4; i++) {
for (int j = 0; j <= 13; j++) {
String tempSuit = suits[i];
int tempRank = j;
Card newCard = new Card();
newCard.setSuit(suits[i]);
newCard.setRank(j);
cards.add(newCard);
}
}
return cards;
}
//洗牌
public void swap(List<Card> cards,int i,int j){
Card tempCard = cards.get(i);
cards.set(j,tempCard);
cards.set(i,cards.get(j));
}
//洗牌
public void shuffle(List<Card> cards){
Random random = new Random();
for (int i = cards.size()-1; i > 0; i--) {
int index = random.nextInt(i);
swap(cards,index,i);
}
}
//发牌
public List<List<Card>> dealCard(List<Card> cardList){
List<Card> hand1 = new ArrayList<>();
List<Card> hand2 = new ArrayList<>();
List<Card> hand3 = new ArrayList<>();
List<List<Card>> hands = new ArrayList<>();
hands.add(hand1);
hands.add(hand2);
hands.add(hand3);
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 3; j++) {
Card tempCard = cardList.remove(0);
hands.get(j).add(tempCard);
}
}
return hands;
}
}
6. Test
We can write a test case:
public static void main(String[] args) {
CardList cardGame = new CardList();
List<Card> ret = cardGame.buyCard();
System.out.println("买牌:");
System.out.println(ret);
System.out.println("洗牌:");
cardGame.shuffle(ret);
System.out.println(ret);
System.out.println("揭牌:");
List<List<Card>> hand = cardGame.dealCard(ret);
for (int i = 0; i < hand.size(); i++) {
System.out.println("第 "+(i+1)+" 个人的牌:"+hand.get(i));
}
System.out.println("剩下的牌:");
System.out.println(ret);
}
Output results
It can be found that it can run normally
That’s it for this sharing. I hope my sharing can be helpful to you. I also welcome everyone’s support. Your likes are the biggest motivation for bloggers to update!
If you have different opinions, you are welcome to actively discuss and exchange in the comment area, let us learn and make progress together!
If you have relevant questions, you can also send a private message to the blogger. The comment area and private messages will be carefully checked. See you next time