summaryrefslogtreecommitdiff
path: root/noncore/games/zsame/dropin/krandomsequence.h
Unidiff
Diffstat (limited to 'noncore/games/zsame/dropin/krandomsequence.h') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/zsame/dropin/krandomsequence.h143
1 files changed, 143 insertions, 0 deletions
diff --git a/noncore/games/zsame/dropin/krandomsequence.h b/noncore/games/zsame/dropin/krandomsequence.h
new file mode 100644
index 0000000..402e106
--- a/dev/null
+++ b/noncore/games/zsame/dropin/krandomsequence.h
@@ -0,0 +1,143 @@
1/* This file is part of the KDE libraries
2 Copyright (c) 1999 Sean Harmer <sh@astro.keele.ac.uk>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License version 2 as published by the Free Software Foundation.
7
8 This library is distributed in the hope that it will be useful,
9 but WITHOUT ANY WARRANTY; without even the implied warranty of
10 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 Library General Public License for more details.
12
13 You should have received a copy of the GNU Library General Public License
14 along with this library; see the file COPYING.LIB. If not, write to
15 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16 Boston, MA 02111-1307, USA.
17*/
18#ifndef K_RANDOM_SEQUENCE_H
19#define K_RANDOM_SEQUENCE_H
20
21class KRandomSequencePrivate;
22class QGList;
23/**
24 * A class to create a pseudo-random sequence
25 *
26 * Given a seed number, this class will produce a sequence of
27 * pseudo-random numbers. This would typically be used in
28 * applications like games.
29 *
30 * In general, you should instantiate a KRandomSequence object and
31 * pass along your seed number in the constructor. From then on,
32 * simply call getDouble or getLong to obtain the next
33 * number in the sequence.
34 *
35 * @author Sean Harmer <sh@astro.keele.ac.uk>
36 */
37class KRandomSequence
38{
39public:
40 /**
41 * Creates a pseudo-random sequence based on the seed lngSeed.
42 *
43 * A Pseudo-random sequence is different for each seed but can be
44 * reproduced by starting the sequence with the same seed.
45 *
46 * If you need a single value which needs to be unpredictable,
47 * you need to use kapp->random() instead.
48 *
49 * @param lngSeed Seed to initialize the sequence with.
50 * If lngSeed is 0, the sequence is initialized with a value from
51 * KApplication::random().
52 */
53 KRandomSequence( long lngSeed = 0 );
54
55 /**
56 * Standard destructor
57 */
58 virtual ~KRandomSequence();
59
60 /**
61 * Copy constructor
62 */
63 KRandomSequence(const KRandomSequence &a);
64
65 /**
66 * Assignment
67 */
68 KRandomSequence &operator=(const KRandomSequence &a);
69
70 /**
71 * Restart the sequence based on lngSeed.
72 * @param lngSeed Seed to initialize the sequence with.
73 * If lngSeed is 0, the sequence is initialized with a value from
74 * KApplication::random().
75 */
76 void setSeed( long lngSeed = 1 );
77
78 /**
79 * Get the next number from the pseudo-random sequence.
80 *
81 * @return a pseudo-random double value between [0,1[
82 */
83 double getDouble();
84
85 /**
86 * Get the next number from the pseudo-random sequence.
87 *
88 * @return a pseudo-random integer value between [0, max[
89 * with 0 <= max < 1.000.000
90 */
91 unsigned long getLong(unsigned long max);
92
93 /**
94 * Get a boolean from the pseudo-random sequence.
95 *
96 * @return a boolean which is either true or false
97 */
98 bool getBool();
99
100 /**
101 * Put a list in random order.
102 *
103 * @param list the list whose order will be modified
104 */
105 void randomize(QGList *list);
106
107 /**
108 * Modulate the random sequence.
109 *
110 * If S(i) is the sequence of numbers that will follow
111 * given the current state after calling modulate(i),
112 * then S(i) != S(j) for i != j and
113 * S(i) == S(j) for i == j.
114 *
115 * This can be useful in game situation where "undo" restores
116 * the state of the random sequence. If the game modulates the
117 * random sequence with the move chosen by the player, the
118 * random sequence will be identical whenever the player "redo"-s
119 * his or hers original move, but different when the player
120 * chooses another move.
121 *
122 * With this scenario "undo" can no longer be used to repeat a
123 * certain move over and over again until the computer reacts
124 * with a favorable response or to predict the response for a
125 * certain move based on the response to another move.
126 * @param i the sequence identified
127 */
128 void modulate(int i);
129
130private:
131 void Draw(); // Generate the random number
132 long m_lngSeed1;
133 long m_lngSeed2;
134 long m_lngShufflePos;
135
136 static const int m_nShuffleTableSize;
137 long *m_ShuffleArray;
138
139 KRandomSequencePrivate *d;
140};
141
142#endif
143