diff --git a/SchiffeVersenken/.gitignore b/SchiffeVersenken/.gitignore new file mode 100644 index 0000000..f68d109 --- /dev/null +++ b/SchiffeVersenken/.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/SchiffeVersenken/src/BrokenSchiff.java b/SchiffeVersenken/src/BrokenSchiff.java new file mode 100644 index 0000000..cfeafc0 --- /dev/null +++ b/SchiffeVersenken/src/BrokenSchiff.java @@ -0,0 +1,17 @@ +public class BrokenSchiff extends Schiff { + // a broken ship that has sunken + + @Override + public boolean insertToMap(int x, int y, Feld map, char orientation) { + return super.insertToMap(x, y, map, orientation); + } + + public BrokenSchiff(int schiffLaenge) { + super(); + } + + public BrokenSchiff() { + super(); + repr='X'; + } +} diff --git a/SchiffeVersenken/src/Feld.java b/SchiffeVersenken/src/Feld.java new file mode 100644 index 0000000..e3f5aa9 --- /dev/null +++ b/SchiffeVersenken/src/Feld.java @@ -0,0 +1,67 @@ +public class Feld { + public static final int sizeX = 16; + public static final int sizeY = 8; + public final NoSchiff none = new NoSchiff(); + + public Schiff[][] map = { + {none, none, none, none, none, none, none, none, none, none, none, none, none, none, none, none}, + {none, none, none, none, none, none, none, none, none, none, none, none, none, none, none, none}, + {none, none, none, none, none, none, none, none, none, none, none, none, none, none, none, none}, + {none, none, none, none, none, none, none, none, none, none, none, none, none, none, none, none}, + {none, none, none, none, none, none, none, none, none, none, none, none, none, none, none, none}, + {none, none, none, none, none, none, none, none, none, none, none, none, none, none, none, none}, + {none, none, none, none, none, none, none, none, none, none, none, none, none, none, none, none}, + {none, none, none, none, none, none, none, none, none, none, none, none, none, none, none, none}, + }; + + public Feld() { + } + + public void printMap() { + StringBuilder output = new StringBuilder("\033[2J"); + for (Schiff[] row : this.map) { + for (Schiff ship : row) { + // this seems to be the repr of the Schiff class, not the object actually? + output.append(ship.repr()); + } + output.append('\n'); + } + System.out.println(output); + } + + public boolean insertShip(Schiff ship, int x, int y) { + System.out.println(String.format( + "Inserting ship into map: %s", + ship + )); + if (x >= sizeX || y >= sizeY || y < 0 || x < 0) { + System.err.println( + String.format("Tried to insert a ship at invalid coordinates:\n"+ + "x: %s\n"+ + "y: %s\n", x, y) + ); + return false; + } + + this.map[x][y] = ship; + assert this.map[x][y] == ship; + return true; + } + + public boolean hit(int x, int y) { + if (x >= sizeX || y >= sizeY || y < 0 || x < 0) { + System.err.println("Tried to hit a ship at invalid coordinates."); + return false; + } + + if (map[x][y] instanceof RealSchiff) { + + } else { + // no hit + return false; + } + + // unreachable code + return false; + } +} diff --git a/SchiffeVersenken/src/NoSchiff.java b/SchiffeVersenken/src/NoSchiff.java new file mode 100644 index 0000000..1be8262 --- /dev/null +++ b/SchiffeVersenken/src/NoSchiff.java @@ -0,0 +1,12 @@ +public class NoSchiff extends Schiff { + public final int laenge = 1; + + @Override + public boolean insertToMap(int x, int y, Feld map, char orientation) { + return super.insertToMap(x, y, map, orientation); + } + + public NoSchiff() { + repr = '~'; + } +} diff --git a/SchiffeVersenken/src/RealSchiff.java b/SchiffeVersenken/src/RealSchiff.java new file mode 100644 index 0000000..f9394f8 --- /dev/null +++ b/SchiffeVersenken/src/RealSchiff.java @@ -0,0 +1,11 @@ +public class RealSchiff extends Schiff { + @Override + public boolean insertToMap(int x, int y, Feld map, char orientation) { + return super.insertToMap(x, y, map, orientation); + } + + public RealSchiff(int schiffLaenge) { + super(schiffLaenge); + repr = 'S'; + } +} diff --git a/SchiffeVersenken/src/Schiff.java b/SchiffeVersenken/src/Schiff.java new file mode 100644 index 0000000..b038adf --- /dev/null +++ b/SchiffeVersenken/src/Schiff.java @@ -0,0 +1,51 @@ +public abstract class Schiff { + public int laenge; + public char repr = '?'; + + public Schiff(int schiffLaenge) { + laenge = schiffLaenge; + } + + public char repr() { + return this.repr; + } + + public boolean insertToMap(int x, int y, Feld map, char orientation) { + if (orientation == '>') { + for (int i = 0; i < this.laenge; i++){ + if (!map.insertShip(this, x+i, y)) { + return false; + } + } + return true; + } + else if (orientation == '<') { + for (int i = 0; i < this.laenge; i++){ + if (!map.insertShip(this, x-i, y)) { + return false; + } + } + return true; + } + else if (orientation == 'v') { + for (int i = 0; i < this.laenge; i++){ + if (!map.insertShip(this, x, y+i)) { + return false; + } + } + return true; + } + else if (orientation == '|') { + for (int i = 0; i < this.laenge; i++){ + if (!map.insertShip(this, x, y-i)) { + return false; + } + } + return true; + } + return false; + } + + public Schiff() { + } +} diff --git a/out/production/SchiffeVersenken/BrokenSchiff.class b/out/production/SchiffeVersenken/BrokenSchiff.class new file mode 100644 index 0000000..a7eecdc Binary files /dev/null and b/out/production/SchiffeVersenken/BrokenSchiff.class differ diff --git a/out/production/SchiffeVersenken/Feld.class b/out/production/SchiffeVersenken/Feld.class new file mode 100644 index 0000000..442210b Binary files /dev/null and b/out/production/SchiffeVersenken/Feld.class differ diff --git a/out/production/SchiffeVersenken/NoSchiff.class b/out/production/SchiffeVersenken/NoSchiff.class new file mode 100644 index 0000000..85452cc Binary files /dev/null and b/out/production/SchiffeVersenken/NoSchiff.class differ diff --git a/out/production/SchiffeVersenken/RealSchiff.class b/out/production/SchiffeVersenken/RealSchiff.class new file mode 100644 index 0000000..dc6e44e Binary files /dev/null and b/out/production/SchiffeVersenken/RealSchiff.class differ diff --git a/out/production/SchiffeVersenken/Schiff.class b/out/production/SchiffeVersenken/Schiff.class new file mode 100644 index 0000000..03cc92e Binary files /dev/null and b/out/production/SchiffeVersenken/Schiff.class differ diff --git a/out/production/SchiffeVersenken/SchiffeVersenken.class b/out/production/SchiffeVersenken/SchiffeVersenken.class new file mode 100644 index 0000000..fef057f Binary files /dev/null and b/out/production/SchiffeVersenken/SchiffeVersenken.class differ