【Java--数据结构】“从扑克到程序:深入探讨洗牌算法的原理与魅力“

05-13 1983阅读

前言

以下是学习Java顺序表的一个实例应用———简单的洗牌算法。

【Java--数据结构】“从扑克到程序:深入探讨洗牌算法的原理与魅力“

欢迎关注个人主页:逸狼


创造不易,可以点点赞吗~

如有错误,欢迎指出~



目录

前言

定义每张扑克牌的属性

生成一副扑克牌(不包含大小王)

洗牌方法

发牌方法

发牌测试

结果


简单的洗牌算法

定义每张扑克牌的属性

package Demo;
public class Card {
    public int rank;//数字
    public String suit;//花色
    public Card(int rank,String suit){
        this.rank=rank;
        this.suit=suit;
    }
    @Override
    public String toString() {
        return "{" + suit + " "+ rank + '}';
    }
}

生成一副扑克牌(不包含大小王)

package Demo;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
public class Cards {
    //4个花色  13张牌
    public static final String[] suits={"♥","♠","♣","♦"};
    //生成一副牌52张  4*13 不包含大小王
    public List buyCard(){
        List cardList=new ArrayList();
        for (int i = 0; i  

洗牌方法

    //洗牌
    public void shuffle(List cardList){
        Random random=new Random();
        //让i=51,生成一个0~i的随机数,与i交换。i--,重复此过程,洗牌
        for (int i = cardList.size()-1;i>0; i--) {
            int randIndex= random.nextInt(i);//生成0~i的随机数放入randIndex
            swap(cardList,i,randIndex);
        }
    }
    //写一个交换方法,交换i和j下标的牌
    private void swap(List cardList,int i,int j){
        Card tmp=cardList.get(i);
        cardList.set(i,cardList.get(j));
        cardList.set(j,tmp);
    }

发牌方法

//三个人, 每个人轮流抓5张牌
    public void drawCard(List cardList){
        List hand1=new ArrayList();
        List hand2=new ArrayList();
        List hand3=new ArrayList();
        Listhands=new ArrayList();
        hands.add(hand1);
        hands.add(hand2);
        hands.add(hand3);
        for (int i = 0; i  

发牌测试

package Demo;
import java.util.List;
public class TestCard {
    public static void main(String[] args) {
        Cards cards=new Cards();
        List cardList=cards.buyCard();
        System.out.println("生成一副扑克牌:");
        System.out.println(cardList);
        cards.shuffle(cardList);
        System.out.println("洗牌后:");
        System.out.println(cardList);
        System.out.println("抓牌:");
        cards.drawCard(cardList);
        System.out.println("剩下的牌:");
        System.out.println(cardList);
    }
}

结果

生成一副扑克牌:

[{♥ 0}, {♥ 1}, {♥ 2}, {♥ 3}, {♥ 4}, {♥ 5}, {♥ 6}, {♥ 7}, {♥ 8}, {♥ 9}, {♥ 10}, {♥ 11}, {♥ 12}, {♠ 0}, {♠ 1}, {♠ 2}, {♠ 3}, {♠ 4}, {♠ 5}, {♠ 6}, {♠ 7}, {♠ 8}, {♠ 9}, {♠ 10}, {♠ 11}, {♠ 12}, {♣ 0}, {♣ 1}, {♣ 2}, {♣ 3}, {♣ 4}, {♣ 5}, {♣ 6}, {♣ 7}, {♣ 8}, {♣ 9}, {♣ 10}, {♣ 11}, {♣ 12}, {♦ 0}, {♦ 1}, {♦ 2}, {♦ 3}, {♦ 4}, {♦ 5}, {♦ 6}, {♦ 7}, {♦ 8}, {♦ 9}, {♦ 10}, {♦ 11}, {♦ 12}]

洗牌后:

[{♦ 9}, {♠ 9}, {♥ 7}, {♥ 4}, {♠ 4}, {♥ 0}, {♥ 10}, {♣ 7}, {♠ 6}, {♥ 5}, {♣ 4}, {♥ 1}, {♠ 1}, {♥ 11}, {♦ 3}, {♦ 1}, {♥ 2}, {♣ 10}, {♣ 1}, {♣ 0}, {♣ 2}, {♥ 12}, {♦ 4}, {♥ 8}, {♣ 9}, {♠ 11}, {♠ 12}, {♣ 5}, {♦ 12}, {♠ 0}, {♠ 7}, {♦ 8}, {♦ 11}, {♦ 2}, {♠ 5}, {♥ 3}, {♣ 12}, {♥ 9}, {♣ 3}, {♦ 5}, {♦ 0}, {♠ 8}, {♦ 10}, {♠ 2}, {♦ 7}, {♠ 10}, {♠ 3}, {♥ 6}, {♦ 6}, {♣ 8}, {♣ 11}, {♣ 6}]

抓牌:

第1个人的牌:[{♦ 9}, {♥ 4}, {♥ 10}, {♥ 5}, {♠ 1}]

第2个人的牌:[{♠ 9}, {♠ 4}, {♣ 7}, {♣ 4}, {♥ 11}]

第3个人的牌:[{♥ 7}, {♥ 0}, {♠ 6}, {♥ 1}, {♦ 3}]

剩下的牌:

[{♦ 1}, {♥ 2}, {♣ 10}, {♣ 1}, {♣ 0}, {♣ 2}, {♥ 12}, {♦ 4}, {♥ 8}, {♣ 9}, {♠ 11}, {♠ 12}, {♣ 5}, {♦ 12}, {♠ 0}, {♠ 7}, {♦ 8}, {♦ 11}, {♦ 2}, {♠ 5}, {♥ 3}, {♣ 12}, {♥ 9}, {♣ 3}, {♦ 5}, {♦ 0}, {♠ 8}, {♦ 10}, {♠ 2}, {♦ 7}, {♠ 10}, {♠ 3}, {♥ 6}, {♦ 6}, {♣ 8}, {♣ 11}, {♣ 6}]

 

VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]