Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 按随机顺序排序arraylist_Java_Arraylist_Collections - Fatal编程技术网

Java 按随机顺序排序arraylist

Java 按随机顺序排序arraylist,java,arraylist,collections,Java,Arraylist,Collections,我正在编写一个纸牌游戏,我有一个ArrayList持有纸牌(Object),其中 他们每个人都有自己的id 因为我想让这个游戏支持多人模式, 我必须以某种方式发送/接收两名玩家之间的游戏进度 现在,如果我洗牌在一边,我必须在另一边做同样的事情,但它将是一个大数据包发送,因为每个卡都有一个图像 我的想法是发送一个洗牌列表的整数数组,这样它将在另一端接收,并按照整数数组重新排序 如何发送洗牌顺序并将其应用到另一端?该类可用于此操作 如果使用同一种子创建了两个Random实例,则 对每个方法调用的顺序

我正在编写一个纸牌游戏,我有一个
ArrayList
持有纸牌(
Object
),其中 他们每个人都有自己的id

因为我想让这个游戏支持多人模式, 我必须以某种方式发送/接收两名玩家之间的游戏进度

现在,如果我洗牌在一边,我必须在另一边做同样的事情,但它将是一个大数据包发送,因为每个卡都有一个图像

我的想法是发送一个洗牌列表的整数数组,这样它将在另一端接收,并按照整数数组重新排序

如何发送洗牌顺序并将其应用到另一端?

该类可用于此操作

如果使用同一种子创建了两个Random实例,则 对每个方法调用的顺序相同,它们将生成 返回相同的数字序列。为了保证这一点, 属性,则为Random类指定了特定的算法。 Java实现必须使用此处所示的所有算法 类随机,以确保Java代码的绝对可移植性

这意味着您只能将种子值传输到客户端,使用它实例化一个新的
Random
实例,并期望接收与其他玩家机器上相同的随机数序列


可以使用
随机
源调用。

是的,只需初始化卡并将其存储在两侧开头52个卡对象的只读数组中:

final Card[] allCards = new Card[52] {...};
在两侧的整个应用程序中专门使用此数组,仅使用
int
索引而不是
card
实例来引用每张卡


然后,您将有一个
deck
对象,它是
int[52]
。最初,它将包含从0到51的所有数字。然后洗牌数组。然后将int数组发送到另一端,另一端则具有相同的随机数据组。

尽管可以使用相同的种子初始化两个java.util.Random实例,并将这些实例与Henrik提到的
Collections.shuffle()
一起使用。唯一的问题是对这些实例的
Collections.shuffle()
调用必须以相同的顺序调用(即同步)。可能无法保证这一点,例如,如果一名玩家连续快速地请求多张牌洗牌,而另一名玩家不同步(由于网络问题)

另一种方法是在两端手动进行排序。下面给出了一个例子

public class Card {
    private final int id;
    private final String imageURL;

    public Card(int id, String imageURL) {
        this.id = id;
        this.imageURL = imageURL;
    }

    public int getId() {
        return id;
    }

    public String getImageURL() {
        return imageURL;
    }

   /**
    *  Getters and setters below
    */
}


public class Example {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        final int NUMBER_OF_CARDS = 6;
        //
        final List<Card> originalList = new ArrayList<>();
        //
        for (int i = 1; i <= NUMBER_OF_CARDS; i++) {
            originalList.add(new Card(i, "image_url"));
        }
        //Create player card list
        final List<Card> firstPlayerCardsList = new ArrayList<>(originalList);
        final List<Card> secondPlayerCardsList = new ArrayList<>(originalList);
        //
        //1. Shuffle list on one side
        Collections.shuffle(firstPlayerCardsList);
        //
        //2. Iterate over the list and add indices to array
        final int[] indices = new int[NUMBER_OF_CARDS];
        //note indices are zero based to allign with arrays
        for (int i = 0; i < NUMBER_OF_CARDS; i++) {
            indices[i] = firstPlayerCardsList.get(i).getId();
        }
        //
        // 3. Send the shuffle indices array across to second player
        // ********* omitted ******
        //
        // 4. Recreate the shuffle order at the second players end, based on the shuffle indices array
        final List<Card> tempCardsList = new ArrayList<>(secondPlayerCardsList);
        secondPlayerCardsList.clear();
        IntStream.range(0, NUMBER_OF_CARDS).forEach((int index) -> {
            final int id = indices[index];
            for (final Card c : tempCardsList) {
                if (c.getId() == id) {
                    secondPlayerCardsList.add(c);
                }
            }
            // can also use in place of the above
            //tempCardsList.stream().filter((c) -> (c.getId() == id)).forEachOrdered((c) -> {
            //    secondPlayerCardsList.add(c);
            //});
        });
        // Show the results for this illustration
        System.out.println("   Original" + "   First" +  "    Second");
        IntStream.range(0, NUMBER_OF_CARDS).forEach((int index) -> {
            System.out.println("\t" + originalList.get(index).getId() +"\t" + firstPlayerCardsList.get(index).getId() + "\t" + secondPlayerCardsList.get(index).getId());
        });
    }

}
公共类卡{
私有最终int id;
私有最终字符串imageURL;
公共卡(int-id,String-imageURL){
this.id=id;
this.imageURL=imageURL;
}
公共int getId(){
返回id;
}
公共字符串getImageURL(){
返回imageURL;
}
/**
*下面是接受者和接受者
*/
}
公开课范例{
/**
*@param指定命令行参数
*/
公共静态void main(字符串[]args){
卡的最终整数=6;
//
最终列表originalList=新的ArrayList();
//
对于(int i=1;i{
最终int id=索引[索引];
用于(最终卡片c:临时卡片列表){
if(c.getId()==id){
第二个玩家卡片列表。添加(c);
}
}
//也可以用在上述地方
//tempCardsList.stream().filter((c)->(c.getId()==id)).forEachOrdered((c)->{
//第二个玩家卡片列表。添加(c);
//});
});
//显示此图的结果
System.out.println(“原始”+“第一”+“第二”);
IntStream.range(0,卡的数量)。forEach((int索引)->{
System.out.println(“\t”+原始列表.get(index).getId()+“\t”+第一个PlayerCardsList.get(index).getId()+“\t”+第二个PlayerCardsList.get(index.getId());
});
}
}

collections.shuffle()将对数组的一侧进行洗牌,有没有办法将另一侧的列表按原始列表的顺序进行洗牌?如果您希望另一侧的列表与原始列表相同…那么这不是随机洗牌。好主意,但该程序的许多架构都应该更改。。我在整个程序中使用数组列表来传递Card对象的实例,处理索引需要很多更改。。一般来说,谢谢兄弟:)