home *** CD-ROM | disk | FTP | other *** search
/ 3D Games (Spidla) / 3dhry2.iso / Cube Drop 2001 1.0 / Matrix.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2003-03-17  |  7.3 KB  |  316 lines

  1. /*
  2. Authors: David Nishimoto and Thomas Lee Young
  3. Website: http://www.listensoftware.com
  4. Program: Cube Drop
  5. */
  6.  
  7. #include "matrix.h"
  8.  
  9. Matrix::Matrix()
  10. {
  11.     score = 0;
  12.     ZeroMatrix();
  13. }
  14.  
  15. void Matrix::ZeroMatrix()
  16. {
  17.     for(int i = 0; i < MAT_WIDTH; ++i)
  18.         for(int j = 0; j < MAT_HEIGHT; ++j)
  19.             for(int k = 0; k < MAT_DEPTH; ++k)
  20.                 loc[i][j][k] = 0;
  21. }
  22.  
  23. int Matrix::isSet(int x, int y, int z)
  24. {
  25.     //return the value(color) stored in the matrix at x,y,z
  26.     //if the value is 0, it is not set and therefore
  27.     //no cube is located there
  28.     return(loc[x][y][z]);    
  29. }
  30.  
  31. void Matrix::set(int x, int y, int z)
  32. {
  33.     int color;
  34.     
  35. //    srand( (unsigned)time(NULL) );//seed the rand num generator
  36.  
  37.     color = rand()%6+1; //0=no cube, 1 = red, 2=green, 3=blue,
  38.                         //4=yellow, 5=cyan, 6=magenta
  39.  
  40.     setActiveCube(x,y,z);
  41.  
  42.     //arrays are zero based so subtract one
  43.     --x;
  44.     --y;
  45.     --z;
  46.  
  47.     loc[x][y][z] = color;
  48. }
  49.  
  50. void Matrix::moveLeft()
  51. {
  52.     int temp;
  53.  
  54.     //cube can only be moved if it is not at rest
  55.     if( (activeCube.x > 0) && (activeCube.y != 0) &&
  56.         (loc[activeCube.x][activeCube.y-1][activeCube.z] == 0) )
  57.     {
  58.         //move cube left if the space is not already occupied
  59.         if(loc[activeCube.x-1][activeCube.y][activeCube.z]== 0)
  60.         {
  61.             //store the color value of the cube and move it
  62.             temp = loc[activeCube.x][activeCube.y][activeCube.z];
  63.             loc[activeCube.x][activeCube.y][activeCube.z] = 0;
  64.             activeCube.x = activeCube.x - 1;
  65.             loc[activeCube.x][activeCube.y][activeCube.z] = temp;
  66.         }
  67.     }
  68. }
  69.  
  70. void Matrix::moveRight()
  71. {
  72.     int temp;
  73.  
  74.     //cube can only be moved if it is not at rest
  75.     if( (activeCube.x < 4) && (activeCube.y != 0) &&
  76.         (loc[activeCube.x][activeCube.y-1][activeCube.z] == 0) )
  77.     {
  78.         //store the color value of the cube and move it
  79.         if(loc[activeCube.x+1][activeCube.y][activeCube.z]== 0)
  80.         {
  81.             temp = loc[activeCube.x][activeCube.y][activeCube.z];
  82.             loc[activeCube.x][activeCube.y][activeCube.z] = 0;
  83.             activeCube.x = activeCube.x + 1;
  84.             loc[activeCube.x][activeCube.y][activeCube.z] = temp;
  85.         }
  86.     }
  87. }
  88.  
  89. void Matrix::moveIn()
  90. {
  91.     int temp;
  92.  
  93.     //cube can only be moved if it is not at rest
  94.     if( (activeCube.z < 4) && (activeCube.y != 0) &&
  95.         (loc[activeCube.x][activeCube.y-1][activeCube.z] == 0) )
  96.     {
  97.         //store the color value of the cube and move it
  98.         if(loc[activeCube.x][activeCube.y][activeCube.z+1]== 0)
  99.         {
  100.             temp = loc[activeCube.x][activeCube.y][activeCube.z];
  101.             loc[activeCube.x][activeCube.y][activeCube.z] = 0;
  102.             activeCube.z = activeCube.z + 1;
  103.             loc[activeCube.x][activeCube.y][activeCube.z] = temp;
  104.         }
  105.     }
  106. }
  107.  
  108. void Matrix::moveOut()
  109. {
  110.     int temp;
  111.  
  112.     //cube can only be moved if it is not at rest
  113.     if( (activeCube.z > 0) && (activeCube.y != 0) &&
  114.         (loc[activeCube.x][activeCube.y-1][activeCube.z] == 0) )
  115.     {
  116.         //store the color value of the cube and move it
  117.         if(loc[activeCube.x][activeCube.y][activeCube.z-1]== 0)
  118.         {
  119.             temp = loc[activeCube.x][activeCube.y][activeCube.z];
  120.             loc[activeCube.x][activeCube.y][activeCube.z] = 0;
  121.             activeCube.z = activeCube.z - 1;
  122.             loc[activeCube.x][activeCube.y][activeCube.z] = temp;
  123.         }
  124.     }
  125. }
  126.  
  127. void Matrix::lowerFloaters(char rowDir)
  128. {
  129.     const MatrixLocation tempLoc = activeCube;
  130.     int startHeight = 1 + activeCube.y;
  131.  
  132.     //go through evey location above the row that was cleared
  133.     //and drop all of the cubes
  134.     for(int j = 0; j < MAT_WIDTH; ++j)
  135.     {
  136.         for(int i = startHeight; i < MAT_HEIGHT; ++i)
  137.         {
  138.             if(rowDir == 'x')
  139.             {
  140.                 //activeCube.x = activeCube.x;
  141.                 activeCube.y = i;
  142.                 activeCube.z = j;
  143.  
  144.             }
  145.             else // rowDir == 'z'
  146.             {
  147.                 activeCube.x = j;
  148.                 activeCube.y = i;
  149.                 //activeCube.z = activeCube.z;
  150.             }
  151.  
  152.             //if there is a cube in the spot, drop it
  153.             if(loc[activeCube.x][activeCube.y][activeCube.z])
  154.                 drop();
  155.         }
  156.     }
  157.  
  158.     activeCube = tempLoc;
  159. }
  160.  
  161.  
  162. void Matrix::SetTitleBar()
  163. {
  164.     char scorePhrase[50];
  165.  
  166.     sprintf(scorePhrase,
  167.         "Cube Space                     Score: %i", score);
  168.     //glutSetWindowTitle(scorePhrase);
  169. }
  170.  
  171.  
  172. void Matrix::clearRows()
  173. {
  174.     int oldScore = score;
  175.     int color;
  176.     BOOL hit_flag=FALSE;
  177.  
  178.     for(int y=0; y<MAT_HEIGHT; ++y)
  179.     {
  180.         for(int x=0; x<MAT_WIDTH; ++x)
  181.         {
  182.             //for a given value of x, if all cells along z are set
  183.             // to the same color, then clear the row
  184. //            if(loc[x][y][0] && loc[x][y][1] && loc[x][y][2] && loc[x][y][3] && loc[x][y][4])
  185.             color = loc[activeCube.x][activeCube.y][activeCube.z];
  186.             if( color == loc[x][y][0] &&
  187.                 color == loc[x][y][1] &&
  188.                 color == loc[x][y][2] &&
  189.                 color == loc[x][y][3] &&
  190.                 color == loc[x][y][4]
  191.               )
  192.             {
  193.                 //increase the score by the values in the row
  194.                 score += loc[x][y][0];
  195.                 score += loc[x][y][1];
  196.                 score += loc[x][y][2];
  197.                 score += loc[x][y][3];
  198.                 score += loc[x][y][4];
  199.  
  200.                 loc[x][y][0] = 0;
  201.                 loc[x][y][1] = 0;
  202.                 loc[x][y][2] = 0;
  203.                 loc[x][y][3] = 0;
  204.                 loc[x][y][4] = 0;
  205.  
  206.                 lowerFloaters('x');
  207.                 hit_flag=TRUE;
  208.             }
  209.         }
  210.  
  211.         for(int z=0; z<MAT_WIDTH; ++z)
  212.         {
  213.             //for a given value of z, if all cells along x are set
  214.             //to the same color, then clear the row
  215. //            if(loc[0][y][z] && loc[1][y][z] && loc[2][y][z] && loc[3][y][z] && loc[4][y][z])
  216.             color = loc[activeCube.x][activeCube.y][activeCube.z];
  217.             if( color == loc[0][y][z] &&
  218.                 color == loc[1][y][z] &&
  219.                 color == loc[2][y][z] &&
  220.                 color == loc[3][y][z] &&
  221.                 color == loc[4][y][z]
  222.               )
  223.             {
  224.                 //increase the score by the values in the row
  225.                 score += loc[0][y][z];
  226.                 score += loc[1][y][z];
  227.                 score += loc[2][y][z];
  228.                 score += loc[3][y][z];
  229.                 score += loc[4][y][z];
  230.  
  231.                 loc[0][y][z] = 0;
  232.                 loc[1][y][z] = 0;
  233.                 loc[2][y][z] = 0;
  234.                 loc[3][y][z] = 0;
  235.                 loc[4][y][z] = 0;
  236.  
  237.                 lowerFloaters('z');
  238.                 hit_flag=TRUE;
  239.             }
  240.         }
  241.     } //end of y loop
  242.  
  243.     if(hit_flag==TRUE)
  244.     {
  245.         level+=50;
  246.     }
  247. }
  248.  
  249. BOOL Matrix::moveDown()
  250. {
  251.     int temp = 0;
  252.     int lowestOpenSpot = 0;
  253.  
  254.     //find the lowest empty spot in the current column
  255.     while(loc[activeCube.x][lowestOpenSpot][activeCube.z])
  256.     {
  257.         ++lowestOpenSpot;
  258.         if(lowestOpenSpot >= MAT_HEIGHT)
  259.             return (FALSE); //game over        
  260.     }
  261.  
  262.     //if the active cube is not immediately above the lowest
  263.     //spot, then lower the cube by one cell
  264.     if(activeCube.y > lowestOpenSpot)
  265.     {
  266.         temp = loc[activeCube.x][activeCube.y][activeCube.z];
  267.         loc[activeCube.x][activeCube.y][activeCube.z] = 0;
  268.         activeCube.y = activeCube.y - 1;
  269.         loc[activeCube.x][activeCube.y][activeCube.z] = temp;
  270.     }
  271.     else //start a new cube
  272.     {
  273.         //clear any completed rows
  274.         clearRows();
  275.         set(3,10,3);
  276.     }
  277.  
  278.     return (TRUE);
  279. }
  280.  
  281. BOOL Matrix::drop()
  282. {
  283.     int temp = 0;
  284.     int lowestOpenSpot = 0;
  285.  
  286.     while(loc[activeCube.x][lowestOpenSpot][activeCube.z])
  287.     {
  288.         ++lowestOpenSpot;
  289.         if(lowestOpenSpot >= MAT_HEIGHT)
  290.             return(FALSE); //game over
  291.     }
  292.  
  293.     //if the cube is not at the lowest open spot, drop it to the lowest spot
  294.     if(activeCube.y > lowestOpenSpot)
  295.     {
  296.         temp = loc[activeCube.x][activeCube.y][activeCube.z]; //save the color value
  297.         loc[activeCube.x][activeCube.y][activeCube.z] = 0;
  298.         activeCube.y = lowestOpenSpot;
  299.         loc[activeCube.x][activeCube.y][activeCube.z] = temp; //restore the color
  300.     }
  301. //    else
  302. //    {
  303. //        //check for completed rows, then start a new cube
  304. //        clearRows();
  305. //        set(3,10,3);
  306. //    }
  307.     return(TRUE);
  308. }
  309.  
  310. void Matrix::setActiveCube(int xx, int yy, int zz)
  311. {
  312.     activeCube.x = (--xx);
  313.     activeCube.y = (--yy);
  314.     activeCube.z = (--zz);
  315. }
  316.