为了账号安全,请及时绑定邮箱和手机立即绑定

通过SetList,无重复元素和无序的特性,来洗牌,但每次生成的结果却基本一致,这是怎么回事呢?

package esoon.study.pokergame;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Random;
import java.util.Set;

public class pokerGame {
    
    List<pokerBox> pokerBox;
    List<pokerBox> pokerRandom;
    List<pokerPlayer> pokerPlayer;
    Set<pokerBox> pokerRandomSet;
    
    public pokerGame(){
        
        pokerBox = new ArrayList<pokerBox>();
        pokerPlayer = new ArrayList<pokerPlayer>();
        pokerRandom = new ArrayList<pokerBox>();
        pokerRandomSet = new HashSet<pokerBox>();
        
    }
    
    /**
     *往pokerBox中添加黑桃、红桃、梅花、方片四种花色,以及对应的2——A数值
     */
    public void pokerBoxAdd(){
        
        String[] colour = {"黑桃","红桃","梅花","方片"};
        String[] number = {"2","3","4","5","6","7","8","9","10","J","Q","k","A"};
        
        for(int i=0;i<colour.length;i++){
            for(int j=0;j<number.length;j++){
                pokerBox pb = new pokerBox(colour[i], number[j]);
                pokerBox.add(pb);
            }
        }
        System.out.println("创建扑克牌:");
        for (pokerBox pokerbox : pokerBox) {
            System.out.print(pokerbox.colour+pokerbox.number+" ");
        }
        System.out.println();
    }

    /**
     * 实现洗牌的功能
     * 1、将pokerBox中的元素,通过Random随机生成List下标,并重新赋值到一个新的pokerBox中
     */
    public void pokerRandom(){
        Random random = new Random();
        List<Integer> r = new ArrayList<Integer>();
        int r1;
        for(int i=0;i<pokerBox.size();i++){
            do{
                r1 = random.nextInt(pokerBox.size());
            }while(r.contains(r1));
            r.add(r1);
        pokerBox pokerbox = new pokerBox(pokerBox.get(r1).colour,pokerBox.get(r1).number);
        pokerRandom.add(pokerbox);
        }
        System.out.println("洗牌:");
        for (pokerBox pokerbox : pokerRandom) {
            System.out.print(pokerbox.colour+pokerbox.number+" ");
        }
        System.out.println();
    }
    
    public void pokerRandomSet(){
        
        for(pokerBox pokerbox : pokerBox) {    
            pokerRandomSet.add(pokerbox);
        }
        
        System.out.println("Set集合洗牌:");
        for (pokerBox pokerbox : pokerRandomSet) {
            System.out.print(pokerbox.colour+pokerbox.number+" ");
        }
        System.out.println();
        
    }
    
    
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        
        pokerGame pg = new pokerGame();
        pg.pokerBoxAdd();
        pg.pokerRandom();
        pg.pokerRandomSet();

    }

}

https://img1.sycdn.imooc.com//5cde70170001ed5112580107.jpg

https://img1.sycdn.imooc.com//5cde703100011a1112580107.jpg

https://img1.sycdn.imooc.com//5cde704e00017bb912550109.jpg

在纸牌游戏中,多次对比pokerRandom()方法和pokerRandomSet()方法洗牌后的结果,发现pokerRandom()方法排序基本是每次都不一样。而通过SetList的pokerRandomSet()方法排序结果却基本一样。这是什么回事呢?

正在回答

举报

0/150
提交
取消

通过SetList,无重复元素和无序的特性,来洗牌,但每次生成的结果却基本一致,这是怎么回事呢?

我要回答 关注问题
意见反馈 帮助中心 APP下载
官方微信