Skip to content

Commit ea8836a

Browse files
committed
Refactored
1 parent 30346a8 commit ea8836a

File tree

2 files changed

+31
-24
lines changed

2 files changed

+31
-24
lines changed

src/main/java/tetris/Board.java

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -59,14 +59,7 @@ public char cellAt(int row, int col) {
5959

6060
private char fallingCellAt(int row, int col) {
6161
if (hasFalling()) {
62-
int pieceRow = row - falling.rowOffset;
63-
int pieceCol = col - falling.colOffset;
64-
if (pieceRow >= 0
65-
&& pieceRow < falling.rows()
66-
&& pieceCol >= 0
67-
&& pieceCol < falling.columns()) {
68-
return falling.cellAt(pieceRow, pieceCol);
69-
}
62+
return falling.boardCellAt(row, col);
7063
}
7164
return EMPTY;
7265
}

src/main/java/tetris/MovableGrid.java

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,14 @@ public MovableGrid(Grid shape, int rowOffset, int colOffset) {
1717
}
1818

1919
public boolean isOutside(Board board) {
20-
for (int row = 0; row < rows(); row++) {
21-
for (int col = 0; col < columns(); col++) {
22-
if (hasCellAt(row, col)) {
23-
if (colOffset + col < 0
24-
|| colOffset + col >= board.columns()
25-
|| rowOffset + row >= board.rows()) {
20+
for (int myRow = 0; myRow < rows(); myRow++) {
21+
for (int myCol = 0; myCol < columns(); myCol++) {
22+
if (hasCellAt(myRow, myCol)) {
23+
int boardRow = rowOffset + myRow;
24+
int boardCol = colOffset + myCol;
25+
if (boardCol < 0
26+
|| boardCol >= board.columns()
27+
|| boardRow >= board.rows()) {
2628
return true;
2729
}
2830
}
@@ -32,11 +34,11 @@ public boolean isOutside(Board board) {
3234
}
3335

3436
public boolean collidesWith(char[][] board) {
35-
for (int row = 0; row < rows(); row++) {
36-
for (int col = 0; col < columns(); col++) {
37-
if (hasCellAt(row, col)) {
38-
int boardRow = rowOffset + row;
39-
int boardCol = colOffset + col;
37+
for (int myRow = 0; myRow < rows(); myRow++) {
38+
for (int myCol = 0; myCol < columns(); myCol++) {
39+
if (hasCellAt(myRow, myCol)) {
40+
int boardRow = rowOffset + myRow;
41+
int boardCol = colOffset + myCol;
4042
if (board[boardRow][boardCol] != EMPTY) {
4143
return true;
4244
}
@@ -46,6 +48,23 @@ public boolean collidesWith(char[][] board) {
4648
return false;
4749
}
4850

51+
public char boardCellAt(int boardRow, int boardCol) {
52+
int myRow = boardRow - rowOffset;
53+
int myCol = boardCol - colOffset;
54+
if (myRow >= 0
55+
&& myRow < rows()
56+
&& myCol >= 0
57+
&& myCol < columns()) {
58+
return cellAt(myRow, myCol);
59+
}
60+
return EMPTY;
61+
}
62+
63+
@Override
64+
public char cellAt(int row, int col) {
65+
return shape.cellAt(row, col);
66+
}
67+
4968
@Override
5069
public int rows() {
5170
return shape.rows();
@@ -56,11 +75,6 @@ public int columns() {
5675
return shape.columns();
5776
}
5877

59-
@Override
60-
public char cellAt(int row, int col) {
61-
return shape.cellAt(row, col);
62-
}
63-
6478
public MovableGrid moveDown() {
6579
return new MovableGrid(shape, rowOffset + 1, colOffset);
6680
}

0 commit comments

Comments
 (0)