summaryrefslogtreecommitdiff
path: root/noncore/games/tetrix
Unidiff
Diffstat (limited to 'noncore/games/tetrix') (more/less context) (ignore whitespace changes)
-rw-r--r--noncore/games/tetrix/.cvsignore3
-rw-r--r--noncore/games/tetrix/Makefile.in157
-rw-r--r--noncore/games/tetrix/gtetrix.cpp514
-rw-r--r--noncore/games/tetrix/gtetrix.h104
-rw-r--r--noncore/games/tetrix/main.cpp33
-rw-r--r--noncore/games/tetrix/qpe-tetrix.control10
-rw-r--r--noncore/games/tetrix/qtetrix.cpp170
-rw-r--r--noncore/games/tetrix/qtetrix.h78
-rw-r--r--noncore/games/tetrix/qtetrixb.cpp251
-rw-r--r--noncore/games/tetrix/qtetrixb.h80
-rw-r--r--noncore/games/tetrix/tetrix.pro17
-rw-r--r--noncore/games/tetrix/tpiece.cpp201
-rw-r--r--noncore/games/tetrix/tpiece.h62
13 files changed, 1680 insertions, 0 deletions
diff --git a/noncore/games/tetrix/.cvsignore b/noncore/games/tetrix/.cvsignore
new file mode 100644
index 0000000..edfa921
--- a/dev/null
+++ b/noncore/games/tetrix/.cvsignore
@@ -0,0 +1,3 @@
1moc_*
2*.moc
3Makefile
diff --git a/noncore/games/tetrix/Makefile.in b/noncore/games/tetrix/Makefile.in
new file mode 100644
index 0000000..3a74fdc
--- a/dev/null
+++ b/noncore/games/tetrix/Makefile.in
@@ -0,0 +1,157 @@
1#############################################################################
2
3####### Compiler, tools and options
4
5 CXX =$(SYSCONF_CXX) $(QT_CXX_MT)
6 CXXFLAGS=$(SYSCONF_CXXFLAGS_QT) $(SYSCONF_CXXFLAGS)
7 CC =$(SYSCONF_CC) $(QT_C_MT)
8 CFLAGS =$(SYSCONF_CFLAGS)
9 INCPATH =-I$(QPEDIR)/include
10 LFLAGS =$(SYSCONF_LFLAGS_QT) $(SYSCONF_RPATH_QT) $(SYSCONF_LFLAGS) $(QT_LFLAGS_MT)
11 LIBS =$(SUBLIBS) -lqpe $(SYSCONF_LIBS_QT) $(SYSCONF_LIBS) $(SYSCONF_LIBS_QTAPP)
12 MOC =$(SYSCONF_MOC)
13 UIC =$(SYSCONF_UIC)
14
15####### Target
16
17DESTDIR = ../bin/
18VER_MAJ = 1
19VER_MIN = 0
20VER_PATCH = 0
21 TARGET= tetrix
22TARGET1 = lib$(TARGET).so.$(VER_MAJ)
23
24####### Files
25
26 HEADERS =minefield.h \
27 gtetrix.h \
28 qtetrix.h \
29 qtetrixb.h \
30 tpiece.h
31 SOURCES =main.cpp \
32 gtetrix.cpp \
33 qtetrix.cpp \
34 qtetrixb.cpp \
35 tpiece.cpp
36 OBJECTS =main.o \
37 gtetrix.o \
38 qtetrix.o \
39 qtetrixb.o \
40 tpiece.o
41INTERFACES =
42UICDECLS =
43UICIMPLS =
44 SRCMOC =moc_qtetrix.cpp \
45 moc_qtetrixb.cpp
46 OBJMOC =moc_qtetrix.o \
47 moc_qtetrixb.o
48
49
50####### Implicit rules
51
52.SUFFIXES: .cpp .cxx .cc .C .c
53
54.cpp.o:
55 $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
56
57.cxx.o:
58 $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
59
60.cc.o:
61 $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
62
63.C.o:
64 $(CXX) -c $(CXXFLAGS) $(INCPATH) -o $@ $<
65
66.c.o:
67 $(CC) -c $(CFLAGS) $(INCPATH) -o $@ $<
68
69####### Build rules
70
71
72all: $(DESTDIR)$(TARGET)
73
74$(DESTDIR)$(TARGET): $(UICDECLS) $(OBJECTS) $(OBJMOC) $(SUBLIBS)
75 $(SYSCONF_LINK) $(LFLAGS) -o $(DESTDIR)$(TARGET) $(OBJECTS) $(OBJMOC) $(LIBS)
76
77moc: $(SRCMOC)
78
79tmake:
80 tmake tetrix.pro
81
82clean:
83 -rm -f $(OBJECTS) $(OBJMOC) $(SRCMOC) $(UICIMPLS) $(UICDECLS)
84 -rm -f *~ core
85 -rm -f allmoc.cpp
86
87####### Extension Modules
88
89listpromodules:
90 @echo
91
92listallmodules:
93 @echo
94
95listaddonpromodules:
96 @echo
97
98listaddonentmodules:
99 @echo
100
101
102REQUIRES=
103
104####### Sub-libraries
105
106
107###### Combined headers
108
109
110
111####### Compile
112
113main.o: main.cpp \
114 qtetrix.h \
115 qtetrixb.h \
116 gtetrix.h \
117 tpiece.h \
118 $(QPEDIR)/include/qpe/qpeapplication.h
119
120gtetrix.o: gtetrix.cpp \
121 gtetrix.h \
122 tpiece.h
123
124qtetrix.o: qtetrix.cpp \
125 qtetrix.h \
126 qtetrixb.h \
127 gtetrix.h \
128 tpiece.h \
129 $(QPEDIR)/include/qpe/resource.h
130
131qtetrixb.o: qtetrixb.cpp \
132 qtetrixb.h \
133 gtetrix.h \
134 tpiece.h \
135 qtetrix.h
136
137tpiece.o: tpiece.cpp \
138 tpiece.h
139
140moc_qtetrix.o: moc_qtetrix.cpp \
141 qtetrix.h \
142 qtetrixb.h \
143 gtetrix.h \
144 tpiece.h
145
146moc_qtetrixb.o: moc_qtetrixb.cpp \
147 qtetrixb.h \
148 gtetrix.h \
149 tpiece.h
150
151moc_qtetrix.cpp: qtetrix.h
152 $(MOC) qtetrix.h -o moc_qtetrix.cpp
153
154moc_qtetrixb.cpp: qtetrixb.h
155 $(MOC) qtetrixb.h -o moc_qtetrixb.cpp
156
157
diff --git a/noncore/games/tetrix/gtetrix.cpp b/noncore/games/tetrix/gtetrix.cpp
new file mode 100644
index 0000000..d1f38b1
--- a/dev/null
+++ b/noncore/games/tetrix/gtetrix.cpp
@@ -0,0 +1,514 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21
22#include "gtetrix.h"
23
24#include <string.h>
25
26GenericTetrix::GenericTetrix(int boardWidth,int boardHeight)
27{
28 int i,j;
29
30 width = boardWidth;
31 height = boardHeight;
32 boardPtr = new int[height*width]; // Note the order, this makes it easier
33 // to remove full lines.
34 for(i = 0 ; i < height ; i++)
35 for(j = 0 ; j < width ; j++)
36 board(j,i) = 0;
37 currentLine = -1; // -1 if no falling piece.
38 currentPos = 0;
39 showNext = 0; // FALSE
40 nLinesRemoved = 0;
41 nPiecesDropped = 0;
42 score = 0;
43 level = 1;
44 gameID = 0;
45 nClearLines = height;
46}
47
48GenericTetrix::~GenericTetrix()
49{
50 delete[] boardPtr;
51}
52
53
54void GenericTetrix::clearBoard(int fillRandomLines)
55{
56 int i,j;
57
58 if (fillRandomLines >= height)
59 fillRandomLines = height - 1;
60
61 erasePiece();
62 for(i = height - nClearLines - 1 ; i >= fillRandomLines ; i--)
63 for(j = 0 ; j < width ; j++)
64 if (board(j,i) != 0) {
65 draw(j,i,0);
66 board(j,i) = 0;
67 }
68 if (fillRandomLines != 0)
69 for (i = 0 ; i < fillRandomLines ; i++) {
70 fillRandom(i);
71 }
72 nClearLines = height - fillRandomLines;
73}
74
75void GenericTetrix::showBoard()
76{
77 int i,j;
78
79 showPiece();
80 for(i = height - nClearLines - 1 ; i >= 0 ; i--)
81 for(j = 0 ; j < width ; j++)
82 if (board(j,i) != 0)
83 draw(j,i,board(j,i));
84}
85
86void GenericTetrix::hideBoard()
87{
88 int i,j;
89
90 erasePiece();
91 for(i = height - nClearLines - 1 ; i >= 0 ; i--)
92 for(j = 0 ; j < width ; j++)
93 if (board(j,i) != 0)
94 draw(j,i,0);
95}
96
97void GenericTetrix::startGame(int gameType,int fillRandomLines)
98{
99 gameID = gameType;
100 clearBoard(fillRandomLines);
101 nLinesRemoved = 0;
102 updateRemoved(nLinesRemoved);
103 nClearLines = height;
104 nPiecesDropped = 0;
105 score = 0;
106 updateScore(score);
107 level = 1;
108 updateLevel(level);
109 newPiece();
110}
111
112void GenericTetrix::revealNextPiece(int revealIt)
113{
114 if (showNext == revealIt)
115 return;
116 showNext = revealIt;
117 if (!showNext)
118 eraseNextPiece();
119 else
120 showNextPiece();
121}
122
123void GenericTetrix::updateBoard(int x1,int y1,int x2, int y2,
124 int dontUpdateBlanks)
125{
126 int i,j;
127 int tmp;
128
129 if (x1 > x2) {
130 tmp = x2;
131 x2 = x1;
132 x1 = tmp;
133 }
134 if (y1 > y2) {
135 tmp = y2;
136 y2 = y1;
137 y1 = tmp;
138 }
139 if (x1 < 0)
140 x1 = 0;
141 if (x2 >= width)
142 x2 = width - 1;
143 if (y1 < 0)
144 y1 = 0;
145 if (y2 >= height)
146 y2 = height - 1;
147 for(i = y1 ; i <= y2 ; i++)
148 for(j = x1 ; j <= x2 ; j++)
149 if (!dontUpdateBlanks || board(j,height - i - 1) != 0)
150 draw(j,height - i - 1,board(j,height - i - 1));
151 showPiece(); // Remember to update piece correctly!!!!
152}
153
154
155void GenericTetrix::fillRandom(int line)
156{
157 int i,j;
158 int holes;
159
160 for(i = 0 ; i < width ; i++)
161 board(i,line) = TetrixPiece::randomValue(7);
162 holes = 0;
163 for(i = 0 ; i < width ; i++)
164 if (board(i,line) == 0) // Count holes in the line.
165 holes++;
166 if (holes == 0) // Full line, make a random hole:
167 board(TetrixPiece::randomValue(width),line) = 0;
168 if (holes == width) // Empty line, make a random square:
169 board(TetrixPiece::randomValue(width),line) =
170 TetrixPiece::randomValue(6) + 1;
171 for(j = 0 ; j < width ; j++)
172 draw(j,i,board(j,i));
173}
174
175void GenericTetrix::moveLeft(int steps)
176{
177 while(steps) {
178 if (!canMoveTo(currentPos - 1,currentLine))
179 return;
180 moveTo(currentPos - 1,currentLine);
181 steps--;
182 }
183}
184
185void GenericTetrix::moveRight(int steps)
186{
187 while(steps) {
188 if (!canMoveTo(currentPos + 1,currentLine))
189 return;
190 moveTo(currentPos + 1,currentLine);
191 steps--;
192 }
193}
194
195void GenericTetrix::rotateLeft()
196{
197 TetrixPiece tmp(currentPiece);
198
199 tmp.rotateLeft();
200 if (!canPosition(tmp))
201 return;
202 position(tmp);
203 currentPiece = tmp;
204}
205
206void GenericTetrix::rotateRight()
207{
208 TetrixPiece tmp(currentPiece);
209
210 tmp.rotateRight();
211 if (!canPosition(tmp))
212 return;
213 position(tmp);
214 currentPiece = tmp;
215}
216
217void GenericTetrix::dropDown()
218{
219 if (currentLine == -1)
220 return;
221
222 int dropHeight = 0;
223 int newLine = currentLine;
224 while(newLine) {
225 if (!canMoveTo(currentPos,newLine - 1))
226 break;
227 newLine--;
228 dropHeight++;
229 }
230 if (dropHeight != 0)
231 moveTo(currentPos,newLine);
232 internalPieceDropped(dropHeight);
233}
234
235void GenericTetrix::oneLineDown()
236{
237 if (currentLine == -1)
238 return;
239 if (canMoveTo(currentPos,currentLine - 1)) {
240 moveTo(currentPos,currentLine - 1);
241 } else {
242 internalPieceDropped(0);
243 }
244}
245
246void GenericTetrix::newPiece()
247{
248 currentPiece = nextPiece;
249 if (showNext)
250 eraseNextPiece();
251 nextPiece.setRandomType();
252 if (showNext)
253 showNextPiece();
254 currentLine = height - 1 + currentPiece.getMinY();
255 currentPos = width/2 + 1;
256 if (!canMoveTo(currentPos,currentLine)) {
257 currentLine = -1;
258 gameOver();
259 } else {
260 showPiece();
261 }
262}
263
264void GenericTetrix::removePiece()
265{
266 erasePiece();
267 currentLine = -1;
268}
269
270void GenericTetrix::drawNextSquare(int,int,int)
271{
272
273}
274
275void GenericTetrix::pieceDropped(int)
276{
277 newPiece();
278}
279
280void GenericTetrix::updateRemoved(int)
281{
282}
283
284void GenericTetrix::updateScore(int)
285{
286}
287
288void GenericTetrix::updateLevel(int)
289{
290}
291
292void GenericTetrix::removeFullLines()
293{
294 int i,j,k;
295 int nFullLines;
296
297 for(i = 0 ; i < height - nClearLines ; i++) {
298 for(j = 0 ; j < width ; j++)
299 if (board(j,i) == 0)
300 break;
301 if (j == width) {
302 nFullLines = 1;
303 for(k = i + 1 ; k < height - nClearLines ; k++) {
304 for(j = 0 ; j < width ; j++)
305 if (board(j,k) == 0)
306 break;
307 if (j == width) {
308 nFullLines++;
309 } else {
310 for(j = 0 ; j < width ; j++) {
311 if (board(j,k - nFullLines) != board(j,k)) {
312 board(j,k - nFullLines) = board(j,k);
313 draw( j,k - nFullLines,
314 board(j,k - nFullLines));
315 }
316 }
317 }
318 }
319 nClearLines = nClearLines + nFullLines;
320 nLinesRemoved = nLinesRemoved + nFullLines;
321 updateRemoved(nLinesRemoved);
322 score = score + 10*nFullLines; // updateScore must be
323 // called by caller!
324 for (i = height - nClearLines ;
325 i < height - nClearLines + nFullLines ;
326 i++)
327 for(j = 0 ; j < width ; j++)
328 if (board(j,i) != 0) {
329 draw(j,i,0);
330 board(j,i) = 0;
331 }
332 }
333 }
334}
335
336void GenericTetrix::showPiece()
337{
338 int x,y;
339
340 if (currentLine == -1)
341 return;
342
343 for(int i = 0 ; i < 4 ; i++) {
344 currentPiece.getCoord(i,x,y);
345 draw(currentPos + x,currentLine - y,currentPiece.getType());
346 }
347}
348
349void GenericTetrix::erasePiece()
350{
351 int x,y;
352
353 if (currentLine == -1)
354 return;
355
356 for(int i = 0 ; i < 4 ; i++) {
357 currentPiece.getCoord(i,x,y);
358 draw(currentPos + x,currentLine - y,0);
359 }
360}
361
362void GenericTetrix::internalPieceDropped(int dropHeight)
363{
364 gluePiece();
365 nPiecesDropped++;
366 if (nPiecesDropped % 25 == 0) {
367 level++;
368 updateLevel(level);
369 }
370 score = score + 7 + dropHeight;
371 removeFullLines();
372 updateScore(score);
373 pieceDropped(dropHeight);
374}
375
376void GenericTetrix::gluePiece()
377{
378 int x,y;
379 int min;
380
381 if (currentLine == -1)
382 return;
383
384 for(int i = 0 ; i < 4 ; i++) {
385 currentPiece.getCoord(i,x,y);
386 board(currentPos + x,currentLine - y) = currentPiece.getType();
387 }
388 min = currentPiece.getMinY();
389 if (currentLine - min >= height - nClearLines)
390 nClearLines = height - currentLine + min - 1;
391}
392
393void GenericTetrix::showNextPiece(int erase)
394{
395 int x,y;
396 int minX = nextPiece.getMinX();
397 int minY = nextPiece.getMinY();
398 int maxX = nextPiece.getMaxX();
399 int maxY = nextPiece.getMaxY();
400
401 int xOffset = (3 - (maxX - minX))/2;
402 int yOffset = (3 - (maxY - minY))/2;
403
404 for(int i = 0 ; i < 4 ; i++) {
405 nextPiece.getCoord(i,x,y);
406 if (erase)
407 drawNextSquare(x + xOffset - minX,
408 y + yOffset - minY,0);
409 else
410 drawNextSquare(x + xOffset - minX,
411 y + yOffset - minY,nextPiece.getType());
412 }
413}
414
415int GenericTetrix::canPosition(TetrixPiece &piece)
416{
417 if (currentLine == -1)
418 return 0;
419
420 int x,y;
421
422 for(int i = 0 ; i < 4 ; i++) {
423 piece.getCoord(i,x,y);
424 x = currentPos + x;
425 y = currentLine - y; // Board and pieces have inverted y-coord. systems.
426 if (x < 0 || x >= width || y < 0 || y >= height)
427 return 0; // Outside board, cannot put piece here.
428 if (board(x,y) != 0)
429 return 0; // Over a non-zero square, cannot put piece here.
430 }
431 return 1; // Inside board and no non-zero squares underneath.
432
433}
434
435int GenericTetrix::canMoveTo(int xPosition,int line)
436{
437 if (currentLine == -1)
438 return 0;
439
440 int x,y;
441
442 for(int i = 0 ; i < 4 ; i++) {
443 currentPiece.getCoord(i,x,y);
444 x = xPosition + x;
445 y = line - y; // Board and pieces have inverted y-coord. systems.
446 if (x < 0 || x >= width || y < 0 || y >= height)
447 return 0; // Outside board, cannot put piece here.
448 if (board(x,y) != 0)
449 return 0; // Over a non-zero square, cannot put piece here.
450 }
451 return 1; // Inside board and no non-zero squares underneath.
452}
453
454void GenericTetrix::moveTo(int xPosition,int line)
455{
456 if (currentLine == -1)
457 return;
458 optimizedMove(xPosition,line,currentPiece);
459 currentPos = xPosition;
460 currentLine = line;
461}
462
463void GenericTetrix::position(TetrixPiece &piece)
464{
465 if (currentLine == -1)
466 return;
467
468 optimizedMove(currentPos,currentLine,piece);
469}
470
471void GenericTetrix::optimizedMove(int newPos, int newLine,
472 TetrixPiece &newPiece)
473{
474 int updates [8][3];
475 int nUpdates;
476 int value;
477 int x,y;
478 int i,j;
479
480 for(i = 0 ; i < 4 ; i++) { // Put the erasing coords into updates
481 currentPiece.getCoord(i,x,y);
482 updates[i][0] = currentPos + x;
483 updates[i][1] = currentLine - y;
484 updates[i][2] = 0;
485 }
486 nUpdates = 4;
487 for(i = 0 ; i < 4 ; i++) { // Any drawing coord same as an erasing one?
488 newPiece.getCoord(i,x,y);
489 x = newPos + x;
490 y = newLine - y;
491 for (j = 0 ; j < 4 ; j++)
492 if (updates[j][0] == x && updates[j][1] == y) { // Same coord,
493 // don't have to erase
494 if (currentPiece.getType() == newPiece.getType())
495 updates[j][2] = -1; // Correct on screen, no update!
496 else
497 updates[j][2] = newPiece.getType();
498 break;
499 }
500 if (j == 4) { // This coord does not overlap an erasing one
501 updates[nUpdates][0] = x;
502 updates[nUpdates][1] = y;
503 updates[nUpdates][2] = newPiece.getType();
504 nUpdates++;
505 }
506 }
507 for (i = 0 ; i < nUpdates ; i++) { // Do the updating
508 x = updates[i][0];
509 y = updates[i][1];
510 value = updates[i][2];
511 if (value != -1) // Only update if new value != current
512 draw(x,y,value);
513 }
514}
diff --git a/noncore/games/tetrix/gtetrix.h b/noncore/games/tetrix/gtetrix.h
new file mode 100644
index 0000000..520dd89
--- a/dev/null
+++ b/noncore/games/tetrix/gtetrix.h
@@ -0,0 +1,104 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21
22#ifndef GTETRIX_H
23#define GTETRIX_H
24
25#include "tpiece.h"
26
27
28class GenericTetrix
29{
30public:
31 GenericTetrix(int boardWidth = 10,int boardHeight = 22);
32 virtual ~GenericTetrix();
33
34 void clearBoard(int fillRandomLines = 0);
35 void revealNextPiece(int revealIt);
36 void updateBoard(int x1,int y1,int x2,int y2,int dontUpdateBlanks = 0);
37 void updateNext(){if (showNext) showNextPiece();}
38 void hideBoard();
39 void showBoard();
40 void fillRandom(int line);
41
42 void moveLeft(int steps = 1);
43 void moveRight(int steps = 1);
44 void rotateLeft();
45 void rotateRight();
46 void dropDown();
47 void oneLineDown();
48 void newPiece();
49 void removePiece();
50
51 int noOfClearLines() {return nClearLines;}
52 int getLinesRemoved() {return nLinesRemoved;}
53 int getPiecesDropped() {return nPiecesDropped;}
54 int getScore() {return score;}
55 int getLevel() {return level;}
56 int boardHeight() {return height;}
57 int boardWidth() {return width;}
58
59 virtual void drawSquare(int x,int y,int value) = 0;
60 virtual void gameOver() = 0;
61
62 virtual void startGame(int gameType = 0,int fillRandomLines = 0);
63 virtual void drawNextSquare(int x,int y,int value);
64 virtual void pieceDropped(int dropHeight);
65 virtual void updateRemoved(int noOfLines);
66 virtual void updateScore(int newScore);
67 virtual void updateLevel(int newLevel);
68
69private:
70 void draw(int x, int y, int value){drawSquare(x,height - y,value);}
71 void removeFullLines();
72 void removeLine(int line);
73 void showPiece();
74 void erasePiece();
75 void internalPieceDropped(int dropHeight);
76 void gluePiece();
77 void showNextPiece(int erase = 0);
78 void eraseNextPiece(){showNextPiece(1);};
79 int canPosition(TetrixPiece &piece); // Returns a boolean value.
80 int canMoveTo(int xPosition, int line); // Returns a boolean value.
81 void moveTo(int xPosition,int line);
82 void position(TetrixPiece &piece);
83 void optimizedMove(int newPos, int newLine,TetrixPiece &newPiece);
84
85 int &board(int x,int y){return boardPtr[width*y + x];}
86
87 TetrixPiece currentPiece;
88 TetrixPiece nextPiece;
89 int currentLine;
90 int currentPos;
91 int showNext; // Boolean variable.
92 int nLinesRemoved;
93 int nPiecesDropped;
94 int score;
95 int level;
96 int gameID;
97 int nClearLines;
98 int width;
99 int height;
100 int *boardPtr;
101};
102
103
104#endif
diff --git a/noncore/games/tetrix/main.cpp b/noncore/games/tetrix/main.cpp
new file mode 100644
index 0000000..e36d52d
--- a/dev/null
+++ b/noncore/games/tetrix/main.cpp
@@ -0,0 +1,33 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21#include "qtetrix.h"
22
23#include <qpe/qpeapplication.h>
24
25int main( int argc, char **argv )
26{
27 QPEApplication a(argc,argv);
28
29 QTetrix *tetrix = new QTetrix;
30 a.showMainWidget(tetrix);
31
32 return a.exec();
33}
diff --git a/noncore/games/tetrix/qpe-tetrix.control b/noncore/games/tetrix/qpe-tetrix.control
new file mode 100644
index 0000000..46dfdf5
--- a/dev/null
+++ b/noncore/games/tetrix/qpe-tetrix.control
@@ -0,0 +1,10 @@
1Files: bin/tetrix apps/Games/tetrix.desktop
2Priority: optional
3Section: qpe/games
4Maintainer: Warwick Allison <warwick@trolltech.com>
5Architecture: arm
6Arch: iPAQ
7Version: $QPE_VERSION-3
8Depends: qpe-base ($QPE_VERSION)
9Description: Game: control falling blocks
10 A game for the Qtopia environment.
diff --git a/noncore/games/tetrix/qtetrix.cpp b/noncore/games/tetrix/qtetrix.cpp
new file mode 100644
index 0000000..f649894
--- a/dev/null
+++ b/noncore/games/tetrix/qtetrix.cpp
@@ -0,0 +1,170 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21
22#include "qtetrix.h"
23
24#include <qpe/resource.h>
25
26#include <qapplication.h>
27#include <qlabel.h>
28#include <qdatetime.h>
29#include <qlayout.h>
30
31
32
33void drawTetrixButton( QPainter *p, int x, int y, int w, int h,
34 const QColor *color )
35{
36 QColor fc;
37 if ( color ) {
38 QPointArray a;
39 a.setPoints( 3, x,y+h-1, x,y, x+w-1,y );
40 p->setPen( color->light() );
41 p->drawPolyline( a );
42 a.setPoints( 3, x+1,y+h-1, x+w-1,y+h-1, x+w-1,y+1 );
43 p->setPen( color->dark() );
44 p->drawPolyline( a );
45 x++;
46 y++;
47 w -= 2;
48 h -= 2;
49 fc = *color;
50 }
51 else
52 fc = p->backgroundColor();
53 p->fillRect( x, y, w, h, fc );
54}
55
56
57ShowNextPiece::ShowNextPiece( QWidget *parent, const char *name )
58 : QFrame( parent, name )
59{
60 setFrameStyle( QFrame::Panel | QFrame::Sunken );
61 xOffset = -1; // -1 until first resizeEvent.
62}
63
64void ShowNextPiece::resizeEvent( QResizeEvent *e )
65{
66 QSize sz = e->size();
67 blockWidth = (sz.width() - 3)/5;
68 blockHeight = (sz.height() - 3)/6;
69 xOffset = (sz.width() - 3)/5;
70 yOffset = (sz.height() - 3)/6;
71}
72
73
74void ShowNextPiece::paintEvent( QPaintEvent * )
75{
76 QPainter p( this );
77 drawFrame( &p );
78 p.end(); // explicit end() so any slots can paint too
79 emit update();
80}
81
82
83void ShowNextPiece::drawNextSquare(int x, int y,QColor *color)
84{
85 if (xOffset == -1) // Before first resizeEvent?
86 return;
87
88 QPainter paint;
89 paint.begin(this);
90 drawTetrixButton( &paint, xOffset+x*blockWidth, yOffset+y*blockHeight,
91 blockWidth, blockHeight, color );
92 paint.end();
93}
94
95
96QTetrix::QTetrix( QWidget *parent, const char *name, WFlags f )
97 : QMainWindow( parent, name, f )
98{
99 setIcon( Resource::loadPixmap( "tetrix_icon" ) );
100 setCaption( tr("Tetrix" ) );
101
102 QTime t = QTime::currentTime();
103 TetrixPiece::setRandomSeed( (((double)t.hour())+t.minute()+t.second())/
104 (24+60+60) );
105
106 QWidget *gameArea = new QWidget( this );
107 setCentralWidget( gameArea );
108
109 QGridLayout *gl = new QGridLayout( gameArea, 5, 3, 8 );
110
111 QLabel *l;
112 l = new QLabel( tr("Next"), gameArea );
113 gl->addWidget( l, 0, 0 );
114 showNext = new ShowNextPiece(gameArea);
115 showNext->setBackgroundColor(QColor(0,0,0));
116 gl->addWidget( showNext, 0, 1 );
117
118 l = new QLabel( tr("Score"), gameArea );
119 gl->addWidget( l, 1, 0 );
120 showScore = new QLabel(gameArea);
121 gl->addWidget( showScore, 1, 1 );
122 l = new QLabel( tr("Level"), gameArea );
123 gl->addWidget( l, 2, 0 );
124 showLevel = new QLabel(gameArea);
125 gl->addWidget( showLevel, 2, 1 );
126 l = new QLabel( tr("Removed"), gameArea );
127 gl->addWidget( l, 3, 0 );
128 showLines = new QLabel(gameArea);
129 gl->addWidget( showLines, 3, 1 );
130
131 board = new QTetrixBoard(gameArea);
132 board->setBackgroundColor(QColor(0,0,0));
133 board->setFixedWidth( 124 );
134 gl->addMultiCellWidget( board, 0, 4, 2, 2 );
135 gl->addColSpacing( 2, 100 );
136 gl->addColSpacing( 1, 35 );
137 gl->addRowSpacing( 0, 35 );
138
139 QPushButton *pb = new QPushButton( tr("Start"), gameArea );
140 pb->setFocusPolicy( NoFocus );
141 connect( pb, SIGNAL( clicked() ), board, SLOT( start() ) );
142 gl->addMultiCellWidget( pb, 4, 4, 0, 1 );
143
144 connect( board, SIGNAL(gameOverSignal()), SLOT(gameOver()) );
145 connect( board, SIGNAL(drawNextSquareSignal(int,int,QColor*)), showNext,
146 SLOT(drawNextSquare(int,int,QColor*)) );
147 connect( showNext, SIGNAL(update()), board, SLOT(updateNext()) );
148 connect( board, SIGNAL(updateScoreSignal(int)), showScore,
149 SLOT(setNum(int)) );
150 connect( board, SIGNAL(updateLevelSignal(int)), showLevel,
151 SLOT(setNum(int)));
152 connect( board, SIGNAL(updateRemovedSignal(int)), showLines,
153 SLOT(setNum(int)));
154
155 showScore->setNum( 0 );
156 showLevel->setNum( 0 );
157 showLines->setNum( 0 );
158 board->revealNextPiece(TRUE);
159 board->setFocusPolicy( StrongFocus );
160}
161
162void QTetrix::gameOver()
163{
164}
165
166
167void QTetrix::quit()
168{
169 close();
170}
diff --git a/noncore/games/tetrix/qtetrix.h b/noncore/games/tetrix/qtetrix.h
new file mode 100644
index 0000000..b6e058a
--- a/dev/null
+++ b/noncore/games/tetrix/qtetrix.h
@@ -0,0 +1,78 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21
22#ifndef QTETRIX_H
23#define QTETRIX_H
24
25#include "qtetrixb.h"
26#include <qframe.h>
27#include <qlcdnumber.h>
28#include <qpushbutton.h>
29#include <qpainter.h>
30#include <qmainwindow.h>
31
32class QLabel;
33
34class ShowNextPiece : public QFrame
35{
36 Q_OBJECT
37 friend class QTetrix;
38public:
39 ShowNextPiece( QWidget *parent=0, const char *name=0 );
40public slots:
41 void drawNextSquare( int x, int y,QColor *color );
42signals:
43 void update();
44private:
45 void paintEvent( QPaintEvent * );
46 void resizeEvent( QResizeEvent * );
47
48 int blockWidth,blockHeight;
49 int xOffset,yOffset;
50};
51
52
53class QTetrix : public QMainWindow
54{
55 Q_OBJECT
56public:
57 QTetrix( QWidget *parent=0, const char *name=0, WFlags f=0 );
58 void startGame() { board->startGame(); }
59
60public slots:
61 void gameOver();
62 void quit();
63private:
64 void keyPressEvent( QKeyEvent *e ) { board->keyPressEvent(e); }
65
66 QTetrixBoard *board;
67 ShowNextPiece *showNext;
68 QLabel *showScore;
69 QLabel *showLevel;
70 QLabel *showLines;
71};
72
73
74void drawTetrixButton( QPainter *, int x, int y, int w, int h,
75 const QColor *color );
76
77
78#endif
diff --git a/noncore/games/tetrix/qtetrixb.cpp b/noncore/games/tetrix/qtetrixb.cpp
new file mode 100644
index 0000000..521f171
--- a/dev/null
+++ b/noncore/games/tetrix/qtetrixb.cpp
@@ -0,0 +1,251 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21
22#include "qtetrixb.h"
23#include "qtetrix.h"
24#include <qtimer.h>
25#include <qkeycode.h>
26#include <qpainter.h>
27
28const int waitAfterLineTime = 500;
29
30QTetrixBoard::QTetrixBoard( QWidget *p, const char *name )
31 : QFrame( p, name )
32{
33 setFrameStyle( QFrame::Panel | QFrame::Sunken );
34 paint = 0;
35 timer = new QTimer(this);
36 connect( timer, SIGNAL(timeout()), SLOT(timeout()) );
37
38 colors[0].setRgb(200,100,100);
39 colors[1].setRgb(100,200,100);
40 colors[2].setRgb(100,100,200);
41 colors[3].setRgb(200,200,100);
42 colors[4].setRgb(200,100,200);
43 colors[5].setRgb(100,200,200);
44 colors[6].setRgb(218,170, 0);
45
46 xOffset = -1; // -1 until a resizeEvent is received.
47 blockWidth = 20;
48 yOffset = 30;
49 blockHeight = 20;
50 noGame = TRUE;
51 isPaused = FALSE;
52 waitingAfterLine = FALSE;
53 updateTimeoutTime(); // Sets timeoutTime
54}
55
56void QTetrixBoard::startGame(int gameType,int fillRandomLines)
57{
58 if ( isPaused )
59 return; // ignore if game is paused
60 noGame = FALSE;
61 GenericTetrix::startGame( gameType, fillRandomLines );
62 // Note that the timer is started by updateLevel!
63}
64
65
66void QTetrixBoard::pause()
67{
68 if ( noGame ) // game not active
69 return;
70 isPaused = !isPaused;
71 if ( isPaused ) {
72 timer->stop();
73 hideBoard();
74 }
75 else
76 timer->start(timeoutTime);
77 update();
78}
79
80
81void QTetrixBoard::drawSquare(int x,int y,int value)
82{
83 if (xOffset == -1) // Before first resizeEvent?
84 return;
85
86 const int X = xOffset + x*blockWidth;
87 const int Y = yOffset + (y - 1)*blockHeight;
88
89 bool localPainter = paint == 0;
90 QPainter *p;
91 if ( localPainter )
92 p = new QPainter( this );
93 else
94 p = paint;
95 drawTetrixButton( p, X, Y, blockWidth, blockHeight,
96 value == 0 ? 0 : &colors[value-1] );
97 /*
98 if ( value != 0 ) {
99 QColor tc, bc;
100 tc = colors[value-1].light();
101 bc = colors[value-1].dark();
102 p->drawShadePanel( X, Y, blockWidth, blockHeight,
103 tc, bc, 1, colors[value-1], TRUE );
104 }
105 else
106 p->fillRect( X, Y, blockWidth, blockHeight, backgroundColor() );
107 */
108 if ( localPainter )
109 delete p;
110}
111
112void QTetrixBoard::drawNextSquare( int x, int y, int value )
113{
114 if ( value == 0 )
115 emit drawNextSquareSignal (x, y, 0 );
116 else
117 emit drawNextSquareSignal( x, y, &colors[value-1] );
118}
119
120void QTetrixBoard::updateRemoved( int noOfLines )
121{
122 if ( noOfLines > 0 ) {
123 timer->stop();
124 timer->start( waitAfterLineTime );
125 waitingAfterLine = TRUE;
126 }
127 emit updateRemovedSignal( noOfLines );
128}
129
130void QTetrixBoard::updateScore( int newScore )
131{
132 emit updateScoreSignal( newScore );
133}
134
135void QTetrixBoard::updateLevel( int newLevel )
136{
137 timer->stop();
138 updateTimeoutTime();
139 timer->start( timeoutTime );
140 emit updateLevelSignal( newLevel );
141}
142
143void QTetrixBoard::pieceDropped(int)
144{
145 if ( waitingAfterLine ) // give player a break if a line has been removed
146 return;
147 newPiece();
148}
149
150void QTetrixBoard::gameOver()
151{
152 timer->stop();
153 noGame = TRUE;
154 emit gameOverSignal();
155}
156
157void QTetrixBoard::timeout()
158{
159 if ( waitingAfterLine ) {
160 timer->stop();
161 waitingAfterLine = FALSE;
162 newPiece();
163 timer->start( timeoutTime );
164 } else {
165 oneLineDown();
166 }
167}
168
169void QTetrixBoard::drawContents( QPainter *p )
170{
171 const char *text = "Press \"Pause\"";
172 QRect r = contentsRect();
173 paint = p; // set widget painter
174 if ( isPaused ) {
175 p->drawText( r, AlignCenter | AlignVCenter, text );
176 return;
177 }
178 int x1,y1,x2,y2;
179 x1 = (r.left() - xOffset) / blockWidth;
180 if (x1 < 0)
181 x1 = 0;
182 if (x1 >= boardWidth())
183 x1 = boardWidth() - 1;
184
185 x2 = (r.right() - xOffset) / blockWidth;
186 if (x2 < 0)
187 x2 = 0;
188 if (x2 >= boardWidth())
189 x2 = boardWidth() - 1;
190
191 y1 = (r.top() - yOffset) / blockHeight;
192 if (y1 < 0)
193 y1 = 0;
194 if (y1 >= boardHeight())
195 y1 = boardHeight() - 1;
196
197 y2 = (r.bottom() - yOffset) / blockHeight;
198 if (y2 < 0)
199 y2 = 0;
200 if (y2 >= boardHeight())
201 y2 = boardHeight() - 1;
202
203 updateBoard( x1, y1, x2, y2, TRUE );
204 paint = 0; // reset widget painter
205 return;
206}
207
208void QTetrixBoard::resizeEvent(QResizeEvent *e)
209{
210 QSize sz = e->size();
211 blockWidth = (sz.width() - 2)/10;
212 blockHeight = (sz.height() - 2)/22;
213 xOffset = 1;
214 //yOffset = 1;
215 yOffset = (sz.height() - 2) - (blockHeight *22);
216}
217
218void QTetrixBoard::keyPressEvent( QKeyEvent *e )
219{
220 if ( noGame || isPaused || waitingAfterLine )
221 return;
222 switch( e->key() ) {
223 case Key_Left :
224 moveLeft();
225 break;
226 case Key_Right :
227 moveRight();
228 break;
229 case Key_Down :
230 // rotateRight();
231 dropDown();
232 break;
233 case Key_Up :
234 rotateLeft();
235 break;
236 case Key_Space :
237 dropDown();
238 break;
239 case Key_D :
240 oneLineDown();
241 break;
242 default:
243 return;
244 }
245 e->accept();
246}
247
248void QTetrixBoard::updateTimeoutTime()
249{
250 timeoutTime = 1000/(1 + getLevel());
251}
diff --git a/noncore/games/tetrix/qtetrixb.h b/noncore/games/tetrix/qtetrixb.h
new file mode 100644
index 0000000..4134a84
--- a/dev/null
+++ b/noncore/games/tetrix/qtetrixb.h
@@ -0,0 +1,80 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21
22#ifndef QTETRIXB_H
23#define QTETRIXB_H
24
25#include "gtetrix.h"
26#include <qframe.h>
27
28class QTimer;
29
30class QTetrixBoard : public QFrame, public GenericTetrix
31{
32 Q_OBJECT
33public:
34 QTetrixBoard( QWidget *parent=0, const char *name=0 );
35
36 void gameOver();
37 void startGame(int gameType = 0,int fillRandomLines = 0);
38
39public slots:
40 void timeout();
41 void updateNext(){ GenericTetrix::updateNext(); }
42 void key(QKeyEvent *e) { keyPressEvent(e); }
43 void start() { startGame(); }
44 void pause();
45
46signals:
47 void gameOverSignal();
48 void drawNextSquareSignal(int x,int y,QColor *color1);
49 void updateRemovedSignal(int noOfLines);
50 void updateScoreSignal(int score);
51 void updateLevelSignal(int level);
52
53public: // until we have keyboard focus, should be protected
54 void keyPressEvent( QKeyEvent * );
55
56private:
57 void drawContents( QPainter * );
58 void resizeEvent( QResizeEvent * );
59 void drawSquare(int x,int y,int value);
60 void drawNextSquare(int x,int y,int value);
61 void updateRemoved(int noOfLines);
62 void updateScore(int newScore);
63 void updateLevel(int newLlevel);
64 void pieceDropped(int dropHeight);
65 void updateTimeoutTime();
66
67 QTimer *timer;
68
69 int xOffset,yOffset;
70 int blockWidth,blockHeight;
71 int timeoutTime;
72 bool noGame;
73 bool isPaused;
74 bool waitingAfterLine;
75
76 QColor colors[7];
77 QPainter *paint;
78};
79
80#endif
diff --git a/noncore/games/tetrix/tetrix.pro b/noncore/games/tetrix/tetrix.pro
new file mode 100644
index 0000000..1160585
--- a/dev/null
+++ b/noncore/games/tetrix/tetrix.pro
@@ -0,0 +1,17 @@
1 TEMPLATE= app
2 CONFIG = qt warn_on release
3 DESTDIR = ../bin
4 HEADERS = minefield.h \
5 gtetrix.h \
6 qtetrix.h \
7 qtetrixb.h \
8 tpiece.h
9 SOURCES = main.cpp \
10 gtetrix.cpp \
11 qtetrix.cpp \
12 qtetrixb.cpp \
13 tpiece.cpp
14INCLUDEPATH += $(QPEDIR)/include
15 DEPENDPATH+= $(QPEDIR)/include
16LIBS += -lqpe
17 INTERFACES=
diff --git a/noncore/games/tetrix/tpiece.cpp b/noncore/games/tetrix/tpiece.cpp
new file mode 100644
index 0000000..fe8b766
--- a/dev/null
+++ b/noncore/games/tetrix/tpiece.cpp
@@ -0,0 +1,201 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21
22#include "tpiece.h"
23#include "qstring.h"
24#include <stdlib.h>
25#include <time.h>
26
27void TetrixPiece::rotateLeft()
28{
29 if ( pieceType == 5 ) // don't rotate square piece type
30 return;
31 int tmp;
32 for (int i = 0 ; i < 4 ; i++) {
33 tmp = getXCoord(i);
34 setXCoord(i,getYCoord(i));
35 setYCoord(i,-tmp);
36 }
37}
38
39void TetrixPiece::rotateRight()
40{
41 if ( pieceType == 5 ) // don't rotate square piece type
42 return;
43 int tmp;
44 for (int i = 0 ; i < 4 ; i++) {
45 tmp = getXCoord(i);
46 setXCoord(i,-getYCoord(i));
47 setYCoord(i,tmp);
48 }
49}
50
51int TetrixPiece::getMinX()
52{
53 int tmp = coordinates[0][0];
54 for(int i = 1 ; i < 4 ; i++)
55 if (tmp > coordinates[i][0])
56 tmp = coordinates[i][0];
57 return tmp;
58}
59
60int TetrixPiece::getMaxX()
61{
62 int tmp = coordinates[0][0];
63 for(int i = 1 ; i < 4 ; i++)
64 if (tmp < coordinates[i][0])
65 tmp = coordinates[i][0];
66 return tmp;
67
68}
69
70int TetrixPiece::getMinY()
71{
72 int tmp = coordinates[0][1];
73 for(int i = 1 ; i < 4 ; i++)
74 if (tmp > coordinates[i][1])
75 tmp = coordinates[i][1];
76 return tmp;
77}
78
79int TetrixPiece::getMaxY()
80{
81 int tmp = coordinates[0][1];
82 for(int i = 1 ; i < 4 ; i++)
83 if (tmp < coordinates[i][1])
84 tmp = coordinates[i][1];
85 return tmp;
86}
87
88void TetrixPiece::initialize(int type)
89{
90 static int pieceTypes[7][4][2] = {{{ 0,-1},
91 { 0, 0},
92 {-1, 0},
93 {-1, 1}},
94
95 {{ 0,-1},
96 { 0, 0},
97 { 1, 0},
98 { 1, 1}},
99
100 {{ 0,-1},
101 { 0, 0},
102 { 0, 1},
103 { 0, 2}},
104
105 {{-1, 0},
106 { 0, 0},
107 { 1, 0},
108 { 0, 1}},
109
110 {{ 0, 0},
111 { 1, 0},
112 { 0, 1},
113 { 1, 1}},
114
115 {{-1,-1},
116 { 0,-1},
117 { 0, 0},
118 { 0, 1}},
119
120 {{ 1,-1},
121 { 0,-1},
122 { 0, 0},
123 { 0, 1}}};
124 if (type < 1 || type > 7)
125 type = 1;
126 pieceType = type;
127 for(int i = 0 ; i < 4 ; i++) {
128 coordinates[i][0] = pieceTypes[type - 1][i][0];
129 coordinates[i][1] = pieceTypes[type - 1][i][1];
130 }
131}
132
133
134/*
135 *Sigh, oh beautiful nostalgia! This random algorithm has
136 *been taken from the book "Adventures with your pocket calculator"
137 *and I used it in my first implemented and machine-
138 *run program of any size to speak of. Imagine how hungry I
139 *was after having programmed BASIC on paper for
140 *half a year?!!?!?!?!?!? The first program I typed in was a
141 *slot machine game and was made in BASIC on a SHARP
142 *PC-1211 with 1,47 KB RAM (one point four seven kilobytes) and
143 *a one-line LCD-display (I think it had 32 characters) in the
144 *year of our lord 1981. The man I had bought the machine from worked
145 *as a COBOL programmer and was amazed and impressed
146 *when I demonstrated the program 2 days after I had
147 *bought the machine, quote: "Gees, I have been looking so long
148 *for a "random" command in that BASIC, what is it called?"
149 *Oh, how I still get a thrill out of the thought of the
150 *explanation I then gave him...
151 */
152
153/*
154 *Sukk, aa vakre nostalgi! Denne random algoritmen er
155 *tatt fra boka "Adventures with your pocket calculator"
156 *og den brukte jeg i mitt foerste implementerte og maskin-
157 *kjoerte program av nevneverdig stoerrelse. Tror du jeg var
158 *noe sulten etter aa ha programmert BASIC paa papir i et
159 *halvt aar?!!?!?!?!?!? Programmet jeg tasta inn foerst var et
160 *"enarmet banditt" spill og ble laget i BASIC paa en SHARP
161 *PC-1211 med 1,47 KB RAM (en komma foertisju kilobyte) og
162 *et en-linjers LCD-display (tror det hadde 32 karakterer) i det
163 *herrens aar 1981. Mannen jeg kjoepte maskinen av jobbet til
164 *daglig med COBOL programmering og var forbloeffet og imponert
165 *da jeg demonstrerte programmet 2 dager etter at jeg hadde
166 *kjoept maskinen, sitat: "Joess, jeg som har leita saa lenge
167 *etter en random kommando i den BASICen, hva var det den
168 *het?" Aa, jeg frydes ennaa ved tanken paa forklaringen jeg
169 *deretter ga ham...
170 */
171
172double TetrixPiece::randomSeed = 0.33333;
173
174void TetrixPiece::setRandomSeed(double seed)
175{
176#ifdef __MIPSEL__
177 srand( clock() );
178#else
179 QCString buffer;
180 if (seed < 0)
181 seed = - seed;
182 if (seed >= 1)
183 seed = seed - (double) ((int) seed);
184 buffer.sprintf("%1.5f",(float) seed);
185 for (int i = 0 ; i < 5 ; i++)
186 if ((buffer[i + 2] - '0') % 2 == 0)
187 buffer[i + 2]++;
188 randomSeed = atof(buffer);
189#endif
190}
191
192int TetrixPiece::randomValue(int maxPlusOne)
193{
194#ifdef __MIPSEL__
195 return rand() % maxPlusOne;
196#else
197 randomSeed = randomSeed*147;
198 randomSeed = randomSeed - (double) ((int) randomSeed);
199 return (int) (randomSeed*maxPlusOne);
200#endif
201}
diff --git a/noncore/games/tetrix/tpiece.h b/noncore/games/tetrix/tpiece.h
new file mode 100644
index 0000000..9c1c629
--- a/dev/null
+++ b/noncore/games/tetrix/tpiece.h
@@ -0,0 +1,62 @@
1/**********************************************************************
2** Copyright (C) 2000 Trolltech AS. All rights reserved.
3**
4** This file is part of Qtopia Environment.
5**
6** This file may be distributed and/or modified under the terms of the
7** GNU General Public License version 2 as published by the Free Software
8** Foundation and appearing in the file LICENSE.GPL included in the
9** packaging of this file.
10**
11** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
12** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
13**
14** See http://www.trolltech.com/gpl/ for GPL licensing information.
15**
16** Contact info@trolltech.com if any conditions of this licensing are
17** not clear to you.
18**
19**********************************************************************/
20
21
22#ifndef TPIECE_H
23#define TPIECE_H
24
25class TetrixPiece
26{
27public:
28 TetrixPiece() {setRandomType();}
29 TetrixPiece(int type) {initialize(type % 7 + 1);}
30
31 void setRandomType() {initialize(randomValue(7) + 1);}
32
33 void rotateLeft();
34 void rotateRight();
35
36 int getType() {return pieceType;}
37 int getXCoord(int index) {return coordinates[index][0];}
38 int getYCoord(int index) {return coordinates[index][1];}
39 void getCoord(int index,int &x,int&y){x = coordinates[index][0];
40 y = coordinates[index][1];}
41 int getMinX();
42 int getMaxX();
43 int getMinY();
44 int getMaxY();
45
46 static void setRandomSeed(double seed);
47 static int randomValue(int maxPlusOne);
48
49private:
50 void setXCoord(int index,int value) {coordinates[index][0] = value;}
51 void setYCoord(int index,int value) {coordinates[index][1] = value;}
52 void setCoords(int index,int x,int y){coordinates[index][0] = x;
53 coordinates[index][1] = y;}
54 void initialize(int type);
55
56 int pieceType;
57 int coordinates[4][2];
58
59 static double randomSeed;
60};
61
62#endif