commit 5c835ceae0233601d3aeac3b47f989a237535473
parent a5b70527fa17be6df9730ee909190eb25e34cdc1
Author: William Lindholm <william_lindholm@outlook.com>
Date: Tue, 21 Mar 2023 23:21:08 +0100
Now prevents illegal rotations
Diffstat:
10 files changed, 90 insertions(+), 42 deletions(-)
diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/blocks/I_Block.java b/OOP2023_a22willi_a21liltr_assignment4/src/blocks/I_Block.java
@@ -20,7 +20,12 @@ public class I_Block extends Poly {
}
@Override
- public void rotate() {
- Shape = super.rotateRight(Shape);
+ public void rotateRight() {
+ Shape = super.rotateClockwise(Shape);
+ }
+
+ @Override
+ public void rotateLeft() {
+ Shape = super.rotateCounterClockwise(Shape);
}
}
diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/blocks/J_Block.java b/OOP2023_a22willi_a21liltr_assignment4/src/blocks/J_Block.java
@@ -19,7 +19,12 @@ public class J_Block extends Poly {
}
@Override
- public void rotate() {
- Shape = super.rotateRight(Shape);
+ public void rotateRight() {
+ Shape = super.rotateClockwise(Shape);
+ }
+
+ @Override
+ public void rotateLeft() {
+ Shape = super.rotateCounterClockwise(Shape);
}
}
diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/blocks/L_Block.java b/OOP2023_a22willi_a21liltr_assignment4/src/blocks/L_Block.java
@@ -19,7 +19,12 @@ public class L_Block extends Poly {
}
@Override
- public void rotate() {
- Shape = super.rotateRight(Shape);
+ public void rotateRight() {
+ Shape = super.rotateClockwise(Shape);
+ }
+
+ @Override
+ public void rotateLeft() {
+ Shape = super.rotateCounterClockwise(Shape);
}
}
diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/blocks/O_Block.java b/OOP2023_a22willi_a21liltr_assignment4/src/blocks/O_Block.java
@@ -19,7 +19,12 @@ public class O_Block extends Poly {
}
@Override
- public void rotate() {
- Shape = super.rotateRight(Shape);
+ public void rotateRight() {
+ Shape = super.rotateClockwise(Shape);
+ }
+
+ @Override
+ public void rotateLeft() {
+ Shape = super.rotateCounterClockwise(Shape);
}
}
diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/blocks/Poly.java b/OOP2023_a22willi_a21liltr_assignment4/src/blocks/Poly.java
@@ -14,7 +14,7 @@ public abstract class Poly implements Tileable {
occupied = false;
}
- public int[][] rotateRight(int[][] shape) {
+ public int[][] rotateClockwise(int[][] shape) {
int h = shape.length;
int w = shape[0].length;
int[][] rotatedShape = new int[w][h];
@@ -26,7 +26,21 @@ public abstract class Poly implements Tileable {
return rotatedShape;
}
- public abstract void rotate();
+ public int[][] rotateCounterClockwise(int[][] shape) {
+ int h = shape.length;
+ int w = shape[0].length;
+ int[][] rotatedShape = new int[w][h];
+ for (int y = 0; y < h; y++) {
+ for (int x = 0; x < w; x++) {
+ rotatedShape[w - 1 - x][y] = shape[y][x];
+ }
+ }
+ return rotatedShape;
+ }
+
+ public abstract void rotateRight();
+
+ public abstract void rotateLeft();
public void move(int x, int y) {
this.posX += x;
@@ -54,13 +68,8 @@ public abstract class Poly implements Tileable {
public boolean isOccupied() {
return occupied;
}
-
+
public void setOccupied(boolean occupied) {
this.occupied = occupied;
}
-
- public int[][] rotateRight() {
- // TODO Auto-generated method stub
- return null;
- }
}
diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/blocks/S_Block.java b/OOP2023_a22willi_a21liltr_assignment4/src/blocks/S_Block.java
@@ -19,7 +19,12 @@ public class S_Block extends Poly {
}
@Override
- public void rotate() {
- Shape = super.rotateRight(Shape);
+ public void rotateRight() {
+ Shape = super.rotateClockwise(Shape);
+ }
+
+ @Override
+ public void rotateLeft() {
+ Shape = super.rotateCounterClockwise(Shape);
}
}
diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/blocks/T_Block.java b/OOP2023_a22willi_a21liltr_assignment4/src/blocks/T_Block.java
@@ -19,7 +19,12 @@ public class T_Block extends Poly {
}
@Override
- public void rotate() {
- Shape = super.rotateRight(Shape);
+ public void rotateRight() {
+ Shape = super.rotateClockwise(Shape);
+ }
+
+ @Override
+ public void rotateLeft() {
+ Shape = super.rotateCounterClockwise(Shape);
}
}
diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/blocks/Z_Block.java b/OOP2023_a22willi_a21liltr_assignment4/src/blocks/Z_Block.java
@@ -21,7 +21,12 @@ public class Z_Block extends Poly {
}
@Override
- public void rotate() {
- Shape = super.rotateRight(Shape);
+ public void rotateRight() {
+ Shape = super.rotateClockwise(Shape);
+ }
+
+ @Override
+ public void rotateLeft() {
+ Shape = super.rotateCounterClockwise(Shape);
}
}
diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/game/Board.java b/OOP2023_a22willi_a21liltr_assignment4/src/game/Board.java
@@ -95,7 +95,7 @@ public class Board extends JPanel {
return false;
}
- private boolean isLegal(Poly poly, int x, int y) {
+ public boolean isLegal(Poly poly, int x, int y) {
int[][] shape = poly.getShape();
int[] position = poly.getPos();
@@ -105,24 +105,22 @@ public class Board extends JPanel {
int realX = position[0] + j;
int realY = position[1] + i;
if (shape[i][j] == 1) {
+
// check X direction
- if (x != 0) {
- if (realX + x >= grid.length || realX + x < 0) {
- return false;
- }
- if (grid[realX + x][realY].isOccupied()) {
- return false;
- }
+ if (realX + x >= grid.length || realX + x < 0) {
+ return false;
}
+ if (grid[realX + x][realY].isOccupied()) {
+ return false;
+ }
+
// check y direction
- if (y != 0) {
- if (realY + y >= grid[0].length - 1) {
- return false;
- }
- if (grid[realX][realY + y].isOccupied()) {
- return false;
- }
+ if (realY + y >= grid[0].length - 1) {
+ return false;
+ }
+ if (grid[realX][realY + y].isOccupied()) {
+ return false;
}
}
}
@@ -138,7 +136,7 @@ public class Board extends JPanel {
return null;
}
- public void freeze(Poly poly) {
+ private void freeze(Poly poly) {
int[][] shape = poly.getShape();
int[] position = poly.getPos();
for (int i = 0; i < shape.length; i++) {
@@ -258,11 +256,11 @@ public class Board extends JPanel {
if (getFallingPoly() != null) {
Poly poly = getFallingPoly();
int[][] shape = poly.getShape();
- g.drawLine(poly.getPos()[0] * tileSize + (shape[0].length * tileSize),
+ g.drawLine(poly.getPos()[0] * tileSize + (shape[0].length * tileSize) - 1,
poly.getPos()[1] * tileSize + shape.length * tileSize,
- poly.getPos()[0] * tileSize + (shape[0].length * tileSize), size.height);
+ poly.getPos()[0] * tileSize + (shape[0].length * tileSize) - 1, size.height - tileSize * 2);
g.drawLine(poly.getPos()[0] * tileSize, poly.getPos()[1] * tileSize + shape.length * tileSize,
- poly.getPos()[0] * tileSize, size.height);
+ poly.getPos()[0] * tileSize, size.height - tileSize * 2);
}
}
diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/game/GUI.java b/OOP2023_a22willi_a21liltr_assignment4/src/game/GUI.java
@@ -53,8 +53,14 @@ public class GUI extends JFrame implements KeyListener {
// rotate
case 38:
if (board.getFallingPoly() != null) {
- board.getFallingPoly().rotate();
+ board.getFallingPoly().rotateRight();
+
+ //if the rotate is not legal, undo rotation.
+ if (!(board.isLegal(board.getFallingPoly(), 0, 0))) {
+ board.getFallingPoly().rotateLeft();
+ }
}
+ break;
// slow fall
case 40:
movePoly(0, 1);