Classic example of java sequence table - poker practice

1. Understanding sequence table and linear table

A linear list is a finite sequence of n data elements with the same properties. Linear table is a data structure widely used in practice, common linear table: sequential list, linked list, stack, queue, string...

A linear table is logically a linear structure. When a linear table is physically stored, it is usually stored in the form of an array and a chain structure. 2. insert image description here
insert image description here
Background introduction
Generate a deck of 54 playing cards. There are three players. Draw 15 cards, and then deal the cards to three players in sequence. Each player will get 5 cards. Finally, the drawn 15 cards and the cards of the three players will be printed and displayed on the console.

Knowledge point: loop structure sequence table method

3. Solution ideas
Write a card class and a PokerGame class, give the card the attributes of color and size, store the card in the sequence table, use random numbers to write functions to shuffle the cards before drawing cards, and use the three players as elements Store it in the linked list, and finally output the hands of the three players through a loop.

Fourth, code analysis
1. Card class

package List;

public class Card {
    
    
    public String suit;//花色
    public String rank;//牌面大小

    public Card(String suit,String rank)
    {
    
    
        this.suit=suit;
        this.rank=rank;

    }
    @Override
    public String toString(){
    
    
        return "("+this.suit+this.rank+")";//重载了打印函数
    }
}

2. PokerGame class

package List;

import java.util.*;

public class PokerGame {
    
    
    public static final String[] suits={
    
    "♥","♠","♣","♦"};//创建一个含有四种花色的数组

    private static List<Card> buyPoker(){
    
    
        ArrayList<Card> poker=new ArrayList<>();//新建一个顺序表,表中的内容需要是Card对象
        for(int i=0;i<4;i++){
    
    //外循环对应四种花色
            for(int j=2;j<10;j++){
    
    //内循环对应数字2-8
                poker.add(new Card(suits[i],""+j));
            }
            poker.add(new Card(suits[i],"J"));
            poker.add(new Card(suits[i],"Q"));
            poker.add(new Card(suits[i],"K"));
            poker.add(new Card(suits[i],"A"));//四种特殊的数字
        }
        poker.add(new Card("","big joker"));
        poker.add(new Card("","small joker"));//添加大小王
        return  poker;//返回的是一个顺序表
    }

    private static void shuffle(List<Card> poker)
    {
    
    
        Random random=new Random();
        for(int i=poker.size()-1;i>0;i--)
        {
    
    
            int pos=random.nextInt(i);//生成0-i之间的一个随机数
            swap(poker,i,pos);
        }
    }


    private static void swap(List<Card> poker,int i,int j)
    {
    
    
        Card tmp=poker.get(i);//调取poker中的第i个card赋给temp
        poker.set(i,poker.get(j));
        poker.set(j,tmp);//进行顺序的调换
    }

    public static void main(String[] args){
    
    
        List<Card> poker=buyPoker();//1.创建一副扑克牌
        Collections.shuffle(poker);//2.洗牌
        System.out.println(poker);
        List<List<Card>> players=new ArrayList<>();//新建一个ArrayList,中放置的是,List<Card>
        players.add(new ArrayList<>());
        players.add(new ArrayList<>());
        players.add(new ArrayList<>());//放置三名玩家至新的List中
        for(int i=0;i<5;i++)
        {
    
    
            for(int j=0;j<3;j++)
            {
    
    
                Card top=poker.remove(0);//从牌堆中取牌,也就是取poker中的第一个Card
                List<Card> player=players.get(j);
                player.add(top);

            }

        }
        //4.展示最终拿到的手牌
        for(int i=0;i<players.size();i++)
        {
    
    
            List<Card> player=players.get(i);
            System.out.println("玩家"+i+"的手牌是:"+player);

        }
    }
}

insert image description here

4. Some method explanations contained in the program

java Random.nextInt()方法 lic int nextInt(int n)

The function of this method is to generate a random int value, which is in the interval [0,n), that is, a random int value between 0 and n, including 0 but not n.

元素类型 get(int index);
/**
 * 用新的元素 e 替换 index 位置的元素,并返回 index 位置的原来的元素
 * index 的可选范围是 0 <= index < size()
 * @参数 index 待替换元素的位置(下标)
 * @参数 e 要替换的新元素
 * @返回值 index 位置的老元素
 */
元素类型 set(int index, 元素类型 e);
/**
 * 通过遍历的方式,判断与元素 e 相等(equals)的元素是否存在于线性表中
 * @参数 e 待查找元素
 * @返回 true:包含;false:不包含
 */

5. Run the screenshot
insert image description here

Guess you like

Origin blog.csdn.net/qq_45742383/article/details/114557615