1 | /****************************************************************************
|
---|
2 | ** $Id: tpiece.cpp 160 2006-12-11 20:15:57Z dmik $
|
---|
3 | **
|
---|
4 | ** Copyright (C) 1992-2000 Trolltech AS. All rights reserved.
|
---|
5 | **
|
---|
6 | ** This file is part of an example program for Qt. This example
|
---|
7 | ** program may be used, distributed and modified without limitation.
|
---|
8 | **
|
---|
9 | *****************************************************************************/
|
---|
10 |
|
---|
11 | #include "tpiece.h"
|
---|
12 | #include "qstring.h"
|
---|
13 | #include <stdlib.h>
|
---|
14 |
|
---|
15 | void TetrixPiece::rotateLeft()
|
---|
16 | {
|
---|
17 | if ( pieceType == 5 ) // don't rotate square piece type
|
---|
18 | return;
|
---|
19 | int tmp;
|
---|
20 | for (int i = 0 ; i < 4 ; i++) {
|
---|
21 | tmp = getXCoord(i);
|
---|
22 | setXCoord(i,getYCoord(i));
|
---|
23 | setYCoord(i,-tmp);
|
---|
24 | }
|
---|
25 | }
|
---|
26 |
|
---|
27 | void TetrixPiece::rotateRight()
|
---|
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 |
|
---|
39 | int TetrixPiece::getMinX()
|
---|
40 | {
|
---|
41 | int tmp = coordinates[0][0];
|
---|
42 | for(int i = 1 ; i < 4 ; i++)
|
---|
43 | if (tmp > coordinates[i][0])
|
---|
44 | tmp = coordinates[i][0];
|
---|
45 | return tmp;
|
---|
46 | }
|
---|
47 |
|
---|
48 | int TetrixPiece::getMaxX()
|
---|
49 | {
|
---|
50 | int tmp = coordinates[0][0];
|
---|
51 | for(int i = 1 ; i < 4 ; i++)
|
---|
52 | if (tmp < coordinates[i][0])
|
---|
53 | tmp = coordinates[i][0];
|
---|
54 | return tmp;
|
---|
55 |
|
---|
56 | }
|
---|
57 |
|
---|
58 | int TetrixPiece::getMinY()
|
---|
59 | {
|
---|
60 | int tmp = coordinates[0][1];
|
---|
61 | for(int i = 1 ; i < 4 ; i++)
|
---|
62 | if (tmp > coordinates[i][1])
|
---|
63 | tmp = coordinates[i][1];
|
---|
64 | return tmp;
|
---|
65 | }
|
---|
66 |
|
---|
67 | int TetrixPiece::getMaxY()
|
---|
68 | {
|
---|
69 | int tmp = coordinates[0][1];
|
---|
70 | for(int i = 1 ; i < 4 ; i++)
|
---|
71 | if (tmp < coordinates[i][1])
|
---|
72 | tmp = coordinates[i][1];
|
---|
73 | return tmp;
|
---|
74 | }
|
---|
75 |
|
---|
76 | void TetrixPiece::initialize(int type)
|
---|
77 | {
|
---|
78 | static int pieceTypes[7][4][2] = {{{ 0,-1},
|
---|
79 | { 0, 0},
|
---|
80 | {-1, 0},
|
---|
81 | {-1, 1}},
|
---|
82 |
|
---|
83 | {{ 0,-1},
|
---|
84 | { 0, 0},
|
---|
85 | { 1, 0},
|
---|
86 | { 1, 1}},
|
---|
87 |
|
---|
88 | {{ 0,-1},
|
---|
89 | { 0, 0},
|
---|
90 | { 0, 1},
|
---|
91 | { 0, 2}},
|
---|
92 |
|
---|
93 | {{-1, 0},
|
---|
94 | { 0, 0},
|
---|
95 | { 1, 0},
|
---|
96 | { 0, 1}},
|
---|
97 |
|
---|
98 | {{ 0, 0},
|
---|
99 | { 1, 0},
|
---|
100 | { 0, 1},
|
---|
101 | { 1, 1}},
|
---|
102 |
|
---|
103 | {{-1,-1},
|
---|
104 | { 0,-1},
|
---|
105 | { 0, 0},
|
---|
106 | { 0, 1}},
|
---|
107 |
|
---|
108 | {{ 1,-1},
|
---|
109 | { 0,-1},
|
---|
110 | { 0, 0},
|
---|
111 | { 0, 1}}};
|
---|
112 | if (type < 1 || type > 7)
|
---|
113 | type = 1;
|
---|
114 | pieceType = type;
|
---|
115 | for(int i = 0 ; i < 4 ; i++) {
|
---|
116 | coordinates[i][0] = pieceTypes[type - 1][i][0];
|
---|
117 | coordinates[i][1] = pieceTypes[type - 1][i][1];
|
---|
118 | }
|
---|
119 | }
|
---|
120 |
|
---|
121 |
|
---|
122 | /*
|
---|
123 | * Sigh, oh beautiful nostalgia! This random algorithm has
|
---|
124 | * been taken from the book "Adventures with your pocket calculator"
|
---|
125 | * and I used it in my first implemented and machine-
|
---|
126 | * run program of any size to speak of. Imagine how hungry I
|
---|
127 | * was after having programmed BASIC on paper for
|
---|
128 | * half a year?!!?!?!?!?!? The first program I typed in was a
|
---|
129 | * slot machine game and was made in BASIC on a SHARP
|
---|
130 | * PC-1211 with 1,47 KB RAM (one point four seven kilobytes) and
|
---|
131 | * a one-line LCD-display (I think it had 32 characters) in the
|
---|
132 | * year of our lord 1981. The man I had bought the machine from worked
|
---|
133 | * as a COBOL programmer and was amazed and impressed
|
---|
134 | * when I demonstrated the program 2 days after I had
|
---|
135 | * bought the machine, quote: "Gees, I have been looking so long
|
---|
136 | * for a "random" command in that BASIC, what is it called?"
|
---|
137 | * Oh, how I still get a thrill out of the thought of the
|
---|
138 | * explanation I then gave him...
|
---|
139 | */
|
---|
140 |
|
---|
141 | /*
|
---|
142 | * Sukk, aa vakre nostalgi! Denne random algoritmen er
|
---|
143 | * tatt fra boka "Adventures with your pocket calculator"
|
---|
144 | * og den brukte jeg i mitt foerste implementerte og maskin-
|
---|
145 | * kjoerte program av nevneverdig stoerrelse. Tror du jeg var
|
---|
146 | * noe sulten etter aa ha programmert BASIC paa papir i et
|
---|
147 | * halvt aar?!!?!?!?!?!? Programmet jeg tasta inn foerst var et
|
---|
148 | * "enarmet banditt" spill og ble laget i BASIC paa en SHARP
|
---|
149 | * PC-1211 med 1,47 KB RAM (en komma foertisju kilobyte) og
|
---|
150 | * et en-linjers LCD-display (tror det hadde 32 karakterer) i det
|
---|
151 | * herrens aar 1981. Mannen jeg kjoepte maskinen av jobbet til
|
---|
152 | * daglig med COBOL programmering og var forbloeffet og imponert
|
---|
153 | * da jeg demonstrerte programmet 2 dager etter at jeg hadde
|
---|
154 | * kjoept maskinen, sitat: "Joess, jeg som har leita saa lenge
|
---|
155 | * etter en random kommando i den BASICen, hva var det den
|
---|
156 | * het?" Aa, jeg frydes ennaa ved tanken paa forklaringen jeg
|
---|
157 | * deretter ga ham...
|
---|
158 | */
|
---|
159 |
|
---|
160 | double TetrixPiece::randomSeed = 0.33333;
|
---|
161 |
|
---|
162 | void TetrixPiece::setRandomSeed(double seed)
|
---|
163 | {
|
---|
164 | QCString buffer;
|
---|
165 | if (seed < 0)
|
---|
166 | seed = - seed;
|
---|
167 | if (seed >= 1)
|
---|
168 | seed = seed - (double) ((int) seed);
|
---|
169 | buffer.sprintf("%1.5f",(float) seed);
|
---|
170 | for (int i = 0 ; i < 5 ; i++)
|
---|
171 | if ((buffer[i + 2] - '0') % 2 == 0)
|
---|
172 | buffer[i + 2]++;
|
---|
173 | randomSeed = atof(buffer);
|
---|
174 | }
|
---|
175 |
|
---|
176 | int TetrixPiece::randomValue(int maxPlusOne)
|
---|
177 | {
|
---|
178 | randomSeed = randomSeed*147;
|
---|
179 | randomSeed = randomSeed - (double) ((int) randomSeed);
|
---|
180 | return (int) (randomSeed*maxPlusOne);
|
---|
181 | }
|
---|