Advertisement
Amorf

Untitled

Dec 14th, 2021
858
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 KB | None | 0 0
  1. #include "PieceSet.h"
  2.  
  3. PieceSet::PieceSet()
  4. {
  5.     for (int i = 0; i < NUM_PIECES; i++)
  6.         for (int j = 0; j < NUM_ROTATIONS; j++)
  7.             pieces[i][j] = 0;
  8.  
  9.     POINT apt[NUM_ROTATIONS];
  10.  
  11.     // 0, I piece, red
  12.     apt[0].x = 0;    apt[0].y = 0;
  13.     apt[1].x = 0;    apt[1].y = 1;
  14.     apt[2].x = 0;    apt[2].y = 2;
  15.     apt[3].x = 0;    apt[3].y = 3;
  16.     pieces[0][0] = new Piece(0, 0, RGB(255,0,0), apt);
  17.  
  18.     // 1, L piece, orange
  19.     apt[0].x = 0;    apt[0].y = 0;
  20.     apt[1].x = 1;    apt[1].y = 0;
  21.     apt[2].x = 0;    apt[2].y = 1;
  22.     apt[3].x = 0;    apt[3].y = 2;
  23.     pieces[1][0] = new Piece(1, 0, RGB(230,130,24), apt);
  24.  
  25.     // 2, counter-L piece, yellow
  26.     apt[0].x = 0;    apt[0].y = 0;
  27.     apt[1].x = 1;    apt[1].y = 0;
  28.     apt[2].x = 1;    apt[2].y = 1;
  29.     apt[3].x = 1;    apt[3].y = 2;
  30.     pieces[2][0] = new Piece(2, 0, RGB(255,255,0), apt);
  31.  
  32.     // 3, S piece, green
  33.     apt[0].x = 0;    apt[0].y = 0;
  34.     apt[1].x = 1;    apt[1].y = 0;
  35.     apt[2].x = 1;    apt[2].y = 1;
  36.     apt[3].x = 2;    apt[3].y = 1;
  37.     pieces[3][0] = new Piece(3, 0, RGB(120,200,80), apt);
  38.  
  39.     // 4, Z piece, blue
  40.     apt[0].x = 1;    apt[0].y = 0;
  41.     apt[1].x = 2;    apt[1].y = 0;
  42.     apt[2].x = 0;    apt[2].y = 1;
  43.     apt[3].x = 1;    apt[3].y = 1;
  44.     pieces[4][0] = new Piece(4, 0, RGB(100,180,255), apt);
  45.  
  46.     // 5, Square piece, dark blue
  47.     apt[0].x = 0;    apt[0].y = 0;
  48.     apt[1].x = 1;    apt[1].y = 0;
  49.     apt[2].x = 0;    apt[2].y = 1;
  50.     apt[3].x = 1;    apt[3].y = 1;
  51.     pieces[5][0] = new Piece(5, 0, RGB(20,100,200), apt);
  52.  
  53.     // 6, T piece, purple
  54.     apt[0].x = 0;    apt[0].y = 0;
  55.     apt[1].x = 1;    apt[1].y = 0;
  56.     apt[2].x = 2;    apt[2].y = 0;
  57.     apt[3].x = 1;    apt[3].y = 1;
  58.     pieces[6][0] = new Piece(6, 0, RGB(220,180,255), apt);
  59.  
  60.     // Create piece rotations
  61.     rotateAll();
  62. }
  63.  
  64. PieceSet::~PieceSet()
  65. {
  66.     for (int i = 0; i < NUM_PIECES; i++)
  67.         for (int j = 0; j < NUM_ROTATIONS; j++)
  68.             if (pieces[i][j] != 0)
  69.                 delete pieces[i][j];
  70. }
  71.  
  72. Piece *PieceSet::getPiece(int id, int rotation) const
  73. {
  74.     if (id >= NUM_PIECES || id < 0 || rotation >= NUM_ROTATIONS || rotation < 0)
  75.         return NULL;
  76.     return pieces[id][rotation];
  77. }
  78.  
  79. void PieceSet::rotateAll()
  80. {
  81.     POINT apt[NUM_ROTATIONS];
  82.     for (int i = 0; i < NUM_PIECES; i++)
  83.     {
  84.         pieces[i][0]->getBody(apt);
  85.         for (int j = 1; j < NUM_ROTATIONS; j++)
  86.         {
  87.             rotate(apt);
  88.             if (pieces[i][j] != 0)
  89.                 delete pieces[i][j];
  90.             pieces[i][j] = new Piece(i, j, pieces[i][0]->getColor(), apt);
  91.         }
  92.     }
  93. }
  94.  
  95. void PieceSet::rotate(POINT* apt, int numPoints)
  96. {
  97.     int tmp;
  98.  
  99.     // X' = -Y
  100.     // Y' = X
  101.     for (int i = 0; i < numPoints; i++)
  102.     {
  103.         tmp = apt[i].x;
  104.         apt[i].x = -apt[i].y;
  105.         apt[i].y = tmp;
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement