从池中选择国际象棋棋手 - 算法

发布于 2024-11-08 05:39:09 字数 163 浏览 0 评论 0原文

你好,我有这个问题
有 n[偶] 名玩家想要下棋
我怎样才能随机化玩家和对手? [每个玩家只有一次机会]

认为有 6 个玩家 - p1,p2,p3,p4,p5,p6
我想做一个代码来为我完成这样的例程
p1 与 p5
p2 与 p6
p3 与 p4

Hii, i have this problem
There are n[even] number of players who want to play chess
How can i get randomize player and opponents ? [every player will get only one chance]

Think there are 6 player - p1,p2,p3,p4,p5,p6
I want to do a code which will do such routine for me
p1 vs p5
p2 vs p6
p3 vs p4

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

独闯女儿国 2024-11-15 05:39:09

您可以为每个玩家分配一个唯一的随机数,使用该数字对玩家列表进行排序,并选择 1-2、3-4、5-6 等对。应该非常快,因为现在几乎每种语言都有内置排序。

You can assign a unique random number to every player, sort the player list using that number and select pairs 1-2, 3-4, 5-6 and so on. Should be very fast because of built-in sorting that almost every language has now.

终难遇 2024-11-15 05:39:09

执行所有组合并随机播放Fisher-Yates

Do all combinations and shuffle them Fisher-Yates

千年*琉璃梦 2024-11-15 05:39:09
store players in array a of size n
let p be a pair(w, b)
let l be a list of pairs
while n > 0
    let x be a random number between 0 and n
    add player at index x in a to p as w
    remove index x from a
    n--
    let y be a random number between 0 and n
    add player at index y in a to p as b
    remove index y from a
    n--
    add p to l
endwhile
store players in array a of size n
let p be a pair(w, b)
let l be a list of pairs
while n > 0
    let x be a random number between 0 and n
    add player at index x in a to p as w
    remove index x from a
    n--
    let y be a random number between 0 and n
    add player at index y in a to p as b
    remove index y from a
    n--
    add p to l
endwhile
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文