schiffe overengineered

This commit is contained in:
Christoph J. Scherr 2023-04-24 15:35:55 +02:00
parent f1d4de3620
commit 95fe3639ec
Signed by: PlexSheep
GPG Key ID: 25B4ACF7D88186CC
15 changed files with 172 additions and 72 deletions

View File

@ -3,6 +3,7 @@
<component name="ProjectModuleManager"> <component name="ProjectModuleManager">
<modules> <modules>
<module fileurl="file://$PROJECT_DIR$/Kapselung/Kapselung.iml" filepath="$PROJECT_DIR$/Kapselung/Kapselung.iml" /> <module fileurl="file://$PROJECT_DIR$/Kapselung/Kapselung.iml" filepath="$PROJECT_DIR$/Kapselung/Kapselung.iml" />
<module fileurl="file://$PROJECT_DIR$/SchiffeVersenken/SchiffeVersenken.iml" filepath="$PROJECT_DIR$/SchiffeVersenken/SchiffeVersenken.iml" />
<module fileurl="file://$PROJECT_DIR$/Welt/Welt.iml" filepath="$PROJECT_DIR$/Welt/Welt.iml" /> <module fileurl="file://$PROJECT_DIR$/Welt/Welt.iml" filepath="$PROJECT_DIR$/Welt/Welt.iml" />
<module fileurl="file://$PROJECT_DIR$/aviation/aviation.iml" filepath="$PROJECT_DIR$/aviation/aviation.iml" /> <module fileurl="file://$PROJECT_DIR$/aviation/aviation.iml" filepath="$PROJECT_DIR$/aviation/aviation.iml" />
<module fileurl="file://$PROJECT_DIR$/.idea/dhbw.iml" filepath="$PROJECT_DIR$/.idea/dhbw.iml" /> <module fileurl="file://$PROJECT_DIR$/.idea/dhbw.iml" filepath="$PROJECT_DIR$/.idea/dhbw.iml" />

View File

@ -5,20 +5,21 @@
</component> </component>
<component name="ChangeListManager"> <component name="ChangeListManager">
<list default="true" id="7992561a-5a42-4f2e-a81a-79d8a9c03d77" name="Changes" comment="weirdo intellij stuff"> <list default="true" id="7992561a-5a42-4f2e-a81a-79d8a9c03d77" name="Changes" comment="weirdo intellij stuff">
<change afterPath="$PROJECT_DIR$/.idea/uiDesigner.xml" afterDir="false" /> <change afterPath="$PROJECT_DIR$/SchiffeVersenken/SchiffeVersenken.iml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Welt/.gitignore" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Welt/Welt.iml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/Welt/src/Welt.java" afterDir="false" />
<change afterPath="$PROJECT_DIR$/aviation/.idea/.gitignore" afterDir="false" />
<change afterPath="$PROJECT_DIR$/aviation/.idea/misc.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/aviation/.idea/modules.xml" afterDir="false" />
<change afterPath="$PROJECT_DIR$/aviation/.idea/vcs.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/misc.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/misc.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/modules.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/modules.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/modules.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/modules.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/vcs.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/vcs.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/.idea/workspace.xml" afterDir="false" />
<change beforePath="$PROJECT_DIR$/ambiguos overloaded constructor/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/ambiguos overloaded constructor/.idea/workspace.xml" afterDir="false" /> <change beforePath="$PROJECT_DIR$/SchiffeVersenken/src/BrokenSchiff.java" beforeDir="false" afterPath="$PROJECT_DIR$/SchiffeVersenken/src/BrokenSchiff.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/ambiguos overloaded constructor/src/Main.java" beforeDir="false" afterPath="$PROJECT_DIR$/ambiguos overloaded constructor/src/Main.java" afterDir="false" /> <change beforePath="$PROJECT_DIR$/SchiffeVersenken/src/Feld.java" beforeDir="false" afterPath="$PROJECT_DIR$/SchiffeVersenken/src/Feld.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/SchiffeVersenken/src/NoSchiff.java" beforeDir="false" afterPath="$PROJECT_DIR$/SchiffeVersenken/src/NoSchiff.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/SchiffeVersenken/src/RealSchiff.java" beforeDir="false" afterPath="$PROJECT_DIR$/SchiffeVersenken/src/RealSchiff.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/SchiffeVersenken/src/Schiff.java" beforeDir="false" afterPath="$PROJECT_DIR$/SchiffeVersenken/src/Schiff.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/SchiffeVersenken/src/SchiffeVersenken.java" beforeDir="false" afterPath="$PROJECT_DIR$/SchiffeVersenken/src/SchiffeVersenken.java" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/SchiffeVersenken/BrokenSchiff.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/SchiffeVersenken/BrokenSchiff.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/SchiffeVersenken/Feld.class" beforeDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/SchiffeVersenken/NoSchiff.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/SchiffeVersenken/NoSchiff.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/SchiffeVersenken/RealSchiff.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/SchiffeVersenken/RealSchiff.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/SchiffeVersenken/Schiff.class" beforeDir="false" afterPath="$PROJECT_DIR$/out/production/SchiffeVersenken/Schiff.class" afterDir="false" />
<change beforePath="$PROJECT_DIR$/out/production/SchiffeVersenken/SchiffeVersenken.class" beforeDir="false" />
</list> </list>
<option name="SHOW_DIALOG" value="false" /> <option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" /> <option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -36,19 +37,26 @@
<option name="hideEmptyMiddlePackages" value="true" /> <option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" /> <option name="showLibraryContents" value="true" />
</component> </component>
<component name="PropertiesComponent"><![CDATA[{ <component name="PropertiesComponent">{
"keyToString": { &quot;keyToString&quot;: {
"ASKED_ADD_EXTERNAL_FILES": "true", &quot;ASKED_ADD_EXTERNAL_FILES&quot;: &quot;true&quot;,
"ASKED_SHARE_PROJECT_CONFIGURATION_FILES": "true", &quot;ASKED_SHARE_PROJECT_CONFIGURATION_FILES&quot;: &quot;true&quot;,
"RunOnceActivity.ShowReadmeOnStart": "true", &quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
"SHARE_PROJECT_CONFIGURATION_FILES": "true", &quot;SHARE_PROJECT_CONFIGURATION_FILES&quot;: &quot;true&quot;,
"git-widget-placeholder": "master", &quot;git-widget-placeholder&quot;: &quot;master&quot;,
"jdk.selected.JAVA_MODULE": "17", &quot;jdk.selected.JAVA_MODULE&quot;: &quot;17&quot;,
"last_opened_file_path": "/home/plex/Documents/code/java/dhbw", &quot;last_opened_file_path&quot;: &quot;/home/plex/Documents/code/java/dhbw&quot;,
"settings.editor.selected.configurable": "preferences.lookFeel" &quot;settings.editor.selected.configurable&quot;: &quot;preferences.lookFeel&quot;
} }
}]]></component> }</component>
<component name="RunManager"> <component name="RunManager" selected="Application.SchiffeVersenken">
<configuration name="SchiffeVersenken" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="SchiffeVersenken" />
<module name="SchiffeVersenken" />
<method v="2">
<option name="Make" enabled="true" />
</method>
</configuration>
<configuration name="Welt" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true"> <configuration name="Welt" type="Application" factoryName="Application" temporary="true" nameIsGenerated="true">
<option name="MAIN_CLASS_NAME" value="Welt" /> <option name="MAIN_CLASS_NAME" value="Welt" />
<module name="Welt" /> <module name="Welt" />
@ -58,6 +66,7 @@
</configuration> </configuration>
<recent_temporary> <recent_temporary>
<list> <list>
<item itemvalue="Application.SchiffeVersenken" />
<item itemvalue="Application.Welt" /> <item itemvalue="Application.Welt" />
</list> </list>
</recent_temporary> </recent_temporary>
@ -100,7 +109,19 @@
<map> <map>
<entry key="MAIN"> <entry key="MAIN">
<value> <value>
<State /> <State>
<option name="FILTERS">
<map>
<entry key="branch">
<value>
<list>
<option value="origin/master" />
</list>
</value>
</entry>
</map>
</option>
</State>
</value> </value>
</entry> </entry>
</map> </map>
@ -113,4 +134,19 @@
<MESSAGE value="weirdo intellij stuff" /> <MESSAGE value="weirdo intellij stuff" />
<option name="LAST_COMMIT_MESSAGE" value="weirdo intellij stuff" /> <option name="LAST_COMMIT_MESSAGE" value="weirdo intellij stuff" />
</component> </component>
<component name="XDebuggerManager">
<breakpoint-manager>
<breakpoints>
<breakpoint enabled="true" type="java-exception">
<properties class="java.lang.ArrayIndexOutOfBoundsException" package="java.lang" />
<option name="timeStamp" value="33" />
</breakpoint>
</breakpoints>
</breakpoint-manager>
<watches-manager>
<configuration name="Application">
<watch expression="ship.repr()" language="JAVA" />
</configuration>
</watches-manager>
</component>
</project> </project>

View File

@ -0,0 +1,11 @@
<?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" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -2,8 +2,8 @@ public class BrokenSchiff extends Schiff {
// a broken ship that has sunken // a broken ship that has sunken
@Override @Override
public boolean insertToMap(int x, int y, Feld map, char orientation) { public boolean insertToMap(int y, int x, Feld map, char orientation) {
return super.insertToMap(x, y, map, orientation); return super.insertToMap(y, x, map, orientation);
} }
public BrokenSchiff(int schiffLaenge) { public BrokenSchiff(int schiffLaenge) {

View File

@ -1,7 +1,8 @@
public class Feld { public class Feld {
public static final int sizeX = 16; public static final int sizeX = 16;
public static final int sizeY = 8; 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 = { 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},
@ -19,17 +20,24 @@ public class Feld {
public void printMap() { public void printMap() {
StringBuilder output = new StringBuilder("\033[2J "); StringBuilder output = new StringBuilder("\033[2J ");
for (Schiff[] row : this.map) { int index = 0;
for (Schiff ship : row) { for (int i=0; i < sizeX; i++) {
// this seems to be the repr of the Schiff class, not the object actually? output.append(String.format("%x ", i));
output.append(ship.repr());
} }
output.append('\n'); 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(String.format("%s ", ship.repr()));
}
output.append('\n');
index++;
} }
System.out.println(output); 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( System.out.println(String.format(
"Inserting ship into map: %s", "Inserting ship into map: %s",
ship ship
@ -43,25 +51,33 @@ public class Feld {
return false; return false;
} }
this.map[x][y] = ship; this.map[y][x] = ship;
assert this.map[x][y] == ship; assert this.map[y][x] == ship;
return true; 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) { if (x >= sizeX || y >= sizeY || y < 0 || x < 0) {
System.err.println("Tried to hit a ship at invalid coordinates."); System.err.println("Tried to hit a ship at invalid coordinates.");
return false; return false;
} }
if (map[x][y] instanceof RealSchiff) { if (map[y][x] instanceof RealSchiff) {
if (map[y][x].laenge > 0) {
} else { 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 // no hit
return false; return false;
} }
// unreachable code
return false;
} }
} }

View File

@ -2,8 +2,8 @@ public class NoSchiff extends Schiff {
public final int laenge = 1; public final int laenge = 1;
@Override @Override
public boolean insertToMap(int x, int y, Feld map, char orientation) { public boolean insertToMap(int y, int x, Feld map, char orientation) {
return super.insertToMap(x, y, map, orientation); return super.insertToMap(y, x, map, orientation);
} }
public NoSchiff() { public NoSchiff() {

View File

@ -1,7 +1,7 @@
public class RealSchiff extends Schiff { public class RealSchiff extends Schiff {
@Override @Override
public boolean insertToMap(int x, int y, Feld map, char orientation) { public boolean insertToMap(int y, int x, Feld map, char orientation) {
return super.insertToMap(x, y, map, orientation); return super.insertToMap(y, x, map, orientation);
} }
public RealSchiff(int schiffLaenge) { public RealSchiff(int schiffLaenge) {

View File

@ -1,6 +1,6 @@
public abstract class Schiff { public abstract class Schiff {
public int laenge; public int laenge;
public char repr = '?'; char repr = '?';
public Schiff(int schiffLaenge) { public Schiff(int schiffLaenge) {
laenge = schiffLaenge; laenge = schiffLaenge;
@ -10,26 +10,10 @@ public abstract class Schiff {
return this.repr; return this.repr;
} }
public boolean insertToMap(int x, int y, Feld map, char orientation) { public boolean insertToMap(int y, int x, Feld map, char orientation) {
if (orientation == '>') { if (orientation == 'v') {
for (int i = 0; i < this.laenge; i++){ for (int i = 0; i < this.laenge; i++){
if (!map.insertShip(this, x+i, y)) { 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, 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 false;
} }
} }
@ -37,7 +21,23 @@ public abstract class Schiff {
} }
else if (orientation == '|') { else if (orientation == '|') {
for (int i = 0; i < this.laenge; i++){ 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; return false;
} }
} }

View File

@ -1,4 +1,5 @@
import java.util.Arrays; import java.io.IOException;
import java.util.Scanner;
/** Aufgabe 2: Objekte zerstören /** Aufgabe 2: Objekte zerstören
* *
@ -16,16 +17,51 @@ public class SchiffeVersenken {
public static void main(String[] args) throws InterruptedException { public static void main(String[] args) throws InterruptedException {
Feld map = new Feld(); Feld map = new Feld();
RealSchiff a = new RealSchiff(4); RealSchiff a = new RealSchiff(1);
a.insertToMap(1, 2, map, 'v'); a.insertToMap(1, 2, map, 'v');
BrokenSchiff b = new BrokenSchiff(4); RealSchiff b = new RealSchiff(2);
b.insertToMap(3, 2, map, '>'); 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; boolean gameover = false;
Scanner scanner = new Scanner(System.in);
int x;
int y;
String userin;
String[] userItems;
boolean result;
while (!gameover) { while (!gameover) {
try {
System.out.println("Hit where? (x,y; 0-15, 0-7)");
userin = scanner.nextLine();
if (userin.equals("!map"))
map.printMap(); map.printMap();
System.out.println(Arrays.deepToString(map.map)); else if (userin.equals("!exit")) {
break; break;
//Thread.sleep(1000); }
} 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();
} }
} }