diff --git a/.idea/modules.xml b/.idea/modules.xml
index 056eeed..247cfb6 100644
--- a/.idea/modules.xml
+++ b/.idea/modules.xml
@@ -3,6 +3,7 @@
+
diff --git a/.idea/workspace.xml b/.idea/workspace.xml
index e0b4067..4187164 100644
--- a/.idea/workspace.xml
+++ b/.idea/workspace.xml
@@ -5,20 +5,21 @@
-
-
-
-
-
-
-
-
-
+
-
-
-
+
+
+
+
+
+
+
+
+
+
+
+
@@ -36,19 +37,26 @@
- {
+ "keyToString": {
+ "ASKED_ADD_EXTERNAL_FILES": "true",
+ "ASKED_SHARE_PROJECT_CONFIGURATION_FILES": "true",
+ "RunOnceActivity.ShowReadmeOnStart": "true",
+ "SHARE_PROJECT_CONFIGURATION_FILES": "true",
+ "git-widget-placeholder": "master",
+ "jdk.selected.JAVA_MODULE": "17",
+ "last_opened_file_path": "/home/plex/Documents/code/java/dhbw",
+ "settings.editor.selected.configurable": "preferences.lookFeel"
}
-}]]>
-
+}
+
+
+
+
+
+
+
+
@@ -58,6 +66,7 @@
+
@@ -100,7 +109,19 @@
@@ -113,4 +134,19 @@
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/SchiffeVersenken/SchiffeVersenken.iml b/SchiffeVersenken/SchiffeVersenken.iml
new file mode 100644
index 0000000..c90834f
--- /dev/null
+++ b/SchiffeVersenken/SchiffeVersenken.iml
@@ -0,0 +1,11 @@
+
+
+
+
+
+
+
+
+
+
+
\ No newline at end of file
diff --git a/SchiffeVersenken/src/BrokenSchiff.java b/SchiffeVersenken/src/BrokenSchiff.java
index cfeafc0..ed0a243 100644
--- a/SchiffeVersenken/src/BrokenSchiff.java
+++ b/SchiffeVersenken/src/BrokenSchiff.java
@@ -2,8 +2,8 @@ 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 boolean insertToMap(int y, int x, Feld map, char orientation) {
+ return super.insertToMap(y, x, map, orientation);
}
public BrokenSchiff(int schiffLaenge) {
diff --git a/SchiffeVersenken/src/Feld.java b/SchiffeVersenken/src/Feld.java
index e3f5aa9..a89bd96 100644
--- a/SchiffeVersenken/src/Feld.java
+++ b/SchiffeVersenken/src/Feld.java
@@ -1,7 +1,8 @@
public class Feld {
public static final int sizeX = 16;
public static final int sizeY = 8;
- public final NoSchiff none = new NoSchiff();
+ private final NoSchiff none = new NoSchiff();
+ private final BrokenSchiff broken = new BrokenSchiff();
public Schiff[][] map = {
{none, none, none, none, none, none, none, none, none, none, none, none, none, none, none, none},
@@ -18,18 +19,25 @@ public class Feld {
}
public void printMap() {
- StringBuilder output = new StringBuilder("\033[2J");
+ StringBuilder output = new StringBuilder("\033[2J ");
+ int index = 0;
+ for (int i=0; i < sizeX; i++) {
+ output.append(String.format("%x ", i));
+ }
+ output.append('\n');
for (Schiff[] row : this.map) {
+ output.append(String.format("%x ", index));
for (Schiff ship : row) {
// this seems to be the repr of the Schiff class, not the object actually?
- output.append(ship.repr());
+ output.append(String.format("%s ", ship.repr()));
}
output.append('\n');
+ index++;
}
System.out.println(output);
}
- public boolean insertShip(Schiff ship, int x, int y) {
+ public boolean insertShip(Schiff ship, int y, int x) {
System.out.println(String.format(
"Inserting ship into map: %s",
ship
@@ -43,25 +51,33 @@ public class Feld {
return false;
}
- this.map[x][y] = ship;
- assert this.map[x][y] == ship;
+ this.map[y][x] = ship;
+ assert this.map[y][x] == ship;
return true;
}
- public boolean hit(int x, int y) {
+ public boolean hit(int y, int x) {
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 {
+ if (map[y][x] instanceof RealSchiff) {
+ if (map[y][x].laenge > 0) {
+ map[y][x].laenge -= 1;
+ map[y][x] = broken;
+ System.out.println("Hit!");
+ }
+ else {
+ map[y][x].laenge = 0;
+ map[y][x] = broken;
+ System.out.println("Ship destroyed!");
+ }
+ return true;
+ }
+ else {
// no hit
return false;
}
-
- // unreachable code
- return false;
}
}
diff --git a/SchiffeVersenken/src/NoSchiff.java b/SchiffeVersenken/src/NoSchiff.java
index 1be8262..d9b1a66 100644
--- a/SchiffeVersenken/src/NoSchiff.java
+++ b/SchiffeVersenken/src/NoSchiff.java
@@ -2,8 +2,8 @@ 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 boolean insertToMap(int y, int x, Feld map, char orientation) {
+ return super.insertToMap(y, x, map, orientation);
}
public NoSchiff() {
diff --git a/SchiffeVersenken/src/RealSchiff.java b/SchiffeVersenken/src/RealSchiff.java
index f9394f8..a6e3c6a 100644
--- a/SchiffeVersenken/src/RealSchiff.java
+++ b/SchiffeVersenken/src/RealSchiff.java
@@ -1,7 +1,7 @@
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 boolean insertToMap(int y, int x, Feld map, char orientation) {
+ return super.insertToMap(y, x, map, orientation);
}
public RealSchiff(int schiffLaenge) {
diff --git a/SchiffeVersenken/src/Schiff.java b/SchiffeVersenken/src/Schiff.java
index b038adf..0e0b5e8 100644
--- a/SchiffeVersenken/src/Schiff.java
+++ b/SchiffeVersenken/src/Schiff.java
@@ -1,6 +1,6 @@
public abstract class Schiff {
public int laenge;
- public char repr = '?';
+ char repr = '?';
public Schiff(int schiffLaenge) {
laenge = schiffLaenge;
@@ -10,26 +10,10 @@ public abstract class Schiff {
return this.repr;
}
- public boolean insertToMap(int x, int y, Feld map, char orientation) {
- if (orientation == '>') {
+ public boolean insertToMap(int y, int x, Feld map, char orientation) {
+ if (orientation == 'v') {
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)) {
+ if (!map.insertShip(this, y+i, x)) {
return false;
}
}
@@ -37,7 +21,23 @@ public abstract class Schiff {
}
else if (orientation == '|') {
for (int i = 0; i < this.laenge; i++){
- if (!map.insertShip(this, x, y-i)) {
+ if (!map.insertShip(this, y-i, x)) {
+ return false;
+ }
+ }
+ return true;
+ }
+ else if (orientation == '>') {
+ for (int i = 0; i < this.laenge; i++){
+ if (!map.insertShip(this, y, x+i)) {
+ return false;
+ }
+ }
+ return true;
+ }
+ else if (orientation == '<') {
+ for (int i = 0; i < this.laenge; i++){
+ if (!map.insertShip(this, y, x-i)) {
return false;
}
}
diff --git a/SchiffeVersenken/src/SchiffeVersenken.java b/SchiffeVersenken/src/SchiffeVersenken.java
index 0900720..c184c44 100644
--- a/SchiffeVersenken/src/SchiffeVersenken.java
+++ b/SchiffeVersenken/src/SchiffeVersenken.java
@@ -1,4 +1,5 @@
-import java.util.Arrays;
+import java.io.IOException;
+import java.util.Scanner;
/** Aufgabe 2: Objekte zerstören
*
@@ -16,16 +17,51 @@ public class SchiffeVersenken {
public static void main(String[] args) throws InterruptedException {
Feld map = new Feld();
- RealSchiff a = new RealSchiff(4);
+ RealSchiff a = new RealSchiff(1);
a.insertToMap(1, 2, map, 'v');
- BrokenSchiff b = new BrokenSchiff(4);
+ RealSchiff b = new RealSchiff(2);
b.insertToMap(3, 2, map, '>');
+ RealSchiff c = new RealSchiff(3);
+ c.insertToMap(3, 14, map, '|');
+ RealSchiff d = new RealSchiff(16);
+ d.insertToMap(7, 0, map, '>');
+ a = null;
+ b = null;
+ c = null;
+ d = null;
boolean gameover = false;
+ Scanner scanner = new Scanner(System.in);
+ int x;
+ int y;
+ String userin;
+ String[] userItems;
+ boolean result;
while (!gameover) {
- map.printMap();
- System.out.println(Arrays.deepToString(map.map));
- break;
- //Thread.sleep(1000);
+ try {
+ System.out.println("Hit where? (x,y; 0-15, 0-7)");
+ userin = scanner.nextLine();
+ if (userin.equals("!map"))
+ map.printMap();
+ else if (userin.equals("!exit")) {
+ break;
+ }
+ userItems = userin.split(",");
+ if (userItems.length != 2) {
+ System.out.println("Bad input!");
+ continue;
+ }
+ x = Integer.parseInt(userItems[0]);
+ y = Integer.parseInt(userItems[1]);
+ result = map.hit(y, x);
+ System.out.println(result);
+ System.out.println(String.format("dbg: at x %x, y %x -> %s", x, y, map.map[y][x]));
+ }
+ catch(Exception e) {
+ System.err.println(e);
+ }
+
}
+ System.out.println("You win!");
+ map.printMap();
}
}
\ No newline at end of file
diff --git a/out/production/SchiffeVersenken/BrokenSchiff.class b/out/production/SchiffeVersenken/BrokenSchiff.class
index a7eecdc..b73fced 100644
Binary files a/out/production/SchiffeVersenken/BrokenSchiff.class and b/out/production/SchiffeVersenken/BrokenSchiff.class differ
diff --git a/out/production/SchiffeVersenken/Feld.class b/out/production/SchiffeVersenken/Feld.class
deleted file mode 100644
index 442210b..0000000
Binary files a/out/production/SchiffeVersenken/Feld.class and /dev/null differ
diff --git a/out/production/SchiffeVersenken/NoSchiff.class b/out/production/SchiffeVersenken/NoSchiff.class
index 85452cc..ac04085 100644
Binary files a/out/production/SchiffeVersenken/NoSchiff.class and b/out/production/SchiffeVersenken/NoSchiff.class differ
diff --git a/out/production/SchiffeVersenken/RealSchiff.class b/out/production/SchiffeVersenken/RealSchiff.class
index dc6e44e..78a8920 100644
Binary files a/out/production/SchiffeVersenken/RealSchiff.class and b/out/production/SchiffeVersenken/RealSchiff.class differ
diff --git a/out/production/SchiffeVersenken/Schiff.class b/out/production/SchiffeVersenken/Schiff.class
index 03cc92e..3d432c7 100644
Binary files a/out/production/SchiffeVersenken/Schiff.class and b/out/production/SchiffeVersenken/Schiff.class differ
diff --git a/out/production/SchiffeVersenken/SchiffeVersenken.class b/out/production/SchiffeVersenken/SchiffeVersenken.class
deleted file mode 100644
index fef057f..0000000
Binary files a/out/production/SchiffeVersenken/SchiffeVersenken.class and /dev/null differ