+2008-06-28 Daigo Moriwaki <daigo at debian dot org>
+
+ * [shogi-server]
+ - Pairing players might not have shuffled players because of
+ poor shuffling algorithm. This issue has been fixed.
+ (Closes: #12661)
+
2008-06-25 Daigo Moriwaki <daigo at debian dot org>
* [players_graph.rb]
## along with this program; if not, write to the Free Software
## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+require 'shogi_server/util'
+
module ShogiServer
class Pairing
log_warning("#Players should be even: %d" % [players.size])
return
end
- sorted = players.sort{ rand < 0.5 ? 1 : -1 }
- pairs = [[sorted.shift]]
- while !sorted.empty? do
+ ShogiServer.shuffle(players)
+
+ pairs = [[players.shift]]
+ while !players.empty? do
if pairs.last.size < 2
- pairs.last << sorted.shift
+ pairs.last << players.shift
else
- pairs << [sorted.shift]
+ pairs << [players.shift]
end
end
pairs.each do |pair|
--- /dev/null
+## $Id$
+
+## Copyright (C) 2004 NABEYA Kenichi (aka nanami@2ch)
+## Copyright (C) 2007-2008 Daigo Moriwaki (daigo at debian dot org)
+##
+## This program is free software; you can redistribute it and/or modify
+## it under the terms of the GNU General Public License as published by
+## the Free Software Foundation; either version 2 of the License, or
+## (at your option) any later version.
+##
+## This program is distributed in the hope that it will be useful,
+## but WITHOUT ANY WARRANTY; without even the implied warranty of
+## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+## GNU General Public License for more details.
+##
+## You should have received a copy of the GNU General Public License
+## along with this program; if not, write to the Free Software
+## Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
+
+module ShogiServer
+
+ # Generate a random number such as i<=n<max
+ def random(i, max)
+ raise if i >= max
+ return rand(max-i)+i
+ end
+ module_function :random
+
+ def shuffle(array)
+ return if array.size < 2
+ for i in 0...(array.size-1)
+ r = random(i, array.size)
+ a = array[i]
+ array[i] = array[r]
+ array[r] = a
+ end
+ end
+ module_function :shuffle
+
+end