@@ -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