summaryrefslogtreecommitdiff
path: root/noncore/games/zsame/dropin
authorzecke <zecke>2004-02-17 15:55:24 (UTC)
committer zecke <zecke>2004-02-17 15:55:24 (UTC)
commit1207607ebbc59841718b79508fc222cb4eee9fde (patch) (unidiff)
tree60c292a98621385006fbacca82dd11326765c0ab /noncore/games/zsame/dropin
parent2ec4085cc290a212c7bec8bdf7a2475f3ee6e069 (diff)
downloadopie-1207607ebbc59841718b79508fc222cb4eee9fde.zip
opie-1207607ebbc59841718b79508fc222cb4eee9fde.tar.gz
opie-1207607ebbc59841718b79508fc222cb4eee9fde.tar.bz2
Add the source of zsame
Diffstat (limited to 'noncore/games/zsame/dropin') (more/less context) (show whitespace changes)
-rw-r--r--noncore/games/zsame/dropin/kapplication.h23
-rw-r--r--noncore/games/zsame/dropin/krandomsequence.cpp240
-rw-r--r--noncore/games/zsame/dropin/krandomsequence.h143
3 files changed, 406 insertions, 0 deletions
diff --git a/noncore/games/zsame/dropin/kapplication.h b/noncore/games/zsame/dropin/kapplication.h
new file mode 100644
index 0000000..f83d08e
--- a/dev/null
+++ b/noncore/games/zsame/dropin/kapplication.h
@@ -0,0 +1,23 @@
1#include <stdio.h>
2#include <stdlib.h>
3#include <sys/types.h>
4#include <sys/stat.h>
5#include <fcntl.h>
6#include <unistd.h>
7#include <time.h>
8
9static int _random() {
10 static int init = false;
11 if (!init) {
12 unsigned int seed;
13 init = true;
14 int fd = ::open("/dev/urandom", O_RDONLY);
15 if( fd<=0 || ::read(fd, &seed, sizeof(seed)) != sizeof(seed) ) {
16 srand(getpid());
17 seed = rand()+time(0);
18 }
19 if(fd>=0) close( fd );
20 srand(seed);
21 }
22 return rand();
23}
diff --git a/noncore/games/zsame/dropin/krandomsequence.cpp b/noncore/games/zsame/dropin/krandomsequence.cpp
new file mode 100644
index 0000000..d27a1c5
--- a/dev/null
+++ b/noncore/games/zsame/dropin/krandomsequence.cpp
@@ -0,0 +1,240 @@
1/*
2 This file is part of the KDE libraries
3 Copyright (c) 1999 Sean Harmer <sh@astro.keele.ac.uk>
4
5 This library is free software; you can redistribute it and/or
6 modify it under the terms of the GNU Library General Public
7 License as published by the Free Software Foundation; either
8 version 2 of the License, or (at your option) any later version.
9
10 This library is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 Library General Public License for more details.
14
15 You should have received a copy of the GNU Library General Public License
16 along with this library; see the file COPYING.LIB. If not, write to
17 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
18 Boston, MA 02111-1307, USA.
19*/
20
21#include <qlist.h>
22#include <string.h>
23
24#include "kapplication.h"
25#include "krandomsequence.h"
26
27const int KRandomSequence::m_nShuffleTableSize = 32;
28
29//////////////////////////////////////////////////////////////////////////////
30 //Construction / Destruction
31//////////////////////////////////////////////////////////////////////////////
32
33KRandomSequence::KRandomSequence( long lngSeed1 )
34{
35 // Seed the generator
36 setSeed( lngSeed1 );
37
38
39 // Set the size of the shuffle table
40 m_ShuffleArray = new long [m_nShuffleTableSize];
41}
42
43KRandomSequence::~KRandomSequence()
44{
45 delete [] m_ShuffleArray;
46}
47
48KRandomSequence::KRandomSequence(const KRandomSequence &a)
49{
50 // Set the size of the shuffle table
51 m_ShuffleArray = new long [m_nShuffleTableSize];
52 *this = a;
53}
54
55KRandomSequence &
56KRandomSequence::operator=(const KRandomSequence &a)
57{
58 m_lngSeed1 = a.m_lngSeed1;
59 m_lngSeed2 = a.m_lngSeed2;
60 m_lngShufflePos = a.m_lngShufflePos;
61 memcpy(m_ShuffleArray, a.m_ShuffleArray, sizeof(long)*m_nShuffleTableSize);
62 return *this;
63}
64
65
66//////////////////////////////////////////////////////////////////////////////
67 //Member Functions
68//////////////////////////////////////////////////////////////////////////////
69
70void KRandomSequence::setSeed( long lngSeed1 )
71{
72 // Convert the positive seed number to a negative one so that the Draw()
73 // function can intialise itself the first time it is called. We just have
74 // to make sure that the seed used != 0 as zero perpetuates itself in a
75 // sequence of random numbers.
76 if ( lngSeed1 < 0 )
77 {
78 m_lngSeed1 = -1;
79 }
80 else if (lngSeed1 == 0)
81 {
82 m_lngSeed1 = -((_random() & ~1)+1);
83 }
84 else
85 {
86 m_lngSeed1 = -lngSeed1;
87 }
88}
89
90static const long sMod1 = 2147483563;
91static const long sMod2 = 2147483399;
92
93void KRandomSequence::Draw()
94{
95 static const long sMM1 = sMod1 - 1;
96 static const long sA1 = 40014;
97 static const long sA2 = 40692;
98 static const long sQ1 = 53668;
99 static const long sQ2 = 52774;
100 static const long sR1 = 12211;
101 static const long sR2 = 3791;
102 static const long sDiv = 1 + sMM1 / m_nShuffleTableSize;
103
104 // Long period (>2 * 10^18) random number generator of L'Ecuyer with
105 // Bayes-Durham shuffle and added safeguards. Returns a uniform random
106 // deviate between 0.0 and 1.0 (exclusive of the endpoint values). Call
107 // with a negative number to initialize; thereafter, do not alter idum
108 // between successive deviates in a sequence. RNMX should approximate
109 // the largest floating point value that is less than 1.
110
111 int j; // Index for the shuffle table
112 long k;
113
114 // Initialise
115 if ( m_lngSeed1 <= 0 )
116 {
117 m_lngSeed2 = m_lngSeed1;
118
119 // Load the shuffle table after 8 warm-ups
120 for ( j = m_nShuffleTableSize + 7; j >= 0; j-- )
121 {
122 k = m_lngSeed1 / sQ1;
123 m_lngSeed1 = sA1 * ( m_lngSeed1 - k*sQ1) - k*sR1;
124 if ( m_lngSeed1 < 0 )
125 {
126 m_lngSeed1 += sMod1;
127 }
128
129 if ( j < m_nShuffleTableSize )
130 {
131 m_ShuffleArray[j] = m_lngSeed1;
132 }
133 }
134
135 m_lngShufflePos = m_ShuffleArray[0];
136 }
137
138 // Start here when not initializing
139
140 // Compute m_lngSeed1 = ( lngIA1*m_lngSeed1 ) % lngIM1 without overflows
141 // by Schrage's method
142 k = m_lngSeed1 / sQ1;
143 m_lngSeed1 = sA1 * ( m_lngSeed1 - k*sQ1 ) - k*sR1;
144 if ( m_lngSeed1 < 0 )
145 {
146 m_lngSeed1 += sMod1;
147 }
148
149 // Compute m_lngSeed2 = ( lngIA2*m_lngSeed2 ) % lngIM2 without overflows
150 // by Schrage's method
151 k = m_lngSeed2 / sQ2;
152 m_lngSeed2 = sA2 * ( m_lngSeed2 - k*sQ2 ) - k*sR2;
153 if ( m_lngSeed2 < 0 )
154 {
155 m_lngSeed2 += sMod2;
156 }
157
158 j = m_lngShufflePos / sDiv;
159 m_lngShufflePos = m_ShuffleArray[j] - m_lngSeed2;
160 m_ShuffleArray[j] = m_lngSeed1;
161
162 if ( m_lngShufflePos < 1 )
163 {
164 m_lngShufflePos += sMM1;
165 }
166}
167
168void
169KRandomSequence::modulate(int i)
170{
171 m_lngSeed2 -= i;
172 if ( m_lngSeed2 < 0 )
173 {
174 m_lngShufflePos += sMod2;
175 }
176 Draw();
177 m_lngSeed1 -= i;
178 if ( m_lngSeed1 < 0 )
179 {
180 m_lngSeed1 += sMod1;
181 }
182 Draw();
183}
184
185double
186KRandomSequence::getDouble()
187{
188 static const double finalAmp = 1.0 / double( sMod1 );
189 static const double epsilon = 1.2E-7;
190 static const double maxRand = 1.0 - epsilon;
191 double temp;
192 Draw();
193 // Return a value that is not one of the endpoints
194 if ( ( temp = finalAmp * m_lngShufflePos ) > maxRand )
195 {
196 // We don't want to return 1.0
197 return maxRand;
198 }
199 else
200 {
201 return temp;
202 }
203}
204
205unsigned long
206KRandomSequence::getLong(unsigned long max)
207{
208 Draw();
209
210 return max ? (((unsigned long) m_lngShufflePos) % max) : 0;
211}
212
213bool
214KRandomSequence::getBool()
215{
216 Draw();
217
218 return (((unsigned long) m_lngShufflePos) & 1);
219}
220
221class KRandomSequenceList : public QGList
222{
223 friend class KRandomSequence;
224public:
225 KRandomSequenceList() : QGList() { }
226 virtual void deleteItem( QCollection::Item ) {}
227};
228
229void
230KRandomSequence::randomize(QGList *_list)
231{
232 KRandomSequenceList *list = (KRandomSequenceList *)_list;
233 KRandomSequenceList l;
234 while(list->count())
235 l.append(list->takeFirst());
236
237 list->append(l.takeFirst()); // Start with 1
238 while(l.count())
239 list->insertAt(getLong(list->count()+1), l.takeFirst());
240}
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