Advertisement
Amorf

Untitled

Dec 14th, 2021
775
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.66 KB | None | 0 0
  1. #include "Piece.h"
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. Piece::Piece(int pieceId, int pieceRotation, COLORREF pieceColor,
  7.              const POINT *apt, int numPoints) :
  8. id(pieceId), rotation(pieceRotation), nPoints(numPoints),
  9. color(pieceColor), width(0), height(0)
  10. {
  11.     POINT bottomLeft = apt[0];
  12.  
  13.     for (int i = 1; i < nPoints; i++)
  14.     {
  15.         bottomLeft.x = min(apt[i].x, bottomLeft.x);
  16.         bottomLeft.y = min(apt[i].y, bottomLeft.y);
  17.     }
  18.  
  19.     body = new POINT[nPoints];
  20.  
  21.     for (int i = 0; i < nPoints; i++)
  22.     {
  23.         body[i].x = apt[i].x - bottomLeft.x;
  24.         body[i].y = apt[i].y - bottomLeft.y;
  25.  
  26.         width = max((int)body[i].x + 1, width);
  27.         height = max((int)body[i].y + 1, height);
  28.     }
  29. }
  30.  
  31. Piece::~Piece()
  32. {
  33.     if (body)
  34.         delete [] body;
  35. }
  36.  
  37. void Piece::getBody(POINT *apt) const
  38. {
  39.     for (int i = 0; i < nPoints; i++)
  40.         apt[i] = body[i];
  41. }
  42.  
  43. int Piece::getSkirt(POINT *apt) const
  44. {
  45.     int i = 0;
  46.     for (int x = 0; x < width; x++)
  47.     {
  48.         for (int y = 0; y < height; y++)
  49.         {
  50.             if (isPointExists(x, y))
  51.             {
  52.                 apt[i].x = x;
  53.                 apt[i].y = y;
  54.                 i++;
  55.                 break;
  56.             }
  57.         }
  58.     }
  59.     return i;
  60. }
  61.  
  62. int Piece::getLeftSide(POINT *apt) const
  63. {
  64.     int i = 0;
  65.     for (int y = 0; y < height; y++)
  66.     {
  67.         for (int x = 0; x < height; x++)
  68.         {
  69.             if (isPointExists(x, y))
  70.             {
  71.                 apt[i].x = x;
  72.                 apt[i].y = y;
  73.                 i++;
  74.                 break;
  75.             }
  76.         }
  77.     }
  78.     return i;
  79. }
  80.  
  81. int Piece::getRightSide(POINT *apt) const
  82. {
  83.     int i = 0;
  84.     for (int y = 0; y < height; y++)
  85.     {
  86.         for (int x = width - 1; x >= 0; x--)
  87.         {
  88.             if (isPointExists(x, y))
  89.             {
  90.                 apt[i].x = x;
  91.                 apt[i].y = y;
  92.                 i++;
  93.                 break;
  94.             }
  95.         }
  96.     }
  97.     return i;
  98. }
  99.  
  100. void Piece::print() const
  101. {
  102.     cout << "width = " << width << endl;
  103.     cout << "height = " << height << endl;
  104.     cout << "nPoints = " << nPoints << endl;
  105.     cout << "color = " << hex << color << endl;
  106.     for (int y = height - 1; y >= 0; y--)
  107.     {
  108.         for (int x = 0; x < width; x++)
  109.         {
  110.             if (isPointExists(x, y))
  111.                 cout << "#";
  112.             else
  113.                 cout << " ";
  114.         }
  115.         cout << endl;
  116.     }
  117. }
  118.  
  119. bool Piece::isPointExists(int x, int y) const
  120. {
  121.     for (int i = 0; i < 4; i++)
  122.     {
  123.         if (body[i].x == x && body[i].y == y)
  124.             return true;
  125.     }
  126.     return false;
  127. }
  128.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement