From f97bf5de9895776ca4668b5e31ab3a88a12b0f45 Mon Sep 17 00:00:00 2001 From: Oshgnacknak Date: Sat, 11 Jan 2025 16:41:13 +0100 Subject: [PATCH] add symlinks for marathon --- solution/H00/src/main | 1 + solution/H00/src/main/java/h00/Main.java | 121 ------ solution/H00/src/test | 1 + .../src/test/java/h00/ExampleJUnitTest.java | 16 - solution/H01/src/main | 1 + solution/H01/src/main/java/h01/BlueGhost.java | 50 --- .../H01/src/main/java/h01/GameController.java | 23 -- solution/H01/src/main/java/h01/Main.java | 16 - .../H01/src/main/java/h01/OrangeGhost.java | 53 --- solution/H01/src/main/java/h01/Pacman.java | 74 ---- solution/H01/src/main/java/h01/PinkGhost.java | 50 --- solution/H01/src/main/java/h01/RedGhost.java | 57 --- .../main/java/h01/template/Controllable.java | 5 - .../src/main/java/h01/template/Families.java | 36 -- .../h01/template/GameControllerTemplate.java | 378 ----------------- .../java/h01/template/GameInputHandler.java | 108 ----- .../H01/src/main/java/h01/template/Ghost.java | 5 - .../src/main/java/h01/template/TickBased.java | 15 - .../H01/src/main/java/h01/template/Util.java | 55 --- .../src/main/resources/robots/ghost_blue.svg | 67 --- .../main/resources/robots/ghost_orange.svg | 31 -- .../src/main/resources/robots/ghost_pink.svg | 67 --- .../src/main/resources/robots/ghost_red.svg | 67 --- .../H01/src/main/resources/robots/pacman.svg | 61 --- solution/H01/src/test | 1 + .../src/test/java/h01/ExampleJUnitTest.java | 16 - solution/H02/src/main | 1 + solution/H02/src/main/java/h02/FourWins.java | 388 ------------------ solution/H02/src/main/java/h02/Main.java | 218 ---------- .../java/h02/OneDimensionalArrayStuff.java | 64 --- .../java/h02/TwoDimensionalArrayStuff.java | 89 ---- .../main/java/h02/template/InputHandler.java | 201 --------- .../H02/src/main/resources/h02.properties | 2 - solution/H02/src/test | 1 + .../src/test/java/h02/ExampleJUnitTest.java | 16 - solution/H03/src/main | 1 + solution/H03/src/main/java/h03/Main.java | 70 ---- .../src/main/java/h03/RobotsChallenge.java | 88 ---- .../java/h03/robots/DoublePowerRobot.java | 57 --- .../main/java/h03/robots/HackingRobot.java | 111 ----- .../main/java/h03/robots/MovementType.java | 21 - .../main/java/h03/robots/VersatileRobot.java | 59 --- solution/H03/src/test | 1 + .../src/test/java/h03/ExampleJUnitTest.java | 16 - solution/H04/src/main | 1 + .../H04/src/main/java/h04/GameController.java | 18 - solution/H04/src/main/java/h04/Main.java | 17 - .../src/main/java/h04/chesspieces/Bishop.java | 29 -- .../main/java/h04/chesspieces/ChessPiece.java | 20 - .../main/java/h04/chesspieces/Families.java | 81 ---- .../src/main/java/h04/chesspieces/King.java | 45 -- .../src/main/java/h04/chesspieces/Knight.java | 44 -- .../src/main/java/h04/chesspieces/Pawn.java | 54 --- .../src/main/java/h04/chesspieces/Queen.java | 33 -- .../src/main/java/h04/chesspieces/Rook.java | 32 -- .../src/main/java/h04/chesspieces/Team.java | 25 -- .../main/java/h04/movement/DiagonalMover.java | 21 - .../main/java/h04/movement/MoveStrategy.java | 8 - .../java/h04/movement/OrthogonalMover.java | 21 - .../h04/movement/TeleportingMoveStrategy.java | 10 - .../h04/movement/WalkingMoveStrategy.java | 40 -- .../main/java/h04/template/ChessUtils.java | 117 ------ .../h04/template/GameControllerTemplate.java | 179 -------- .../main/java/h04/template/InputHandler.java | 68 --- .../H04/src/main/resources/h04.properties | 2 - .../main/resources/pieces/bishop_black.svg | 46 --- .../main/resources/pieces/bishop_white.svg | 47 --- .../src/main/resources/pieces/king_black.svg | 46 --- .../src/main/resources/pieces/king_white.svg | 47 --- .../main/resources/pieces/knight_black.svg | 46 --- .../main/resources/pieces/knight_white.svg | 47 --- .../src/main/resources/pieces/pawn_black.svg | 46 --- .../src/main/resources/pieces/pawn_white.svg | 47 --- .../src/main/resources/pieces/queen_black.svg | 46 --- .../src/main/resources/pieces/queen_white.svg | 47 --- .../src/main/resources/pieces/rook_black.svg | 46 --- .../src/main/resources/pieces/rook_white.svg | 47 --- solution/H04/src/test | 1 + .../src/test/java/h04/ExampleJUnitTest.java | 16 - solution/H05/src/main | 1 + solution/H05/src/main/java/h05/Airspace.java | 57 --- .../H05/src/main/java/h05/CargoPlane.java | 34 -- .../H05/src/main/java/h05/CargoStack.java | 27 -- .../H05/src/main/java/h05/CarriesCargo.java | 8 - .../src/main/java/h05/CarriesPassengers.java | 7 - .../H05/src/main/java/h05/CombinedPlane.java | 32 -- solution/H05/src/main/java/h05/Flying.java | 5 - solution/H05/src/main/java/h05/FuelType.java | 14 - solution/H05/src/main/java/h05/Main.java | 69 ---- .../H05/src/main/java/h05/PassengerPlane.java | 36 -- solution/H05/src/main/java/h05/Plane.java | 77 ---- .../H05/src/main/java/h05/Refuelling.java | 5 - solution/H05/src/main/java/h05/Runway.java | 41 -- solution/H05/src/main/java/h05/Tank.java | 21 - .../H05/src/main/java/h05/TankerPlane.java | 35 -- .../H05/src/main/java/h05/WeatherBalloon.java | 23 -- solution/H05/src/test | 1 + .../src/test/java/h05/ExampleJUnitTest.java | 16 - solution/H06/src/main | 1 + solution/H06/src/main/java/h06/Main.java | 28 -- .../src/main/java/h06/problems/Fibonacci.java | 71 ---- .../src/main/java/h06/problems/Fractals.java | 155 ------- .../main/java/h06/problems/LinearSearch.java | 54 --- .../src/main/java/h06/ui/DrawInstruction.java | 15 - .../src/main/java/h06/ui/FractalDrawer.java | 103 ----- .../main/java/h06/ui/FractalVisualizer.java | 32 -- solution/H06/src/test | 1 + .../src/test/java/h06/ExampleJUnitTest.java | 16 - solution/H07/src/main | 1 + .../main/java/h07/ArithmeticExpression.java | 18 - .../h07/ConvertNumberToPeanoExpression.java | 18 - .../ConvertNumberToPeanoExpressionImpl.java | 23 -- .../h07/ConvertPeanoToNumberExpression.java | 18 - .../ConvertPeanoToNumberExpressionImpl.java | 23 -- solution/H07/src/main/java/h07/Main.java | 78 ---- .../src/main/java/h07/NumberExpression.java | 16 - .../java/h07/NumberExpressionFactory.java | 89 ---- .../main/java/h07/peano/NaturalNumber.java | 17 - .../java/h07/peano/PeanoAddExpression.java | 27 -- .../h07/peano/PeanoArithmeticExpression.java | 18 - .../h07/peano/PeanoMultiplyExpression.java | 30 -- .../java/h07/peano/PeanoNumberExpression.java | 16 - .../peano/PeanoNumberExpressionFactory.java | 44 -- .../src/main/java/h07/peano/Successor.java | 20 - .../H07/src/main/java/h07/peano/Zero.java | 14 - solution/H07/src/test | 1 + .../src/test/java/h07/ExampleJUnitTest.java | 16 - 127 files changed, 16 insertions(+), 5675 deletions(-) create mode 120000 solution/H00/src/main delete mode 100644 solution/H00/src/main/java/h00/Main.java create mode 120000 solution/H00/src/test delete mode 100644 solution/H00/src/test/java/h00/ExampleJUnitTest.java create mode 120000 solution/H01/src/main delete mode 100644 solution/H01/src/main/java/h01/BlueGhost.java delete mode 100644 solution/H01/src/main/java/h01/GameController.java delete mode 100644 solution/H01/src/main/java/h01/Main.java delete mode 100644 solution/H01/src/main/java/h01/OrangeGhost.java delete mode 100644 solution/H01/src/main/java/h01/Pacman.java delete mode 100644 solution/H01/src/main/java/h01/PinkGhost.java delete mode 100644 solution/H01/src/main/java/h01/RedGhost.java delete mode 100644 solution/H01/src/main/java/h01/template/Controllable.java delete mode 100644 solution/H01/src/main/java/h01/template/Families.java delete mode 100644 solution/H01/src/main/java/h01/template/GameControllerTemplate.java delete mode 100644 solution/H01/src/main/java/h01/template/GameInputHandler.java delete mode 100644 solution/H01/src/main/java/h01/template/Ghost.java delete mode 100644 solution/H01/src/main/java/h01/template/TickBased.java delete mode 100644 solution/H01/src/main/java/h01/template/Util.java delete mode 100644 solution/H01/src/main/resources/robots/ghost_blue.svg delete mode 100644 solution/H01/src/main/resources/robots/ghost_orange.svg delete mode 100644 solution/H01/src/main/resources/robots/ghost_pink.svg delete mode 100644 solution/H01/src/main/resources/robots/ghost_red.svg delete mode 100644 solution/H01/src/main/resources/robots/pacman.svg create mode 120000 solution/H01/src/test delete mode 100644 solution/H01/src/test/java/h01/ExampleJUnitTest.java create mode 120000 solution/H02/src/main delete mode 100644 solution/H02/src/main/java/h02/FourWins.java delete mode 100644 solution/H02/src/main/java/h02/Main.java delete mode 100644 solution/H02/src/main/java/h02/OneDimensionalArrayStuff.java delete mode 100644 solution/H02/src/main/java/h02/TwoDimensionalArrayStuff.java delete mode 100644 solution/H02/src/main/java/h02/template/InputHandler.java delete mode 100644 solution/H02/src/main/resources/h02.properties create mode 120000 solution/H02/src/test delete mode 100644 solution/H02/src/test/java/h02/ExampleJUnitTest.java create mode 120000 solution/H03/src/main delete mode 100644 solution/H03/src/main/java/h03/Main.java delete mode 100644 solution/H03/src/main/java/h03/RobotsChallenge.java delete mode 100644 solution/H03/src/main/java/h03/robots/DoublePowerRobot.java delete mode 100644 solution/H03/src/main/java/h03/robots/HackingRobot.java delete mode 100644 solution/H03/src/main/java/h03/robots/MovementType.java delete mode 100644 solution/H03/src/main/java/h03/robots/VersatileRobot.java create mode 120000 solution/H03/src/test delete mode 100644 solution/H03/src/test/java/h03/ExampleJUnitTest.java create mode 120000 solution/H04/src/main delete mode 100644 solution/H04/src/main/java/h04/GameController.java delete mode 100644 solution/H04/src/main/java/h04/Main.java delete mode 100644 solution/H04/src/main/java/h04/chesspieces/Bishop.java delete mode 100644 solution/H04/src/main/java/h04/chesspieces/ChessPiece.java delete mode 100644 solution/H04/src/main/java/h04/chesspieces/Families.java delete mode 100644 solution/H04/src/main/java/h04/chesspieces/King.java delete mode 100644 solution/H04/src/main/java/h04/chesspieces/Knight.java delete mode 100644 solution/H04/src/main/java/h04/chesspieces/Pawn.java delete mode 100644 solution/H04/src/main/java/h04/chesspieces/Queen.java delete mode 100644 solution/H04/src/main/java/h04/chesspieces/Rook.java delete mode 100644 solution/H04/src/main/java/h04/chesspieces/Team.java delete mode 100644 solution/H04/src/main/java/h04/movement/DiagonalMover.java delete mode 100644 solution/H04/src/main/java/h04/movement/MoveStrategy.java delete mode 100644 solution/H04/src/main/java/h04/movement/OrthogonalMover.java delete mode 100644 solution/H04/src/main/java/h04/movement/TeleportingMoveStrategy.java delete mode 100644 solution/H04/src/main/java/h04/movement/WalkingMoveStrategy.java delete mode 100644 solution/H04/src/main/java/h04/template/ChessUtils.java delete mode 100644 solution/H04/src/main/java/h04/template/GameControllerTemplate.java delete mode 100644 solution/H04/src/main/java/h04/template/InputHandler.java delete mode 100644 solution/H04/src/main/resources/h04.properties delete mode 100644 solution/H04/src/main/resources/pieces/bishop_black.svg delete mode 100644 solution/H04/src/main/resources/pieces/bishop_white.svg delete mode 100644 solution/H04/src/main/resources/pieces/king_black.svg delete mode 100644 solution/H04/src/main/resources/pieces/king_white.svg delete mode 100644 solution/H04/src/main/resources/pieces/knight_black.svg delete mode 100644 solution/H04/src/main/resources/pieces/knight_white.svg delete mode 100644 solution/H04/src/main/resources/pieces/pawn_black.svg delete mode 100644 solution/H04/src/main/resources/pieces/pawn_white.svg delete mode 100644 solution/H04/src/main/resources/pieces/queen_black.svg delete mode 100644 solution/H04/src/main/resources/pieces/queen_white.svg delete mode 100644 solution/H04/src/main/resources/pieces/rook_black.svg delete mode 100644 solution/H04/src/main/resources/pieces/rook_white.svg create mode 120000 solution/H04/src/test delete mode 100644 solution/H04/src/test/java/h04/ExampleJUnitTest.java create mode 120000 solution/H05/src/main delete mode 100644 solution/H05/src/main/java/h05/Airspace.java delete mode 100644 solution/H05/src/main/java/h05/CargoPlane.java delete mode 100644 solution/H05/src/main/java/h05/CargoStack.java delete mode 100644 solution/H05/src/main/java/h05/CarriesCargo.java delete mode 100644 solution/H05/src/main/java/h05/CarriesPassengers.java delete mode 100644 solution/H05/src/main/java/h05/CombinedPlane.java delete mode 100644 solution/H05/src/main/java/h05/Flying.java delete mode 100644 solution/H05/src/main/java/h05/FuelType.java delete mode 100644 solution/H05/src/main/java/h05/Main.java delete mode 100644 solution/H05/src/main/java/h05/PassengerPlane.java delete mode 100644 solution/H05/src/main/java/h05/Plane.java delete mode 100644 solution/H05/src/main/java/h05/Refuelling.java delete mode 100644 solution/H05/src/main/java/h05/Runway.java delete mode 100644 solution/H05/src/main/java/h05/Tank.java delete mode 100644 solution/H05/src/main/java/h05/TankerPlane.java delete mode 100644 solution/H05/src/main/java/h05/WeatherBalloon.java create mode 120000 solution/H05/src/test delete mode 100644 solution/H05/src/test/java/h05/ExampleJUnitTest.java create mode 120000 solution/H06/src/main delete mode 100644 solution/H06/src/main/java/h06/Main.java delete mode 100644 solution/H06/src/main/java/h06/problems/Fibonacci.java delete mode 100644 solution/H06/src/main/java/h06/problems/Fractals.java delete mode 100644 solution/H06/src/main/java/h06/problems/LinearSearch.java delete mode 100644 solution/H06/src/main/java/h06/ui/DrawInstruction.java delete mode 100644 solution/H06/src/main/java/h06/ui/FractalDrawer.java delete mode 100644 solution/H06/src/main/java/h06/ui/FractalVisualizer.java create mode 120000 solution/H06/src/test delete mode 100644 solution/H06/src/test/java/h06/ExampleJUnitTest.java create mode 120000 solution/H07/src/main delete mode 100644 solution/H07/src/main/java/h07/ArithmeticExpression.java delete mode 100644 solution/H07/src/main/java/h07/ConvertNumberToPeanoExpression.java delete mode 100644 solution/H07/src/main/java/h07/ConvertNumberToPeanoExpressionImpl.java delete mode 100644 solution/H07/src/main/java/h07/ConvertPeanoToNumberExpression.java delete mode 100644 solution/H07/src/main/java/h07/ConvertPeanoToNumberExpressionImpl.java delete mode 100644 solution/H07/src/main/java/h07/Main.java delete mode 100644 solution/H07/src/main/java/h07/NumberExpression.java delete mode 100644 solution/H07/src/main/java/h07/NumberExpressionFactory.java delete mode 100644 solution/H07/src/main/java/h07/peano/NaturalNumber.java delete mode 100644 solution/H07/src/main/java/h07/peano/PeanoAddExpression.java delete mode 100644 solution/H07/src/main/java/h07/peano/PeanoArithmeticExpression.java delete mode 100644 solution/H07/src/main/java/h07/peano/PeanoMultiplyExpression.java delete mode 100644 solution/H07/src/main/java/h07/peano/PeanoNumberExpression.java delete mode 100644 solution/H07/src/main/java/h07/peano/PeanoNumberExpressionFactory.java delete mode 100644 solution/H07/src/main/java/h07/peano/Successor.java delete mode 100644 solution/H07/src/main/java/h07/peano/Zero.java create mode 120000 solution/H07/src/test delete mode 100644 solution/H07/src/test/java/h07/ExampleJUnitTest.java diff --git a/solution/H00/src/main b/solution/H00/src/main new file mode 120000 index 0000000..a037265 --- /dev/null +++ b/solution/H00/src/main @@ -0,0 +1 @@ +/home/osh/Desktop/FOP-2425-Marathon/H00/src/main \ No newline at end of file diff --git a/solution/H00/src/main/java/h00/Main.java b/solution/H00/src/main/java/h00/Main.java deleted file mode 100644 index bea6d86..0000000 --- a/solution/H00/src/main/java/h00/Main.java +++ /dev/null @@ -1,121 +0,0 @@ -package h00; - -import fopbot.Direction; -import fopbot.Robot; -import fopbot.RobotFamily; -import fopbot.World; - -/** - * Main entry point in executing the program. - */ -public class Main { - - /** - * The subtask to run. - */ - public static int runToSubtask = -1; - - /** - * The world speed in milliseconds. - */ - public static int delay = 300; - - /** - * Main entry point in executing the program. - * - * @param args program arguments, currently ignored - */ - public static void main(String[] args) { - // This sets up the FOPBot World with a 4x4 grid. (DO NOT TOUCH) - setupWorld(); - - // TODO: H0.4 - Initializing FOPBot - Robot kasper = new Robot(0, 0, Direction.DOWN, 4, RobotFamily.SQUARE_BLUE); - Robot alfred = new Robot(0, 3, Direction.DOWN, 0, RobotFamily.SQUARE_GREEN); - - if (runToSubtask == 0) return; // DO NOT TOUCH! - // TODO: H0.5.1 - Turning with repeated instructions - alfred.turnLeft(); - alfred.turnLeft(); - - if (runToSubtask == 1) return; // DO NOT TOUCH! - // TODO: H0.5.2 - Turning with for-loop - int numberOfTurns = 2; - for (int i = 0; i < numberOfTurns; i++) { - alfred.turnLeft(); - } - - if (runToSubtask == 2) return; // DO NOT TOUCH! - // TODO: H0.5.3 - Turning with while-loop - while (!kasper.isFacingRight()) { - kasper.turnLeft(); - } - - if (runToSubtask == 3) return; // DO NOT TOUCH! - // TODO: H0.6.1 - Put with repeated instructions - kasper.putCoin(); - kasper.move(); - kasper.move(); - kasper.move(); - kasper.turnLeft(); - - if (runToSubtask == 4) return; // DO NOT TOUCH! - // TODO: H0.6.2 - Pick with repeated instructions - alfred.move(); - alfred.move(); - alfred.move(); - alfred.pickCoin(); - alfred.turnLeft(); - - if (runToSubtask == 5) return; // DO NOT TOUCH! - // TODO: H0.6.3 - Put with for-loop - kasper.putCoin(); - int numberOfSteps = 3; - for (int i = 0; i < numberOfSteps; i++) { - kasper.move(); - } - kasper.turnLeft(); - - if (runToSubtask == 6) return; // DO NOT TOUCH! - // TODO: H0.7.1 - Pick with while-loop - while (alfred.isFrontClear()) { - alfred.move(); - } - alfred.pickCoin(); - alfred.turnLeft(); - - if (runToSubtask == 7) return; // DO NOT TOUCH! - // TODO: H0.7.2 - Pick and put with while-loop - kasper.putCoin(); - while (alfred.isFrontClear()) { - kasper.move(); - alfred.move(); - } - alfred.pickCoin(); - alfred.turnLeft(); - kasper.turnLeft(); - - if (runToSubtask == 8) return; // DO NOT TOUCH! - // TODO: H0.7.3 - Put with reversed for-loop - for (int i = alfred.getNumberOfCoins(); i > 0; i--) { - alfred.putCoin(); - alfred.move(); - } - alfred.turnLeft(); - } - - public static void setupWorld() { - // variable representing width/size of world - final int worldSize = 4; - - // setting world size symmetrical, meaning height = width - World.setSize(worldSize, worldSize); - - // speed of how fast the world gets refreshed (e.g. how fast the robot(s) act) - // the lower the number, the faster the refresh - World.setDelay(delay); - - // make it possible to see the world window - World.setVisible(true); - } -} diff --git a/solution/H00/src/test b/solution/H00/src/test new file mode 120000 index 0000000..c9fda71 --- /dev/null +++ b/solution/H00/src/test @@ -0,0 +1 @@ +/home/osh/Desktop/FOP-2425-Marathon/H00/src/test \ No newline at end of file diff --git a/solution/H00/src/test/java/h00/ExampleJUnitTest.java b/solution/H00/src/test/java/h00/ExampleJUnitTest.java deleted file mode 100644 index ac6b21a..0000000 --- a/solution/H00/src/test/java/h00/ExampleJUnitTest.java +++ /dev/null @@ -1,16 +0,0 @@ -package h00; - -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.*; - -/** - * An example JUnit test class. - */ -public class ExampleJUnitTest { - - @Test - public void testAddition() { - assertEquals(2, 1 + 1); - } -} diff --git a/solution/H01/src/main b/solution/H01/src/main new file mode 120000 index 0000000..67a296b --- /dev/null +++ b/solution/H01/src/main @@ -0,0 +1 @@ +/home/osh/Desktop/FOP-2425-Marathon/H01/src/main \ No newline at end of file diff --git a/solution/H01/src/main/java/h01/BlueGhost.java b/solution/H01/src/main/java/h01/BlueGhost.java deleted file mode 100644 index 94bb064..0000000 --- a/solution/H01/src/main/java/h01/BlueGhost.java +++ /dev/null @@ -1,50 +0,0 @@ -package h01; - -import org.tudalgo.algoutils.student.annotation.SolutionOnly; -import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired; - -import fopbot.Robot; -import h01.template.Families; -import h01.template.Ghost; -import h01.template.TickBased; - -/** - * The {@link BlueGhost} is a {@link Robot} that looks like a blue ghost. - * It tries to move in a circle. - */ -public class BlueGhost extends Robot implements Ghost, TickBased { - /** - * Creates a new {@link BlueGhost} at the given position. - * - * @param x the x-coordinate - * @param y the y-coordinate - */ - public BlueGhost(int x, int y) { - super(x, y, Families.GHOST_BLUE); - } - - /** - * Turns the robot to the right and then moves in the first direction that is - * clear. - */ - @Override - @StudentImplementationRequired("H2.1") - public void doMove() { - turnRight(); - while (!isFrontClear()) { - turnLeft(); - } - move(); - } - - /** - * Turns the robot to the right. - * - */ - @SolutionOnly - private void turnRight() { - for (int i = 0; i < 3; i++) { - turnLeft(); - } - } -} diff --git a/solution/H01/src/main/java/h01/GameController.java b/solution/H01/src/main/java/h01/GameController.java deleted file mode 100644 index f6f864f..0000000 --- a/solution/H01/src/main/java/h01/GameController.java +++ /dev/null @@ -1,23 +0,0 @@ -package h01; - -import h01.template.GameControllerTemplate; - -/** - * The {@link GameController} class is responsible for controlling the game. - */ -public class GameController extends GameControllerTemplate { - /** - * Creates a new {@link GameController} object and sets up the game. - */ - public GameController() { - setup(); - } - - /** - * Checks if the player has won the game. - */ - @Override - public boolean checkWinCondition() { - return pacman.getNumberOfCoins() == totalCoins; - } -} diff --git a/solution/H01/src/main/java/h01/Main.java b/solution/H01/src/main/java/h01/Main.java deleted file mode 100644 index 7b003c4..0000000 --- a/solution/H01/src/main/java/h01/Main.java +++ /dev/null @@ -1,16 +0,0 @@ -package h01; - -/** - * Main entry point in executing the program. - */ -public class Main { - - /** - * Main entry point in executing the program. - * - * @param args program arguments, currently ignored - */ - public static void main(String[] args) { - new GameController().startGame(); - } -} diff --git a/solution/H01/src/main/java/h01/OrangeGhost.java b/solution/H01/src/main/java/h01/OrangeGhost.java deleted file mode 100644 index b57cc90..0000000 --- a/solution/H01/src/main/java/h01/OrangeGhost.java +++ /dev/null @@ -1,53 +0,0 @@ -package h01; - -import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired; - -import fopbot.Robot; -import h01.template.Families; -import h01.template.Ghost; -import h01.template.TickBased; - -/** - * {@link OrangeGhost} is a {@link Robot} that looks like a orange ghost. - * It tries to move in a straight line and alternates between turning left and - * right. - */ -public class OrangeGhost extends Robot implements Ghost, TickBased { - /** - * Creates a new {@link OrangeGhost} at the given position. - * - * @param x the x-coordinate - * @param y the y-coordinate - */ - public OrangeGhost(int x, int y) { - super(x, y, Families.GHOST_ORANGE); - } - - private boolean leftTurnNext = false; - - /** - * Moves the robot in a straight line if possible. - * If the robot cannot move forward, it turns left or right until there is no - * wall in front. - * The robot alternates between turning left and right. - */ - @Override - @StudentImplementationRequired("H2.3") - public void doMove() { - if (isFrontClear()) { - move(); - return; - } else { - while (!isFrontClear()) { - if (leftTurnNext) { - turnLeft(); - } else { - turnLeft(); - turnLeft(); - turnLeft(); - } - } - leftTurnNext = !leftTurnNext; - } - } -} diff --git a/solution/H01/src/main/java/h01/Pacman.java b/solution/H01/src/main/java/h01/Pacman.java deleted file mode 100644 index c6ad8c0..0000000 --- a/solution/H01/src/main/java/h01/Pacman.java +++ /dev/null @@ -1,74 +0,0 @@ -package h01; - -import org.tudalgo.algoutils.student.annotation.SolutionOnly; -import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired; - -import fopbot.Direction; -import fopbot.Robot; -import h01.template.Controllable; -import h01.template.Families; -import h01.template.TickBased; - -/** - * {@link Pacman} is a {@link Robot} that can be controlled by the user and - * looks like Pacman. - * It can move in four directions and pick up coins. - */ -public class Pacman extends Robot implements Controllable, TickBased { - /** - * Creates a new {@link Pacman} at the given position. - * - * @param x the x-coordinate - * @param y the y-coordinate - */ - public Pacman(int x, int y) { - super(x, y, Families.PACMAN); - } - - /** - * Handles the key input of the user. - * The keys 0, 1, 2, 3 represent the arrow keys up, right, down, left. - * If the key is not in this range, the method does nothing. - * If the key is in the range, the robot turns in the corresponding direction, - * moves one field and collects a coin if there is one. - * - * @param k the int value of the pressed key - */ - @Override - @StudentImplementationRequired("H1.1") - public void handleKeyInput(int k) { - if (k < 0 || k > 3) { - return; - } - - if (k == 0) { - turnDirection(Direction.UP); - } else if (k == 1) { - turnDirection(Direction.RIGHT); - } else if (k == 2) { - turnDirection(Direction.DOWN); - } else { - turnDirection(Direction.LEFT); - } - - if (isFrontClear()) { - move(); - } - - if (isOnACoin()) { - pickCoin(); - } - } - - /** - * Turns the robot in the given direction. - * - * @param dir the direction to turn to - */ - @SolutionOnly - private void turnDirection(Direction dir) { - while (getDirection() != dir) { - turnLeft(); - } - } -} diff --git a/solution/H01/src/main/java/h01/PinkGhost.java b/solution/H01/src/main/java/h01/PinkGhost.java deleted file mode 100644 index 7ead508..0000000 --- a/solution/H01/src/main/java/h01/PinkGhost.java +++ /dev/null @@ -1,50 +0,0 @@ -package h01; - -import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired; - -import fopbot.Robot; -import h01.template.Families; -import h01.template.Ghost; -import h01.template.TickBased; -import h01.template.Util; - -/** - * The {@link PinkGhost} is a {@link Robot} that looks like a pink ghost. - * It tries to move in a random direction. - */ -public class PinkGhost extends Robot implements Ghost, TickBased { - /** - * Creates a new {@link PinkGhost} at the given position. - * - * @param x the x-coordinate - * @param y the y-coordinate - */ - public PinkGhost(int x, int y) { - super(x, y, Families.GHOST_PINK); - } - - /** - * Finds in how many directions the ghost can move and then turns a random - * amount of times to the left. - * The ghost then moves forward or turns to the left until it can move forward. - */ - @Override - @StudentImplementationRequired("H2.2") - public void doMove() { - int freeLanes = 0; - for (int i = 0; i < 4; i++) { - turnLeft(); - if (isFrontClear()) { - freeLanes++; - } - } - - int rand = Util.getRandomInteger(1, freeLanes); - for (int i = 0; i < rand; i++) { - do { - turnLeft(); - } while (!isFrontClear()); - } - move(); - } -} diff --git a/solution/H01/src/main/java/h01/RedGhost.java b/solution/H01/src/main/java/h01/RedGhost.java deleted file mode 100644 index 119fa16..0000000 --- a/solution/H01/src/main/java/h01/RedGhost.java +++ /dev/null @@ -1,57 +0,0 @@ -package h01; - -import org.tudalgo.algoutils.student.annotation.SolutionOnly; -import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired; - -import fopbot.Direction; -import fopbot.Robot; -import h01.template.Families; -import h01.template.Ghost; -import h01.template.TickBased; -import h01.template.Util; - -/** - * The {@link RedGhost} is a {@link Robot} that looks like a red ghost. - * It tries to move in the direction of the chased robot. - */ -public class RedGhost extends Robot implements Ghost, TickBased { - private final Robot chased; - - /** - * Creates a new {@link RedGhost} at the given position. - * - * @param x the x-coordinate - * @param y the y-coordinate - * @param chased the robot that is being chased - */ - public RedGhost(int x, int y, Robot chased) { - super(x, y, Families.GHOST_RED); - this.chased = chased; - } - - /** - * Moves the robot in the direction of the chased robot. - */ - @Override - @StudentImplementationRequired("H2.4") - public void doMove() { - turnDirection(Util.furthestDirection(chased, this)); - while (!isFrontClear()) { - turnLeft(); - } - move(); - } - - /** - * Turns the robot in the given direction. - * - * @param dir the direction to turn to - */ - @SolutionOnly - private void turnDirection(Direction dir) { - while (getDirection() != dir) { - turnLeft(); - } - } - -} diff --git a/solution/H01/src/main/java/h01/template/Controllable.java b/solution/H01/src/main/java/h01/template/Controllable.java deleted file mode 100644 index cd648eb..0000000 --- a/solution/H01/src/main/java/h01/template/Controllable.java +++ /dev/null @@ -1,5 +0,0 @@ -package h01.template; - -public interface Controllable { - void handleKeyInput(int k); -} diff --git a/solution/H01/src/main/java/h01/template/Families.java b/solution/H01/src/main/java/h01/template/Families.java deleted file mode 100644 index 1309c6f..0000000 --- a/solution/H01/src/main/java/h01/template/Families.java +++ /dev/null @@ -1,36 +0,0 @@ -package h01.template; - -import fopbot.RobotFamily; -import fopbot.SvgBasedRobotFamily; - -import java.awt.*; - -public class Families { - public static RobotFamily GHOST_ORANGE = new SvgBasedRobotFamily( - "GHOST_ORANGE", - "/robots/ghost_orange.svg", - "/robots/ghost_orange.svg", - Color.ORANGE); - public static RobotFamily GHOST_BLUE = new SvgBasedRobotFamily( - "GHOST_BLUE", - "/robots/ghost_blue.svg", - "/robots/ghost_blue.svg", - Color.BLUE); - public static RobotFamily GHOST_RED = new SvgBasedRobotFamily( - "GHOST_RED", - "/robots/ghost_red.svg", - "/robots/ghost_red.svg", - Color.RED); - public static RobotFamily GHOST_PINK = new SvgBasedRobotFamily( - "GHOST_PINK", - "/robots/ghost_pink.svg", - "/robots/ghost_pink.svg", - Color.PINK); - public static RobotFamily PACMAN = new SvgBasedRobotFamily( - "PACMAN", - "/robots/pacman.svg", - "/robots/pacman.svg", - Color.YELLOW, - 270, - 270); -} diff --git a/solution/H01/src/main/java/h01/template/GameControllerTemplate.java b/solution/H01/src/main/java/h01/template/GameControllerTemplate.java deleted file mode 100644 index c1901db..0000000 --- a/solution/H01/src/main/java/h01/template/GameControllerTemplate.java +++ /dev/null @@ -1,378 +0,0 @@ -package h01.template; - -import java.awt.Color; -import java.awt.Graphics; -import java.awt.Graphics2D; -import java.awt.Point; -import java.awt.geom.Ellipse2D; -import java.awt.geom.Rectangle2D; -import java.util.ArrayList; -import java.util.HashMap; -import java.util.Map; -import java.util.Timer; -import java.util.TimerTask; - -import fopbot.Coin; -import fopbot.ColorProfile; -import fopbot.GuiPanel; -import fopbot.PaintUtils; -import fopbot.Robot; -import fopbot.World; -import h01.BlueGhost; -import h01.OrangeGhost; -import h01.Pacman; -import h01.PinkGhost; -import h01.RedGhost; - -/** - * A {@link GameControllerTemplate} controls the game loop and the - * {@link Robot}s and checks the win condition. - */ -public abstract class GameControllerTemplate { - /** - * The {@link Timer} that controls the game loop. - */ - private final Timer gameLoopTimer = new Timer(); - - /** - * The {@link GameInputHandler} that handles the input of the user. - */ - private final GameInputHandler inputHandler = new GameInputHandler(); - - /** - * The total umber of coins in the map. - */ - protected int totalCoins; - - /** - * The {@link Robot}s that are controlled by the {@link GameControllerTemplate}. - */ - protected final ArrayList robots = new ArrayList<>(); - - /** - * The {@link Pacman} {@link Robot}. - */ - protected Robot pacman; - - /** - * The {@link BlueGhost} {@link Robot}. - */ - protected Robot blue; - - /** - * The {@link OrangeGhost} {@link Robot}. - */ - protected Robot orange; - - /** - * The {@link PinkGhost} {@link Robot}. - */ - protected Robot pink; - - /** - * The {@link RedGhost} {@link Robot}. - */ - protected Robot red; - - private Point ghostField = new Point(4, 4); - - /** - * A {@link Map} that maps a {@link Robot} to the amount of ticks that have - * passed since the last tick action. - */ - private final Map robotTicks = new HashMap<>(); - - /** - * The {@link TimerTask} that is executed every tick. - */ - private final TimerTask gameLoopTask = new TimerTask() { - @Override - public void run() { - for (final Robot robot : GameControllerTemplate.this.robots) { - if (!(robot instanceof final TickBased tb)) { - continue; - } - if (!GameControllerTemplate.this.robotTicks.containsKey(robot)) { - GameControllerTemplate.this.robotTicks.put(robot, 0); - } - if (GameControllerTemplate.this.robotTicks.get(robot) < tb.getUpdateDelay()) { - GameControllerTemplate.this.robotTicks.put(robot, - GameControllerTemplate.this.robotTicks.get(robot) + 1); - continue; - } - GameControllerTemplate.this.robotTicks.put(robot, 0); - // do tick action - if (robot instanceof final Pacman r) { - r.handleKeyInput( - GameControllerTemplate.this.inputHandler.getDirection()); - } else if (robot instanceof final Ghost r) { - r.doMove(); - } - } - // check win condition - if (checkWinCondition()) - stopGame(true); - if (checkLoseCondition()) - stopGame(false); - } - }; - - /** - * Gets the {@link h01.Pacman} {@link Robot}. - * - * @return the {@link h01.Pacman} {@link Robot} - */ - public Robot getPacman() { - return pacman; - } - - /** - * Gets the {@link BlueGhost} {@link Robot}. - * - * @return the {@link BlueGhost} {@link Robot} - */ - public Robot getBlue() { - return blue; - } - - /** - * Gets the {@link OrangeGhost} {@link Robot}. - * - * @return the {@link OrangeGhost} {@link Robot} - */ - public Robot getOrange() { - return orange; - } - - /** - * Gets the {@link PinkGhost} {@link Robot}. - * - * @return the {@link PinkGhost} {@link Robot} - */ - public Robot getPink() { - return pink; - } - - /** - * Gets the {@link RedGhost} {@link Robot}. - * - * @return the {@link RedGhost} {@link Robot} - */ - public Robot getRed() { - return red; - } - - /** - * Starts the game loop. - */ - public void startGame() { - System.out.println("Starting game..."); - this.gameLoopTimer.scheduleAtFixedRate(this.gameLoopTask, 0, 200); - } - - /** - * Stops the game loop. - */ - public void stopGame(boolean won) { - this.gameLoopTimer.cancel(); - endscreen(won ? Color.GREEN : Color.RED); - } - - public void endscreen(Color color) { - World.getGlobalWorld().getGuiPanel().setColorProfile( - ColorProfile.DEFAULT.toBuilder() - .backgroundColorDark(Color.BLACK) - .backgroundColorLight(Color.BLACK) - .fieldColorDark(color) - .fieldColorLight(color) - .innerBorderColorLight(color) - .innerBorderColorDark(color) - .wallColorDark(Color.BLUE) - .wallColorLight(Color.BLUE) - .outerBorderColorDark(Color.BLUE) - .outerBorderColorLight(Color.BLUE) - .coinColorDark(color) - .coinColorLight(color) - .build()); - World.getGlobalWorld().getGuiPanel().updateGui(); - } - - /** - * Sets up the game. - */ - protected void setup() { - setupWorld(); - setupTheme(); - setupRobots(); - totalCoins = 2;// World.getHeight()*World.getWidth()-2; - setupCoins(totalCoins); - this.inputHandler.install(); - } - - public void setupTheme() { - World.getGlobalWorld().getGuiPanel().setColorProfile( - ColorProfile.DEFAULT.toBuilder() - .backgroundColorDark(Color.BLACK) - .backgroundColorLight(Color.BLACK) - .fieldColorDark(Color.BLACK) - .fieldColorLight(Color.BLACK) - .innerBorderColorLight(Color.BLACK) - .innerBorderColorDark(Color.BLACK) - .wallColorDark(Color.BLUE) - .wallColorLight(Color.BLUE) - .outerBorderColorDark(Color.BLUE) - .outerBorderColorLight(Color.BLUE) - .build()); - } - - /** - * Initializes the {@link World} and adds the {@link Robot}s to it. - */ - public void setupWorld() { - World.setSize(9, 9); - - World.getGlobalWorld().setGuiPanel(new GuiPanel(World.getGlobalWorld()) { - @Override - @SuppressWarnings("UnstableApiUsage") - protected void drawCoin(final Coin c, final Graphics g, final boolean evadeRobots) { - final var g2d = (Graphics2D) g; - final var oldColor = g2d.getColor(); - g2d.setColor(getColorProfile().getCoinColor()); - final Rectangle2D fieldBounds = scale(PaintUtils.getFieldBounds(c, world)); - final double radius = scale(5d); - g2d.fill( - new Ellipse2D.Double( - fieldBounds.getCenterX() - radius, - fieldBounds.getCenterY() - radius, - 2 * radius, - 2 * radius)); - } - }); - - World.setDelay(0); - World.setVisible(true); - World.getGlobalWorld().setDrawTurnedOffRobots(false); - - World.placeVerticalWall(0, 3); - World.placeVerticalWall(0, 4); - World.placeVerticalWall(0, 5); - - World.placeVerticalWall(1, 2); - World.placeVerticalWall(1, 4); - World.placeVerticalWall(1, 6); - - World.placeVerticalWall(2, 2); - World.placeVerticalWall(2, 3); - World.placeVerticalWall(2, 5); - World.placeVerticalWall(2, 6); - - World.placeVerticalWall(3, 4); - World.placeVerticalWall(3, 7); - - World.placeVerticalWall(4, 4); - World.placeVerticalWall(4, 1); - - World.placeVerticalWall(5, 2); - World.placeVerticalWall(5, 3); - World.placeVerticalWall(5, 5); - World.placeVerticalWall(5, 6); - - World.placeVerticalWall(6, 2); - World.placeVerticalWall(6, 4); - World.placeVerticalWall(6, 6); - - World.placeVerticalWall(7, 3); - World.placeVerticalWall(7, 4); - World.placeVerticalWall(7, 5); - - World.placeHorizontalWall(1, 0); - World.placeHorizontalWall(1, 1); - World.placeHorizontalWall(1, 6); - World.placeHorizontalWall(1, 7); - - World.placeHorizontalWall(2, 0); - World.placeHorizontalWall(2, 7); - - World.placeHorizontalWall(3, 0); - World.placeHorizontalWall(3, 2); - World.placeHorizontalWall(3, 5); - - World.placeHorizontalWall(4, 1); - World.placeHorizontalWall(4, 3); - World.placeHorizontalWall(4, 6); - - World.placeHorizontalWall(5, 2); - World.placeHorizontalWall(5, 5); - World.placeHorizontalWall(5, 7); - - World.placeHorizontalWall(6, 0); - World.placeHorizontalWall(6, 7); - - World.placeHorizontalWall(7, 0); - World.placeHorizontalWall(7, 1); - World.placeHorizontalWall(7, 6); - World.placeHorizontalWall(7, 7); - - World.getGlobalWorld().setFieldColor(ghostField.x, ghostField.y, Color.YELLOW); - } - - /** - * Adds the {@link Robot}s to the {@link World}. - */ - public void setupRobots() { - this.robots.add(pacman = new Pacman(4, 3)); - this.robots.add(blue = new BlueGhost(ghostField.x, ghostField.y)); - this.robots.add(orange = new OrangeGhost(ghostField.x, ghostField.y)); - this.robots.add(pink = new PinkGhost(ghostField.x, ghostField.y)); - this.robots.add(red = new RedGhost(ghostField.x, ghostField.y, pacman)); - } - - public void setupCoins(int numberOfCoins) { - if (numberOfCoins > World.getHeight() * World.getWidth() - 2) { - throw new IllegalArgumentException("Too many coins for this world size."); - } - - ArrayList Fields = new ArrayList<>(); - for (int y = 0; y < World.getHeight(); y++) { - for (int x = 0; x < World.getWidth(); x++) { - if (!((x == ghostField.x && y == ghostField.y) || x == pacman.getX() && y == pacman.getY())) - Fields.add(new Point(x, y)); - } - } - - for (int i = 0; i < numberOfCoins; i++) { - int randomIndex = (int) (Math.random() * Fields.size()); - Point spot = Fields.remove(randomIndex); - World.putCoins(spot.x, spot.y, 1); - } - } - - /** - * Checks the win condition. - * - * @return Returns true if the game is won. - */ - public abstract boolean checkWinCondition(); - - /** - * Checks the lose condition. - * - * @return Returns true if the game is lost. - */ - public boolean checkLoseCondition() { - boolean gameDone = pacman.getX() == orange.getX() && pacman.getY() == orange.getY(); - - if (pacman.getX() == blue.getX() && pacman.getY() == blue.getY()) { - gameDone = true; - } - - if (pacman.getX() == pink.getX() && pacman.getY() == pink.getY()) { - gameDone = true; - } - - if (pacman.getX() == red.getX() && pacman.getY() == red.getY()) { - gameDone = true; - } - return gameDone; - } -} diff --git a/solution/H01/src/main/java/h01/template/GameInputHandler.java b/solution/H01/src/main/java/h01/template/GameInputHandler.java deleted file mode 100644 index 2eba352..0000000 --- a/solution/H01/src/main/java/h01/template/GameInputHandler.java +++ /dev/null @@ -1,108 +0,0 @@ -package h01.template; - -import fopbot.Direction; -import fopbot.World; - -import java.awt.event.KeyAdapter; -import java.awt.event.KeyEvent; -import java.util.HashSet; -import java.util.List; -import java.util.Map; -import java.util.Optional; -import java.util.Set; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.concurrent.atomic.AtomicInteger; - -/** - * A {@link GameInputHandler} handles the input of the user. - */ -public class GameInputHandler { - /** - * The {@link fopbot.Direction} to turn to. If {@code -1} the robot should not turn. - */ - private final AtomicInteger direction = new AtomicInteger(-1); - /** - * If {@code true} the robot should put a coin on the current field. - */ - private final AtomicBoolean shouldPutCoins = new AtomicBoolean(false); - /** - * If {@code true} the robot should pick a coin from the current field. - */ - private final AtomicBoolean shouldPickCoins = new AtomicBoolean(false); - - /** - * Parses the inputs to a direction. - * - * @param keysPressed the keys pressed - * @return the direction or null if no direction is pressed - */ - public static Direction getDirectionFromKeysPressed(final Set keysPressed) { - final Map> directionKeys = Map.of( - Direction.UP, List.of(KeyEvent.VK_UP, KeyEvent.VK_W), - Direction.LEFT, List.of(KeyEvent.VK_LEFT, KeyEvent.VK_A), - Direction.DOWN, List.of(KeyEvent.VK_DOWN, KeyEvent.VK_S), - Direction.RIGHT, List.of(KeyEvent.VK_RIGHT, KeyEvent.VK_D) - ); - final Set pressedDirections = new HashSet<>(); - for (final Direction direction : directionKeys.keySet()) { - for (final Integer key : directionKeys.get(direction)) { - if (keysPressed.contains(key)) { - pressedDirections.add(direction); - } - } - } - if (pressedDirections.size() == 1) { - return pressedDirections.iterator().next(); - } else { - return null; - } - } - - /** - * Installs the {@link GameInputHandler} to the {@link World}. - */ - public void install() { - World.getGlobalWorld().getInputHandler().addKeyListener(new KeyAdapter() { - @Override - public void keyTyped(final KeyEvent e) { - updateKeysPressed(); - } - - @Override - public void keyPressed(final KeyEvent e) { - updateKeysPressed(); - } - - @Override - public void keyReleased(final KeyEvent e) { - updateKeysPressed(); - } - }); - } - - /** - * Updates the {@link #direction}, {@link #shouldPutCoins} and {@link #shouldPickCoins} based on the pressed keys. - */ - protected void updateKeysPressed() { - this.direction.set( - Optional.ofNullable(getDirectionFromKeysPressed(World.getGlobalWorld().getInputHandler().getKeysPressed())) - .map(Enum::ordinal) - .orElse(-1) - ); - this.shouldPickCoins.set( - World.getGlobalWorld().getInputHandler().getKeysPressed().contains(java.awt.event.KeyEvent.VK_SPACE) - ); - this.shouldPutCoins.set( - World.getGlobalWorld().getInputHandler().getKeysPressed().contains(java.awt.event.KeyEvent.VK_R) - ); - } - - /** - * Returns the current {@link fopbot.Direction} to turn to. If {@code -1} the robot should not turn. - * - * @return the current {@link fopbot.Direction} to turn to. If {@code -1} the robot should not turn. - */ - public int getDirection() { - return this.direction.get(); - } -} diff --git a/solution/H01/src/main/java/h01/template/Ghost.java b/solution/H01/src/main/java/h01/template/Ghost.java deleted file mode 100644 index d13053f..0000000 --- a/solution/H01/src/main/java/h01/template/Ghost.java +++ /dev/null @@ -1,5 +0,0 @@ -package h01.template; - -public interface Ghost { - void doMove(); -} diff --git a/solution/H01/src/main/java/h01/template/TickBased.java b/solution/H01/src/main/java/h01/template/TickBased.java deleted file mode 100644 index 2b18bee..0000000 --- a/solution/H01/src/main/java/h01/template/TickBased.java +++ /dev/null @@ -1,15 +0,0 @@ -package h01.template; - -/** - * An interface that enables an object to be updated every n-th tick. - */ -public interface TickBased { - /** - * Returns the number of ticks between two updates. - * - * @return the number of ticks between two updates - */ - default int getUpdateDelay() { - return 1; - } -} diff --git a/solution/H01/src/main/java/h01/template/Util.java b/solution/H01/src/main/java/h01/template/Util.java deleted file mode 100644 index eefcb35..0000000 --- a/solution/H01/src/main/java/h01/template/Util.java +++ /dev/null @@ -1,55 +0,0 @@ -package h01.template; - -import java.util.Random; - -import fopbot.Direction; -import fopbot.Robot; - -/** - * Utility class for the ghosts - */ -public class Util { - - private static Random rnd = new Random(); - - /** - * Returns a random integer between min and max (both inclusive) - * - * @param min the minimum value - * @param max the maximum value - * @return a random integer between min and max (both inclusive) - */ - public static int getRandomInteger(int min, int max) { - return min + rnd.nextInt(max - min + 1); - } - - /** - * Returns the direction in which the robot is the closest to the chaser - * - * @param pacman the robot that is being chased - * @param chaser the robot that is chasing - * @return the direction in which the robot is the closest to the chaser - */ - public static Direction furthestDirection(Robot pacman, Robot chaser) { - int px = pacman.getX(); - int py = pacman.getY(); - int cx = chaser.getX(); - int cy = chaser.getY(); - - int xdelta = Math.abs(px - cx); - int ydelta = Math.abs(py - cy); - - if (xdelta > ydelta) { - if (cx > px) - return Direction.LEFT; - else - return Direction.RIGHT; - } else { - if (cy > py) - return Direction.DOWN; - else - return Direction.UP; - } - } - -} diff --git a/solution/H01/src/main/resources/robots/ghost_blue.svg b/solution/H01/src/main/resources/robots/ghost_blue.svg deleted file mode 100644 index 2d21577..0000000 --- a/solution/H01/src/main/resources/robots/ghost_blue.svg +++ /dev/null @@ -1,67 +0,0 @@ - - - - - - - - - - - - - diff --git a/solution/H01/src/main/resources/robots/ghost_orange.svg b/solution/H01/src/main/resources/robots/ghost_orange.svg deleted file mode 100644 index 21428d3..0000000 --- a/solution/H01/src/main/resources/robots/ghost_orange.svg +++ /dev/null @@ -1,31 +0,0 @@ - - - - - - - - - - - - \ No newline at end of file diff --git a/solution/H01/src/main/resources/robots/ghost_pink.svg b/solution/H01/src/main/resources/robots/ghost_pink.svg deleted file mode 100644 index 589d67d..0000000 --- a/solution/H01/src/main/resources/robots/ghost_pink.svg +++ /dev/null @@ -1,67 +0,0 @@ - - - - - - - - - - - - - diff --git a/solution/H01/src/main/resources/robots/ghost_red.svg b/solution/H01/src/main/resources/robots/ghost_red.svg deleted file mode 100644 index a1eca1a..0000000 --- a/solution/H01/src/main/resources/robots/ghost_red.svg +++ /dev/null @@ -1,67 +0,0 @@ - - - - - - - - - - - - - diff --git a/solution/H01/src/main/resources/robots/pacman.svg b/solution/H01/src/main/resources/robots/pacman.svg deleted file mode 100644 index 9c8a8cb..0000000 --- a/solution/H01/src/main/resources/robots/pacman.svg +++ /dev/null @@ -1,61 +0,0 @@ - - - - - - - - - - - - - - - diff --git a/solution/H01/src/test b/solution/H01/src/test new file mode 120000 index 0000000..a998220 --- /dev/null +++ b/solution/H01/src/test @@ -0,0 +1 @@ +/home/osh/Desktop/FOP-2425-Marathon/H01/src/test \ No newline at end of file diff --git a/solution/H01/src/test/java/h01/ExampleJUnitTest.java b/solution/H01/src/test/java/h01/ExampleJUnitTest.java deleted file mode 100644 index 14bb830..0000000 --- a/solution/H01/src/test/java/h01/ExampleJUnitTest.java +++ /dev/null @@ -1,16 +0,0 @@ -package h01; - -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.*; - -/** - * An example JUnit test class. - */ -public class ExampleJUnitTest { - - @Test - public void testAddition() { - assertEquals(2, 1 + 1); - } -} diff --git a/solution/H02/src/main b/solution/H02/src/main new file mode 120000 index 0000000..58de0dd --- /dev/null +++ b/solution/H02/src/main @@ -0,0 +1 @@ +/home/osh/Desktop/FOP-2425-Marathon/H02/src/main \ No newline at end of file diff --git a/solution/H02/src/main/java/h02/FourWins.java b/solution/H02/src/main/java/h02/FourWins.java deleted file mode 100644 index 19cfc05..0000000 --- a/solution/H02/src/main/java/h02/FourWins.java +++ /dev/null @@ -1,388 +0,0 @@ -package h02; - -import fopbot.Direction; -import fopbot.Robot; -import fopbot.RobotFamily; -import fopbot.World; -import h02.template.InputHandler; -import org.tudalgo.algoutils.student.annotation.DoNotTouch; -import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired; - -import java.util.Optional; - -/** - * The {@link FourWins} class represents the main class of the FourWins game. - */ -public class FourWins { - private final InputHandler inputHandler = new InputHandler(this); - /** - * The width of the game board. - */ - private final int width; - /** - * The height of the game board. - */ - private final int height; - /** - * Indicates whether the game has finished. - */ - @SuppressWarnings({"FieldMayBeFinal", "FieldCanBeLocal"}) - private boolean finished = false; - - /** - * Creates a new {@link FourWins} instance with the given width and height. - * - * @param width the width of the game board - * @param height the height of the game board - */ - FourWins(final int width, final int height) { - this.width = width; - this.height = height; - } - - /** - * Starts the game by setting up the world and executing the game loop. - */ - void startGame() { - setupWorld(); - gameLoop(); - } - - /** - * Sets up the world and installs the {@link InputHandler}. - */ - void setupWorld() { - World.setSize(width, height); - World.setDelay(10); - World.setVisible(true); - inputHandler.install(); - } - - - /** - * Validates if a given column index is within the bounds of the game board and not fully occupied. - * - * @param column The column index to validate. - * @param stones 2D array representing the game board, where each cell contains a RobotFamily color indicating the - * player that has placed a stone in that position. - * @return true if the column is within bounds and has at least one unoccupied cell; false otherwise. - */ - @StudentImplementationRequired("H2.2.1") - public static boolean validateInput(final int column, final RobotFamily[][] stones) { - return column >= 0 && column < World.getWidth() && stones[World.getHeight() - 1][column] == null; - } - - - /** - * Calculates the next unoccupied row index in the specified column. This row index is the next destination for a - * falling stone. - * - * @param column The column index where the stone is to be dropped. - * @param stones 2D array representing the game board, where each cell contains a RobotFamily object indicating the - * player that has placed a stone in that position. - * @return Index of the next unoccupied row index in the specified column. - */ - @StudentImplementationRequired("H2.2.2") - public static int getDestinationRow(final int column, final RobotFamily[][] stones) { - for (int row = 0; row < stones.length; row++) { - if (stones[row][column] == null) { - return row; - } - } - return -1; - } - - /** - * Drops a stone into the specified column of the game board, simulating a falling animation. This method gets the - * destination row for the stone in the specified column with the `getDestinationRow` method. It creates a new Robot - * instance to represent the stone with the currentPlayer's RobotFamily in the given column and the destination row. - * After that it simulates the stone's fall by decrementing its position until it reaches the destination row. Once - * the stone reaches its destination, the method updates the stones array (a 2D array of RobotFamily colors) to - * mark the slot as occupied by the currentPlayer. - * - * @param column The column index where the stone is to be dropped. - * @param stones 2D array representing the game board, where each cell contains a RobotFamily object - * indicating the player that has placed a stone in that position. - * @param currentPlayer The RobotFamily object representing the current player dropping the stone. - */ - @StudentImplementationRequired("H2.2.2") - public static void dropStone(final int column, final RobotFamily[][] stones, final RobotFamily currentPlayer) { - // spawn stone - final Robot stone = new Robot(column, World.getHeight() - 1, Direction.DOWN, 0, currentPlayer); - - // let stone fall - final int row = getDestinationRow(column, stones); - for (int currentRow = World.getHeight() - 1; currentRow > row; currentRow--) { - stone.move(); - } - - // turn stone up - stone.turnLeft(); - stone.turnLeft(); - - // set slot as occupied - stones[row][column] = currentPlayer; - } - - - /** - * Checks if the current player has won by any condition. The conditions can be a horizontal, vertical, diagonal, or - * anti-diagonal line of at least four stones. - * - * @param stones 2D array representing the game board, where each cell contains a RobotFamily color - * indicating the player that has placed a stone in that position. - * @param currentPlayer The RobotFamily color representing the current player to check for a win. - * @return true if the current player has formed a horizontal line of at least four stones; false otherwise. - */ - @StudentImplementationRequired("H2.2.3") - public static boolean testWinConditions(final RobotFamily[][] stones, final RobotFamily currentPlayer) { - return testWinVertical(stones, currentPlayer) - || testWinHorizontal(stones, currentPlayer) - || testWinDiagonal(stones, currentPlayer); - } - - /** - * Checks if the current player has won by forming a horizontal line of at least consecutive four stones. - * - * @param stones 2D array representing the game board, where each cell contains a RobotFamily color - * indicating the player that has placed a stone in that position. - * @param currentPlayer The RobotFamily color representing the current player to check for a win. - * @return true if the current player has formed a horizontal line of at least four stones; false otherwise. - */ - @StudentImplementationRequired("H2.2.3") - public static boolean testWinHorizontal(final RobotFamily[][] stones, final RobotFamily currentPlayer) { - for (int row = 0; row < World.getHeight(); row++) { - int stoneCount = 0; - for (int column = 0; column < World.getWidth(); column++) { - stoneCount = stones[row][column] == currentPlayer ? stoneCount + 1 : 0; - if (stoneCount >= 4) { - return true; - } - } - } - return false; - } - - /** - * Checks if the current player has won by forming a vertical line of at least consecutive four stones. - * - * @param stones 2D array representing the game board, where each cell contains a RobotFamily color - * indicating the player that has placed a stone in that position. - * @param currentPlayer The RobotFamily color representing the current player to check for a win. - * @return true if the current player has formed a vertical line of at least four stones; false otherwise. - */ - @StudentImplementationRequired("H2.2.3") - public static boolean testWinVertical(final RobotFamily[][] stones, final RobotFamily currentPlayer) { - for (int column = 0; column < World.getWidth(); column++) { - int stoneCount = 0; - for (int row = 0; row < World.getHeight(); row++) { - stoneCount = stones[row][column] == currentPlayer ? stoneCount + 1 : 0; - if (stoneCount >= 4) { - return true; - } - } - } - return false; - } - - /** - * Checks if the current player has won by forming a diagonal line of at least consecutive four stones. - * - * @param stones 2D array representing the game board, where each cell contains a RobotFamily color - * indicating the player that has placed a stone in that position. - * @param currentPlayer The RobotFamily color representing the current player to check for a win. - * @return true if the current player has formed a diagonal line of at least four stones; false otherwise. - */ - @DoNotTouch - public static boolean testWinDiagonal(final RobotFamily[][] stones, final RobotFamily currentPlayer) { - @SuppressWarnings("CheckStyle") final int MAX_STONES = 4; - - @SuppressWarnings("CheckStyle") final int WIDTH = World.getWidth(); - @SuppressWarnings("CheckStyle") final int HEIGHT = World.getHeight(); - int[] direction = new int[]{1, 1}; - - // for every field - for (int y = 0; y < HEIGHT; y++) { - for (int x = 0; x < WIDTH; x++) { - - // for every direction - for (int nthDirection = 0; nthDirection < 4; nthDirection++) { - final int[] pos = {x, y}; - - // test for consecutive coins - int coinCount = 0; // start counting at 0 - while (pos[0] >= 0 && pos[0] < WIDTH - && pos[1] >= 0 && pos[1] < HEIGHT - && stones[pos[1]][pos[0]] == currentPlayer) { - coinCount++; // count every stone that has currentPlayer's color - if (coinCount >= MAX_STONES) { - return true; - } - pos[0] += direction[0]; - pos[1] += direction[1]; - } - - direction = new int[]{direction[1], -direction[0]}; // next direction (rotate by 90 deg) - } - } - } - - return false; - } - - - /** - * Switches the player for each turn. If the current player is SQUARE_BLUE, SQUARE_RED is returned as the next - * player. If the current player is SQUARE_RED, SQUARE_BLUE is returned as the next player. - * - * @param currentPlayer The player color of the current player. - * @return The player color of the next player. - */ - @StudentImplementationRequired("H2.2.4") - public static RobotFamily nextPlayer(final RobotFamily currentPlayer) { - return currentPlayer == RobotFamily.SQUARE_BLUE ? RobotFamily.SQUARE_RED : RobotFamily.SQUARE_BLUE; - } - - /** - * Displays a Message in the console and on the game board indicating the game is drawn. - */ - @StudentImplementationRequired("H2.2.4") - public void writeDrawMessage() { - inputHandler.displayDrawStatus(); - - // student implementation here: - System.out.println("No valid columns found. Hence, game ends with a draw."); - } - - /** - * Displays a Message in the console and on the game board indicating the game is won by a player. - * - * @param winner {@link RobotFamily} of the winning player - */ - @StudentImplementationRequired("H2.2.4") - public void writeWinnerMessage(final RobotFamily winner) { - inputHandler.displayWinnerStatus(winner); - - // student implementation here: - System.out.println("Player " + winner + " wins the game!"); - } - - /** - * Displays the winner of the game by printing the winning color in the console and filling the whole field with - * Robots of the winning color. - * - * @param winner The RobotFamily color of the winner. - */ - @StudentImplementationRequired("H2.2.4") - public static void colorFieldBackground(final RobotFamily winner) { - for (int x = 0; x < World.getWidth(); x++) { - for (int y = 0; y < World.getHeight(); y++) { - setFieldColor(x, y, winner); - } - } - } - - /** - * Executes the main game loop, handling player turns, stone drops, and win condition checks. This method - * initializes the game board as a 2D array of RobotFamily colors, representing the slots that can be filled with - * players' stones. It starts with a predefined currentPlayer and continues in a loop until a win condition is met. - * Each iteration of the loop waits for player input to select a column to drop a stone into, switches the current - * player, drops the stone in the selected column, and checks for win conditions. If a win condition is met, the - * loop ends, and the winner is displayed. - */ - @StudentImplementationRequired("H2.2.4") - void gameLoop() { - final RobotFamily[][] stones = new RobotFamily[World.getHeight()][World.getWidth()]; - RobotFamily currentPlayer = RobotFamily.SQUARE_BLUE; - - boolean draw = false; - finished = false; - - while (!finished) { - // student implementation here: - currentPlayer = nextPlayer(currentPlayer); - - // wait for click in column (DO NOT TOUCH) - finished = draw = isGameBoardFull(stones); - if (draw) { - break; - } - final int column = inputHandler.getNextInput(currentPlayer, stones); - - // student implementation here: - dropStone(column, stones, currentPlayer); - finished = testWinConditions(stones, currentPlayer); - } - - // displaying either draw or winner (DO NOT TOUCH) - if (draw) { - writeDrawMessage(); - colorFieldBackground(getDrawnRobotFamily()); - } else { - writeWinnerMessage(currentPlayer); - colorFieldBackground(currentPlayer); - } - } - - - /** - * Executes the main game loop, handling player turns, stone drops, and win condition checks. Sets the background - * color of a field at the specified coordinates. The color is derived from the {@link RobotFamily} SQUARE_BLUE or - * SQUARE_RED. - * - * @param x the x coordinate of the field - * @param y the y coordinate of the field - * @param color the {@link RobotFamily} corresponding to the field color to set - */ - @DoNotTouch - public static void setFieldColor(final int x, final int y, final RobotFamily color) { - World.getGlobalWorld().setFieldColor(x, y, color.getColor()); - } - - /** - * Returns the {@link RobotFamily} which represents a drawn game. - * - * @return the {@link RobotFamily} which represents a drawn game. - */ - @DoNotTouch - @SuppressWarnings("UnstableApiUsage") - protected static RobotFamily getDrawnRobotFamily() { - return Optional.ofNullable(World.getGlobalWorld().getGuiPanel()).filter(guiPanel -> !guiPanel.isDarkMode()).map(guiPanel -> RobotFamily.SQUARE_ORANGE).orElse(RobotFamily.SQUARE_YELLOW); - } - - /** - * Checks if all columns of the game board are fully occupied. - * - * @param stones 2D array representing the game board, where each cell contains a RobotFamily - * @return true if all columns of the game board are fully occupied; false otherwise. - */ - @DoNotTouch - public static boolean isGameBoardFull(final RobotFamily[][] stones) { - for (int x = 0; x < World.getWidth(); x++) { - if (FourWins.validateInput(x, stones)) { - return false; - } - } - return true; - } - - /** - * Returns this instance's {@link InputHandler}. - * - * @return the input handler - */ - @DoNotTouch - public InputHandler getInputHandler() { - return inputHandler; - } - - /** - * Returns {@code true} when the game is finished, {@code false} otherwise. - * - * @return whether the game is finished. - */ - public boolean isFinished() { - return finished; - } - -} diff --git a/solution/H02/src/main/java/h02/Main.java b/solution/H02/src/main/java/h02/Main.java deleted file mode 100644 index 452d68f..0000000 --- a/solution/H02/src/main/java/h02/Main.java +++ /dev/null @@ -1,218 +0,0 @@ -package h02; - -import fopbot.RobotFamily; -import fopbot.World; -import org.tudalgo.algoutils.student.annotation.SolutionOnly; -import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired; - -import static org.tudalgo.algoutils.student.io.PropertyUtils.getIntProperty; -import static org.tudalgo.algoutils.student.test.StudentTestUtils.printTestResults; -import static org.tudalgo.algoutils.student.test.StudentTestUtils.testEquals; - -/** - * Main entry point in executing the program. - */ -public class Main { - /** - * Main entry point in executing the program. - * - * @param args program arguments, currently ignored - */ - public static void main(final String[] args) { - // H1 - sanityChecksH211(); - sanityChecksH212(); - printTestResults(); - - // H2 - sanityChecksH22(); - printTestResults(); - - // starting game (comment out if you just want to run the tests) - final var propFile = "h02.properties"; - new FourWins( - getIntProperty(propFile, "FW_WORLD_WIDTH"), - getIntProperty(propFile, "FW_WORLD_HEIGHT") - ).startGame(); - } - - /** - * Perform sanity checks for exercise H2.1.1. - */ - @StudentImplementationRequired("H2.3") - public static void sanityChecksH211() { - // push test - final int[] newArray = OneDimensionalArrayStuff.push(new int[]{0, 1}, 2); - final int[] expectedArray = {0, 1, 2}; - testEquals(expectedArray.length, newArray.length); - for (int i = 0; i < newArray.length; i++) { - testEquals(expectedArray[i], newArray[i]); - } - - // calculateNextFibonacci test - int[] fibonacciArray = {0, 1}; - for (int i = 0; i < 20; i++) { - fibonacciArray = OneDimensionalArrayStuff.calculateNextFibonacci(fibonacciArray); - } - testEquals(22, fibonacciArray.length); - testEquals(0, fibonacciArray[0]); - testEquals(1, fibonacciArray[1]); - for (int i = 2; i < fibonacciArray.length; i++) { - testEquals(fibonacciArray[i - 1] + fibonacciArray[i - 2], fibonacciArray[i]); - } - - // fibonacci test - final int[] reference = {0, 1, 1, 2, 3, 5, 8, 13, 21, 34}; - for (int i = 0; i < 10; i++) { - testEquals(reference[i], OneDimensionalArrayStuff.fibonacci(i)); - } - } - - /** - * Perform sanity checks for exercise H2.1.2. - */ - @StudentImplementationRequired("H2.3") - public static void sanityChecksH212() { - // predefined simple test - final String[][] simpleTest = new String[][]{ - "a b c d e f".split(" "), - "a b c d e f".split(" "), - "a b c d e f".split(" "), - }; - // predefined complex test - final String[][] complexTest = new String[][]{ - "a a b b c c".split(" "), - "a b c d e f".split(" "), - "a a a b b b c c c".split(" "), - }; - - - // student implementation here: - - sanityChecksH212Helper( - simpleTest, - "b", - new int[]{1, 1, 1}, - 1 - ); - - sanityChecksH212Helper( - complexTest, - "b", - new int[]{2, 1, 3}, - 2 - ); - } - - /** - * Helper method for sanity checks for exercise H2.1.2. - * - * @param input the input array - * @param query the query string - * @param refOcc the reference occurrences - * @param refMean the reference mean - */ - @SolutionOnly - public static void sanityChecksH212Helper( - final String[][] input, - final String query, - final int[] refOcc, - final float refMean - ) { - final int[] occ = TwoDimensionalArrayStuff.occurrences(input, query); - testEquals(refOcc.length, occ.length); - for (int i = 0; i < occ.length; i++) { - testEquals(refOcc[i], occ[i]); - } - testEquals(refMean, TwoDimensionalArrayStuff.meanOccurrencesPerLine(input, query)); - } - - /** - * Perform sanity checks for exercise H2.2 - */ - @StudentImplementationRequired("H2.4") - public static void sanityChecksH22() { - // setting world size - World.setSize(4, 5); - - // predefined stones1 array - final RobotFamily[][] stones1 = { - {null, RobotFamily.SQUARE_BLUE, null, RobotFamily.SQUARE_RED}, - {null, null, null, RobotFamily.SQUARE_BLUE}, - {null, null, null, RobotFamily.SQUARE_RED}, - {null, null, null, RobotFamily.SQUARE_BLUE}, - {null, null, null, RobotFamily.SQUARE_RED}, - }; - - // predefined stones2 array - final RobotFamily[][] stones2 = { - {RobotFamily.SQUARE_BLUE, RobotFamily.SQUARE_BLUE, RobotFamily.SQUARE_BLUE, RobotFamily.SQUARE_BLUE}, - {RobotFamily.SQUARE_RED, RobotFamily.SQUARE_RED, RobotFamily.SQUARE_BLUE, RobotFamily.SQUARE_RED}, - {RobotFamily.SQUARE_BLUE, RobotFamily.SQUARE_RED, RobotFamily.SQUARE_BLUE, RobotFamily.SQUARE_BLUE}, - {RobotFamily.SQUARE_BLUE, RobotFamily.SQUARE_RED, RobotFamily.SQUARE_BLUE, RobotFamily.SQUARE_RED}, - {RobotFamily.SQUARE_BLUE, RobotFamily.SQUARE_BLUE, RobotFamily.SQUARE_BLUE, RobotFamily.SQUARE_RED}, - }; - - - // student implementation here: - - // H2.2.1 validateInput - final boolean isInCol1 = FourWins.validateInput(1, stones1); - final boolean isInCol3 = FourWins.validateInput(3, stones1); - - testEquals(true, isInCol1); - testEquals(false, isInCol3); - - - // H2.2.2 getDestinationRow - final int rowCol1 = FourWins.getDestinationRow(1, stones1); - final int rowCol3 = FourWins.getDestinationRow(3, stones1); - - testEquals(1, rowCol1); - testEquals(-1, rowCol3); - - - // H2.2.2 dropStone - FourWins.dropStone(1, stones1, RobotFamily.SQUARE_RED); - // System.out.println(Arrays.deepToString(stones1)); - // System.out.println(stones1); - testEquals(RobotFamily.SQUARE_RED, stones1[1][1]); - - - // H2.2.3 testWinHorizontal - final boolean winRowBlue = FourWins.testWinHorizontal(stones2, RobotFamily.SQUARE_BLUE); - final boolean winRowRed = FourWins.testWinHorizontal(stones2, RobotFamily.SQUARE_RED); - - testEquals(true, winRowBlue); - testEquals(false, winRowRed); - - - // H2.2.3 testWinVertical - final boolean winColStones2 = FourWins.testWinVertical(stones2, RobotFamily.SQUARE_BLUE); - final boolean winColStones1 = FourWins.testWinVertical(stones1, RobotFamily.SQUARE_BLUE); - - testEquals(true, winColStones2); - testEquals(false, winColStones1); - - - // H2.2.3 testWinConditions - final boolean winStones2 = FourWins.testWinConditions(stones2, RobotFamily.SQUARE_BLUE); - final boolean winStones1 = FourWins.testWinConditions(stones1, RobotFamily.SQUARE_BLUE); - - testEquals(true, winStones2); - testEquals(false, winStones1); - - - // H2.2.4 switchPlayer - final RobotFamily nextPlayer1 = FourWins.nextPlayer(RobotFamily.SQUARE_BLUE); - final RobotFamily nextPlayer2 = FourWins.nextPlayer(RobotFamily.SQUARE_RED); - - testEquals(RobotFamily.SQUARE_RED, nextPlayer1); - testEquals(RobotFamily.SQUARE_BLUE, nextPlayer2); - - - // H2.2.4 colorFieldBackground, writeDrawMessage, writeWinnerMessage, gameLoop - // Test by playing - } - -} diff --git a/solution/H02/src/main/java/h02/OneDimensionalArrayStuff.java b/solution/H02/src/main/java/h02/OneDimensionalArrayStuff.java deleted file mode 100644 index 4fa4b5d..0000000 --- a/solution/H02/src/main/java/h02/OneDimensionalArrayStuff.java +++ /dev/null @@ -1,64 +0,0 @@ -package h02; - -import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired; - -/** - * This class serves as a container for the methods that are to be implemented by the students for exercise H2.1.1. - */ -public class OneDimensionalArrayStuff { - - /** - * Prevent instantiation of this utility class. - */ - private OneDimensionalArrayStuff() { - throw new IllegalStateException("This class is not meant to be instantiated."); - } - - /** - * Returns a new array that is a copy of the input array with the given value appended at the end. - * - * @param array the input array - * @param value the value to append - * @return a new array that is a copy of the input array with the given value appended at the end - */ - @StudentImplementationRequired("H2.1.1") - public static int[] push(final int[] array, final int value) { - final int[] newArray = new int[array.length + 1]; - //noinspection ManualArrayCopy - for (int i = 0; i < array.length; i++) { - newArray[i] = array[i]; - } - newArray[array.length] = value; - return newArray; - } - - /** - * Calculates the next Fibonacci number based on the given array and returns a new array with the next Fibonacci - * number appended at the end. - * - * @param array the input array containing the last two Fibonacci numbers up to the current point - * @return a new array with the next Fibonacci number appended at the end - */ - @StudentImplementationRequired("H2.1.1") - public static int[] calculateNextFibonacci(final int[] array) { - return push(array, array[array.length - 1] + array[array.length - 2]); - } - - /** - * Returns the n-th Fibonacci number. - * - * @param n the index of the Fibonacci number to return - * @return the n-th Fibonacci number - */ - @StudentImplementationRequired("H2.1.1") - public static int fibonacci(final int n) { - if (n < 2) { - return n; // base case (n=0 or n=1) - } - int[] array = {0, 1}; - for (int i = 2; i <= n; i++) { - array = calculateNextFibonacci(array); - } - return array[array.length - 1]; - } -} diff --git a/solution/H02/src/main/java/h02/TwoDimensionalArrayStuff.java b/solution/H02/src/main/java/h02/TwoDimensionalArrayStuff.java deleted file mode 100644 index 2db8863..0000000 --- a/solution/H02/src/main/java/h02/TwoDimensionalArrayStuff.java +++ /dev/null @@ -1,89 +0,0 @@ -package h02; - -import org.tudalgo.algoutils.student.annotation.DoNotTouch; -import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired; - -import java.util.Arrays; - -/** - * This class serves as a container for the methods that are to be implemented by the students for exercise H2.1.2. - */ -public class TwoDimensionalArrayStuff { - - /** - * Prevent instantiation of this utility class. - */ - private TwoDimensionalArrayStuff() { - throw new IllegalStateException("This class is not meant to be instantiated."); - } - - /** - * Returns an array containing the number of occurrences of the query {@link String} in each line of the input array. - * - * @param input the input array - * @param query the query {@link String} - * @return an array containing the number of occurrences of the query {@link String} in each line of the input array - */ - @StudentImplementationRequired("H2.1.2") - public static int[] occurrences(final String[][] input, final String query) { - final int[] result = new int[input.length]; - for (int row = 0; row < input.length; row++) { - for (int col = 0; col < input[row].length; col++) { - if (input[row][col].equals(query)) { - result[row]++; - } - } - } - return result; - } - - /** - * Returns the mean of the input array. - * - * @param input the input array - * @return the mean of the input array - */ - @StudentImplementationRequired("H2.1.2") - public static float mean(final int[] input) { - int sum = 0; - for (final int j : input) { - sum += j; - } - return (float) sum / input.length; - } - - /** - * Returns the mean number of occurrences of the query {@link String} in each line of the input array. - * - * @param input the input array - * @param query the query {@link String} - * @return the mean number of occurrences of the query {@link String} in each line of the input array - */ - @DoNotTouch - public static float meanOccurrencesPerLine(final String[][] input, final String query) { - return mean(occurrences(input, query)); - } - - /** - * Overload that splits the input string by lines and spaces, then calls regular meanOccurrencesPerLine. - * - * @param input the input string to split by lines and spaces - * @param query the query {@link String} - * @return the mean number of occurrences of the query {@link String} in each line of the input array - */ - @DoNotTouch - public static float meanOccurrencesPerLine(final String input, final String query) { - // filter out unwanted symbols - final String filteredInput = input.replaceAll("[^\\w\\s]", ""); - // split by lines - final var processedInput = Arrays.stream(filteredInput.split("(\\r\\n|\\r|\\n)")) - // split by spaces - .map(line -> line.split("\\s")) - // collect to 2D array - .toArray(String[][]::new); - /// uncomment the following line to log processed input - // System.out.printf("Processed input: %s%n", Arrays.deepToString(processedInput)); - // call regular meanOccurrencesPerLine - return meanOccurrencesPerLine(processedInput, query); - } -} diff --git a/solution/H02/src/main/java/h02/template/InputHandler.java b/solution/H02/src/main/java/h02/template/InputHandler.java deleted file mode 100644 index cb65bba..0000000 --- a/solution/H02/src/main/java/h02/template/InputHandler.java +++ /dev/null @@ -1,201 +0,0 @@ -package h02.template; - -import fopbot.RobotFamily; -import fopbot.World; -import h02.FourWins; -import org.tudalgo.algoutils.student.annotation.DoNotTouch; - -import javax.swing.*; -import javax.swing.border.EmptyBorder; -import java.awt.*; -import java.awt.event.ComponentAdapter; -import java.awt.event.ComponentEvent; -import java.beans.PropertyChangeEvent; -import java.util.concurrent.BlockingDeque; -import java.util.concurrent.LinkedBlockingDeque; -import java.util.concurrent.atomic.AtomicBoolean; -import java.util.function.Supplier; - -/** - * The {@link InputHandler} handles the input of the users. - */ -@DoNotTouch -public class InputHandler { - /** - * The input queue. - */ - private final BlockingDeque inputQueue = new LinkedBlockingDeque<>(); - - /** - * The {@link FourWins} instance. - */ - private final FourWins fourWins; - - /** - * Whether the row select mode is active. - */ - private final AtomicBoolean rowSelectMode = new AtomicBoolean(false); - - /** - * The status label. - */ - private final JLabel statusLabel = new JLabel("", SwingConstants.CENTER); - - /** - * Creates a new {@link InputHandler} instance. - * - * @param fourWins the {@link FourWins} instance - */ - public InputHandler(final FourWins fourWins) { - this.fourWins = fourWins; - final int padding = 4; // Padding in pixels - statusLabel.setBorder(new EmptyBorder(padding, padding, padding, padding)); - } - - /** - * Sets the color of the given column to the given color. - * - * @param column the column to set the color of - * @param colorSupplier the color to set - */ - private void setColumnColor(final int column, final Supplier colorSupplier) { - for (int i = 0; i < World.getHeight(); i++) { - final int finalI = i; - SwingUtilities.invokeLater(() -> World.getGlobalWorld().getField(column, finalI).setFieldColor(colorSupplier)); - } - } - - /** - * Executes the given action only if the game is running. - * - * @param action the action to execute - */ - private void whenGameIsRunning(final Runnable action) { - if (!fourWins.isFinished()) { - action.run(); - } - } - - /** - * Installs the input handler to the fopbot world. - */ - @SuppressWarnings("UnstableApiUsage") - public void install() { - final var guiPanel = World.getGlobalWorld().getGuiPanel(); - final var guiFrame = World.getGlobalWorld().getGuiFrame(); - World.getGlobalWorld().getInputHandler().addFieldClickListener( - e -> whenGameIsRunning(() -> addInput(e.getField().getX())) - ); - World.getGlobalWorld().getInputHandler().addFieldHoverListener(e -> whenGameIsRunning(() -> { - // deselect last hovered field, if any - if (e.getPreviousField() != null) { - setColumnColor(e.getPreviousField().getX(), () -> null); - } - if (rowSelectMode.get()) { - // select current hovered field - if (e.getField() != null) { - setColumnColor( - e.getField().getX(), - () -> guiPanel.isDarkMode() - ? Color.yellow - : Color.orange - ); - } - } - })); - statusLabel.setFont(statusLabel.getFont().deriveFont(guiPanel.scale(20.0f))); - guiFrame.add(statusLabel, BorderLayout.NORTH); - guiFrame.pack(); - guiPanel.addDarkModeChangeListener(this::onDarkModeChange); - guiPanel.addComponentListener(new ComponentAdapter() { - @Override - public void componentResized(final ComponentEvent e) { - statusLabel.setFont( - statusLabel.getFont().deriveFont( - Math.max(20f, 0.04f * Math.min(guiPanel.getWidth(), guiPanel.getHeight())) - ) - ); - } - }); - // trigger dark mode change to set the correct color - guiPanel.setDarkMode(World.getGlobalWorld().getGuiPanel().isDarkMode()); - } - - /** - * Called when the dark mode changes. - * - * @param e the property change event - */ - @SuppressWarnings("UnstableApiUsage") - public void onDarkModeChange(final PropertyChangeEvent e) { - final var darkMode = (boolean) e.getNewValue(); - statusLabel.setForeground(darkMode ? Color.white : Color.black); - World.getGlobalWorld().getGuiFrame().getContentPane().setBackground(darkMode ? Color.black : Color.white); - } - - /** - * Adds an input to the input queue. When {@link #getNextInput(RobotFamily, RobotFamily[][])} is called, the program - * will wait until this method is called. - * - * @param input the input to add - */ - public void addInput(final int input) { - inputQueue.add(input); - } - - /** - * Returns the next input from the input queue. If the input is invalid, the user will be prompted to enter a new - * input. The program will halt until a valid input is entered. - * - * @param currentPlayer the current player - * @param stones the current state of the game board - * @return the next input from the input queue - */ - public int getNextInput(final RobotFamily currentPlayer, final RobotFamily[][] stones) { - rowSelectMode.set(true); - statusLabel.setText( - "Click on a column to insert a disc.
Current Player: %s".formatted(currentPlayer.getName()) - ); - try { - final int input = inputQueue.take(); - System.out.println("Received column input: " + input); - if (!FourWins.validateInput(input, stones)) { - System.out.println("Invalid column input, please try again."); - return getNextInput(currentPlayer, stones); - } - rowSelectMode.set(false); - return input; - } catch (final InterruptedException e) { - rowSelectMode.set(false); - throw new RuntimeException(e); - } - } - - /** - * Sets a status message, saying that the game has ended in a draw. - */ - public void displayDrawStatus() { - statusLabel.setText("No valid columns found.
Hence, game ends with a draw."); - } - - /** - * Sets a status message, saying that the game has ended with a winner. - * - * @param winner the winner of the game - */ - public void displayWinnerStatus(final RobotFamily winner) { - statusLabel.setText("Player %s has won the game!".formatted(winner.getName())); - } - - /** - * Returns the {@link #statusLabel} of this {@link InputHandler}. - * - *

Use the {@link JLabel#getText()} method to get the current text of the label, and the - * {@link JLabel#setText(String)} method to update the text. - * - * @return the {@link #statusLabel} of this {@link InputHandler} - */ - public JLabel getStatusLabel() { - return statusLabel; - } -} diff --git a/solution/H02/src/main/resources/h02.properties b/solution/H02/src/main/resources/h02.properties deleted file mode 100644 index 58ef558..0000000 --- a/solution/H02/src/main/resources/h02.properties +++ /dev/null @@ -1,2 +0,0 @@ -FW_WORLD_WIDTH = 7 -FW_WORLD_HEIGHT = 6 diff --git a/solution/H02/src/test b/solution/H02/src/test new file mode 120000 index 0000000..6708b02 --- /dev/null +++ b/solution/H02/src/test @@ -0,0 +1 @@ +/home/osh/Desktop/FOP-2425-Marathon/H02/src/test \ No newline at end of file diff --git a/solution/H02/src/test/java/h02/ExampleJUnitTest.java b/solution/H02/src/test/java/h02/ExampleJUnitTest.java deleted file mode 100644 index 6e4a903..0000000 --- a/solution/H02/src/test/java/h02/ExampleJUnitTest.java +++ /dev/null @@ -1,16 +0,0 @@ -package h02; - -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.*; - -/** - * An example JUnit test class. - */ -public class ExampleJUnitTest { - - @Test - public void testAddition() { - assertEquals(2, 1 + 1); - } -} diff --git a/solution/H03/src/main b/solution/H03/src/main new file mode 120000 index 0000000..91748dd --- /dev/null +++ b/solution/H03/src/main @@ -0,0 +1 @@ +/home/osh/Desktop/FOP-2425-Marathon/H03/src/main \ No newline at end of file diff --git a/solution/H03/src/main/java/h03/Main.java b/solution/H03/src/main/java/h03/Main.java deleted file mode 100644 index 9e1cd5c..0000000 --- a/solution/H03/src/main/java/h03/Main.java +++ /dev/null @@ -1,70 +0,0 @@ -package h03; - -import fopbot.World; -import h03.robots.DoublePowerRobot; -import h03.robots.HackingRobot; -import h03.robots.MovementType; -import h03.robots.VersatileRobot; - -/** - * Main entry point in executing the program. - */ -public class Main { - /** - * Main entry point in executing the program. - * - * @param args program arguments, currently ignored - */ - public static void main(String[] args) { - // Create a 5x5 world and make it visible - World.setSize(5, 5); - World.setVisible(true); - - // Create at least one Hacking Robot with different positions and both cases for the array shift - HackingRobot hackingRobot1 = new HackingRobot(1, 1, true); - HackingRobot hackingRobot2 = new HackingRobot(2, 2, false); - - // Change the type of the Hacking Robot and check the current and next type - hackingRobot1.shuffle(); - System.out.println("HackingRobot1 current type: " + hackingRobot1.getType()); - System.out.println("HackingRobot1 next type: " + hackingRobot1.getNextType()); - - hackingRobot2.shuffle(); - System.out.println("HackingRobot2 current type: " + hackingRobot2.getType()); - System.out.println("HackingRobot2 next type: " + hackingRobot2.getNextType()); - - // Create at least two Versatile Robots with both cases for coordinate exchange - VersatileRobot versatileRobot1 = new VersatileRobot(1, 2, true, false); - VersatileRobot versatileRobot2 = new VersatileRobot(3, 4, false, true); - - // Change the type of the Versatile Robot until the type is DIAGONAL and check coordinates - while (versatileRobot1.getType() != MovementType.DIAGONAL) { - versatileRobot1.shuffle(); - } - System.out.println("VersatileRobot1 type is DIAGONAL. x: " + versatileRobot1.getX() + ", y: " + versatileRobot1.getY()); - - while (versatileRobot2.getType() != MovementType.DIAGONAL) { - versatileRobot2.shuffle(); - } - System.out.println("VersatileRobot2 type is DIAGONAL. x: " + versatileRobot2.getX() + ", y: " + versatileRobot2.getY()); - - // Create at least three Double Power Robots and change their types to get all movement types - DoublePowerRobot doublePowerRobot1 = new DoublePowerRobot(0, 0, true); - DoublePowerRobot doublePowerRobot2 = new DoublePowerRobot(1, 1, false); - DoublePowerRobot doublePowerRobot3 = new DoublePowerRobot(2, 2, true); - - // Create a RobotsChallenge with previously created Double Power Robots - DoublePowerRobot[] robots = {doublePowerRobot1, doublePowerRobot2, doublePowerRobot3}; - RobotsChallenge challenge = new RobotsChallenge(0, 2, robots); - - // Find and display the winning Double Power Robots - DoublePowerRobot[] winners = challenge.findWinners(); - System.out.println("Winning DoublePowerRobots:"); - for (DoublePowerRobot winner : winners) { - if (winner != null) { - //print the winner robot's coordinates - System.out.println("Winner robot coordinates: x: " + winner.getX() + ", y: " + winner.getY()); - } - } - } -} diff --git a/solution/H03/src/main/java/h03/RobotsChallenge.java b/solution/H03/src/main/java/h03/RobotsChallenge.java deleted file mode 100644 index 30c4a0a..0000000 --- a/solution/H03/src/main/java/h03/RobotsChallenge.java +++ /dev/null @@ -1,88 +0,0 @@ -package h03; - -import h03.robots.DoublePowerRobot; -import h03.robots.MovementType; - -/** - * The {@code RobotsChallenge} class performs a challenge between robots of the {@code DoublePowerRobot} class. - */ -public class RobotsChallenge { - - private final DoublePowerRobot[] robots; - private final int goal; - private final int begin; - private final int winThreshold = 2; - - /** - * Constructs a new {@code RobotsChallenge} with the specified starting position, goal, and array of robots. - * - * @param begin The starting position of the robots. - * @param goal The target coordinates. - * @param robots The array of {@code DoublePowerRobot} objects participating in the challenge. - */ - public RobotsChallenge(int begin, int goal, final DoublePowerRobot[] robots) { - this.begin = begin / 2; - this.goal = goal; - this.robots = robots; - } - - /** - * Calculates the number of steps needed for a robot to reach the goal for the diagonal type. - * - * @return The number of steps required to reach the goal. - */ - public int calculateStepsDiagonal() { - return Math.abs(begin - goal); - } - - /** - * Calculates the number of steps needed for a robot to reach the goal for the overstep type. - * - * @return The number of steps required to reach the goal. - */ - public int calculateStepsOverstep() { - return (Math.abs(begin - goal) % 2 == 0) ? Math.abs(begin - goal) : Math.abs(begin - goal) + 1; - } - - /** - * Calculates the number of steps needed for a robot to reach the goal for the teleport type. - * - * @return The number of steps required to reach the goal. - */ - public int calculateStepsTeleport() { - return (Math.abs(begin - goal) % 2 == 0) ? Math.abs(begin - goal) / 2 : (Math.abs(begin - goal) / 2) + 2; - } - - /** - * Calculates the number of steps needed for a robot to reach the goal based on its movement type. - * - * @param type The {@code MovementType} of the robot. - * @return The number of steps required to reach the goal. - */ - public int calculateSteps(MovementType type) { - return type == MovementType.DIAGONAL ? calculateStepsDiagonal() : type == MovementType.OVERSTEP ? calculateStepsOverstep() : calculateStepsTeleport(); - } - - /** - * Finds the winning robots in the challenge based on their movement types and the number of steps required to reach the goal. - * - * @return An array of {@code DoublePowerRobot} objects that are the winners of the challenge. - */ - public DoublePowerRobot[] findWinners() { - int winnerCount = 0; - DoublePowerRobot[] winners = new DoublePowerRobot[robots.length]; - - for (DoublePowerRobot robot : robots) { - int stepsFirstType = calculateSteps(robot.getType()); - int stepsSecondType = calculateSteps(robot.getNextType()); - int steps = Math.min(stepsFirstType, stepsSecondType); - - if (steps <= winThreshold) { - winners[winnerCount] = robot; - winnerCount++; - } - } - - return winners; - } -} diff --git a/solution/H03/src/main/java/h03/robots/DoublePowerRobot.java b/solution/H03/src/main/java/h03/robots/DoublePowerRobot.java deleted file mode 100644 index f67f1fa..0000000 --- a/solution/H03/src/main/java/h03/robots/DoublePowerRobot.java +++ /dev/null @@ -1,57 +0,0 @@ -package h03.robots; - -/** - * Subclass DoublePowerRobot, which inherits from the {@code HackingRobot} class and allows the robot to have two types simultaneously. - */ -public class DoublePowerRobot extends HackingRobot { - - /** - * Private array doublePowerTypes containing the two types for the DoublePowerRobot. - */ - private MovementType[] doublePowerTypes = new MovementType[2]; - - /** - * Constructor of the DoublePowerRobot class with parameters x, y, and order. - * Initializes the robot and assigns two movement types to the robot. - * - * @param x The x-coordinate of the robot. - * @param y The y-coordinate of the robot. - * @param order If true, the movement types are shifted to the right by one index, otherwise to the left by one index. - */ - public DoublePowerRobot(int x, int y, boolean order) { - super(x, y, order); - - // Assigning the two types to doublePowerTypes - doublePowerTypes[0] = getType(); - doublePowerTypes[1] = getNextType(); - } - - /** - * Overrides the shuffle method of the superclass. - * Shuffles the robot's type a specified number of times and updates the types in doublePowerTypes. - * - * @param itNr The number of iterations to shuffle the type. - * @return True if the types have changed, false otherwise. - */ - @Override - public boolean shuffle(int itNr) { - boolean changed = super.shuffle(itNr); - - // Updating the types in doublePowerTypes based on the new value of type - doublePowerTypes[0] = getType(); - doublePowerTypes[1] = getNextType(); - - return changed; - } - - /** - * Overrides the shuffle method of the superclass. - * Shuffles the robot's type until the type is different from the current type and updates the types in doublePowerTypes. - */ - @Override - public void shuffle() { - super.shuffle(); - doublePowerTypes[0] = getType(); - doublePowerTypes[1] = getNextType(); - } -} diff --git a/solution/H03/src/main/java/h03/robots/HackingRobot.java b/solution/H03/src/main/java/h03/robots/HackingRobot.java deleted file mode 100644 index a947bcd..0000000 --- a/solution/H03/src/main/java/h03/robots/HackingRobot.java +++ /dev/null @@ -1,111 +0,0 @@ -package h03.robots; - -import fopbot.Robot; -import java.util.Random; - -/** - * The HackingRobot class extends the Robot class and provides additional methods for movement in the grid. - * The robot can have different types of movements which can be shuffled. - */ -public class HackingRobot extends Robot { - - /** - * Private array "robotTypes" containing the elements of the enumeration MovementType in reverse alphabetical order. - */ - private MovementType[] robotTypes = {MovementType.TELEPORT, MovementType.OVERSTEP, MovementType.DIAGONAL}; - - /** - * Private variable that contains the type of the robot. - */ - private MovementType type; - - /** - * Constructs a new HackingRobot at the specified coordinates. - * The order parameter determines the initial order of the movement types. - * - * @param x The x-coordinate of the robot. - * @param y The y-coordinate of the robot. - * @param order If true, the movement types are shifted to the right by one index, otherwise to the left by one index. - */ - public HackingRobot(int x, int y, boolean order) { - super(x, y); - - if (order) { - // Move elements to the right by 1 index - MovementType lastElement = robotTypes[robotTypes.length - 1]; - for (int i = robotTypes.length - 1; i > 0; i--) { - robotTypes[i] = robotTypes[i - 1]; - } - robotTypes[0] = lastElement; - } else { - // Move elements to the left by 1 index - MovementType firstElement = robotTypes[0]; - for (int i = 0; i < robotTypes.length - 1; i++) { - robotTypes[i] = robotTypes[i + 1]; - } - robotTypes[robotTypes.length - 1] = firstElement; - } - - this.type = robotTypes[0]; - } - - /** - * Returns the current type of the robot. - * - * @return The current MovementType of the robot. - */ - public MovementType getType() { - return type; - } - - /** - * Returns the movement type located 1 index to the right of the current type of the robot. - * - * @return The next MovementType of the robot. - */ - public MovementType getNextType() { - int currentIndex = -1; - for (int i = 0; i < robotTypes.length; i++) { - if (robotTypes[i] == type) { - currentIndex = i; - break; - } - } - return robotTypes[(currentIndex + 1) % robotTypes.length]; - } - - /** - * Generates a random number between zero (inclusive) and the specified limit (exclusive). - * - * @param limit The upper bound (exclusive) for the random number. - * @return A random integer between 0 (inclusive) and the specified limit (exclusive). - */ - public int getRandom(int limit) { - Random random = new Random(); - return random.nextInt(limit); - } - - /** - * Randomly changes the type of the robot a specified number of times. - * - * @param itNr The number of iterations to shuffle the type. - * @return True if the type changed after shuffling, false otherwise. - */ - public boolean shuffle(int itNr) { - MovementType previousType = this.type; - for (int i = 0; i < itNr; i++) { - int randomIndex = getRandom(robotTypes.length); - this.type = robotTypes[randomIndex]; - } - - return this.type != previousType; - } - - /** - * Randomly changes the type of the robot until the type is different from the current type. - */ - public void shuffle() { - while (!shuffle(1)) { - } - } -} diff --git a/solution/H03/src/main/java/h03/robots/MovementType.java b/solution/H03/src/main/java/h03/robots/MovementType.java deleted file mode 100644 index e5deff1..0000000 --- a/solution/H03/src/main/java/h03/robots/MovementType.java +++ /dev/null @@ -1,21 +0,0 @@ -package h03.robots; - -/** - * The {@code MovementType} enum represents the different types of movements that a robot can perform. - */ -public enum MovementType { - /** - * Represents diagonal movement. - */ - DIAGONAL, - - /** - * Represents overstepping movement. - */ - OVERSTEP, - - /** - * Represents teleportation movement. - */ - TELEPORT -} diff --git a/solution/H03/src/main/java/h03/robots/VersatileRobot.java b/solution/H03/src/main/java/h03/robots/VersatileRobot.java deleted file mode 100644 index 5d40897..0000000 --- a/solution/H03/src/main/java/h03/robots/VersatileRobot.java +++ /dev/null @@ -1,59 +0,0 @@ -package h03.robots; - -/** - * Subclass VersatileRobot, which inherits from the class {@code HackingRobot}. - * This robot can switch its coordinates and has specific behavior when its type is DIAGONAL. - */ -public class VersatileRobot extends HackingRobot { - - /** - * Constructor of the VersatileRobot class with the parameters x, y, order, and exchange. - * Initializes the robot and optionally exchanges its coordinates. - * - * @param x The x-coordinate of the robot. - * @param y The y-coordinate of the robot. - * @param order If true, the movement types are shifted to the right by one index, otherwise to the left by one index. - * @param exchange If true, the coordinates x and y are exchanged. - */ - public VersatileRobot(int x, int y, boolean order, boolean exchange) { - super(x, y, order); - - if (exchange) { - int aux = x; - setX(y); - setY(aux); - } - - if (getType() == MovementType.DIAGONAL) { - setY(getX()); - } - } - - /** - * Overrides the shuffle method of the superclass. - * Shuffles the robot's type a specified number of times and adjusts the y-coordinate if the type is DIAGONAL. - * - * @param itNr The number of iterations to shuffle the type. - * @return True if the types have changed, false otherwise. - */ - @Override - public boolean shuffle(int itNr) { - boolean changed = super.shuffle(itNr); - if (getType() == MovementType.DIAGONAL) { - setY(getX()); - } - return changed; - } - - /** - * Overrides the shuffle method of the superclass. - * Shuffles the robot's type until the type is different from the current type and adjusts the y-coordinate if the type is DIAGONAL. - */ - @Override - public void shuffle() { - super.shuffle(); - if (getType() == MovementType.DIAGONAL) { - setY(getX()); - } - } -} diff --git a/solution/H03/src/test b/solution/H03/src/test new file mode 120000 index 0000000..e38feae --- /dev/null +++ b/solution/H03/src/test @@ -0,0 +1 @@ +/home/osh/Desktop/FOP-2425-Marathon/H03/src/test \ No newline at end of file diff --git a/solution/H03/src/test/java/h03/ExampleJUnitTest.java b/solution/H03/src/test/java/h03/ExampleJUnitTest.java deleted file mode 100644 index 8a13962..0000000 --- a/solution/H03/src/test/java/h03/ExampleJUnitTest.java +++ /dev/null @@ -1,16 +0,0 @@ -package h03; - -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.*; - -/** - * An example JUnit test class. - */ -public class ExampleJUnitTest { - - @Test - public void testAddition() { - assertEquals(2, 1 + 1); - } -} diff --git a/solution/H04/src/main b/solution/H04/src/main new file mode 120000 index 0000000..5d5d8b9 --- /dev/null +++ b/solution/H04/src/main @@ -0,0 +1 @@ +/home/osh/Desktop/FOP-2425-Marathon/H04/src/main \ No newline at end of file diff --git a/solution/H04/src/main/java/h04/GameController.java b/solution/H04/src/main/java/h04/GameController.java deleted file mode 100644 index 6400f54..0000000 --- a/solution/H04/src/main/java/h04/GameController.java +++ /dev/null @@ -1,18 +0,0 @@ -package h04; - -import h04.template.ChessUtils; -import h04.template.GameControllerTemplate; -import h04.chesspieces.King; - -public class GameController extends GameControllerTemplate { - public GameController() { - super(); - setup(); - } - - @Override - public boolean checkWinCondition() { - final King[] kings = ChessUtils.getKings(); - return (kings[0].isTurnedOff() || kings[1].isTurnedOff()); - } -} diff --git a/solution/H04/src/main/java/h04/Main.java b/solution/H04/src/main/java/h04/Main.java deleted file mode 100644 index 9a639e2..0000000 --- a/solution/H04/src/main/java/h04/Main.java +++ /dev/null @@ -1,17 +0,0 @@ -package h04; - -import fopbot.World; - -/** - * Main entry point in executing the program. - */ -public class Main { - /** - * Main entry point in executing the program. - * - * @param args program arguments, currently ignored - */ - public static void main(final String[] args) { - new GameController().startGame(); - } -} diff --git a/solution/H04/src/main/java/h04/chesspieces/Bishop.java b/solution/H04/src/main/java/h04/chesspieces/Bishop.java deleted file mode 100644 index 7f9ad01..0000000 --- a/solution/H04/src/main/java/h04/chesspieces/Bishop.java +++ /dev/null @@ -1,29 +0,0 @@ -package h04.chesspieces; - -import fopbot.Robot; -import h04.movement.DiagonalMover; -import h04.movement.MoveStrategy; - -import java.awt.Point; - - -public class Bishop extends Robot implements ChessPiece, DiagonalMover { - - private final Team team; - - public Bishop(final int x, final int y, final Team team){ - super(x, y, team == Team.WHITE ? Families.BISHOP_WHITE : Families.BISHOP_BLACK); - this.team = team; - } - - @Override - public Team getTeam() { return team;} - - @Override - public void moveStrategy(final int dx, final int dy, final MoveStrategy strategy){strategy.move(this, dx, dy);} - - @Override - public Point[] getPossibleMoveFields() { - return getDiagonalMoves(); - } -} diff --git a/solution/H04/src/main/java/h04/chesspieces/ChessPiece.java b/solution/H04/src/main/java/h04/chesspieces/ChessPiece.java deleted file mode 100644 index a6354bd..0000000 --- a/solution/H04/src/main/java/h04/chesspieces/ChessPiece.java +++ /dev/null @@ -1,20 +0,0 @@ -package h04.chesspieces; - -import h04.movement.MoveStrategy; - -import java.awt.Point; - -public interface ChessPiece { - Team getTeam(); - int getX(); - int getY(); - /** - * Returns {@code true} if this robot is turned off. - * - * @return {@code true} if this robot is turned off - */ - boolean isTurnedOff(); - void turnOff(); - void moveStrategy(int dx, int dy, MoveStrategy strategy); - Point[] getPossibleMoveFields(); -} diff --git a/solution/H04/src/main/java/h04/chesspieces/Families.java b/solution/H04/src/main/java/h04/chesspieces/Families.java deleted file mode 100644 index e1df0a4..0000000 --- a/solution/H04/src/main/java/h04/chesspieces/Families.java +++ /dev/null @@ -1,81 +0,0 @@ -package h04.chesspieces; - -import fopbot.RobotFamily; -import fopbot.SvgBasedRobotFamily; - -import java.awt.Color; - -public class Families { - public static RobotFamily PAWN_WHITE = new SvgBasedRobotFamily( - "PAWN_WHITE", - "/pieces/pawn_white.svg", - "/pieces/pawn_white.svg", - Color.WHITE - ); - public static RobotFamily PAWN_BLACK = new SvgBasedRobotFamily( - "PAWN_BLACK", - "/pieces/pawn_black.svg", - "/pieces/pawn_black.svg", - Color.BLACK - ); - public static RobotFamily ROOK_WHITE = new SvgBasedRobotFamily( - "ROOK_WHITE", - "/pieces/rook_white.svg", - "/pieces/rook_white.svg", - Color.WHITE - ); - public static RobotFamily ROOK_BLACK = new SvgBasedRobotFamily( - "ROOK_BLACK", - "/pieces/rook_black.svg", - "/pieces/rook_black.svg", - Color.BLACK - ); - public static RobotFamily KNIGHT_WHITE = new SvgBasedRobotFamily( - "KNIGHT_WHITE", - "/pieces/knight_white.svg", - "/pieces/knight_white.svg", - Color.WHITE - ); - public static RobotFamily KNIGHT_BLACK = new SvgBasedRobotFamily( - "KNIGHT_BLACK", - "/pieces/knight_black.svg", - "/pieces/knight_black.svg", - Color.BLACK - ); - public static RobotFamily BISHOP_WHITE = new SvgBasedRobotFamily( - "BISHOP_WHITE", - "/pieces/bishop_white.svg", - "/pieces/bishop_white.svg", - Color.WHITE - ); - public static RobotFamily BISHOP_BLACK = new SvgBasedRobotFamily( - "BISHOP_BLACK", - "/pieces/bishop_black.svg", - "/pieces/bishop_black.svg", - Color.BLACK - ); - public static RobotFamily QUEEN_WHITE = new SvgBasedRobotFamily( - "QUEEN_WHITE", - "/pieces/queen_white.svg", - "/pieces/queen_white.svg", - Color.WHITE - ); - public static RobotFamily QUEEN_BLACK = new SvgBasedRobotFamily( - "QUEEN_BLACK", - "/pieces/queen_black.svg", - "/pieces/queen_black.svg", - Color.BLACK - ); - public static RobotFamily KING_WHITE = new SvgBasedRobotFamily( - "KING_WHITE", - "/pieces/king_white.svg", - "/pieces/king_white.svg", - Color.WHITE - ); - public static RobotFamily KING_BLACK = new SvgBasedRobotFamily( - "KING_BLACK", - "/pieces/king_black.svg", - "/pieces/king_black.svg", - Color.BLACK - ); -} diff --git a/solution/H04/src/main/java/h04/chesspieces/King.java b/solution/H04/src/main/java/h04/chesspieces/King.java deleted file mode 100644 index f8b923c..0000000 --- a/solution/H04/src/main/java/h04/chesspieces/King.java +++ /dev/null @@ -1,45 +0,0 @@ -package h04.chesspieces; - -import fopbot.Robot; -import h04.movement.MoveStrategy; -import h04.template.ChessUtils; - -import java.awt.Point; - -public class King extends Robot implements ChessPiece { - - private final Team team; - - public King(final int x, final int y, final Team team) { - super(x, y, team == Team.WHITE ? Families.KING_WHITE : Families.KING_BLACK); - this.team = team; - } - - @Override - public Team getTeam() { - return team; - } - - @Override - public void moveStrategy(final int dx, final int dy, final MoveStrategy strategy) { - strategy.move(this, dx, dy); - } - - @Override - public Point[] getPossibleMoveFields() { - return ChessUtils.getAllowedMoves( - this, - new Point[]{ - new Point(1, 0), - new Point(-1, 0), - new Point(0, 1), - new Point(0, -1), - new Point(1, 1), - new Point(-1, 1), - new Point(1, -1), - new Point(-1, -1) - }, - 1 - ); - } -} diff --git a/solution/H04/src/main/java/h04/chesspieces/Knight.java b/solution/H04/src/main/java/h04/chesspieces/Knight.java deleted file mode 100644 index 9b288dc..0000000 --- a/solution/H04/src/main/java/h04/chesspieces/Knight.java +++ /dev/null @@ -1,44 +0,0 @@ -package h04.chesspieces; - -import fopbot.Robot; -import h04.movement.MoveStrategy; -import h04.template.ChessUtils; - -import java.awt.Point; - -public class Knight extends Robot implements ChessPiece { - - private final Team team; - - public Knight(final int x, final int y, final Team team) { - super(x, y, team == Team.WHITE ? Families.KNIGHT_WHITE : Families.KNIGHT_BLACK); - this.team = team; - } - - @Override - public Team getTeam() { - return team; - } - - @Override - public void moveStrategy(final int dx, final int dy, final MoveStrategy strategy) { - strategy.move(this, dx, dy); - } - - @Override - public Point[] getPossibleMoveFields() { - final Point[] possibleMoves = new Point[8]; - int index = 0; - for (final Point delta : new Point[]{new Point(1, 2), new Point(2, 1)}) { - for (int i = 0; i < 4; i++) { - final Point move = new Point(getX() + delta.x, getY() + delta.y); - if (ChessUtils.isValidCoordinate(move) && ChessUtils.getTeamAt(move) != getTeam()) { - possibleMoves[index++] = move; - } - //noinspection SuspiciousNameCombination - delta.setLocation(-delta.y, delta.x); - } - } - return possibleMoves; - } -} diff --git a/solution/H04/src/main/java/h04/chesspieces/Pawn.java b/solution/H04/src/main/java/h04/chesspieces/Pawn.java deleted file mode 100644 index 09e98e8..0000000 --- a/solution/H04/src/main/java/h04/chesspieces/Pawn.java +++ /dev/null @@ -1,54 +0,0 @@ -package h04.chesspieces; - -import fopbot.Robot; -import h04.movement.MoveStrategy; - -import java.awt.Point; - -import static h04.template.ChessUtils.getTeamAt; -import static h04.template.ChessUtils.isValidCoordinate; - -public class Pawn extends Robot implements ChessPiece { - private final Team team; - - boolean firstMove = true; - - public Pawn(final int x, final int y, final Team team) { - super(x, y, team == Team.WHITE ? Families.PAWN_WHITE : Families.PAWN_BLACK); - this.team = team; - } - - @Override - public Team getTeam() { - return team; - } - - @Override - public void moveStrategy(final int dx, final int dy, final MoveStrategy strategy) { - strategy.move(this, dx, dy); - firstMove = false; - } - - @Override - public Point[] getPossibleMoveFields() { - final Point[] possibleMoves = new Point[4]; - int index = 0; - final int direction = team == Team.WHITE ? 1 : -1; - final Point front1 = new Point(getX(), getY() + direction); - if (isValidCoordinate(front1) && getTeamAt(front1) == null) { - possibleMoves[index++] = front1; - final Point front2 = new Point(getX(), getY() + 2 * direction); - if (firstMove && isValidCoordinate(front2) && getTeamAt(front2) == null) { - possibleMoves[index++] = front2; - } - } - //attack - for (final int i : new int[]{-1, 1}) { - final Point p = new Point(getX() + i, getY() + direction); - if (isValidCoordinate(p) && getTeamAt(p) == getTeam().getOpponent()) { - possibleMoves[index++] = p; - } - } - return possibleMoves; - } -} diff --git a/solution/H04/src/main/java/h04/chesspieces/Queen.java b/solution/H04/src/main/java/h04/chesspieces/Queen.java deleted file mode 100644 index 1cefef7..0000000 --- a/solution/H04/src/main/java/h04/chesspieces/Queen.java +++ /dev/null @@ -1,33 +0,0 @@ -package h04.chesspieces; - -import fopbot.Robot; -import h04.movement.DiagonalMover; -import h04.movement.MoveStrategy; -import h04.movement.OrthogonalMover; -import h04.template.ChessUtils; - -import java.awt.Point; - -public class Queen extends Robot implements OrthogonalMover, DiagonalMover { - private final Team team; - - public Queen(final int x, final int y, final Team team) { - super(x, y, team == Team.WHITE ? Families.QUEEN_WHITE : Families.QUEEN_BLACK); - this.team = team; - } - - @Override - public Team getTeam() { - return team; - } - - @Override - public void moveStrategy(final int dx, final int dy, final MoveStrategy strategy) { - strategy.move(this, dx, dy); - } - - @Override - public Point[] getPossibleMoveFields() { - return ChessUtils.mergePoints(getOrthogonalMoves(), getDiagonalMoves()); - } -} diff --git a/solution/H04/src/main/java/h04/chesspieces/Rook.java b/solution/H04/src/main/java/h04/chesspieces/Rook.java deleted file mode 100644 index b084a77..0000000 --- a/solution/H04/src/main/java/h04/chesspieces/Rook.java +++ /dev/null @@ -1,32 +0,0 @@ -package h04.chesspieces; - -import fopbot.Robot; -import h04.movement.MoveStrategy; -import h04.movement.OrthogonalMover; -import h04.template.ChessUtils; - -import java.awt.Point; - -public class Rook extends Robot implements OrthogonalMover { - private final Team team; - - public Rook(final int x, final int y, final Team team){ - super(x, y, team == Team.WHITE ? Families.ROOK_WHITE : Families.ROOK_BLACK); - this.team = team; - } - - @Override - public Team getTeam() { - return team; - } - - @Override - public void moveStrategy(final int dx, final int dy, final MoveStrategy strategy) { - strategy.move(this, dx, dy); - } - - @Override - public Point[] getPossibleMoveFields() { - return getOrthogonalMoves(); - } -} diff --git a/solution/H04/src/main/java/h04/chesspieces/Team.java b/solution/H04/src/main/java/h04/chesspieces/Team.java deleted file mode 100644 index 8c4365b..0000000 --- a/solution/H04/src/main/java/h04/chesspieces/Team.java +++ /dev/null @@ -1,25 +0,0 @@ -package h04.chesspieces; - -import java.awt.Color; - -public enum Team { - WHITE(Color.WHITE), - BLACK(Color.BLACK); - private final Color color; - - Team(final Color color) { - this.color = color; - } - - public Color getColor() { - return color; - } - - public boolean isOpponent(final Team other) { - return this != other; - } - - public Team getOpponent() { - return this == WHITE ? BLACK : WHITE; - } -} diff --git a/solution/H04/src/main/java/h04/movement/DiagonalMover.java b/solution/H04/src/main/java/h04/movement/DiagonalMover.java deleted file mode 100644 index e96bdf9..0000000 --- a/solution/H04/src/main/java/h04/movement/DiagonalMover.java +++ /dev/null @@ -1,21 +0,0 @@ -package h04.movement; - -import h04.chesspieces.ChessPiece; -import h04.template.ChessUtils; - -import java.awt.Point; - -public interface DiagonalMover extends ChessPiece { - default Point[] getDiagonalMoves() { - return ChessUtils.getAllowedMoves( - this, - new Point[]{ - new Point(1, 1), - new Point(-1, 1), - new Point(1, -1), - new Point(-1, -1) - }, - 7 - ); - } -} diff --git a/solution/H04/src/main/java/h04/movement/MoveStrategy.java b/solution/H04/src/main/java/h04/movement/MoveStrategy.java deleted file mode 100644 index 2a515bf..0000000 --- a/solution/H04/src/main/java/h04/movement/MoveStrategy.java +++ /dev/null @@ -1,8 +0,0 @@ -package h04.movement; - - -import fopbot.Robot; - -public interface MoveStrategy { - void move(Robot r, int dx, int dy); -} diff --git a/solution/H04/src/main/java/h04/movement/OrthogonalMover.java b/solution/H04/src/main/java/h04/movement/OrthogonalMover.java deleted file mode 100644 index fb1c5ff..0000000 --- a/solution/H04/src/main/java/h04/movement/OrthogonalMover.java +++ /dev/null @@ -1,21 +0,0 @@ -package h04.movement; - -import h04.chesspieces.ChessPiece; -import h04.template.ChessUtils; - -import java.awt.Point; - -public interface OrthogonalMover extends ChessPiece { - default Point[] getOrthogonalMoves() { - return ChessUtils.getAllowedMoves( - this, - new Point[]{ - new Point(1, 0), - new Point(-1, 0), - new Point(0, 1), - new Point(0, -1) - }, - 7 - ); - } -} diff --git a/solution/H04/src/main/java/h04/movement/TeleportingMoveStrategy.java b/solution/H04/src/main/java/h04/movement/TeleportingMoveStrategy.java deleted file mode 100644 index c9a205a..0000000 --- a/solution/H04/src/main/java/h04/movement/TeleportingMoveStrategy.java +++ /dev/null @@ -1,10 +0,0 @@ -package h04.movement; - -import fopbot.Robot; - -public class TeleportingMoveStrategy implements MoveStrategy { - @Override - public void move(final Robot r, final int dx, final int dy) { - r.setField(r.getX() + dx, r.getY() + dy); - } -} diff --git a/solution/H04/src/main/java/h04/movement/WalkingMoveStrategy.java b/solution/H04/src/main/java/h04/movement/WalkingMoveStrategy.java deleted file mode 100644 index 3247755..0000000 --- a/solution/H04/src/main/java/h04/movement/WalkingMoveStrategy.java +++ /dev/null @@ -1,40 +0,0 @@ -package h04.movement; - - -import fopbot.Direction; -import fopbot.Robot; - -public class WalkingMoveStrategy implements MoveStrategy { - @Override - public void move(final Robot r, int dx, int dy) { - while (dx != 0) { - if (dx > 0) { - turnToDirection(r, Direction.RIGHT); - r.move(); - dx--; - } else { - turnToDirection(r, Direction.LEFT); - r.move(); - dx++; - } - } - while (dy != 0) { - if (dy > 0) { - turnToDirection(r, Direction.UP); - r.move(); - dy--; - } else { - turnToDirection(r, Direction.DOWN); - r.move(); - dy++; - } - } - turnToDirection(r, Direction.UP); - } - - private void turnToDirection(final Robot r, final Direction d) { - while (r.getDirection() != d) { - r.turnLeft(); - } - } -} diff --git a/solution/H04/src/main/java/h04/template/ChessUtils.java b/solution/H04/src/main/java/h04/template/ChessUtils.java deleted file mode 100644 index 57861fe..0000000 --- a/solution/H04/src/main/java/h04/template/ChessUtils.java +++ /dev/null @@ -1,117 +0,0 @@ -package h04.template; - -import fopbot.World; -import h04.chesspieces.ChessPiece; -import h04.chesspieces.King; -import h04.chesspieces.Team; -import org.jetbrains.annotations.Nullable; - -import java.awt.Color; -import java.awt.Point; -import java.util.Optional; -import java.util.function.Predicate; - -public class ChessUtils { - - public static @Nullable ChessPiece getPieceAt(final int x, final int y) { - return World.getGlobalWorld().getField(x, y).getEntities() - .stream() - .filter(ChessPiece.class::isInstance) - .map(ChessPiece.class::cast) - .filter(Predicate.not(ChessPiece::isTurnedOff)) - .findFirst() - .orElse(null); - } - - public static @Nullable ChessPiece getPieceAt(final Point p) { - return getPieceAt(p.x, p.y); - } - - public static King[] getKings() { - return World.getGlobalWorld().getAllFieldEntities() - .stream() - .filter(King.class::isInstance) - .map(King.class::cast) - .toArray(King[]::new); - } - - /** - * Returns the team at the given position, or {@code null} if there is no team. - * - * @param x the x-coordinate - * @param y the y-coordinate - * @return the team at the given position, or {@code null} if there is no team - */ - public static @Nullable Team getTeamAt(final int x, final int y) { - return Optional.ofNullable(getPieceAt(x, y)) - .map(ChessPiece::getTeam) - .orElse(null); - } - - public static @Nullable Team getTeamAt(final Point p) { - return getTeamAt(p.x, p.y); - } - - public static Point[] getAllowedMoves(final ChessPiece piece, final Point[] directions, final int maxDistance) { - final Point[] moves = new Point[directions.length * maxDistance]; - int index = 0; - for (final var p : directions) { - for (int i = 1; i <= maxDistance; i++) { - final var pos = new Point(piece.getX() + i * p.x, piece.getY() + i * p.y); - if (!isValidCoordinate(pos)) - break; - final var team = getTeamAt(pos); - if (team == piece.getTeam()) - break; - moves[index++] = pos; - if (team == piece.getTeam().getOpponent()) - break; - } - } - return moves; - } - - public static boolean isValidCoordinate(final int x, final int y) { - return x >= 0 && x < 8 && y >= 0 && y < 8; - } - - public static boolean isValidCoordinate(final Point p) { - return isValidCoordinate(p.x, p.y); - } - - public static Point[] mergePoints(final Point[] a, final Point[] b) { - final Point[] result = new Point[a.length + b.length]; - var index = 0; - for (final var p : a) { - if (p == null) - break; - result[index++] = p; - } - for (final var p : b) { - if (p == null) - break; - result[index++] = p; - } - return result; - } - - public static void setFieldColor(final Point field, final @Nullable Color c) { - World.getGlobalWorld().getField(field.x, field.y).setFieldColor(c); - } - - public static void colorMoveFields(final Point... fields) { - for (final Point field : fields) { - if (field == null) - continue; - setFieldColor(field, getPieceAt(field) != null ? Color.RED : Color.GREEN); - } - } - - public static void resetFieldColor() { - for (int y = 0; y < World.getHeight(); y++) { - for (int x = 0; x < World.getWidth(); x++) { - setFieldColor(new Point(x, y), null); - } - } - } -} diff --git a/solution/H04/src/main/java/h04/template/GameControllerTemplate.java b/solution/H04/src/main/java/h04/template/GameControllerTemplate.java deleted file mode 100644 index 4883e6c..0000000 --- a/solution/H04/src/main/java/h04/template/GameControllerTemplate.java +++ /dev/null @@ -1,179 +0,0 @@ -package h04.template; - -import fopbot.ColorProfile; -import fopbot.Robot; -import fopbot.World; -import h04.GameController; -import h04.template.InputHandler; -import h04.chesspieces.Bishop; -import h04.chesspieces.ChessPiece; -import h04.chesspieces.King; -import h04.chesspieces.Knight; -import h04.chesspieces.Pawn; -import h04.chesspieces.Queen; -import h04.chesspieces.Rook; -import h04.chesspieces.Team; -import h04.movement.MoveStrategy; -import h04.movement.TeleportingMoveStrategy; -import h04.movement.WalkingMoveStrategy; -import org.jetbrains.annotations.Nullable; -import org.tudalgo.algoutils.student.io.PropertyUtils; - -import java.awt.Color; -import java.util.Arrays; -import java.util.Objects; - -/** - * A {@link GameControllerTemplate} controls the game loop and the {@link Robot}s and checks the win condition. - */ -public abstract class GameControllerTemplate { - /** - * The {@link InputHandler} that handles the input of the user. - */ - private final InputHandler inputHandler = new InputHandler((GameController) this); - - /** - * The {@link Robot}s that are controlled by the {@link GameControllerTemplate}. - */ - - protected Team nextToMove = Team.WHITE; - - protected boolean gameOver = false; - - protected @Nullable ChessPiece selectedPiece; - - protected MoveStrategy moveStrategy = PropertyUtils.getBooleanProperty( - "h04.properties", - "USE_TELEPORT_MOVE_STRATEGY" - ) ? new TeleportingMoveStrategy() : new WalkingMoveStrategy(); - - - /** - * Starts the game loop. - */ - public void startGame() { - System.out.println("Starting game..."); - - while (!gameOver) { - final var point = inputHandler.getNextInput(nextToMove); - if (ChessUtils.getTeamAt(point) == nextToMove) { - // select piece - selectedPiece = ChessUtils.getPieceAt(point); - //highlight possible moves - final var possibleMoves = Objects.requireNonNull(selectedPiece).getPossibleMoveFields(); - ChessUtils.resetFieldColor(); - ChessUtils.colorMoveFields(possibleMoves); - continue; - } - if (selectedPiece != null) { - // check if piece can move to point - final var possibleMoves = selectedPiece.getPossibleMoveFields(); - if (Arrays.stream(possibleMoves) - .filter(Objects::nonNull) - .noneMatch(point::equals) - ) { - System.out.println("Invalid move"); - // deselect piece - selectedPiece = null; - ChessUtils.resetFieldColor(); - continue; - } - // valid move selected, move piece - final var oldPiece = ChessUtils.getPieceAt(point); - selectedPiece.moveStrategy( - point.x - selectedPiece.getX(), - point.y - selectedPiece.getY(), - moveStrategy - ); - if (oldPiece != null) { - oldPiece.turnOff(); - } - if (selectedPiece instanceof Pawn && (selectedPiece.getY() == 0 || selectedPiece.getY() == World.getHeight() - 1)) { - selectedPiece.turnOff(); - new Queen(selectedPiece.getX(), selectedPiece.getY(), selectedPiece.getTeam()); - } - ChessUtils.resetFieldColor(); - if (checkWinCondition()) stopGame(nextToMove); - - - nextToMove = nextToMove.getOpponent(); - selectedPiece = null; - } - } - } - - public abstract boolean checkWinCondition(); - - - /** - * Stops the game loop. - */ - public void stopGame(final Team winner) { - gameOver = true; - inputHandler.setStatusText(winner.name() + " wins!"); - } - - /** - * Sets up the game. - */ - protected void setup() { - setupWorld(); - setupTheme(); - setupPieces(); - //this.inputHandler.install(); - } - - public void setupTheme() { - //noinspection UnstableApiUsage - World.getGlobalWorld().setColorProfile( - ColorProfile.DEFAULT.toBuilder() - .fieldColorLight(Color.decode("#e0ba97")) - .fieldColorDark(Color.decode("#8d4d2a")) - .customFieldColorPattern( - (cp, p) -> (p.x + p.y) % 2 == 0 ? cp.fieldColorLight() : cp.fieldColorDark() - ) - .backgroundColorDark(Color.decode("#8d4d2a")) - .backgroundColorLight(Color.decode("#8d4d2a")) - .innerBorderColorLight(new Color(0, 0, 0, 0)) - .innerBorderColorDark(new Color(0, 0, 0, 0)) - .fieldBorderThickness(0) - .outerBorderColorDark(Color.BLACK) - .outerBorderColorLight(Color.BLACK) - .build() - ); - } - - /** - * Initializes the {@link World} and adds the {@link Robot}s to it. - */ - public void setupWorld() { - World.setSize(8, 8); - - World.setDelay(0); - World.setVisible(true); - World.getGlobalWorld().setDrawTurnedOffRobots(false); - inputHandler.install(); - } - - public void setupPieces() { - for (final int y : new int[]{0, 1, World.getHeight() - 2, World.getHeight() - 1}) { - for (int x = 0; x < World.getWidth(); x++) { - if (y == 1 || y == World.getHeight() - 2) - new Pawn(x, y, y == 1 ? Team.WHITE : Team.BLACK); - else if (x == 0 || x == World.getWidth() - 1) { - new Rook(x, y, y == 0 ? Team.WHITE : Team.BLACK); - } else if (x == 1 || x == World.getWidth() - 2) { - new Knight(x, y, y == 0 ? Team.WHITE : Team.BLACK); - } else if (x == 2 || x == World.getWidth() - 3) { - new Bishop(x, y, y == 0 ? Team.WHITE : Team.BLACK); - } else if (x == 3) { - new Queen(x, y, y == 0 ? Team.WHITE : Team.BLACK); - } else if (x == World.getWidth() - 4) { - new King(x, y, y == 0 ? Team.WHITE : Team.BLACK); - } - } - } - } - - -} diff --git a/solution/H04/src/main/java/h04/template/InputHandler.java b/solution/H04/src/main/java/h04/template/InputHandler.java deleted file mode 100644 index be7b499..0000000 --- a/solution/H04/src/main/java/h04/template/InputHandler.java +++ /dev/null @@ -1,68 +0,0 @@ -package h04.template; - -import fopbot.World; -import h04.GameController; -import h04.chesspieces.Team; - -import java.awt.Color; -import java.awt.Point; -import java.util.concurrent.BlockingDeque; -import java.util.concurrent.LinkedBlockingDeque; -import javax.swing.JLabel; -import java.beans.PropertyChangeEvent; - -public class InputHandler { - /** - * The input queue. - */ - private final BlockingDeque inputQueue = new LinkedBlockingDeque<>(); - - public final GameController controller; - - private final JLabel statusLabel = new JLabel(""); - - public InputHandler(final GameController controller) { - this.controller = controller; - } - - @SuppressWarnings("UnstableApiUsage") - public void install() { - final var guiPanel = World.getGlobalWorld().getGuiPanel(); - World.getGlobalWorld().getInputHandler().addFieldClickListener(e -> addInput(e.getField().getX(), e.getField().getY())); - statusLabel.setFont(statusLabel.getFont().deriveFont(20.0f)); - guiPanel.add(statusLabel, JLabel.CENTER); - guiPanel.addDarkModeChangeListener(this::onDarkModeChange); - // trigger dark mode change to set the correct color - guiPanel.setDarkMode(World.getGlobalWorld().getGuiPanel().isDarkMode()); - } - - public void onDarkModeChange(final PropertyChangeEvent e) { - final var darkMode = (boolean) e.getNewValue(); - statusLabel.setForeground(darkMode ? Color.white : Color.black); - } - - private void addInput(final int x, final int y) { - inputQueue.add(new Point(x,y)); - System.out.println("Added input: "+x+","+y); - } - - public Point getNextInput(final Team currentPlayer) { - - statusLabel.setText(currentPlayer.name()+" to move."); - - try { - return inputQueue.take(); - } catch (final InterruptedException e) { - throw new RuntimeException(e); - } - - } - - public JLabel getStatusLabel() { - return statusLabel; - } - - public void setStatusText(final String s) { - statusLabel.setText(s); - } -} diff --git a/solution/H04/src/main/resources/h04.properties b/solution/H04/src/main/resources/h04.properties deleted file mode 100644 index dd353c4..0000000 --- a/solution/H04/src/main/resources/h04.properties +++ /dev/null @@ -1,2 +0,0 @@ -# Whether to use the TeleportMoveStrategy or not. If not, the WalkingMoveStrategy will be used. -USE_TELEPORT_MOVE_STRATEGY=false diff --git a/solution/H04/src/main/resources/pieces/bishop_black.svg b/solution/H04/src/main/resources/pieces/bishop_black.svg deleted file mode 100644 index 6b816b6..0000000 --- a/solution/H04/src/main/resources/pieces/bishop_black.svg +++ /dev/null @@ -1,46 +0,0 @@ - - - - - - - - - - diff --git a/solution/H04/src/main/resources/pieces/bishop_white.svg b/solution/H04/src/main/resources/pieces/bishop_white.svg deleted file mode 100644 index 29a08b2..0000000 --- a/solution/H04/src/main/resources/pieces/bishop_white.svg +++ /dev/null @@ -1,47 +0,0 @@ - - - - - - - - - - diff --git a/solution/H04/src/main/resources/pieces/king_black.svg b/solution/H04/src/main/resources/pieces/king_black.svg deleted file mode 100644 index 5c9c769..0000000 --- a/solution/H04/src/main/resources/pieces/king_black.svg +++ /dev/null @@ -1,46 +0,0 @@ - - - - - - - - - - diff --git a/solution/H04/src/main/resources/pieces/king_white.svg b/solution/H04/src/main/resources/pieces/king_white.svg deleted file mode 100644 index 46941f4..0000000 --- a/solution/H04/src/main/resources/pieces/king_white.svg +++ /dev/null @@ -1,47 +0,0 @@ - - - - - - - - - - diff --git a/solution/H04/src/main/resources/pieces/knight_black.svg b/solution/H04/src/main/resources/pieces/knight_black.svg deleted file mode 100644 index 942d016..0000000 --- a/solution/H04/src/main/resources/pieces/knight_black.svg +++ /dev/null @@ -1,46 +0,0 @@ - - - - - - - - - - diff --git a/solution/H04/src/main/resources/pieces/knight_white.svg b/solution/H04/src/main/resources/pieces/knight_white.svg deleted file mode 100644 index e2b58fa..0000000 --- a/solution/H04/src/main/resources/pieces/knight_white.svg +++ /dev/null @@ -1,47 +0,0 @@ - - - - - - - - - - diff --git a/solution/H04/src/main/resources/pieces/pawn_black.svg b/solution/H04/src/main/resources/pieces/pawn_black.svg deleted file mode 100644 index d83505b..0000000 --- a/solution/H04/src/main/resources/pieces/pawn_black.svg +++ /dev/null @@ -1,46 +0,0 @@ - - - - - - - - - - diff --git a/solution/H04/src/main/resources/pieces/pawn_white.svg b/solution/H04/src/main/resources/pieces/pawn_white.svg deleted file mode 100644 index 2c431d5..0000000 --- a/solution/H04/src/main/resources/pieces/pawn_white.svg +++ /dev/null @@ -1,47 +0,0 @@ - - - - - - - - - - diff --git a/solution/H04/src/main/resources/pieces/queen_black.svg b/solution/H04/src/main/resources/pieces/queen_black.svg deleted file mode 100644 index 7a378ec..0000000 --- a/solution/H04/src/main/resources/pieces/queen_black.svg +++ /dev/null @@ -1,46 +0,0 @@ - - - - - - - - - - diff --git a/solution/H04/src/main/resources/pieces/queen_white.svg b/solution/H04/src/main/resources/pieces/queen_white.svg deleted file mode 100644 index 98e87c6..0000000 --- a/solution/H04/src/main/resources/pieces/queen_white.svg +++ /dev/null @@ -1,47 +0,0 @@ - - - - - - - - - - diff --git a/solution/H04/src/main/resources/pieces/rook_black.svg b/solution/H04/src/main/resources/pieces/rook_black.svg deleted file mode 100644 index 2787d91..0000000 --- a/solution/H04/src/main/resources/pieces/rook_black.svg +++ /dev/null @@ -1,46 +0,0 @@ - - - - - - - - - - diff --git a/solution/H04/src/main/resources/pieces/rook_white.svg b/solution/H04/src/main/resources/pieces/rook_white.svg deleted file mode 100644 index 670de3a..0000000 --- a/solution/H04/src/main/resources/pieces/rook_white.svg +++ /dev/null @@ -1,47 +0,0 @@ - - - - - - - - - - diff --git a/solution/H04/src/test b/solution/H04/src/test new file mode 120000 index 0000000..ca297bc --- /dev/null +++ b/solution/H04/src/test @@ -0,0 +1 @@ +/home/osh/Desktop/FOP-2425-Marathon/H04/src/test \ No newline at end of file diff --git a/solution/H04/src/test/java/h04/ExampleJUnitTest.java b/solution/H04/src/test/java/h04/ExampleJUnitTest.java deleted file mode 100644 index e3cc28c..0000000 --- a/solution/H04/src/test/java/h04/ExampleJUnitTest.java +++ /dev/null @@ -1,16 +0,0 @@ -package h04; - -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.*; - -/** - * An example JUnit test class. - */ -public class ExampleJUnitTest { - - @Test - public void testAddition() { - assertEquals(2, 1 + 1); - } -} diff --git a/solution/H05/src/main b/solution/H05/src/main new file mode 120000 index 0000000..4d41d26 --- /dev/null +++ b/solution/H05/src/main @@ -0,0 +1 @@ +/home/osh/Desktop/FOP-2425-Marathon/H05/src/main \ No newline at end of file diff --git a/solution/H05/src/main/java/h05/Airspace.java b/solution/H05/src/main/java/h05/Airspace.java deleted file mode 100644 index b6871bf..0000000 --- a/solution/H05/src/main/java/h05/Airspace.java +++ /dev/null @@ -1,57 +0,0 @@ -package h05; - -import java.util.Collections; -import java.util.HashSet; -import java.util.Set; - -public class Airspace { - - private static Airspace INSTANCE = null; - - - public static Airspace get(){ - if(INSTANCE == null){ - INSTANCE = new Airspace(); - } - return INSTANCE; - } - - - private final Set flyingInAirspace = new HashSet<>(); - - private Airspace(){ - - } - - Set getFlyingInAirspace() { - return Collections.unmodifiableSet(flyingInAirspace); - } - - void register(Flying flying){ - flyingInAirspace.add(flying); - } - - void deregister(Flying flying){ - flyingInAirspace.remove(flying); - } - - - void scanAirspace(){ - System.out.println("Scanning..."); - - if (flyingInAirspace.isEmpty()){ - System.out.println("Airspace is empty"); - return; - } - - for (Flying flying : flyingInAirspace){ - System.out.print(flying.getIdentifier() + " is flying in airspace"); - - if(flying instanceof PassengerPlane passengerPlane){ - System.out.print(" (" + passengerPlane.getPassengerCount() + " PAX)"); - } - - System.out.println(); - } - } -} diff --git a/solution/H05/src/main/java/h05/CargoPlane.java b/solution/H05/src/main/java/h05/CargoPlane.java deleted file mode 100644 index b77e1a9..0000000 --- a/solution/H05/src/main/java/h05/CargoPlane.java +++ /dev/null @@ -1,34 +0,0 @@ -package h05; - -public class CargoPlane extends Plane implements CarriesCargo{ - -/* - Stacks are a type of data structure that follows the Last In First Out (LIFO) principle. - Explanation will be found in Task description. - */ - private final CargoStack containers = new CargoStack(); - - public CargoPlane(String aircraftRegistration, int baseWeight, FuelType fuelType, double maxFuelLevel) { - super(aircraftRegistration, baseWeight, fuelType, maxFuelLevel); - } - - @Override - public void loadContainer(int cargoWeight) { - containers.push(cargoWeight); - } - - @Override - public boolean hasFreightLoaded() { - return !containers.empty(); - } - - @Override - public int unloadNextContainer() { - return containers.pop(); - } - - @Override - protected double mass() { - return baseWeight + containers.getSum(); - } -} diff --git a/solution/H05/src/main/java/h05/CargoStack.java b/solution/H05/src/main/java/h05/CargoStack.java deleted file mode 100644 index 40a6f08..0000000 --- a/solution/H05/src/main/java/h05/CargoStack.java +++ /dev/null @@ -1,27 +0,0 @@ -package h05; - -import java.util.Stack; - -public class CargoStack { - private final Stack stack = new Stack<>(); - private int sum = 0; - - public boolean empty(){ - return stack.empty(); - } - - public void push(int value){ - sum += value; - stack.push(value); - } - - public int pop(){ - int removedValue = stack.pop(); - sum -= removedValue; - return removedValue; - } - - public int getSum(){ - return sum; - } -} diff --git a/solution/H05/src/main/java/h05/CarriesCargo.java b/solution/H05/src/main/java/h05/CarriesCargo.java deleted file mode 100644 index 4890005..0000000 --- a/solution/H05/src/main/java/h05/CarriesCargo.java +++ /dev/null @@ -1,8 +0,0 @@ -package h05; - -public interface CarriesCargo { - void loadContainer(int cargoWeight); - - boolean hasFreightLoaded(); - int unloadNextContainer(); -} diff --git a/solution/H05/src/main/java/h05/CarriesPassengers.java b/solution/H05/src/main/java/h05/CarriesPassengers.java deleted file mode 100644 index f32abd4..0000000 --- a/solution/H05/src/main/java/h05/CarriesPassengers.java +++ /dev/null @@ -1,7 +0,0 @@ -package h05; - -public interface CarriesPassengers { - void board(int peopleCount); - int getPassengerCount(); - void disembark(); -} diff --git a/solution/H05/src/main/java/h05/CombinedPlane.java b/solution/H05/src/main/java/h05/CombinedPlane.java deleted file mode 100644 index cfffdb0..0000000 --- a/solution/H05/src/main/java/h05/CombinedPlane.java +++ /dev/null @@ -1,32 +0,0 @@ -package h05; - - -public class CombinedPlane extends PassengerPlane implements CarriesCargo{ - - private final CargoStack containers = new CargoStack(); - - - public CombinedPlane(String aircraftRegistration, int baseWeight, FuelType fuelType, double maxFuelLevel, int crew) { - super(aircraftRegistration, baseWeight, fuelType, maxFuelLevel, crew); - } - - @Override - public void loadContainer(int cargoWeight) { - containers.push(cargoWeight); - } - - @Override - public boolean hasFreightLoaded() { - return !containers.empty(); - } - - @Override - public int unloadNextContainer() { - return containers.pop(); - } - - @Override - protected double mass() { - return super.mass() + containers.getSum(); - } -} diff --git a/solution/H05/src/main/java/h05/Flying.java b/solution/H05/src/main/java/h05/Flying.java deleted file mode 100644 index de71d6c..0000000 --- a/solution/H05/src/main/java/h05/Flying.java +++ /dev/null @@ -1,5 +0,0 @@ -package h05; - -public interface Flying { - String getIdentifier(); -} diff --git a/solution/H05/src/main/java/h05/FuelType.java b/solution/H05/src/main/java/h05/FuelType.java deleted file mode 100644 index 4b3d2af..0000000 --- a/solution/H05/src/main/java/h05/FuelType.java +++ /dev/null @@ -1,14 +0,0 @@ -package h05; - -public enum FuelType { - JetA(1.0), JetB(1.2), AvGas(0.99), Biokerosin (1.02); - - - private final double consumptionMultiplicator; - FuelType(double consumptionMultiplicator){ - this.consumptionMultiplicator = consumptionMultiplicator; - } - public double getConsumptionMultiplicator() { - return consumptionMultiplicator; - } -} diff --git a/solution/H05/src/main/java/h05/Main.java b/solution/H05/src/main/java/h05/Main.java deleted file mode 100644 index 470adc6..0000000 --- a/solution/H05/src/main/java/h05/Main.java +++ /dev/null @@ -1,69 +0,0 @@ -package h05; - -/** - * Main entry point in executing the program. - */ -public class Main { - /** - * Main entry point in executing the program. - * - * @param args program arguments, currently ignored - */ - public static void main(String[] args) { - Airspace airspace = Airspace.get(); - airspace.scanAirspace(); - - /* - Die konkreten Zahlen sind noch nicht final und können sich ändern. - */ - Runway runway01 = new Runway(2000); - Runway runway02 = new Runway(4000); - - WeatherBalloon weatherBalloon = new WeatherBalloon(99); - weatherBalloon.start(); - - Tank jetATank = new Tank(FuelType.JetA); - Tank jetBTank = new Tank(FuelType.JetB); - TankerPlane tankerPlane = new TankerPlane("D-ABCD", 10000, FuelType.JetA, 1000); - tankerPlane.loadFuel(FuelType.AvGas, 100000); - tankerPlane.loadFuel(FuelType.Biokerosin, 100000); - - PassengerPlane passengerPlane = new PassengerPlane("GAG-67", 10000, FuelType.JetA, 1700, 5); - jetATank.refuelPlane(passengerPlane); - passengerPlane.board(100); - passengerPlane.takeOff(); - - airspace.scanAirspace(); - - CargoPlane cargoPlane = new CargoPlane("D-AFFF", 8000, FuelType.JetB, 1500); - cargoPlane.loadContainer(1000); - jetBTank.refuelPlane(cargoPlane); - - passengerPlane.disembark(); - - airspace.scanAirspace(); - - cargoPlane.takeOff(); - cargoPlane.fly(1000); - - airspace.scanAirspace(); - - CombinedPlane combinedPlane = new CombinedPlane("D-ABBB", 9000, FuelType.AvGas, 10700, 5); - tankerPlane.refuelPlane(combinedPlane); - - combinedPlane.board(30); - combinedPlane.loadContainer(400); - combinedPlane.takeOff(); - combinedPlane.fly(3000); - airspace.scanAirspace(); - - runway01.land(combinedPlane); - runway02.land(cargoPlane); - - airspace.scanAirspace(); - - weatherBalloon.pop(); - - airspace.scanAirspace(); - } -} diff --git a/solution/H05/src/main/java/h05/PassengerPlane.java b/solution/H05/src/main/java/h05/PassengerPlane.java deleted file mode 100644 index f3e0588..0000000 --- a/solution/H05/src/main/java/h05/PassengerPlane.java +++ /dev/null @@ -1,36 +0,0 @@ -package h05; - -public class PassengerPlane extends Plane implements CarriesPassengers { - - protected static final char AVERAGE_PEOPLE_WEIGHT = 100; - protected static final char AVERAGE_LUGGAGE_WEIGHT = 15; - - private int passengerCount = 0; - - private final int crewCount; - - public PassengerPlane(String aircraftRegistration, int baseWeight, FuelType fuelType, double fuelCapacity, int crewCount) { - super(aircraftRegistration, baseWeight, fuelType, fuelCapacity); - this.crewCount = crewCount; - } - - @Override - public void board(int passengerCount) { - this.passengerCount += passengerCount; - } - - @Override - public void disembark() { - this.passengerCount = 0; - } - - @Override - public int getPassengerCount() { - return passengerCount; - } - - @Override - protected double mass() { - return baseWeight + (passengerCount + crewCount) * AVERAGE_PEOPLE_WEIGHT + passengerCount * AVERAGE_LUGGAGE_WEIGHT; - } -} diff --git a/solution/H05/src/main/java/h05/Plane.java b/solution/H05/src/main/java/h05/Plane.java deleted file mode 100644 index 4ddeefc..0000000 --- a/solution/H05/src/main/java/h05/Plane.java +++ /dev/null @@ -1,77 +0,0 @@ -package h05; - -public abstract class Plane implements Flying { - - private static final double CONSUMPTION_PER_KM_KG = 1.1494e-4; - private final String aircraftRegistration; - - protected final int baseWeight; - - private double currentFuelLevel; - - private final FuelType fuelType; - private final double fuelCapacity; - - public Plane(String aircraftRegistration, int baseWeight, FuelType fuelType, double fuelCapacity){ - this.aircraftRegistration = aircraftRegistration; - this.baseWeight = baseWeight; - this.fuelType = fuelType; - this.currentFuelLevel = 0; - this.fuelCapacity = fuelCapacity; - } - - - protected abstract double mass(); - - protected double getFuelConsumptionPerKilometer(){ - return CONSUMPTION_PER_KM_KG * mass() * fuelType.getConsumptionMultiplicator(); - } - - public void takeOff(){ - Airspace.get().register(this); - } - - public void land(){ - Airspace.get().deregister(this); - } - - public void fly(double distance){ - double neededFuel = distance * getFuelConsumptionPerKilometer(); - - // if the plane does not have enough fuel to fly the distance, it will not fly - if(neededFuel > currentFuelLevel){ - System.out.println("Plane " + aircraftRegistration + " does not have enough fuel to fly " + distance + " km."); - return; - } - - currentFuelLevel -= neededFuel; - - System.out.println("Plane " + aircraftRegistration + " flew " + distance + " km and has " + currentFuelLevel + " liters of fuel left."); - } - - public FuelType getFuelType() { - return fuelType; - } - - public double getFuelCapacity() { - return fuelCapacity; - } - - public double getCurrentFuelLevel() { - return currentFuelLevel; - } - - public void refuel(double amount){ - currentFuelLevel += amount; - - if(currentFuelLevel > fuelCapacity){ - currentFuelLevel = fuelCapacity; - System.out.println("The Tank of Plane " + aircraftRegistration + " has overflowed!"); - } - } - - @Override - public String getIdentifier(){ - return aircraftRegistration; - } -} diff --git a/solution/H05/src/main/java/h05/Refuelling.java b/solution/H05/src/main/java/h05/Refuelling.java deleted file mode 100644 index 19c2b22..0000000 --- a/solution/H05/src/main/java/h05/Refuelling.java +++ /dev/null @@ -1,5 +0,0 @@ -package h05; - -public interface Refuelling { - void refuelPlane(Plane plane); -} diff --git a/solution/H05/src/main/java/h05/Runway.java b/solution/H05/src/main/java/h05/Runway.java deleted file mode 100644 index 4a80002..0000000 --- a/solution/H05/src/main/java/h05/Runway.java +++ /dev/null @@ -1,41 +0,0 @@ -package h05; - -public class Runway { - - private final int runwayLength; - - /* - TODO: Man könnte sich noch überlegen, wie Flugzeugtypen, die hier landen können, festgesetzt werden - */ - public Runway(int runwayLength){ - this.runwayLength = runwayLength; - } - - public int getRunwayLength(){ - return runwayLength; - } - /* - TODO: hier müsste man sich noch eine bessere Formel für die Landung überlegen - */ - public static double calculateLandingDistance(double mass){ - return mass/40; - } - - public boolean canLand(Plane plane){ - return calculateLandingDistance(plane.mass()) <= runwayLength; - } - - /* - TODO: Man könnte auch stattdessen die land() methode in plane erweitern und diese hier weglassen - */ - public void land(Plane plane){ - if(canLand(plane)){ - plane.land(); - System.out.println("Plane " + plane.getIdentifier() + " has landed successfully."); - } else { - System.out.println("Plane " + plane.getIdentifier() + " could not land. The runway is too short."); - } - } - - -} diff --git a/solution/H05/src/main/java/h05/Tank.java b/solution/H05/src/main/java/h05/Tank.java deleted file mode 100644 index c3d4e5b..0000000 --- a/solution/H05/src/main/java/h05/Tank.java +++ /dev/null @@ -1,21 +0,0 @@ -package h05; - -public class Tank implements Refuelling{ - - private final FuelType fuelType; - - public Tank(FuelType fuelType){ - this.fuelType = fuelType; - } - - @Override - public void refuelPlane(Plane plane) { - if(plane.getFuelType() != fuelType){ - System.out.println("Incompatible fuel types, not refuelling"); - return; - } - - double missingFuel = plane.getFuelCapacity() - plane.getCurrentFuelLevel(); - plane.refuel(missingFuel); - } -} diff --git a/solution/H05/src/main/java/h05/TankerPlane.java b/solution/H05/src/main/java/h05/TankerPlane.java deleted file mode 100644 index 25f8bca..0000000 --- a/solution/H05/src/main/java/h05/TankerPlane.java +++ /dev/null @@ -1,35 +0,0 @@ -package h05; - -public class TankerPlane extends Plane implements Refuelling{ - - - private final double[] availableAmount = new double[FuelType.values().length]; - - public TankerPlane(String aircraftRegistration, int baseWeight, FuelType fuelType, double maxFuelLevel) { - super(aircraftRegistration, baseWeight, fuelType, maxFuelLevel); - } - - - public void loadFuel(FuelType fuelType, double amount){ - availableAmount[fuelType.ordinal()] += amount; - } - - @Override - protected double mass() { - double totalAmount = 0; - for (int i = 0; i < FuelType.values().length; i++){ - totalAmount += availableAmount[i]; - } - - return baseWeight + totalAmount; - } - - - @Override - public void refuelPlane(Plane plane) { - double missingFuel = plane.getFuelCapacity() - plane.getCurrentFuelLevel(); - double actualAmount = Math.min(availableAmount[plane.getFuelType().ordinal()], missingFuel); - availableAmount[plane.getFuelType().ordinal()] -= actualAmount; - plane.refuel(actualAmount); - } -} diff --git a/solution/H05/src/main/java/h05/WeatherBalloon.java b/solution/H05/src/main/java/h05/WeatherBalloon.java deleted file mode 100644 index 70c4b77..0000000 --- a/solution/H05/src/main/java/h05/WeatherBalloon.java +++ /dev/null @@ -1,23 +0,0 @@ -package h05; - -public class WeatherBalloon implements Flying{ - - private final int balloonNumber; - - public WeatherBalloon(int balloonNumber){ - this.balloonNumber = balloonNumber; - } - - public void start(){ - Airspace.get().register(this); - } - - public void pop(){ - Airspace.get().deregister(this); - } - - @Override - public String getIdentifier() { - return "WeatherBalloon " + balloonNumber; - } -} diff --git a/solution/H05/src/test b/solution/H05/src/test new file mode 120000 index 0000000..18b094a --- /dev/null +++ b/solution/H05/src/test @@ -0,0 +1 @@ +/home/osh/Desktop/FOP-2425-Marathon/H05/src/test \ No newline at end of file diff --git a/solution/H05/src/test/java/h05/ExampleJUnitTest.java b/solution/H05/src/test/java/h05/ExampleJUnitTest.java deleted file mode 100644 index 5502f06..0000000 --- a/solution/H05/src/test/java/h05/ExampleJUnitTest.java +++ /dev/null @@ -1,16 +0,0 @@ -package h05; - -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.*; - -/** - * An example JUnit test class. - */ -public class ExampleJUnitTest { - - @Test - public void testAddition() { - assertEquals(2, 1 + 1); - } -} diff --git a/solution/H06/src/main b/solution/H06/src/main new file mode 120000 index 0000000..00baf83 --- /dev/null +++ b/solution/H06/src/main @@ -0,0 +1 @@ +/home/osh/Desktop/FOP-2425-Marathon/H06/src/main \ No newline at end of file diff --git a/solution/H06/src/main/java/h06/Main.java b/solution/H06/src/main/java/h06/Main.java deleted file mode 100644 index bb8af04..0000000 --- a/solution/H06/src/main/java/h06/Main.java +++ /dev/null @@ -1,28 +0,0 @@ -package h06; - -import h06.problems.*; -import h06.ui.DrawInstruction; -import h06.ui.FractalVisualizer; -import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired; - -/** - * Main entry point in executing the program. - */ -public class Main { - /** - * Main entry point in executing the program. - * - * @param args program arguments, currently ignored - */ - @StudentImplementationRequired - public static void main(String[] args) { - DrawInstruction[] dragonCurveInstructions = Fractals.dragonCurve(14); - DrawInstruction[] kochSnowflakeInstructions = Fractals.kochSnowflake(5); - - FractalVisualizer fracVis = new FractalVisualizer(dragonCurveInstructions, 90); - fracVis.setVisible(true); - - FractalVisualizer fracVis2 = new FractalVisualizer(kochSnowflakeInstructions, 60); - fracVis2.setVisible(true); - } -} diff --git a/solution/H06/src/main/java/h06/problems/Fibonacci.java b/solution/H06/src/main/java/h06/problems/Fibonacci.java deleted file mode 100644 index e4b1fd8..0000000 --- a/solution/H06/src/main/java/h06/problems/Fibonacci.java +++ /dev/null @@ -1,71 +0,0 @@ -package h06.problems; - -import org.tudalgo.algoutils.student.annotation.DoNotTouch; -import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired; - -/** - * A class containing different implementations for computing the nth number in the Fibonacci sequence. - * - * @author Manuel Peters - */ -public class Fibonacci { - - /** - * Default Constructor for this class. - */ - @DoNotTouch - public Fibonacci() {} - - /** - * Computes the nth number from the Fibonacci sequence recursively. - * - * @param n The index of the Fibonacci sequence to compute. - * @return The nth number from the Fibonacci sequence. - */ - @DoNotTouch - public static int fibonacciRecursiveClassic(int n) { - if ( n <= 1) { - return n; - } else { - return fibonacciRecursiveClassic(n - 1) + fibonacciRecursiveClassic(n - 2); - } - } - - /** - * Computes the nth number from the Fibonacci sequence using a different recursive approach. - * - * @param n The index of the Fibonacci sequence to compute. - * @return The nth number from the Fibonacci sequence. - */ - @StudentImplementationRequired - public static int fibonacciRecursiveDifferent(int n) { - return doTheRecursion(0, 1, n); - } - - @StudentImplementationRequired - private static int doTheRecursion(int a, int b, int n) { - // Verbindliche Anforderung: Bedingungsoperator verwenden! - return n <= 0 ? a : doTheRecursion(b, a+b, n-1); - } - - /** - * Computes the nth number from the Fibonacci sequence iteratively. - * - * @param n The index of the Fibonacci sequence to compute. - * @return The nth number from the Fibonacci sequence. - */ - @StudentImplementationRequired - public static int fibonacciIterative(int n) { - int result = n; - int twoBefore = 0; - int oneBefore = 1; - - for (int i = 2; i <= n; i++) { - result = oneBefore + twoBefore; - twoBefore = oneBefore; - oneBefore = result; - } - - return result; - } -} \ No newline at end of file diff --git a/solution/H06/src/main/java/h06/problems/Fractals.java b/solution/H06/src/main/java/h06/problems/Fractals.java deleted file mode 100644 index 4c3f16e..0000000 --- a/solution/H06/src/main/java/h06/problems/Fractals.java +++ /dev/null @@ -1,155 +0,0 @@ -package h06.problems; - -import h06.ui.DrawInstruction; -import org.tudalgo.algoutils.student.annotation.DoNotTouch; -import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired; - -/** - * A class to generate draw instructions in order to draw a dragon curve. - * - * @author Manuel Peters - */ -public class Fractals { - - /** - * Default Constructor for this class. - */ - @DoNotTouch - public Fractals() {} - - /** - * This method calculates a raised to the power of b using recursion. - * a and b are expected to be non-negative integers. - * - * @param a the base, must be non-negative - * @param b the exponent, must be non-negative - * @return the result of a raised to the power of b - */ - @StudentImplementationRequired - public static int pow(int a, int b) { - if(b == 0) { - return 1; - } else { - return a * pow(a, b-1); - } - } - - /** - * This method combines two arrays of DrawInstruction objects into a single array. - * The elements of the first array are followed by the elements of the second array in the new array. - * - * @param arr1 the first array of type DrawInstruction - * @param arr2 the second array of type DrawInstruction - * @return A new array containing all elements of arr1 followed by all elements of arr2 - */ - @StudentImplementationRequired - public static DrawInstruction[] concatenate(DrawInstruction[] arr1, DrawInstruction[] arr2) { - DrawInstruction[] newArr = new DrawInstruction[arr1.length + arr2.length]; - - for (int i = 0; i < arr1.length; i++) { - newArr[i] = arr1[i]; - } - - for (int i = arr1.length; i < arr1.length+arr2.length; i++) { - newArr[i] = arr2[i-arr1.length]; - } - - return newArr; - } - - /** - * This method creates a new array that is a copy of the input array arr, but with the element at the specified - * index idx replaced by elem. - * - * @param arr the original array of type DrawInstruction - * @param idx the index at which to replace the element - * @param elem the new DrawInstruction to place at the specified index - * @return A new array with the element at idx replaced by elem - */ - @StudentImplementationRequired - public static DrawInstruction[] replaceAtIndex(DrawInstruction[] arr, int idx, DrawInstruction elem) { - DrawInstruction[] newArr = new DrawInstruction[arr.length]; - - for (int i = 0; i < newArr.length; i++) { - if(i == idx) { - newArr[idx] = elem; - } else { - newArr[i] = arr[i]; - } - } - - return newArr; - } - - /** - * Generates an array of DrawInstruction objects to draw a dragon curve of order n - * - * @param n The order of the dragon curve to generate - * @return an array of DrawInstruction objects to draw a dragon curve of order n - */ - @StudentImplementationRequired - public static DrawInstruction[] dragonCurve(int n) { - if(n <= 0) { - return new DrawInstruction[]{ - DrawInstruction.DRAW_LINE - }; - } - if (n == 1) { - return new DrawInstruction[]{ - DrawInstruction.DRAW_LINE, - DrawInstruction.TURN_RIGHT, - DrawInstruction.DRAW_LINE - }; - } - else { - DrawInstruction[] lastOrder = dragonCurve(n - 1); - DrawInstruction[] firstHalf = concatenate(lastOrder, new DrawInstruction[]{DrawInstruction.TURN_RIGHT}); - DrawInstruction[] secondHalf = replaceAtIndex(lastOrder, pow(2, n-1)-1, DrawInstruction.TURN_LEFT); - return concatenate(firstHalf, secondHalf); - } - } - - /** - * Generates an array of DrawInstruction objects to draw a koch snowflake of order n - * - * @param n The order of the koch snowflake to generate - * @return an array of DrawInstruction objects to draw a koch snowflake of order n - */ - @StudentImplementationRequired - public static DrawInstruction[] kochSnowflake(int n) { - if (n <= 0) { - return new DrawInstruction[]{ - DrawInstruction.DRAW_LINE, - DrawInstruction.TURN_RIGHT, - DrawInstruction.TURN_RIGHT, - DrawInstruction.DRAW_LINE, - DrawInstruction.TURN_RIGHT, - DrawInstruction.TURN_RIGHT, - DrawInstruction.DRAW_LINE, - }; - } else { - DrawInstruction[] lastOrder = kochSnowflake(n - 1); - DrawInstruction[] currentOrder = new DrawInstruction[lastOrder.length * 4]; - int index = 0; - - for (DrawInstruction instruction : lastOrder) { - if (instruction == DrawInstruction.DRAW_LINE) { - currentOrder[index++] = DrawInstruction.DRAW_LINE; - currentOrder[index++] = DrawInstruction.TURN_LEFT; - currentOrder[index++] = DrawInstruction.DRAW_LINE; - currentOrder[index++] = DrawInstruction.TURN_RIGHT; - currentOrder[index++] = DrawInstruction.TURN_RIGHT; - currentOrder[index++] = DrawInstruction.DRAW_LINE; - currentOrder[index++] = DrawInstruction.TURN_LEFT; - currentOrder[index++] = DrawInstruction.DRAW_LINE; - } else if (instruction == DrawInstruction.TURN_LEFT) { - currentOrder[index++] = instruction; - } else if (instruction == DrawInstruction.TURN_RIGHT) { - currentOrder[index++] = instruction; - } - } - - return currentOrder; - } - } -} diff --git a/solution/H06/src/main/java/h06/problems/LinearSearch.java b/solution/H06/src/main/java/h06/problems/LinearSearch.java deleted file mode 100644 index c235777..0000000 --- a/solution/H06/src/main/java/h06/problems/LinearSearch.java +++ /dev/null @@ -1,54 +0,0 @@ -package h06.problems; - -import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired; - -public class LinearSearch { - - /** - * Recursively searches for a target in an array using linear search. - * - * @param arr the array to search in - * @param target the target to search for - * @return the index of the target in the array, or -1 if the target is not found - */ - @StudentImplementationRequired - public static int linearSearchRecursive(int[] arr, int target) { - return linearSearchRecursiveHelper(arr, target, 0); - } - - /** - * Recursively searches for a target in an array using linear search. - * - * @param arr the array to search in - * @param target the target to search for - * @param index the index to start searching from - * @return the index of the target in the array, or -1 if the target is not found - */ - @StudentImplementationRequired - public static int linearSearchRecursiveHelper(int[] arr, int target, int index) { - if (index == arr.length) { - return -1; - } else if (arr[index] == target) { - return index; - } else { - return linearSearchRecursiveHelper(arr, target, index + 1); - } - } - - /** - * Iteratively searches for a target in an array using linear search. - * - * @param arr the array to search in - * @param target the target to search for - * @return the index of the target in the array, or -1 if the target is not found - */ - @StudentImplementationRequired - public static int linearSearchIterative(int[] arr, int target) { - for (int i = 0; i < arr.length; i++) { - if (arr[i] == target) { - return i; - } - } - return -1; - } -} diff --git a/solution/H06/src/main/java/h06/ui/DrawInstruction.java b/solution/H06/src/main/java/h06/ui/DrawInstruction.java deleted file mode 100644 index e65170c..0000000 --- a/solution/H06/src/main/java/h06/ui/DrawInstruction.java +++ /dev/null @@ -1,15 +0,0 @@ -package h06.ui; - -import org.tudalgo.algoutils.student.annotation.DoNotTouch; - -/** - * The DrawInstruction enum represents draw instructions in order to draw a fractal. - * - * @author Manuel Peters - */ -@DoNotTouch -public enum DrawInstruction { - DRAW_LINE, - TURN_LEFT, - TURN_RIGHT -} diff --git a/solution/H06/src/main/java/h06/ui/FractalDrawer.java b/solution/H06/src/main/java/h06/ui/FractalDrawer.java deleted file mode 100644 index cab0c5a..0000000 --- a/solution/H06/src/main/java/h06/ui/FractalDrawer.java +++ /dev/null @@ -1,103 +0,0 @@ -package h06.ui; - -import org.tudalgo.algoutils.student.annotation.DoNotTouch; - -import javax.swing.*; -import java.awt.*; -import java.awt.geom.Line2D; -import java.awt.geom.Rectangle2D; -import java.util.ArrayList; -import java.util.List; - -/** - * A class for drawing fractals, given their drawing instructions. - * - * @author Manuel Peters - */ -@DoNotTouch -public class FractalDrawer extends JPanel { - private final DrawInstruction[] drawInstructions; - private final List lines; - - /** - * The angle in radians for a turn. - * This angle determines how far a left or right turn will go. - */ - private final double angle; - - /** - * Constructor for this class. - * - * @param drawInstructions the instructions to draw the fractal - * @param angle the angle in degrees for a turn - */ - public FractalDrawer(DrawInstruction[] drawInstructions, int angle) { - this.drawInstructions = drawInstructions; - this.lines = new ArrayList<>(); - this.angle = Math.toRadians(angle); - } - - /** - * Draws on the canvas by following the drawing instructions - */ - @Override - protected void paintComponent(Graphics g) { - super.paintComponent(g); - setBackground(Color.WHITE); - - Graphics2D g2d = (Graphics2D) g; - - // Set the initial position and direction - double x = 0; - double y = 0; - // The first line is drawn to the right - double currentAngle = 0; - - double minX = x; - double minY = y; - double maxX = x; - double maxY = y; - - // Store the lines based on instructions - for (DrawInstruction instruction : drawInstructions) { - if (instruction.equals(DrawInstruction.DRAW_LINE)) { - double x2 = x + Math.cos(currentAngle); - double y2 = y + Math.sin(currentAngle); - lines.add(new Line2D.Double(x, y, x2, y2)); - x = x2; - y = y2; - - // Update Bounding Box values - minX = Math.min(minX, x); - minY = Math.min(minY, y); - maxX = Math.max(maxX, x); - maxY = Math.max(maxY, y); - } else if (instruction.equals(DrawInstruction.TURN_LEFT)) { - currentAngle -= angle; - } else if (instruction.equals(DrawInstruction.TURN_RIGHT)) { - currentAngle += angle; - } - } - - // Define Bounding Box - Rectangle2D bounds = new Rectangle2D.Double(minX - 1, minY - 1, maxX - minX + 2, maxY - minY + 2); - - // Calculate scaling factor to fit all the lines within the panel - double scaleX = getWidth() / bounds.getWidth(); - double scaleY = getHeight() / bounds.getHeight(); - double scale = Math.min(scaleX, scaleY); - - // Calculate the initial position to center the fractal - double offsetX = (getWidth() - bounds.getWidth() * scale) / 2 - bounds.getX() * scale; - double offsetY = (getHeight() - bounds.getHeight() * scale) / 2 - bounds.getY() * scale; - - // Draw the stored lines with scaling and offset - for (Line2D line : lines) { - double x1 = offsetX + line.getX1() * scale; - double y1 = offsetY + line.getY1() * scale; - double x2 = offsetX + line.getX2() * scale; - double y2 = offsetY + line.getY2() * scale; - g2d.draw(new Line2D.Double(x1, y1, x2, y2)); - } - } -} diff --git a/solution/H06/src/main/java/h06/ui/FractalVisualizer.java b/solution/H06/src/main/java/h06/ui/FractalVisualizer.java deleted file mode 100644 index cd5d539..0000000 --- a/solution/H06/src/main/java/h06/ui/FractalVisualizer.java +++ /dev/null @@ -1,32 +0,0 @@ -package h06.ui; - -import org.tudalgo.algoutils.student.annotation.DoNotTouch; - -import javax.swing.*; -import java.awt.*; - -/** - * A class to visualize fractals, given their drawing instructions. - * - * @author Manuel Peters - */ -@DoNotTouch -public class FractalVisualizer extends JFrame { - - /** - * Constructor for this class. - * - * @param drawInstructions the instructions to draw the fractal - * @param angle the angle in degrees for a turn - */ - public FractalVisualizer(DrawInstruction[] drawInstructions, int angle) { - super("Fractal Visualizer"); - setSize(new Dimension(800,800)); - setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); - setLocationRelativeTo(null); - setLayout(new GridLayout(1,1)); - - FractalDrawer fractalDrawer = new FractalDrawer(drawInstructions, angle); - add(fractalDrawer); - } -} diff --git a/solution/H06/src/test b/solution/H06/src/test new file mode 120000 index 0000000..12f1c6b --- /dev/null +++ b/solution/H06/src/test @@ -0,0 +1 @@ +/home/osh/Desktop/FOP-2425-Marathon/H06/src/test \ No newline at end of file diff --git a/solution/H06/src/test/java/h06/ExampleJUnitTest.java b/solution/H06/src/test/java/h06/ExampleJUnitTest.java deleted file mode 100644 index 70753f2..0000000 --- a/solution/H06/src/test/java/h06/ExampleJUnitTest.java +++ /dev/null @@ -1,16 +0,0 @@ -package h06; - -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.*; - -/** - * An example JUnit test class. - */ -public class ExampleJUnitTest { - - @Test - public void testAddition() { - assertEquals(2, 1 + 1); - } -} diff --git a/solution/H07/src/main b/solution/H07/src/main new file mode 120000 index 0000000..e2862ca --- /dev/null +++ b/solution/H07/src/main @@ -0,0 +1 @@ +/home/osh/Desktop/FOP-2425-Marathon/H07/src/main \ No newline at end of file diff --git a/solution/H07/src/main/java/h07/ArithmeticExpression.java b/solution/H07/src/main/java/h07/ArithmeticExpression.java deleted file mode 100644 index e09b72e..0000000 --- a/solution/H07/src/main/java/h07/ArithmeticExpression.java +++ /dev/null @@ -1,18 +0,0 @@ -package h07; - -import org.tudalgo.algoutils.student.annotation.StudentCreationRequired; - -/** - * Represents an arithmetic operation on numbers. - */ -@StudentCreationRequired -public interface ArithmeticExpression { - /** - * Applies the arithmetic operation to the two numbers. - * - * @param num1 the first number - * @param num2 the second number - * @return the result of the operation - */ - NumberExpression evaluate(NumberExpression num1, NumberExpression num2); -} diff --git a/solution/H07/src/main/java/h07/ConvertNumberToPeanoExpression.java b/solution/H07/src/main/java/h07/ConvertNumberToPeanoExpression.java deleted file mode 100644 index 0f70a11..0000000 --- a/solution/H07/src/main/java/h07/ConvertNumberToPeanoExpression.java +++ /dev/null @@ -1,18 +0,0 @@ -package h07; - -import h07.peano.PeanoNumberExpression; -import org.tudalgo.algoutils.student.annotation.StudentCreationRequired; - -/** - * Converts a number expression to a Peano number expression. - */ -@StudentCreationRequired -public interface ConvertNumberToPeanoExpression { - /** - * Converts a number expression to a Peano number expression. - * - * @param numberExpression the number expression to convert - * @return the Peano number expression - */ - PeanoNumberExpression convert(NumberExpression numberExpression); -} diff --git a/solution/H07/src/main/java/h07/ConvertNumberToPeanoExpressionImpl.java b/solution/H07/src/main/java/h07/ConvertNumberToPeanoExpressionImpl.java deleted file mode 100644 index 483f6b6..0000000 --- a/solution/H07/src/main/java/h07/ConvertNumberToPeanoExpressionImpl.java +++ /dev/null @@ -1,23 +0,0 @@ -package h07; - -import h07.peano.PeanoNumberExpression; -import h07.peano.Zero; -import h07.peano.Successor; -import org.tudalgo.algoutils.student.annotation.StudentCreationRequired; - -/** - * Converts a number expression to a Peano number expression. - */ -@StudentCreationRequired -public class ConvertNumberToPeanoExpressionImpl implements ConvertNumberToPeanoExpression { - @Override - public PeanoNumberExpression convert(NumberExpression numberExpression) { - int value = numberExpression.evaluate(); - - if (value == 0) { - return Zero::new; - } else { - return () -> new Successor(new ConvertNumberToPeanoExpressionImpl().convert(() -> value - 1).evaluate()); - } - } -} diff --git a/solution/H07/src/main/java/h07/ConvertPeanoToNumberExpression.java b/solution/H07/src/main/java/h07/ConvertPeanoToNumberExpression.java deleted file mode 100644 index e26db4c..0000000 --- a/solution/H07/src/main/java/h07/ConvertPeanoToNumberExpression.java +++ /dev/null @@ -1,18 +0,0 @@ -package h07; - -import h07.peano.PeanoNumberExpression; -import org.tudalgo.algoutils.student.annotation.StudentCreationRequired; - -/** - * Converts a Peano number expression to a number expression. - */ -@StudentCreationRequired -public interface ConvertPeanoToNumberExpression { - /** - * Converts a Peano number expression to a number expression. - * - * @param peanoNumberExpression the Peano number expression to convert - * @return the number expression - */ - NumberExpression convert(PeanoNumberExpression peanoNumberExpression); -} diff --git a/solution/H07/src/main/java/h07/ConvertPeanoToNumberExpressionImpl.java b/solution/H07/src/main/java/h07/ConvertPeanoToNumberExpressionImpl.java deleted file mode 100644 index c8a289a..0000000 --- a/solution/H07/src/main/java/h07/ConvertPeanoToNumberExpressionImpl.java +++ /dev/null @@ -1,23 +0,0 @@ -package h07; - -import h07.peano.NaturalNumber; -import h07.peano.PeanoNumberExpression; -import h07.peano.Successor; -import org.tudalgo.algoutils.student.annotation.StudentCreationRequired; - -/** - * Converts a Peano number expression to a number expression. - */ -@StudentCreationRequired -public class ConvertPeanoToNumberExpressionImpl implements ConvertPeanoToNumberExpression { - @Override - public NumberExpression convert(PeanoNumberExpression peanoNumberExpression) { - NaturalNumber naturalNumber = peanoNumberExpression.evaluate(); - - if (naturalNumber instanceof Successor successor) { - return () -> new ConvertPeanoToNumberExpressionImpl().convert(() -> successor.predecessor).evaluate() + 1; - } else { - return () -> 0; - } - } -} diff --git a/solution/H07/src/main/java/h07/Main.java b/solution/H07/src/main/java/h07/Main.java deleted file mode 100644 index 186287f..0000000 --- a/solution/H07/src/main/java/h07/Main.java +++ /dev/null @@ -1,78 +0,0 @@ -package h07; - -import h07.peano.*; -import org.tudalgo.algoutils.student.annotation.DoNotTouch; -import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired; - -/** - * Main entry point in executing the program. - */ -public class Main { - /** - * Main entry point in executing the program. - * - * @param args program arguments, currently ignored - */ - public static void main(String[] args) { - testHeader("Multiplication Table"); - numberExpressionMultiplicationTableTests(); - - testHeader("Peano Number Expressions"); - peanoNumberExpressionTests(); - - testHeader("Filter, Fold, Map"); - filterFoldMapTests(); - } - - @DoNotTouch - private static void testHeader(String testName) { - System.out.println("-----------------------------------"); - System.out.println("Running test: " + testName); - System.out.println("-----------------------------------"); - } - - @DoNotTouch - private static void numberExpressionMultiplicationTableTests() { - int lowerBound = 1; - int upperBound = 10; - NumberExpression[] multiplicationTable = NumberExpressionFactory.multiplicationTable(lowerBound, upperBound); - - for (int i = lowerBound; i <= upperBound; i++) { - for (int j = lowerBound; j <= upperBound; j++) { - System.out.printf("| %4s ", multiplicationTable[(i - lowerBound) * (upperBound - lowerBound + 1) + (j - lowerBound)].evaluate()); - } - System.out.println("|"); - } - } - - private static final NaturalNumber THREE = new Successor(new Successor(new Successor(new Zero()))); - private static final NaturalNumber SEVEN = new Successor(new Successor(new Successor(new Successor(new Successor(new Successor(new Successor(new Zero()))))))); - - @StudentImplementationRequired - private static void peanoNumberExpressionTests() { - PeanoNumberExpression three = () -> THREE; - PeanoNumberExpression seven = () -> SEVEN; - - PeanoNumberExpression sum = new PeanoAddExpression().evaluate(three, seven); - PeanoNumberExpression product = new PeanoMultiplyExpression().evaluate(three, seven); - - System.out.println(sum.evaluate()); - System.out.println(product.evaluate()); - - // Not required but can be used to convert Peano numbers to integers - System.out.println(new ConvertPeanoToNumberExpressionImpl().convert(sum).evaluate()); - System.out.println(new ConvertPeanoToNumberExpressionImpl().convert(product).evaluate()); - } - - @StudentImplementationRequired - private static void filterFoldMapTests() { - NumberExpression[] numbers = NumberExpressionFactory.multiplicationTable(1, 10); - NumberExpression[] filteredNumbers = NumberExpressionFactory.filter(numbers, n -> n % 3 == 0); - PeanoNumberExpression[] filteredPeanoNumbers = PeanoNumberExpressionFactory - .fromNumberExpressions(filteredNumbers); - PeanoNumberExpression foldedPeanoNumbers = PeanoNumberExpressionFactory.fold(filteredPeanoNumbers, Zero::new, - new PeanoAddExpression()); - int foldedNumber = new ConvertPeanoToNumberExpressionImpl().convert(foldedPeanoNumbers).evaluate(); - System.out.println(foldedNumber); - } -} diff --git a/solution/H07/src/main/java/h07/NumberExpression.java b/solution/H07/src/main/java/h07/NumberExpression.java deleted file mode 100644 index 4e3d162..0000000 --- a/solution/H07/src/main/java/h07/NumberExpression.java +++ /dev/null @@ -1,16 +0,0 @@ -package h07; - -import org.tudalgo.algoutils.student.annotation.StudentCreationRequired; - -/** - * Represents a number expression. - */ -@StudentCreationRequired -public interface NumberExpression { - /** - * Evaluates the expression represented by this node. - * - * @return the result of the evaluation - */ - int evaluate(); -} diff --git a/solution/H07/src/main/java/h07/NumberExpressionFactory.java b/solution/H07/src/main/java/h07/NumberExpressionFactory.java deleted file mode 100644 index 3879f1f..0000000 --- a/solution/H07/src/main/java/h07/NumberExpressionFactory.java +++ /dev/null @@ -1,89 +0,0 @@ -package h07; - -import java.util.function.IntPredicate; - -import org.tudalgo.algoutils.student.annotation.DoNotTouch; -import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired; - -/** - * A factory class for creating number expressions. - */ -public class NumberExpressionFactory { - /** - * Calculates the product of all possible pairs of numbers in the given array. - * - * @param numbers the array of number expressions to calculate the multiplication table - * @return An array of number expressions representing the result of the - * multiplication table of the given numbers. - */ - @StudentImplementationRequired - public static NumberExpression[] multiplicationTable(NumberExpression[] numbers) { - NumberExpression[] multiplicationTable = new NumberExpression[numbers.length * numbers.length]; - - ArithmeticExpression multiplication = (num1, num2) -> { - return () -> num1.evaluate() * num2.evaluate(); - }; - - for (int i = 0; i < numbers.length; i++) { - for (int j = 0; j < numbers.length; j++) { - multiplicationTable[i * numbers.length + j] = multiplication.evaluate(numbers[i], numbers[j]); - } - } - - return multiplicationTable; - } - - /** - * Calculates the product of all possible pairs of numbers in the given range. - * - * @param lowerBound the lower bound of the multiplication table, inclusive - * @param upperBound the upper bound of the multiplication table, inclusive - * @return An array of number expressions representing the result of the - * multiplication table of the numbers from lowerBound to upperBound. - */ - @DoNotTouch - public static NumberExpression[] multiplicationTable(int lowerBound, int upperBound) { - int numberOfNumbers = upperBound - lowerBound + 1; - NumberExpression[] baseNumbers = new NumberExpression[numberOfNumbers]; - - for (int i = lowerBound; i <= upperBound; i++) { - // Copy to local variable to make it effectively final, so it can be used in - // lambda - int finalI = i; - baseNumbers[i - lowerBound] = () -> finalI; - } - - return multiplicationTable(baseNumbers); - } - - /** - * Filters the given array of number expressions based on the given predicate. - * The returned array should contain only the number expressions that satisfy - * the predicate in the same order as they appear in the input array. - * This means there should be no null values in the returned array. - * - * @param numbers the array of number expressions to filter - * @param predicate the predicate to filter the number expressions - * @return An array of number expressions that satisfy the predicate. - */ - @StudentImplementationRequired - public static NumberExpression[] filter(NumberExpression[] numbers, IntPredicate predicate) { - int count = 0; - for (NumberExpression number : numbers) { - if (predicate.test(number.evaluate())) { - count++; - } - } - - NumberExpression[] result = new NumberExpression[count]; - - int nextIndex = 0; - for (NumberExpression number : numbers) { - if (predicate.test(number.evaluate())) { - result[nextIndex++] = number; - } - } - - return result; - } -} diff --git a/solution/H07/src/main/java/h07/peano/NaturalNumber.java b/solution/H07/src/main/java/h07/peano/NaturalNumber.java deleted file mode 100644 index 2575c74..0000000 --- a/solution/H07/src/main/java/h07/peano/NaturalNumber.java +++ /dev/null @@ -1,17 +0,0 @@ -package h07.peano; - -import org.tudalgo.algoutils.student.annotation.DoNotTouch; - -/** - * Represents a natural number. - */ -@DoNotTouch -public abstract class NaturalNumber { - /** - * Returns the string representation of the natural number. - * - * @return the string representation of the natural number - */ - @Override - public abstract String toString(); -} diff --git a/solution/H07/src/main/java/h07/peano/PeanoAddExpression.java b/solution/H07/src/main/java/h07/peano/PeanoAddExpression.java deleted file mode 100644 index b22c395..0000000 --- a/solution/H07/src/main/java/h07/peano/PeanoAddExpression.java +++ /dev/null @@ -1,27 +0,0 @@ -package h07.peano; - -import org.tudalgo.algoutils.student.annotation.StudentCreationRequired; - -/** - * Represents an addition operation in Peano arithmetic. - */ -@StudentCreationRequired -public class PeanoAddExpression implements PeanoArithmeticExpression { - @Override - public PeanoNumberExpression evaluate(PeanoNumberExpression num1, PeanoNumberExpression num2) { - NaturalNumber naturalNumber1 = num1.evaluate(); - - if (naturalNumber1 instanceof Successor naturalNumber1Peano) { - return () -> { - return new Successor( - new PeanoAddExpression().evaluate( - () -> { return naturalNumber1Peano.predecessor; }, - num2 - ).evaluate() - ); - }; - } else { - return num2; - } - } -} diff --git a/solution/H07/src/main/java/h07/peano/PeanoArithmeticExpression.java b/solution/H07/src/main/java/h07/peano/PeanoArithmeticExpression.java deleted file mode 100644 index f71a126..0000000 --- a/solution/H07/src/main/java/h07/peano/PeanoArithmeticExpression.java +++ /dev/null @@ -1,18 +0,0 @@ -package h07.peano; - -import org.tudalgo.algoutils.student.annotation.StudentCreationRequired; - -/** - * Represents an arithmetic operation in Peano arithmetic. - */ -@StudentCreationRequired -public interface PeanoArithmeticExpression { - /** - * Evaluates the expression represented by this node. - * - * @param num1 the first number to evaluate - * @param num2 the second number to evaluate - * @return the result of the evaluation - */ - PeanoNumberExpression evaluate(PeanoNumberExpression num1, PeanoNumberExpression num2); -} diff --git a/solution/H07/src/main/java/h07/peano/PeanoMultiplyExpression.java b/solution/H07/src/main/java/h07/peano/PeanoMultiplyExpression.java deleted file mode 100644 index ead5768..0000000 --- a/solution/H07/src/main/java/h07/peano/PeanoMultiplyExpression.java +++ /dev/null @@ -1,30 +0,0 @@ -package h07.peano; - -import org.tudalgo.algoutils.student.annotation.StudentCreationRequired; - -/** - * Represents a multiplication operation in Peano arithmetic. -*/ -@StudentCreationRequired -public class PeanoMultiplyExpression implements PeanoArithmeticExpression { - @Override - public PeanoNumberExpression evaluate(PeanoNumberExpression num1, PeanoNumberExpression num2) { - NaturalNumber naturalNumber2 = num2.evaluate(); - - if (naturalNumber2 instanceof Successor naturalNumber2Peano) { - if (naturalNumber2Peano.predecessor instanceof Zero) { - return num1; - } else { - return () -> new PeanoAddExpression().evaluate( - num1, - new PeanoMultiplyExpression().evaluate( - num1, - () -> naturalNumber2Peano.predecessor - ) - ).evaluate(); - } - } else { - return Zero::new; - } - } -} diff --git a/solution/H07/src/main/java/h07/peano/PeanoNumberExpression.java b/solution/H07/src/main/java/h07/peano/PeanoNumberExpression.java deleted file mode 100644 index 2617b99..0000000 --- a/solution/H07/src/main/java/h07/peano/PeanoNumberExpression.java +++ /dev/null @@ -1,16 +0,0 @@ -package h07.peano; - -import org.tudalgo.algoutils.student.annotation.StudentCreationRequired; - -/** - * Represents a Peano number expression. - */ -@StudentCreationRequired -public interface PeanoNumberExpression { - /** - * Evaluates the expression represented by this node. - * - * @return the result of the evaluation - */ - NaturalNumber evaluate(); -} diff --git a/solution/H07/src/main/java/h07/peano/PeanoNumberExpressionFactory.java b/solution/H07/src/main/java/h07/peano/PeanoNumberExpressionFactory.java deleted file mode 100644 index adb1071..0000000 --- a/solution/H07/src/main/java/h07/peano/PeanoNumberExpressionFactory.java +++ /dev/null @@ -1,44 +0,0 @@ -package h07.peano; - -import h07.ConvertNumberToPeanoExpressionImpl; -import h07.NumberExpression; -import org.tudalgo.algoutils.student.annotation.StudentImplementationRequired; - -/** - * Represents a factory for Peano number expressions. - */ -public class PeanoNumberExpressionFactory { - /** - * Converts an array of number expressions to an array of Peano number expressions. - * - * @param numberExpressions the number expressions to convert - * @return the converted Peano number expressions - */ - @StudentImplementationRequired - public static PeanoNumberExpression[] fromNumberExpressions(NumberExpression[] numberExpressions) { - PeanoNumberExpression[] peanoNumberExpressions = new PeanoNumberExpression[numberExpressions.length]; - - for (int i = 0; i < numberExpressions.length; i++) { - peanoNumberExpressions[i] = new ConvertNumberToPeanoExpressionImpl().convert(numberExpressions[i]); - } - - return peanoNumberExpressions; - } - - /** - * Folds an array of Peano number expressions into a single Peano number expression. - * - * @param peanoNumberExpressions the Peano number expressions to fold - * @param initial the initial Peano number expression - * @param operation the operation to apply - * @return the folded Peano number expression - */ - @StudentImplementationRequired - public static PeanoNumberExpression fold(PeanoNumberExpression[] peanoNumberExpressions, PeanoNumberExpression initial, PeanoArithmeticExpression operation) { - PeanoNumberExpression result = initial; - for (PeanoNumberExpression peanoNumberExpression : peanoNumberExpressions) { - result = operation.evaluate(result, peanoNumberExpression); - } - return result; - } -} diff --git a/solution/H07/src/main/java/h07/peano/Successor.java b/solution/H07/src/main/java/h07/peano/Successor.java deleted file mode 100644 index 261f24f..0000000 --- a/solution/H07/src/main/java/h07/peano/Successor.java +++ /dev/null @@ -1,20 +0,0 @@ -package h07.peano; - -import org.tudalgo.algoutils.student.annotation.DoNotTouch; - -/** - * Represents a successor of a natural number in Peano arithmetic. - */ -@DoNotTouch -public class Successor extends NaturalNumber { - public final NaturalNumber predecessor; - - public Successor(NaturalNumber predecessor) { - this.predecessor = predecessor; - } - - @Override - public String toString() { - return "S(" + predecessor.toString() + ")"; - } -} diff --git a/solution/H07/src/main/java/h07/peano/Zero.java b/solution/H07/src/main/java/h07/peano/Zero.java deleted file mode 100644 index 4aa12d9..0000000 --- a/solution/H07/src/main/java/h07/peano/Zero.java +++ /dev/null @@ -1,14 +0,0 @@ -package h07.peano; - -import org.tudalgo.algoutils.student.annotation.DoNotTouch; - -/** - * Represents the number zero in Peano arithmetic. - */ -@DoNotTouch -public class Zero extends NaturalNumber { - @Override - public String toString() { - return "Z"; - } -} diff --git a/solution/H07/src/test b/solution/H07/src/test new file mode 120000 index 0000000..b48ee30 --- /dev/null +++ b/solution/H07/src/test @@ -0,0 +1 @@ +/home/osh/Desktop/FOP-2425-Marathon/H07/src/test \ No newline at end of file diff --git a/solution/H07/src/test/java/h07/ExampleJUnitTest.java b/solution/H07/src/test/java/h07/ExampleJUnitTest.java deleted file mode 100644 index 0dd0ee9..0000000 --- a/solution/H07/src/test/java/h07/ExampleJUnitTest.java +++ /dev/null @@ -1,16 +0,0 @@ -package h07; - -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.*; - -/** - * An example JUnit test class. - */ -public class ExampleJUnitTest { - - @Test - public void testAddition() { - assertEquals(2, 1 + 1); - } -}