File indexing completed on 2024-05-12 04:04:51

0001 /*
0002  * Copyright (C) 1995 Paul Olav Tvete <paul@troll.no>
0003  * Copyright (C) 2000-2009 Stephan Kulow <coolo@kde.org>
0004  * Copyright (C) 2009-2010 Parker Coates <coates@kde.org>
0005  *
0006  * License of original code:
0007  * -------------------------------------------------------------------------
0008  *   Permission to use, copy, modify, and distribute this software and its
0009  *   documentation for any purpose and without fee is hereby granted,
0010  *   provided that the above copyright notice appear in all copies and that
0011  *   both that copyright notice and this permission notice appear in
0012  *   supporting documentation.
0013  *
0014  *   This file is provided AS IS with no warranties of any kind.  The author
0015  *   shall have no liability with respect to the infringement of copyrights,
0016  *   trade secrets or any patents by this file or any part thereof.  In no
0017  *   event will the author be liable for any lost revenue or profits or
0018  *   other special, indirect and consequential damages.
0019  * -------------------------------------------------------------------------
0020  *
0021  * License of modifications/additions made after 2009-01-01:
0022  * -------------------------------------------------------------------------
0023  *   This program is free software; you can redistribute it and/or
0024  *   modify it under the terms of the GNU General Public License as
0025  *   published by the Free Software Foundation; either version 2 of
0026  *   the License, or (at your option) any later version.
0027  *
0028  *   This program is distributed in the hope that it will be useful,
0029  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
0030  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
0031  *   GNU General Public License for more details.
0032  *
0033  *   You should have received a copy of the GNU General Public License
0034  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
0035  * -------------------------------------------------------------------------
0036  */
0037 
0038 // Qt
0039 #include <QList>
0040 // Std
0041 #include <cmath>
0042 
0043 #ifndef SHUFFLE_H
0044 #define SHUFFLE_H
0045 namespace KpatShuffle
0046 {
0047 template<class T>
0048 QList<T> shuffled(const QList<T> &cards, unsigned int seed)
0049 {
0050     QList<T> result = cards;
0051     for (int i = result.size(); i > 1; --i) {
0052         // We use the same pseudorandom number generation algorithm as Windows
0053         // Freecell, so that game numbers are the same between the two applications.
0054         // For more information, see
0055         // https://fc-solve.shlomifish.org/faq.html#what_are_ms_deals
0056         seed = 214013 * seed + 2531011;
0057         int rand = (seed >> 16) & 0x7fff;
0058         result.swapItemsAt(i - 1, rand % i);
0059     }
0060 
0061     return result;
0062 }
0063 };
0064 
0065 #endif