Initial Commit
This commit is contained in:
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;
|
||||
}
|
||||
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user