OOP-Projects-Java

Log | Files | Refs | README

commit 7a9eb7fc5c31585d8238c474bc578b7d463f688e
parent f683a6c87cc04be40644c77044af4c52098ccec3
Author: William Lindholm <William_Lindholm@outlook.com>
Date:   Thu, 23 Mar 2023 20:58:50 +0100

Added comments

Diffstat:
MOOP2023_a22willi_a21liltr_assignment4/src/blocks/I_Block.java | 2+-
MOOP2023_a22willi_a21liltr_assignment4/src/blocks/J_Block.java | 3++-
MOOP2023_a22willi_a21liltr_assignment4/src/blocks/Poly.java | 54++++++++++++++++++++++++++++++++++++++++++++++++++----
MOOP2023_a22willi_a21liltr_assignment4/src/blocks/Tileable.java | 16++++++++++++++++
MOOP2023_a22willi_a21liltr_assignment4/src/game/BlockFactory.java | 20+++++++++++++++++++-
MOOP2023_a22willi_a21liltr_assignment4/src/game/GUI.java | 35++++++++++++++++++++++++++---------
MOOP2023_a22willi_a21liltr_assignment4/src/game/Game.java | 24+++++++++++++++++++-----
MOOP2023_a22willi_a21liltr_assignment4/src/game/Main.java | 4++++
8 files changed, 137 insertions(+), 21 deletions(-)

diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/blocks/I_Block.java b/OOP2023_a22willi_a21liltr_assignment4/src/blocks/I_Block.java @@ -23,7 +23,7 @@ public class I_Block extends Poly { 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 @@ -3,6 +3,7 @@ package blocks; import java.awt.Color; public class J_Block extends Poly { + // holds the information that dictates what shape the object becomes on the // board public int[][] Shape = { { 0, 1 }, { 0, 1 }, { 1, 1 } }; @@ -22,7 +23,7 @@ public class J_Block extends Poly { 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 @@ -8,12 +8,23 @@ public abstract class Poly implements Tileable { private Color color; + /** + * Instantiates a new poly at given coordinates. + * @param x spawn position. + * @param y spawn position. + */ public Poly(int x, int y) { this.posX = x; this.posY = y; occupied = false; } + /** + * Rotates a given matrix clockwise. + * Note: all rows and columns must be of the same cardinality. + * @param shape, the matrix to rotate + * @return the rotated matrix + */ protected int[][] rotateClockwise(int[][] shape) { int h = shape.length; int w = shape[0].length; @@ -26,6 +37,12 @@ public abstract class Poly implements Tileable { return rotatedShape; } + /** + * Rotates a given matrix counter clockwise. + * Note: all rows and columns must be of the same cardinality. + * @param shape, the matrix to rotate + * @return the rotated matrix + */ protected int[][] rotateCounterClockwise(int[][] shape) { int h = shape.length; int w = shape[0].length; @@ -37,38 +54,67 @@ public abstract class Poly implements Tileable { } return rotatedShape; } - + + /** + * Abstract rotate function used by the subclasses of Poly. + */ public abstract void rotateRight(); + /** + * Abstract rotate function used by the subclasses of Poly. + */ public abstract void rotateLeft(); + /** + * Moves the poly in the desired direction. + * @param distance to move in x direction. + * @param distance to move in y direction. + */ public void move(int x, int y) { this.posX += x; this.posY += y; } + /** + * Get the position of an instantiated poly. + * @return + */ public int[] getPos() { int[] pos = { posX, posY }; return pos; } - public int[][] getShape() { - return null; - } + /** + * Get the shape of a Poly, only used by subclasses. + * @return + */ + public abstract int[][] getShape(); + /** + * Set the color of the poly. + */ public void setColor(Color color) { this.color = color; } + /** + * Get the color of the poly. + */ public Color getColor() { // if no other value is set, red will be the default color. return color; } + /** + * @return is it occupied. + */ public boolean isOccupied() { return occupied; } + /** + * Set whether or not the Square is occupied. + */ public void setOccupied(boolean occupied) { this.occupied = occupied; } diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/blocks/Tileable.java b/OOP2023_a22willi_a21liltr_assignment4/src/blocks/Tileable.java @@ -3,12 +3,28 @@ package blocks; import java.awt.Color; public interface Tileable { + /** + * Is the Tile occupied. + * @return occupancy state. + */ public boolean isOccupied(); + /** + * Set if the Tile is occupied or not. + * @param state. + */ void setOccupied(boolean state); + /** + * get the color of the Tile. + * @return Color of tile. + */ public Color getColor(); + /** + * Set the color of the Tile. + * @param color. + */ public void setColor(Color color); diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/game/BlockFactory.java b/OOP2023_a22willi_a21liltr_assignment4/src/game/BlockFactory.java @@ -11,10 +11,20 @@ public class BlockFactory { private static Random random; private int[] spawnPos; + /** + * Initiates the blockFactory. + */ public BlockFactory() { random = new Random(); } + /** + * Generates a Poly of given type and spawns it at desired position. + * Note: the spawn needs to be set before calling this function. + * + * @param type + * @return Poly. + */ public Poly generatePoly(int type) { switch (type) { case 0: @@ -35,11 +45,19 @@ public class BlockFactory { return new O_Block(spawnPos[0], spawnPos[1]); } } - + + /** + * Sets the spawnpoint for newly created Polys. + * @param spawnPos + */ public void setSpawn(int[] spawnPos) { this.spawnPos = spawnPos; } + /** + * Return a Poly of random type. + * @return Poly. + */ public Poly generateRandomPoly() { return generatePoly(random.nextInt(0, 7)); } diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/game/GUI.java b/OOP2023_a22willi_a21liltr_assignment4/src/game/GUI.java @@ -13,6 +13,10 @@ public class GUI extends JFrame implements KeyListener { Board board; JFrame frame; + /** + * Instantiates the GUI and creates a JFrame / window on which the game is played. + * @param board + */ public GUI(Board board) { this.board = board; frame = new JFrame("Tetris"); @@ -32,36 +36,43 @@ public class GUI extends JFrame implements KeyListener { } + /** + * Handle all of the user input. + */ @Override - public void keyPressed(KeyEvent e) { switch (e.getKeyCode()) { - // spaceBar + // instantly move the falling Poly to the lowest possible position. + // Spacebar. case 32: if (board.getFallingPoly() != null) { board.instaFall(board.getFallingPoly()); } break; - // left + // move the currently falling poly to the left. + // left arrow key. case 37: movePoly(-1, 0); break; - // right + // move the currently falling poly to the right. + // right arrow key. case 39: movePoly(1, 0); break; - // rotate + // rotate the falling poly. + // up arrow key. case 38: if (board.getFallingPoly() != null) { board.getFallingPoly().rotateRight(); - - //if the rotate is not legal, undo rotation. + + // if the rotate is not legal, undo rotation. if (!(board.isLegal(board.getFallingPoly(), 0, 0))) { board.getFallingPoly().rotateLeft(); } } break; - // slow fall + // slow fall. + // down arrow key. case 40: movePoly(0, 1); break; @@ -75,7 +86,13 @@ public class GUI extends JFrame implements KeyListener { // TODO Auto-generated method stub } - + + /** + * checks if there is a falling poly, + * then moves it to the desired position. + * @param x + * @param y + */ private void movePoly(int x, int y) { if (board.getFallingPoly() != null) { board.move(board.getFallingPoly(), x, y); diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/game/Game.java b/OOP2023_a22willi_a21liltr_assignment4/src/game/Game.java @@ -11,16 +11,20 @@ public class Game extends Thread { private Board board; private Timer timer; + //gamespeed variables. private int tickSpeed; private final int maxTickSpeed = 250; private final int minTickSpeed = 50; BlockFactory factory; - private Poly poly; + private Poly fallingPoly; + /** + * Initiate game. + * @param The board on which the game shall be played out. + */ public Game(Board board) { - tickSpeed = maxTickSpeed; timer = new Timer(); @@ -29,6 +33,9 @@ public class Game extends Thread { factory.setSpawn(board.getPrefferedSpawn()); } + /** + * Run the game thread. + */ public void run() { while (!(board.hasLost())) { calculateTickSpeed(); @@ -44,21 +51,28 @@ public class Game extends Thread { board.detectFullRow(); board.repaint(); + // if there is a falling Poly move it one step down every iteration + // otherwise spawn a new Poly. if (board.isFalling()) { - board.fall(poly); + board.fall(fallingPoly); } else { board.givePoints(1); - poly = factory.generateRandomPoly(); - board.addPoly(poly); + fallingPoly = factory.generateRandomPoly(); + board.addPoly(fallingPoly); } + //after all movement is done, redraw all graphics. board.repaint(); } + //if the user has lost, display the number of points collected by the user and exit the game. System.out.println("You lost"); JOptionPane.showMessageDialog(null, "You scored: " + board.getScore() + " points"); System.exit(0); } + /** + * Calculate the current speed at which the game should run. + */ private void calculateTickSpeed() { if (tickSpeed >= minTickSpeed) { tickSpeed = maxTickSpeed - (board.getScore() / 2); diff --git a/OOP2023_a22willi_a21liltr_assignment4/src/game/Main.java b/OOP2023_a22willi_a21liltr_assignment4/src/game/Main.java @@ -5,6 +5,10 @@ import java.awt.event.ActionListener; public class Main { + /** + * Starts all the necessary components for the game to run. + * @param args + */ public static void main(String[] args) { Board board = new Board(); GUI gui = new GUI(board);