gpt4 book ai didi

java - 以随机顺序对arraylist进行排序

转载 作者:行者123 更新时间:2023-11-30 07:51:23 27 4
gpt4 key购买 nike

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

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

现在,如果我在一边洗牌,我必须在另一边做同样的事情,但要发送的数据包会很大,因为每张牌都有图像。

我想的是发送一个打乱后的列表的整数数组,所以它会在另一边收到并按照整数数组重新排序。

如何发送打乱的订单并应用到另一边?

最佳答案

Random类可以用于此。

If two instances of Random are created with the same seed, and the same sequence of method calls is made for each, they will generate and return identical sequences of numbers. In order to guarantee this property, particular algorithms are specified for the class Random. Java implementations must use all the algorithms shown here for the class Random, for the sake of absolute portability of Java code.

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

Collections.shuffle可以使用 Random 源调用。

关于java - 以随机顺序对arraylist进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47200129/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com