home *** CD-ROM | disk | FTP | other *** search
/ 220 Jogos / 220 jogos.iso / tetris / tetron / SOURCE / tetramino.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2002-08-12  |  7.6 KB  |  243 lines

  1. //----------------------------------------------------------------------------------------
  2. //----------------------------------------------------------------------------------------
  3. //
  4. //        Filename        :    tetramino.cpp
  5. //        Description        :    Class definition for the Tetramino class
  6. //        Author            :   Marnich van Rensburg (2002)
  7. //
  8. //----------------------------------------------------------------------------------------
  9. //----------------------------------------------------------------------------------------
  10.  
  11. #include "tetramino.h"
  12.  
  13.  
  14. // Data for Tetraminoes
  15. bool TetraminoData[7][4][16] = 
  16. {
  17.     {    // I tetamino
  18.         {0,1,0,0, 0,1,0,0, 0,1,0,0, 0,1,0,0},    //Rotation 0
  19.         {0,0,0,0, 1,1,1,1, 0,0,0,0, 0,0,0,0},    //Rotation 1
  20.         {0,1,0,0, 0,1,0,0, 0,1,0,0, 0,1,0,0},    //Rotation 2
  21.         {0,0,0,0, 1,1,1,1, 0,0,0,0, 0,0,0,0},    //Rotation 3
  22.     },
  23.     {    // J tetamino
  24.         {0,1,0,0, 0,1,0,0, 1,1,0,0, 0,0,0,0},    //Rotation 0
  25.         {0,0,0,0, 1,1,1,0, 0,0,1,0, 0,0,0,0},    //Rotation 1
  26.         {0,1,1,0, 0,1,0,0, 0,1,0,0, 0,0,0,0},    //Rotation 2
  27.         {1,0,0,0, 1,1,1,0, 0,0,0,0, 0,0,0,0},    //Rotation 3
  28.     },
  29.     {    // L tetamino
  30.         {0,1,0,0, 0,1,0,0, 0,1,1,0, 0,0,0,0},    //Rotation 0
  31.         {0,0,1,0, 1,1,1,0, 0,0,0,0, 0,0,0,0},    //Rotation 1
  32.         {1,1,0,0, 0,1,0,0, 0,1,0,0, 0,0,0,0},    //Rotation 2
  33.         {0,0,0,0, 1,1,1,0, 1,0,0,0, 0,0,0,0},    //Rotation 3
  34.     },
  35.     {    // O tetamino
  36.         {0,0,0,0, 0,1,1,0, 0,1,1,0, 0,0,0,0},    //Rotation 0
  37.         {0,0,0,0, 0,1,1,0, 0,1,1,0, 0,0,0,0},    //Rotation 1
  38.         {0,0,0,0, 0,1,1,0, 0,1,1,0, 0,0,0,0},    //Rotation 2
  39.         {0,0,0,0, 0,1,1,0, 0,1,1,0, 0,0,0,0},    //Rotation 3
  40.     },
  41.     {    // S tetamino
  42.         {0,0,0,0, 0,1,1,0, 1,1,0,0, 0,0,0,0},    //Rotation 0
  43.         {0,1,0,0, 0,1,1,0, 0,0,1,0, 0,0,0,0},    //Rotation 1
  44.         {0,0,0,0, 0,1,1,0, 1,1,0,0, 0,0,0,0},    //Rotation 2
  45.         {0,1,0,0, 0,1,1,0, 0,0,1,0, 0,0,0,0},    //Rotation 3
  46.     },
  47.     {    // T tetamino
  48.         {0,0,0,0, 1,1,1,0, 0,1,0,0, 0,0,0,0},    //Rotation 0
  49.         {0,1,0,0, 0,1,1,0, 0,1,0,0, 0,0,0,0},    //Rotation 1
  50.         {0,1,0,0, 1,1,1,0, 0,0,0,0, 0,0,0,0},    //Rotation 2
  51.         {0,1,0,0, 1,1,0,0, 0,1,0,0, 0,0,0,0}    //Rotation 3
  52.     },
  53.     {    // Z tetamino
  54.         {0,0,0,0, 1,1,0,0, 0,1,1,0, 0,0,0,0},    //Rotation 0
  55.         {0,1,0,0, 1,1,0,0, 1,0,0,0, 0,0,0,0},    //Rotation 1
  56.         {0,0,0,0, 1,1,0,0, 0,1,1,0, 0,0,0,0},    //Rotation 2
  57.         {0,1,0,0, 1,1,0,0, 1,0,0,0, 0,0,0,0},    //Rotation 3
  58.     },
  59. };//TetraminoData
  60.  
  61.  
  62.  
  63.  
  64.  
  65. //----------------------------------------------------------------------------------------
  66. //        Description        :    Class Constructor - Initialize class data members
  67. //----------------------------------------------------------------------------------------
  68. Tetramino :: Tetramino()
  69. {
  70.     Speed = 1000.0f;
  71.     Type = 1;                            
  72.     CurrentRotation = 0;                // Reset Rotational state to 0
  73.     x = TET_START_POS_X;                                // (x, y) positions
  74.     y = TET_START_POS_Y;
  75.     AssignData(Type, CurrentRotation);    // Asign data for the specific tetramino
  76.  
  77. }//End Tetramino
  78.  
  79.  
  80.  
  81. //----------------------------------------------------------------------------------------
  82. //        Description        :     Returns true if tetramino shoukd drop to the next row
  83. //----------------------------------------------------------------------------------------
  84.  
  85. bool Tetramino :: DropToNextRow()
  86. {
  87.     //Check if it should fall to the next row
  88.     if(Tmr.CheckFreq(Speed))
  89.         return true;
  90.     else
  91.         return false;
  92.  
  93. }// DropToNextRow
  94.  
  95.  
  96. //----------------------------------------------------------------------------------------
  97. //        Description        :     Assigns data for teramino cell at (x, y)
  98. //----------------------------------------------------------------------------------------
  99.  
  100. void Tetramino :: SetMatrix(char v_x, char v_y, bool v_Data)
  101. {
  102.  
  103.     Matrix[v_x][v_y] = v_Data;
  104.  
  105. }//End SetMatrix
  106.  
  107.  
  108.  
  109. //----------------------------------------------------------------------------------------
  110. //        Description        :    Returns the value of the teramino cell at (x, y)
  111. //----------------------------------------------------------------------------------------
  112.  
  113. bool Tetramino :: GetMatrix(char v_x, char v_y)
  114. {
  115.     return Matrix[v_x][v_y];
  116.  
  117. }//End Tetramino
  118.  
  119.  
  120.  
  121.  
  122. //----------------------------------------------------------------------------------------
  123. //        Description        :    Insert tetrmino data into matrix based on type and rotation passed
  124. //----------------------------------------------------------------------------------------
  125.  
  126. void Tetramino :: AssignData(char v_Type, char v_Rotation)
  127. {
  128.     char cell_n = 0;                        // Counter for referncing the tetramino data
  129.     
  130.     for (char my = 0; my != 4; my++)        // loop 4 rows - Y
  131.     {
  132.         for (char mx = 0; mx != 4; mx++)    // loop 4 cols - X
  133.         {
  134.             Matrix[mx][my] = TetraminoData[v_Type - 1][v_Rotation][cell_n];    // Asign Data
  135.             cell_n++;                        // inc cell counter
  136.         }//for
  137.     }//for
  138.  
  139. }//End AssignData
  140.  
  141.  
  142.  
  143. //----------------------------------------------------------------------------------------
  144. //        Description        :    Initialize a tetrmino based on the type passed
  145. //----------------------------------------------------------------------------------------
  146.  
  147. void Tetramino :: New(char v_Type, unsigned int v_Level)
  148. {
  149.     Speed = 1000.0f - ((v_Level - 1) * 100.0f);
  150.     if(Speed < 1) Speed = 1;
  151.  
  152.     Type = v_Type;                            // Sets the type value for the tetramino
  153.     x = TET_START_POS_X;                                    // (x, y) positions
  154.     y = TET_START_POS_Y;
  155.     CurrentRotation = 0;                    // Have not been rotated yet
  156.     AssignData(Type, CurrentRotation);    // Asign data for the specific tetramino
  157.     
  158. }//End New
  159.  
  160.  
  161.  
  162.  
  163. //----------------------------------------------------------------------------------------
  164. //        Description        :    Moves tetramino one RIGHT
  165. //----------------------------------------------------------------------------------------
  166.  
  167. void Tetramino :: MoveRight()
  168. {
  169.     x++;
  170.     if (x == 36) x = 0;        // Wrap within the container
  171.  
  172. }//End Move
  173.  
  174.  
  175.  
  176. //----------------------------------------------------------------------------------------
  177. //        Description        :    Moves tetramino one LEFT
  178. //----------------------------------------------------------------------------------------
  179.  
  180. void Tetramino :: MoveLeft()
  181. {
  182.     x--;
  183.     if (x == -1) x = 35;    // Wrap within the container
  184.  
  185. }//End Move
  186.  
  187.  
  188.  
  189.  
  190.  
  191. //----------------------------------------------------------------------------------------
  192. //        Description        :    Moves tetramino one UP
  193. //----------------------------------------------------------------------------------------
  194.  
  195. void Tetramino :: MoveUp()
  196. {
  197.     y--;
  198.         
  199. }//End Move
  200.  
  201.  
  202.  
  203. //----------------------------------------------------------------------------------------
  204. //        Description        :    Moves tetramino one DOWN
  205. //----------------------------------------------------------------------------------------
  206.  
  207. void Tetramino :: MoveDown()
  208. {
  209.     Tmr.CheckFreq(0.0f);    // reset CheckFreq to prevent slip effect
  210.     y++;
  211.         
  212. }//End Move
  213.  
  214.  
  215.  
  216.  
  217. //----------------------------------------------------------------------------------------
  218. //        Description        :    Rotates tetranino RIGHT
  219. //----------------------------------------------------------------------------------------
  220.  
  221. void Tetramino :: RotateRight()
  222. {
  223.     CurrentRotation++;                                //Inc current rotational state
  224.     if (CurrentRotation ==  4) CurrentRotation = 0;    //Wrap if full rotation right
  225.     AssignData(Type, CurrentRotation);                //Assign new data to matrix
  226.  
  227. }//End RotateRight
  228.  
  229.  
  230. //----------------------------------------------------------------------------------------
  231. //        Description        :    Rotates tetranino RIGHT
  232. //----------------------------------------------------------------------------------------
  233.  
  234. void Tetramino :: RotateLeft()
  235. {
  236.     CurrentRotation--;                                //Decrement current rotational state
  237.     if (CurrentRotation == -1) CurrentRotation = 3;    //Wrap if full rotation left
  238.     AssignData(Type, CurrentRotation);                //Assign new data to matrix
  239.  
  240. }//End RotateLeft
  241.  
  242.  
  243.