commit d62d6132d2c1dfaf6e44278b9174f84c8636432d Author: tamara1311 Date: Mon Apr 13 17:44:27 2026 +0200 Initial Commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/.gitignore @@ -0,0 +1,29 @@ +### IntelliJ IDEA ### +out/ +!**/src/main/**/out/ +!**/src/test/**/out/ + +### Eclipse ### +.apt_generated +.classpath +.factorypath +.project +.settings +.springBeans +.sts4-cache +bin/ +!**/src/main/**/bin/ +!**/src/test/**/bin/ + +### NetBeans ### +/nbproject/private/ +/nbbuild/ +/dist/ +/nbdist/ +/.nb-gradle/ + +### VS Code ### +.vscode/ + +### Mac OS ### +.DS_Store \ No newline at end of file diff --git a/.idea/.gitignore b/.idea/.gitignore new file mode 100644 index 0000000..13566b8 --- /dev/null +++ b/.idea/.gitignore @@ -0,0 +1,8 @@ +# Default ignored files +/shelf/ +/workspace.xml +# Editor-based HTTP Client requests +/httpRequests/ +# Datasource local storage ignored files +/dataSources/ +/dataSources.local.xml diff --git a/.idea/misc.xml b/.idea/misc.xml new file mode 100644 index 0000000..cf9abe6 --- /dev/null +++ b/.idea/misc.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/.idea/modules.xml b/.idea/modules.xml new file mode 100644 index 0000000..32d6432 --- /dev/null +++ b/.idea/modules.xml @@ -0,0 +1,8 @@ + + + + + + + + \ No newline at end of file diff --git a/.idea/vcs.xml b/.idea/vcs.xml new file mode 100644 index 0000000..94a25f7 --- /dev/null +++ b/.idea/vcs.xml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/CatchTheMouse.iml b/CatchTheMouse.iml new file mode 100644 index 0000000..f9f9807 --- /dev/null +++ b/CatchTheMouse.iml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/protokolle/13-04-2026_174226.txt b/protokolle/13-04-2026_174226.txt new file mode 100644 index 0000000..e69de29 diff --git a/src/Cat.java b/src/Cat.java new file mode 100644 index 0000000..aa9def0 --- /dev/null +++ b/src/Cat.java @@ -0,0 +1,41 @@ +import java.io.IOException; +import java.util.Random; +import java.util.Scanner; + +public class Cat extends Player{ + + Scanner scanner = new Scanner(System.in); + public Cat(Position p, GamingArea g, Game ga) throws IOException { + super(p, g, ga); + p.displayChar = DisplayChars.TOM.getCharacter(); + } + + @Override + public void movePlayer() throws IOException { + System.out.print("Tom moves to (line-column): "); + String input = scanner.next(); + + if (!input.matches("^[0-9]+-[0-9]+$")) { + System.out.println("Invalid input!"); + System.exit(1); + } + + String inputArr[] = input.split("-"); + + int posX = Integer.parseInt(inputArr[0]); + int posY = Integer.parseInt(inputArr[1]); + + if (!gArea.validatePosition(new Position(posX,posY))) { + System.out.println("Invalid input!"); + System.exit(1); + } + + Position p = gArea.playingField[posX][posY]; + p.displayChar = DisplayChars.TOM.getCharacter(); + + game.writer.write("Tom moves from " + positionPlayer.posX+ "-" + positionPlayer.posY + " to " + posX + "-" + posY); + game.writer.newLine(); + + movePlayer(p); + } +} diff --git a/src/CatchTheMouse.java b/src/CatchTheMouse.java new file mode 100644 index 0000000..465e7f1 --- /dev/null +++ b/src/CatchTheMouse.java @@ -0,0 +1,35 @@ +import java.io.IOException; +import java.util.Scanner; + +public class CatchTheMouse { + public static void main(String[] args) throws IOException { + + Scanner scanner = new Scanner(System.in); + int width; + int height; + int round = 0; + + System.out.println("Welcome to Catch the Mouse"); + Game game = Game.getInstance(10, 10); + + try { + while(!game.gameOver()) + { + game.writer.write("Round " + round); + game.writer.newLine(); + game.printField(); + game.cat.movePlayer(); + game.mouse.movePlayer(); + Thread.sleep(1000); + + round++; + } + game.writer.flush(); + + System.out.println("Congrats! You caught the mouse!"); + } catch (Exception e) { + game.writer.flush(); + e.printStackTrace(); + } + } +} diff --git a/src/DisplayChars.java b/src/DisplayChars.java new file mode 100644 index 0000000..6bce3f6 --- /dev/null +++ b/src/DisplayChars.java @@ -0,0 +1,17 @@ +public enum DisplayChars { + TOM('C'), + JERRY('M'), + DEFAULT('.'); + + private char c; + + DisplayChars(char c) + { + this.c = c; + } + + public char getCharacter() + { + return c; + } +} diff --git a/src/Game.java b/src/Game.java new file mode 100644 index 0000000..78db2ec --- /dev/null +++ b/src/Game.java @@ -0,0 +1,92 @@ +import javax.swing.text.DateFormatter; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.sql.Time; +import java.time.LocalDate; +import java.time.LocalDateTime; +import java.time.format.DateTimeFormatter; +import java.time.format.DateTimeFormatterBuilder; +import java.util.Date; + +public class Game { + + private static Game instance; + public Mouse mouse; + public Cat cat; + public GamingArea gArea; + public BufferedWriter writer; + + private String NewFileName() { + DateTimeFormatter df = DateTimeFormatter.ofPattern("dd-MM-YYYY_HHmmss"); + return "./protokolle/" + df.format(LocalDateTime.now()) + ".txt"; + } + + private Game(int width, int height) throws IOException { + gArea = new GamingArea(height, width); + + String filename = NewFileName(); + + File f = new File(filename); + try { + boolean ok = f.createNewFile(); + if (!ok) { + System.out.println("Cannot create file"); + } + } catch (Exception ex) { + ex.printStackTrace(); + } + + writer = new BufferedWriter(new FileWriter(new File(filename))); + + initialisePlayers(); + } + + + public static Game getInstance(int width, int height) throws IOException { + if (instance == null) + { + instance = new Game(width, height); + } + return instance; + } + + private void initialisePlayers() throws IOException { + mouse = new Mouse(new Position(4,4), gArea, this); + cat = new Cat(new Position(0,0), gArea, this); + } + public boolean gameOver() + { + if(mouse.positionPlayer.equals(cat.positionPlayer)) + { + return true; + } + else + { + return false; + } + } + public void printField() + { + int rowNum = 0; + System.out.print(" "); + for(int colNum = 0; colNum < 10; colNum++ ) + { + System.out.print(colNum + " "); + } + System.out.println(); + Position[][] field = gArea.getPlayingField(); + for (Position[] row : field) + { + System.out.print(rowNum + " "); + rowNum++; + + for (Position col : row) + { + System.out.print(col.displayChar + " "); + } + System.out.println(); + } + } +} diff --git a/src/GamingArea.java b/src/GamingArea.java new file mode 100644 index 0000000..2e4e2c2 --- /dev/null +++ b/src/GamingArea.java @@ -0,0 +1,25 @@ +public class GamingArea { + public Position[][] playingField; + public Position[][] getPlayingField() + { + return playingField; + } + public GamingArea(int height, int width) + { + playingField = new Position[height][width]; + for (int row = 0; row < height; row++) { + for (int col = 0; col < width; col++) { + playingField[row][col] = new Position(row, col); + } + } + } + + public boolean validatePosition(Position p) { + try { + Position po = this.playingField[p.posX][p.posY]; + } catch (IndexOutOfBoundsException ex) { + return false; + } + return true; + } +} diff --git a/src/Mouse.java b/src/Mouse.java new file mode 100644 index 0000000..83edb3e --- /dev/null +++ b/src/Mouse.java @@ -0,0 +1,48 @@ +import java.io.IOException; + +public class Mouse extends Player{ + + public static int counter = 0; + + public Mouse(Position p, GamingArea g, Game ga) throws IOException { + super(p, g, ga); + p.displayChar = DisplayChars.JERRY.getCharacter(); + } + @Override + public void movePlayer() throws IOException { + MouseMove mm = MouseMove.getMove(); + int posX = this.positionPlayer.posX + mm.deltaX; + int posY = this.positionPlayer.posY + mm.deltaY; + + if (!gArea.validatePosition(new Position(posX, posY))) { + if(posX < 0 || posX > 9) + { + posX = posX-mm.deltaX; + } + if(posY < 0 || posY > 9) + { + posY = posY-mm.deltaY; + } + } + + Position p = gArea.playingField[posX][posY]; + + if((counter%3) == 0) + { + p.displayChar = DisplayChars.JERRY.getCharacter(); + } + else + { + p.displayChar = DisplayChars.DEFAULT.getCharacter(); + } + + + System.out.println("Jerry moves to " + posX + "-" + posY); + game.writer.write("Jerry moves from " + positionPlayer.posX+ "-" + positionPlayer.posY + " to " + posX + "-" + posY); + game.writer.newLine(); + + movePlayer(p); + + counter++; + } +} diff --git a/src/MouseMove.java b/src/MouseMove.java new file mode 100644 index 0000000..f25688b --- /dev/null +++ b/src/MouseMove.java @@ -0,0 +1,35 @@ +import java.util.Random; + +public class MouseMove { + private static Random random = new Random(); + private static MouseMove[] allMoves = new MouseMove[0]; + protected int deltaX; + protected int deltaY; + + public MouseMove() + {} + + private MouseMove(int deltaX, int deltaY) + { + this.deltaX = deltaX; + this.deltaY = deltaY; + } + + public static MouseMove getMove() + { + int deltaX = random.nextInt(-1, 2); + int deltaY = random.nextInt(-1, 2); + MouseMove mm = new MouseMove(deltaX, deltaY); + + MouseMove[] newMoves = new MouseMove[allMoves.length + 1]; + newMoves[allMoves.length] = mm; + + for (int i = 0; i < allMoves.length; i++) { + newMoves[i] = allMoves[i]; + } + + allMoves = newMoves; + + return mm; + } +} diff --git a/src/Player.java b/src/Player.java new file mode 100644 index 0000000..4850bc7 --- /dev/null +++ b/src/Player.java @@ -0,0 +1,26 @@ +import java.io.IOException; + +public abstract class Player { + protected Position positionPlayer; + protected GamingArea gArea; + protected Game game; + + public Player (Position p, GamingArea g, Game ga) throws IOException { + positionPlayer = p; + gArea = g; + game = ga; + + gArea.playingField[p.posX][p.posY] = p; + } + + public abstract void movePlayer() throws IOException; + + public void movePlayer(Position pos) + { + if(positionPlayer.displayChar == pos.displayChar && !positionPlayer.equals(pos)) { + positionPlayer.displayChar = DisplayChars.DEFAULT.getCharacter(); + } + + positionPlayer = pos; + } +} diff --git a/src/Position.java b/src/Position.java new file mode 100644 index 0000000..10de5bd --- /dev/null +++ b/src/Position.java @@ -0,0 +1,31 @@ +import java.math.BigInteger; + +public class Position { + public int posX; + public int posY; + public char displayChar; + + public Position(int x, int y) + { + posX = x; + posY = y; + displayChar = DisplayChars.DEFAULT.getCharacter(); + } + + @Override + public String toString() { + return displayChar + ""; + } + + @Override + public boolean equals(Object o) { + if (o instanceof Position p) { + return p.posX == this.posX && p.posY == this.posY; + } + + return false; + } + + + +} diff --git a/test/GameTest.java b/test/GameTest.java new file mode 100644 index 0000000..51b383e --- /dev/null +++ b/test/GameTest.java @@ -0,0 +1,35 @@ +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class GameTest { + + @Test + void getInstance_providesInstance_isNotEmpty() { + //Arrange + Game game; + + //Act + game = Game.getInstance(10,10); + + //Assert + assertNotNull(game); + } + + @Test + void gameOver_getsBoolean_isFalse() { + + //Arrange + Game game = Game.getInstance(10,10); + boolean expected = false; + boolean actual; + + //Act + actual = game.gameOver(); + + //Assert + assertEquals(expected, actual); + + } + +} \ No newline at end of file diff --git a/test/GamingAreaTest.java b/test/GamingAreaTest.java new file mode 100644 index 0000000..9c4daf3 --- /dev/null +++ b/test/GamingAreaTest.java @@ -0,0 +1,35 @@ +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.*; + +class GamingAreaTest { + + @Test + void getPlayingField_returnsPlayingField_returnsArray() { + + //Arrange + GamingArea ga = new GamingArea(10,10); + Position[][] playingField; + + //Act + playingField = ga.getPlayingField(); + + //Assert + assertNotNull(playingField); + } + + @Test + void validatePosition_checksPosition_returnsTrue() { + + //Arrange + GamingArea ga = new GamingArea(10,10); + boolean expected = true; + boolean actual; + + //Act + actual = ga.validatePosition(new Position(4,4)); + + //Assert + assertEquals(expected, actual); + } +} \ No newline at end of file