Initial Commit
This commit is contained in:
29
.gitignore
vendored
Normal file
29
.gitignore
vendored
Normal file
@@ -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
|
||||
8
.idea/.gitignore
generated
vendored
Normal file
8
.idea/.gitignore
generated
vendored
Normal file
@@ -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
|
||||
6
.idea/misc.xml
generated
Normal file
6
.idea/misc.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectRootManager" version="2" languageLevel="JDK_20" default="true" project-jdk-name="openjdk-20" project-jdk-type="JavaSDK">
|
||||
<output url="file://$PROJECT_DIR$/out" />
|
||||
</component>
|
||||
</project>
|
||||
8
.idea/modules.xml
generated
Normal file
8
.idea/modules.xml
generated
Normal file
@@ -0,0 +1,8 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="ProjectModuleManager">
|
||||
<modules>
|
||||
<module fileurl="file://$PROJECT_DIR$/CatchTheMouse.iml" filepath="$PROJECT_DIR$/CatchTheMouse.iml" />
|
||||
</modules>
|
||||
</component>
|
||||
</project>
|
||||
6
.idea/vcs.xml
generated
Normal file
6
.idea/vcs.xml
generated
Normal file
@@ -0,0 +1,6 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project version="4">
|
||||
<component name="VcsDirectoryMappings">
|
||||
<mapping directory="$PROJECT_DIR$" vcs="Git" />
|
||||
</component>
|
||||
</project>
|
||||
28
CatchTheMouse.iml
Normal file
28
CatchTheMouse.iml
Normal file
@@ -0,0 +1,28 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<module type="JAVA_MODULE" version="4">
|
||||
<component name="NewModuleRootManager" inherit-compiler-output="true">
|
||||
<exclude-output />
|
||||
<content url="file://$MODULE_DIR$">
|
||||
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
|
||||
<sourceFolder url="file://$MODULE_DIR$/test" isTestSource="true" />
|
||||
</content>
|
||||
<orderEntry type="inheritedJdk" />
|
||||
<orderEntry type="sourceFolder" forTests="false" />
|
||||
<orderEntry type="module-library" scope="TEST">
|
||||
<library name="JUnit5.8.1">
|
||||
<CLASSES>
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" />
|
||||
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" />
|
||||
</CLASSES>
|
||||
<JAVADOC />
|
||||
<SOURCES />
|
||||
</library>
|
||||
</orderEntry>
|
||||
</component>
|
||||
</module>
|
||||
0
protokolle/13-04-2026_174226.txt
Normal file
0
protokolle/13-04-2026_174226.txt
Normal file
41
src/Cat.java
Normal file
41
src/Cat.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
35
src/CatchTheMouse.java
Normal file
35
src/CatchTheMouse.java
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
17
src/DisplayChars.java
Normal file
17
src/DisplayChars.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
92
src/Game.java
Normal file
92
src/Game.java
Normal file
@@ -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();
|
||||
}
|
||||
}
|
||||
}
|
||||
25
src/GamingArea.java
Normal file
25
src/GamingArea.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
48
src/Mouse.java
Normal file
48
src/Mouse.java
Normal file
@@ -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++;
|
||||
}
|
||||
}
|
||||
35
src/MouseMove.java
Normal file
35
src/MouseMove.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
26
src/Player.java
Normal file
26
src/Player.java
Normal file
@@ -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;
|
||||
}
|
||||
}
|
||||
31
src/Position.java
Normal file
31
src/Position.java
Normal file
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
35
test/GameTest.java
Normal file
35
test/GameTest.java
Normal file
@@ -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);
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
35
test/GamingAreaTest.java
Normal file
35
test/GamingAreaTest.java
Normal file
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user