Documentation is available at tgcPolarBears.php
1 <?php
2 /**
3 * Class that runs a tiny little game with polar-pears and water-holes :o)
4 *
5 * $Id: tgcPolarBears.php,v 1.4 2004/02/23 21:42:36 luckec Exp $
6 *
7 * @package tgcPolarBears
8 * @author Carsten Lucke <luckec@tool-garage.de>
9 * @copyright Carsten Lucke <http://www.tool-garage.de>
10 */
11
12
13 /**
14 * Include directory
15 *
16 * @access public
17 */
18 define( 'TGCPOLARBEARS_INCLUDEDIR', 'include' );
19
20 /**
21 * Template directory
22 *
23 * @access public
24 */
25 define( 'TGCPOLARBEARS_TEMPLATEDIR', 'templates' );
26
27 /**
28 * Template directory
29 *
30 * @access public
31 */
32 define( 'TGCPOLARBEARS_STYLEDIR', 'css' );
33
34 /**
35 * number of dices used in the game
36 *
37 * @access public
38 */
39 define( 'TGCPOLARBEARS_DICES_USED', 5 );
40
41 /**
42 * number of dices displayed in a row
43 *
44 * @access public
45 */
46 define( 'TGCPOLARBEARS_DICES_PER_ROW', 10 );
47
48 /**
49 * english-language template
50 *
51 * @access public
52 */
53 define( 'TGCPOLARBEARS_LANG_TEMPLATE_EN', 'frame_en.tmpl' );
54
55 /**
56 * german-language template
57 *
58 * @access public
59 */
60 define( 'TGCPOLARBEARS_LANG_TEMPLATE_DE', 'frame_de.tmpl' );
61
62
63
64 /**
65 * Uses patTemplate for visualization
66 *
67 * @link http://www.php-tools.de
68 */
69 require_once TGCPOLARBEARS_INCLUDEDIR . '/patTemplate.php';
70 //require_once TGCPOLARBEARS_INCLUDEDIR . DIRECTORY_SEPARATOR . 'patTemplate.php';
71
72
73 /**
74 * Class that runs a tiny game with polar-pears and water-holes :o)
75 *
76 * @package tgcPolarBears
77 * @access public
78 * @version 1.1.1
79 * @author Carsten Lucke <luckec@tool-garage.de>
80 */
81 class tgcPolarBears
82 {
83 /**
84 * contains the dices, that shall be displayed
85 *
86 * @access private
87 * @var array
88 */
89 var $_dices;
90
91 /**
92 * number of bears sitting around water-holes
93 *
94 * @access private
95 * @var int
96 */
97 var $_bears;
98
99 /**
100 * number of water-holes
101 *
102 * @access private
103 * @var int
104 */
105 var $_holes;
106
107 /**
108 * determines whether there is fish around or not
109 *
110 * @access private
111 * @var boolean
112 */
113 var $_fish;
114
115 /**
116 * template object
117 *
118 * @access private
119 * @var object
120 */
121 var $_tmpl;
122
123 /**
124 * skin- and template-folder
125 *
126 * @access private
127 * @var boolean
128 */
129 var $_skinDir = 'default';
130
131 /**
132 * language
133 *
134 * @access private
135 * @var boolean
136 */
137 var $_lang = 'de';
138
139 /**
140 * Constructor
141 *
142 * @access public
143 */
144 function tgcPolarBears()
145 {
146 $tmpl =& new patTemplate();
147 $tmpl->setBasedir(TGCPOLARBEARS_TEMPLATEDIR);
148 $this->_tmpl =& $tmpl;
149
150 $this->_generateRandomDices();
151 $this->_bears = $this->_countBears();
152 $this->_holes = $this->_countHoles();
153 $this->_fish = $this->_isFishAround();
154 }
155
156 /**
157 * Set the skin
158 *
159 * @access public
160 * @param string $id skin-id
161 */
162 function setSkin($id)
163 {
164 $this->_skinDir = $id;
165 }
166
167 /**
168 * Set the language
169 *
170 * Currently English (en) and German (de)
171 *
172 * <code>
173 * $pb = new tgcPolarBears();
174 * $pb->setLanguage('de');
175 * echo $pb->runGame();
176 * </code>
177 *
178 * @access public
179 * @param string $lang language
180 */
181 function setLanguage($lang)
182 {
183 $this->_lang = $lang;
184 }
185
186 /**
187 * Returns the ASCII text for a dice
188 *
189 * @access private
190 * @param int $eyeNum a dice's eye-number ('ONE', 'TWO', ..., 'SIX')
191 * @return string
192 */
193 function _getDiceText($eyeNum)
194 {
195 return $this->_tmpl->getParsedTemplate('dice_' . $this->_numberToString($eyeNum));
196 }
197
198 /**
199 * Parses the dices that were randomly chosen into the template
200 *
201 * @access private
202 * @return string content
203 */
204 function _parseDices()
205 {
206 $this->_tmpl->clearTemplate('dices_row');
207 $this->_tmpl->clearTemplate('dices_col');
208 $currentCol = 0;
209
210 foreach ($this->_dices as $diceEyeNum) {
211 $dice = $this->_getDiceText($diceEyeNum);
212 $this->_tmpl->addVar('dices_col', 'DICE', $dice);
213 $this->_tmpl->parseTemplate('dices_col', 'a');
214 if (++$currentCol == TGCPOLARBEARS_DICES_PER_ROW) {
215 $this->_tmpl->parseTemplate('dices_row', 'a');
216 $this->_tmpl->clearTemplate('dices_col');
217 }
218 }
219
220 $openCols = TGCPOLARBEARS_DICES_PER_ROW - $currentCol;
221 for ($i = 0; $i < $openCols; $i++) {
222
223 $this->_tmpl->addVar('dices_col', 'DICE', ' ');
224 $this->_tmpl->parseTemplate('dices_col', 'a');
225 }
226 $this->_tmpl->parseTemplate('dices_row', 'a');
227 }
228
229 /**
230 * Parses the dices that were randomly chosen into the template
231 *
232 * @access private
233 * @return string content
234 */
235 function _parseSolution()
236 {
237 $this->_tmpl->addVar('tgcPolarBears_page', 'NUM_BEARS', $this->_bears);
238 $this->_tmpl->addVar('tgcPolarBears_page', 'NUM_HOLES', $this->_holes);
239 $this->_tmpl->addVar('fish_around', 'IS_FISH_AROUND', $this->_fish ? 'yes' : 'no');
240 }
241
242 /**
243 * Generates randomly chosen dices/eye numbers
244 *
245 * @access private
246 */
247 function _generateRandomDices()
248 {
249 $this->_dices = array();
250 srand((double) microtime() * 1000000);
251
252 for ($i = 0; $i < TGCPOLARBEARS_DICES_USED; $i++)
253 {
254 $randEyeNum = rand(1, 6);
255 array_push($this->_dices, $randEyeNum);
256 }
257 }
258
259 /**
260 * Converts a number (1-6) into a string (one - six)
261 *
262 * @access private
263 * @param int $number number
264 * @param boolean $toUpper upper- or lowercase
265 * @return string number
266 */
267 function _numberToString($number, $toUpper = true)
268 {
269 $strNum = '';
270 switch ($number) {
271 case 1:
272 $strNum = 'one';
273 break;
274
275 case 2:
276 $strNum = 'two';
277 break;
278
279 case 3:
280 $strNum = 'three';
281 break;
282
283 case 4:
284 $strNum = 'four';
285 break;
286
287 case 5:
288 $strNum = 'five';
289 break;
290
291 case 6:
292 $strNum = 'six';
293 break;
294 }
295 if ($toUpper) {
296 return strtoupper($strNum);
297 }
298 return $strNum;
299 }
300
301 /**
302 * Counts the bears that are sitting around water-holes
303 *
304 * @access private
305 * @return int number of bears
306 */
307 function _countBears()
308 {
309 $numBears = 0;
310 foreach ($this->_dices as $dice) {
311 switch ($dice) {
312 case 3:
313 $numBears += 2;
314 break;
315
316 case 5:
317 $numBears += 4;
318 break;
319 }
320 }
321 return $numBears;
322 }
323
324 /**
325 * Counts the number of water-holes
326 *
327 * @access private
328 * @return int number of water-holes
329 */
330 function _countHoles()
331 {
332 $numHoles = 0;
333 foreach ($this->_dices as $dice) {
334 if ($dice == 1 || $dice == 3 || $dice == 5) {
335 ++$numHoles;
336 }
337 }
338 return $numHoles;
339 }
340
341 /**
342 * Checks if fish is in the area
343 *
344 * @access private
345 * @return boolean true if fish is hanging around, otherwise false
346 */
347 function _isFishAround()
348 {
349 if (in_array(6, $this->_dices)) {
350 return true;
351 }
352 return false;
353 }
354
355 /**
356 * Starts processing the game
357 *
358 * @access public
359 * @return string html-markup
360 */
361 function runGame()
362 {
363 switch ($this->_lang) {
364 case 'de':
365 $templateFile = TGCPOLARBEARS_LANG_TEMPLATE_DE;
366 break;
367 case 'en':
368 $templateFile = TGCPOLARBEARS_LANG_TEMPLATE_EN;
369 break;
370 }
371 $this->_tmpl->readTemplatesFromFile($this->_skinDir . '/' . $templateFile);
372
373 $this->_tmpl->addGlobalVar('STYLEDIR', TGCPOLARBEARS_STYLEDIR . '/' . $this->_skinDir);
374 $this->_tmpl->addGlobalVar('SELF_SITE', $_SERVER['SCRIPT_NAME']);
375
376 $this->_parseDices();
377 $this->_parseSolution();
378 return $this->_tmpl->getParsedTemplate('tgcPolarBears_page');
379 }
380 }
381 ?>
Documentation generated on Fri, 19 Nov 2004 23:36:25 +0100 by phpDocumentor 1.2.3