Posted By

ozkriff on 01/17/09


Tagged


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

ozkriff


312312312


 / Published in: C++
 

  1. // DEFINES {{{1
  2. #define m_ROTATE 1
  3. #define m_DOWN 2
  4. #define m_LEFT 3
  5. #define m_RIGHT 4
  6.  
  7. #define m_HEIGH 16 // plane's height
  8. #define m_WIDTH 8 //plane's width
  9.  
  10. #define m_LOCAL_SIZE 5; // default: 5
  11. //}}}1
  12.  
  13. // GLOBALS {{{1
  14. int local_buffer[m_LOCAL_SIZE][m_LOCAL_SIZE];
  15. int local_buffer_two[m_LOCAL_SIZE][m_LOCAL_SIZE];
  16.  
  17. int global_buffer[m_HEIGH][m_WIDTH];
  18. // }}}1
  19.  
  20.  
  21. void special(int key, int x, int y) // {{{1
  22. {
  23. switch ( key ) // {{{2
  24. {
  25. case GLUT_KEY_UP: // {{{3
  26. rotate();
  27. break; // }}}3
  28.  
  29. case GLUT_KEY_DOWN: //{{{3
  30. moveDown();
  31. break; // }}}3
  32.  
  33. case GLUT_KEY_LEFT: // {{{3
  34. if ( currentX > 0 )
  35. {
  36. currentX++;
  37. if ( checkIntersections() )
  38. currentX--:
  39. }
  40. break; // }}}3
  41.  
  42. case GLUT_KEY_RIGHT: // {{{3
  43. if ( currentX < m_WIDTH )
  44. {
  45. currentX--;
  46. if ( checkIntersections() )
  47. currentX++:
  48. }
  49. break; // }}}3
  50. } // }}}2
  51.  
  52. ////////////
  53. checkNextLineCollisions();
  54. ////////////
  55. } // }}}1
  56.  
  57. void timer(int value) // {{{1
  58. {
  59. currentY++; //move piece 1 line down
  60. checkIntersections();
  61. glutPostRedisplay();
  62. glutTimerFunc( speed, timer, 1);
  63. } // }}}1
  64.  
  65. bool checkIntersections() // {{{1
  66. {
  67. for (i=0; i<m_LOCAL_SIZE; i++ )
  68. for (j=0; j<m_LOCAL_SIZE; j++ )
  69. if ( (global_buffer
  70. [ currentX - m_LOCAL_SIZE/2 + i ]
  71. [ currentY - m_LOCAL_SIZE/2 + j ] )
  72. && ( local_buffer[i][j] )
  73. return true;
  74. return false;
  75. } // }}}1
  76.  
  77. bool checkNextLineCollisions() // {{{1
  78. {
  79. currentY++;
  80. if ( checkIntersections() )
  81. {
  82. currentY--;
  83. return true;
  84. }
  85. return false;
  86. } // }}}1
  87.  
  88. void rotate() // {{{1
  89. {
  90. // make copy of local buffer {{{2
  91. for (i=0; i<m_LOCAL_SIZE; i++ )
  92. for (j=0; j<m_LOCAL_SIZE; j++ )
  93. local_buffer_two[i][j]=local_buffer[i][j]; // }}}2
  94.  
  95. // rotating local buffer {{{2
  96. for (i=0; i<m_LOCAL_SIZE; i++ )
  97. for (j=0; j<m_LOCAL_SIZE; j++ )
  98. local_buffer[j][i]=local_buffer_two[i][j]; // }}}2
  99.  
  100.  
  101. if ( checkIntersections() ) return;
  102.  
  103. currentX++;
  104. if ( checkIntersections() ) return;
  105.  
  106. currentX++;
  107. if ( checkIntersections() ) return;
  108.  
  109. currentX-- -- --;
  110. if ( checkIntersections() ) return;
  111.  
  112. currentX--;
  113. if ( checkIntersections() ) return;
  114. currentX++ ++;
  115.  
  116.  
  117. cout<<"no rotation done!"<<endl;
  118.  
  119. // restorig local buffer {{{2
  120. for (i=0; i<m_LOCAL_SIZE; i++ )
  121. for (j=0; j<m_LOCAL_SIZE; j++ )
  122. local_buffer[i][j]=local_buffer_two[i][j]; // }}}2
  123.  
  124.  
  125. } // }}}1
  126.  
  127. void moveDown() // {{{1
  128. {
  129. newY = currentY;
  130.  
  131. // step by step mive piece down
  132. // and check for collisions.
  133. // remember last Y as newY
  134.  
  135. while ( newY < m_HEIGH )
  136. {
  137. if ( checkIntersections() )
  138. break;
  139. newY++;
  140. }
  141.  
  142. currentY = newY;
  143. } // }}}1
  144.  
  145. void global+local() // {{{1
  146. {
  147. for (i=0; i<m_LOCAL_SIZE; i++ )
  148. for (j=0; j<m_LOCAL_SIZE; j++ )
  149. global_buffer
  150. [ currentX - m_LOCAL_SIZE/2 + i ]
  151. [ currentY - m_LOCAL_SIZE/2 + j ]
  152. = local_buffer[i][j];
  153. } // }}}1
  154.  
  155. void global-local() // {{{1
  156. {
  157. for (i=0; i<m_LOCAL_SIZE; i++ )
  158. for (j=0; j<m_LOCAL_SIZE; j++ )
  159. global_buffer
  160. [ currentX - m_LOCAL_SIZE/2 + i ]
  161. [ currentY - m_LOCAL_SIZE/2 + j ]
  162. =0;
  163. } // }}}1
  164.  
  165. void ClearLocalBuffer(void) // {{{1
  166. {
  167. for (i=0; i<m_LOCAL_SIZE; i++ )
  168. for (j=0; j<m_LOCAL_SIZE; j++ )
  169. local_buffer[i][j]=0;
  170. } // }}}1
  171.  
  172. void ClearGlobalBuffer(void) // {{{1
  173. {
  174. for (i=0; i<m_HEIGH; i++ )
  175. for (j=0; j<m_WIDTH; j++ )
  176. global_buffer[i][j]=0;
  177. } // }}} 1
  178.  
  179. bool CheckIfLastLine(void) // {{{1
  180. {
  181. global+local();
  182. for (i=0; i<m_WIDTH; i++ )
  183. if ( global_buffer[m_HEIGH][i] )
  184. {
  185. global-local();
  186. return 1;
  187. }
  188. global-local();
  189. return 0;
  190. } // }}}1

Report this snippet  

You need to login to post a comment.