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">
<modules>
<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$/aviation/aviation.iml" filepath="$PROJECT_DIR$/aviation/aviation.iml" />
<module fileurl="file://$PROJECT_DIR$/.idea/dhbw.iml" filepath="$PROJECT_DIR$/.idea/dhbw.iml" />

View File

@ -5,20 +5,21 @@
</component>
<component name="ChangeListManager">
<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$/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 afterPath="$PROJECT_DIR$/SchiffeVersenken/SchiffeVersenken.iml" 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$/ambiguos overloaded constructor/.idea/workspace.xml" beforeDir="false" afterPath="$PROJECT_DIR$/ambiguos overloaded constructor/.idea/workspace.xml" 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/BrokenSchiff.java" beforeDir="false" afterPath="$PROJECT_DIR$/SchiffeVersenken/src/BrokenSchiff.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>
<option name="SHOW_DIALOG" value="false" />
<option name="HIGHLIGHT_CONFLICTS" value="true" />
@ -36,19 +37,26 @@
<option name="hideEmptyMiddlePackages" value="true" />
<option name="showLibraryContents" value="true" />
</component>
<component name="PropertiesComponent"><![CDATA[{
"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"
<component name="PropertiesComponent">{
&quot;keyToString&quot;: {
&quot;ASKED_ADD_EXTERNAL_FILES&quot;: &quot;true&quot;,
&quot;ASKED_SHARE_PROJECT_CONFIGURATION_FILES&quot;: &quot;true&quot;,
&quot;RunOnceActivity.ShowReadmeOnStart&quot;: &quot;true&quot;,
&quot;SHARE_PROJECT_CONFIGURATION_FILES&quot;: &quot;true&quot;,
&quot;git-widget-placeholder&quot;: &quot;master&quot;,
&quot;jdk.selected.JAVA_MODULE&quot;: &quot;17&quot;,
&quot;last_opened_file_path&quot;: &quot;/home/plex/Documents/code/java/dhbw&quot;,
&quot;settings.editor.selected.configurable&quot;: &quot;preferences.lookFeel&quot;
}
}]]></component>
<component name="RunManager">
}</component>
<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">
<option name="MAIN_CLASS_NAME" value="Welt" />
<module name="Welt" />
@ -58,6 +66,7 @@
</configuration>
<recent_temporary>
<list>
<item itemvalue="Application.SchiffeVersenken" />
<item itemvalue="Application.Welt" />
</list>
</recent_temporary>
@ -100,7 +109,19 @@
<map>
<entry key="MAIN">
<value>
<State />
<State>
<option name="FILTERS">
<map>
<entry key="branch">
<value>
<list>
<option value="origin/master" />
</list>
</value>
</entry>
</map>
</option>
</State>
</value>
</entry>
</map>
@ -113,4 +134,19 @@
<MESSAGE value="weirdo intellij stuff" />
<option name="LAST_COMMIT_MESSAGE" value="weirdo intellij stuff" />
</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>

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
@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) {

View File

@ -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");
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());
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(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;
}
}

View File

@ -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() {

View File

@ -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) {

View File

@ -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;
}
}

View File

@ -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) {
try {
System.out.println("Hit where? (x,y; 0-15, 0-7)");
userin = scanner.nextLine();
if (userin.equals("!map"))
map.printMap();
System.out.println(Arrays.deepToString(map.map));
else if (userin.equals("!exit")) {
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();
}
}