Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

tpiece.cpp

Go to the documentation of this file.
00001 /**********************************************************************
00002 ** Copyright (C) 2000 Trolltech AS.  All rights reserved.
00003 **
00004 ** This file is part of Qtopia Environment.
00005 **
00006 ** This file may be distributed and/or modified under the terms of the
00007 ** GNU General Public License version 2 as published by the Free Software
00008 ** Foundation and appearing in the file LICENSE.GPL included in the
00009 ** packaging of this file.
00010 **
00011 ** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
00012 ** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
00013 **
00014 ** See http://www.trolltech.com/gpl/ for GPL licensing information.
00015 **
00016 ** Contact info@trolltech.com if any conditions of this licensing are
00017 ** not clear to you.
00018 **
00019 **********************************************************************/
00020 
00021 
00022 #include "tpiece.h"
00023 #include "qstring.h"
00024 #include <stdlib.h>
00025 #include <time.h>
00026 
00027 void TetrixPiece::rotateLeft()
00028 {
00029     if ( pieceType == 5 )    // don't rotate square piece type
00030         return;
00031     int tmp;
00032     for (int i = 0 ; i < 4 ; i++) {
00033         tmp = getXCoord(i);
00034         setXCoord(i,getYCoord(i));
00035         setYCoord(i,-tmp);
00036     }
00037 }
00038 
00039 void TetrixPiece::rotateRight()
00040 {
00041     if ( pieceType == 5 )    // don't rotate square piece type
00042         return;
00043     int tmp;
00044     for (int i = 0 ; i < 4 ; i++) {
00045         tmp = getXCoord(i);
00046         setXCoord(i,-getYCoord(i));
00047         setYCoord(i,tmp);
00048     }
00049 }
00050 
00051 int TetrixPiece::getMinX()
00052 {
00053     int tmp = coordinates[0][0];
00054     for(int i = 1 ; i < 4 ; i++)
00055         if (tmp > coordinates[i][0])
00056             tmp = coordinates[i][0];
00057     return tmp;
00058 }
00059 
00060 int TetrixPiece::getMaxX()
00061 {
00062     int tmp = coordinates[0][0];
00063     for(int i = 1 ; i < 4 ; i++)
00064         if (tmp < coordinates[i][0])
00065             tmp = coordinates[i][0];
00066     return tmp;
00067 
00068 }
00069 
00070 int TetrixPiece::getMinY()
00071 {
00072     int tmp = coordinates[0][1];
00073     for(int i = 1 ; i < 4 ; i++)
00074         if (tmp > coordinates[i][1])
00075             tmp = coordinates[i][1];
00076     return tmp;
00077 }
00078 
00079 int TetrixPiece::getMaxY()
00080 {
00081     int tmp = coordinates[0][1];
00082     for(int i = 1 ; i < 4 ; i++)
00083         if (tmp < coordinates[i][1])
00084             tmp = coordinates[i][1];
00085     return tmp;
00086 }
00087 
00088 void TetrixPiece::initialize(int type)
00089 {
00090     static int pieceTypes[7][4][2] = {{{ 0,-1},
00091                                        { 0, 0},
00092                                        {-1, 0},
00093                                        {-1, 1}},
00094 
00095                                       {{ 0,-1},
00096                                        { 0, 0},
00097                                        { 1, 0},
00098                                        { 1, 1}},
00099 
00100                                       {{ 0,-1},
00101                                        { 0, 0},
00102                                        { 0, 1},
00103                                        { 0, 2}},
00104 
00105                                       {{-1, 0},
00106                                        { 0, 0},
00107                                        { 1, 0},
00108                                        { 0, 1}},
00109 
00110                                       {{ 0, 0},
00111                                        { 1, 0},
00112                                        { 0, 1},
00113                                        { 1, 1}},
00114 
00115                                       {{-1,-1},
00116                                        { 0,-1},
00117                                        { 0, 0},
00118                                        { 0, 1}},
00119 
00120                                       {{ 1,-1},
00121                                        { 0,-1},
00122                                        { 0, 0},
00123                                        { 0, 1}}};
00124     if (type < 1 || type > 7)
00125         type = 1;
00126     pieceType = type;
00127     for(int i = 0 ; i < 4 ; i++) {
00128             coordinates[i][0] = pieceTypes[type - 1][i][0];
00129             coordinates[i][1] = pieceTypes[type - 1][i][1];
00130     }
00131 }
00132 
00133 
00134 /*
00135  *      Sigh, oh beautiful nostalgia! This random algorithm has
00136  *      been taken from the book "Adventures with your pocket calculator"
00137  *      and I used it in my first implemented and machine-
00138  *      run program of any size to speak of. Imagine how hungry I
00139  *      was after having programmed BASIC on paper for
00140  *      half a year?!!?!?!?!?!? The first program I typed in was a
00141  *      slot machine game and was made in BASIC on a SHARP
00142  *      PC-1211 with 1,47 KB RAM (one point four seven kilobytes) and
00143  *      a one-line LCD-display (I think it had 32 characters) in the
00144  *      year of our lord 1981. The man I had bought the machine from worked
00145  *      as a COBOL programmer and was amazed and impressed
00146  *      when I demonstrated the program 2 days after I had
00147  *      bought the machine, quote: "Gees, I have been looking so long
00148  *      for a "random" command in that BASIC, what is it called?"
00149  *      Oh, how I still get a thrill out of the thought of the
00150  *      explanation I then gave him...
00151  */
00152 
00153 /*
00154  *      Sukk, aa vakre nostalgi! Denne random algoritmen er
00155  *      tatt fra boka "Adventures with your pocket calculator"
00156  *      og den brukte jeg i mitt foerste implementerte og maskin-
00157  *      kjoerte program av nevneverdig stoerrelse. Tror du jeg var
00158  *      noe sulten etter aa ha programmert BASIC paa papir i et
00159  *      halvt aar?!!?!?!?!?!? Programmet jeg tasta inn foerst var et
00160  *      "enarmet banditt" spill og ble laget i BASIC paa en SHARP
00161  *      PC-1211 med 1,47 KB RAM (en komma foertisju kilobyte) og
00162  *      et en-linjers LCD-display (tror det hadde 32 karakterer) i det
00163  *      herrens aar 1981. Mannen jeg kjoepte maskinen av jobbet til
00164  *      daglig med COBOL programmering og var forbloeffet og imponert
00165  *      da jeg demonstrerte programmet 2 dager etter at jeg hadde
00166  *      kjoept maskinen, sitat: "Joess, jeg som har leita saa lenge
00167  *      etter en random kommando i den BASICen, hva var det den
00168  *      het?" Aa, jeg frydes ennaa ved tanken paa forklaringen jeg
00169  *      deretter ga ham...
00170  */
00171 
00172 double TetrixPiece::randomSeed = 0.33333;
00173 
00174 void TetrixPiece::setRandomSeed(double seed)
00175 {
00176 #ifdef __MIPSEL__
00177     srand( clock() );
00178 #else
00179     QCString buffer;
00180     if (seed < 0)
00181         seed = - seed;
00182     if (seed >= 1)
00183         seed = seed - (double) ((int) seed);
00184     buffer.sprintf("%1.5f",(float) seed);
00185     for (int i = 0 ; i < 5 ; i++)
00186         if ((buffer[i + 2] - '0') % 2 == 0)
00187             buffer[i + 2]++;
00188     randomSeed = atof(buffer);
00189 #endif
00190 }
00191 
00192 int TetrixPiece::randomValue(int maxPlusOne)
00193 {
00194 #ifdef __MIPSEL__
00195     return rand() % maxPlusOne;
00196 #else
00197     randomSeed = randomSeed*147;
00198     randomSeed = randomSeed - (double) ((int) randomSeed);
00199     return (int) (randomSeed*maxPlusOne);
00200 #endif
00201 }

Generated on Sat Nov 5 16:17:27 2005 for OPIE by  doxygen 1.4.2