OSDN Git Service

* [shogi-server]
authorbeatles <beatles@b8c68f68-1e22-0410-b08e-880e1f8202b4>
Sat, 28 Jun 2008 01:56:29 +0000 (01:56 +0000)
committerbeatles <beatles@b8c68f68-1e22-0410-b08e-880e1f8202b4>
Sat, 28 Jun 2008 01:56:29 +0000 (01:56 +0000)
  -  Pairing players might not have shuffled players because of
     poor shuffling algorithm. This issue has been fixed.
     (Closes: #12661)

changelog
shogi_server/pairing.rb
shogi_server/util.rb [new file with mode: 0644]

index bf46119..70b2377 100644 (file)
--- a/changelog
+++ b/changelog
@@ -1,3 +1,10 @@
+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]
index 18f0925..22de6c7 100644 (file)
@@ -17,6 +17,8 @@
 ## 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
@@ -76,14 +78,15 @@ module ShogiServer
         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|
diff --git a/shogi_server/util.rb b/shogi_server/util.rb
new file mode 100644 (file)
index 0000000..b3141e6
--- /dev/null
@@ -0,0 +1,40 @@
+## $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