001    ///////////////////////////////////////////////////////////////////////////////
002    // Copyright (c) 2006, Frank S. Nestel, All Rights Reserved.
003    //
004    // This library is free software; you can redistribute it and/or
005    // modify it under the terms of the GNU Lesser General Public
006    // License as published by the Free Software Foundation; either
007    // version 2.1 of the License, or (at your option) any later version.
008    //
009    // This library is distributed in the hope that it will be useful,
010    // but WITHOUT ANY WARRANTY; without even the implied warranty of
011    // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012    // GNU General Public License for more details.
013    //
014    // You should have received a copy of the GNU Lesser General Public
015    // License along with this program; if not, write to the Free Software
016    // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
017    ///////////////////////////////////////////////////////////////////////////////
018    
019    package de.spieleck.app.turn.pairing;
020    
021    import java.util.Arrays;
022    import java.util.Random;
023    
024    import de.spieleck.app.turn.Game;
025    import de.spieleck.app.turn.Player;
026    import de.spieleck.app.turn.SplittingMode;
027    import de.spieleck.app.turn.PlayerRegistry;
028    
029    /**
030     * A pairing mode, randomly shuffling players. 
031     * E.g. good for a first round in a tourney.
032     *
033     * <p><a href="$URL: https://svn.sourceforge.net/svnroot/jtourney/src/de/spieleck/app/turn/pairing/RandomPairing.java $">$URL: https://svn.sourceforge.net/svnroot/jtourney/src/de/spieleck/app/turn/pairing/RandomPairing.java $</a></p>
034     *
035     * @author Frank S. Nestel
036     * @author $Author: nestefan $
037     * @version $Revision: 2 $ $Date: 2006-03-20 14:33:27 +0100 (Mo, 20 Mrz 2006) $ $Author: nestefan $
038     */
039    public class RandomPairing
040        extends BasePairing
041    {
042        private Random rand = new Random(); // new Random(42L);
043    
044        public Game[] pairing(int round, PlayerRegistry pRegistry, SplittingMode sm)
045        {
046            Player[] standing = pRegistry.getActivePlayers();
047            int n = standing.length;
048            int[] splitting = sm.split(n);
049            Player[] st2 = new Player[n];
050            System.arraycopy(standing, 0, st2, 0, n);
051            // Shuffle the Array
052            for(int i = 0; i < n; i++)
053            {
054                int r = i + rand.nextInt(n-i);
055                Player p1 = st2[i];
056                st2[i] = st2[r];
057                st2[r] = p1;
058            }
059            int gameCount = sum(splitting);
060            int j = 0;
061            int k = 0;
062            int l = 0;
063            GameImpl[] res = new GameImpl[gameCount];
064            while ( k < gameCount )
065            {
066                while ( splitting[j] == 0 )
067                    j++;
068                GameImpl g = new GameImpl(k+1, st2, l, j);
069                l += j;
070                res[k++] = g;
071                splitting[j]--;
072            }
073            return res;
074        }
075    
076        public String toString()
077        {
078            return "RandomPairing";
079        }
080    }