Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 10 additions & 58 deletions src/Level.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,17 +107,18 @@ bool Level::rotate()

bool Level::move(int cxDistance, int cyDistance)
{
if (posX + cxDistance < 0 || posY + cyDistance < 0 ||
posX + current->getWidth() + cxDistance > width)
return false;
if (cxDistance < 0 && isHitLeft())
return false;
if (cxDistance > 0 && isHitRight())
return false;
if (cyDistance < 0 && isHitBottom())
int newX = posX + cxDistance;
int newY = posY + cyDistance;
if (newX < 0 || newY < 0 ||
newX + current->getWidth() > width)
return false;
clear(*current);
return place(posX + cxDistance, posY + cyDistance, *current);
if (!isCovered(*current, newX, newY)) {
place(newX, newY, *current);
return true;
}
place(posX, posY, *current);
return false;
}

void Level::clear(const Piece &piece)
Expand All @@ -141,55 +142,6 @@ void Level::dropRandomPiece()
place(3, height - 1, *current);
}

bool Level::isHitBottom() const
{
POINT apt[4];
int n = current->getSkirt(apt);
int x, y;
for (int i = 0; i < n; i++)
{
x = posX + apt[i].x;
y = posY + apt[i].y;
if (y < height && (y == 0 || board[x][y-1] != RGB(0,0,0)))
return true;
}
return false;
}

bool Level::isHitLeft() const
{
POINT apt[4];
int n = current->getLeftSide(apt);
int x, y;
for (int i = 0; i < n; i++)
{
x = posX + apt[i].x;
y = posY + apt[i].y;
if (y > height - 1)
continue;
if (x == 0 || board[x-1][y] != RGB(0,0,0))
return true;
}
return false;
}

bool Level::isHitRight() const
{
POINT apt[4];
int n = current->getRightSide(apt);
int x, y;
for (int i = 0; i < n; i++)
{
x = posX + apt[i].x;
y = posY + apt[i].y;
if (y > height - 1)
continue;
if (x == width - 1 || board[x+1][y] != RGB(0,0,0))
return true;
}
return false;
}

bool Level::isCovered(const Piece &piece, int x, int y) const
{
POINT apt[4];
Expand Down
5 changes: 0 additions & 5 deletions src/Level.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,6 @@ class Level
// Releases the next dropping piece
void dropRandomPiece();

// Checks if the piece hits the boundary
bool isHitBottom() const;
bool isHitLeft() const;
bool isHitRight() const;

// Checks if a piece can move to a position
bool isCovered(const Piece &piece, int x, int y) const;

Expand Down
57 changes: 0 additions & 57 deletions src/Piece.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,63 +40,6 @@ void Piece::getBody(POINT *apt) const
apt[i] = body[i];
}

int Piece::getSkirt(POINT *apt) const
{
int i = 0;
for (int x = 0; x < width; x++)
{
for (int y = 0; y < height; y++)
{
if (isPointExists(x, y))
{
apt[i].x = x;
apt[i].y = y;
i++;
break;
}
}
}
return i;
}

int Piece::getLeftSide(POINT *apt) const
{
int i = 0;
for (int y = 0; y < height; y++)
{
for (int x = 0; x < height; x++)
{
if (isPointExists(x, y))
{
apt[i].x = x;
apt[i].y = y;
i++;
break;
}
}
}
return i;
}

int Piece::getRightSide(POINT *apt) const
{
int i = 0;
for (int y = 0; y < height; y++)
{
for (int x = width - 1; x >= 0; x--)
{
if (isPointExists(x, y))
{
apt[i].x = x;
apt[i].y = y;
i++;
break;
}
}
}
return i;
}

void Piece::print() const
{
cout << "width = " << width << endl;
Expand Down
5 changes: 0 additions & 5 deletions src/Piece.h
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,6 @@ class Piece
// Gets the points of the piece
void getBody(POINT *apt) const;

// Gets the bottom, left, or right part of points of the piece
int getSkirt(POINT *apt) const;
int getLeftSide(POINT *apt) const;
int getRightSide(POINT *apt) const;

// Determines if the piece has a point (x, y)
bool isPointExists(int x, int y) const;

Expand Down