From 95fe3639ec594a66215581f63f4c32333209b872 Mon Sep 17 00:00:00 2001 From: PlexSheep Date: Mon, 24 Apr 2023 15:35:55 +0200 Subject: [PATCH] schiffe overengineered --- .idea/modules.xml | 1 + .idea/workspace.xml | 86 +++++++++++++----- SchiffeVersenken/SchiffeVersenken.iml | 11 +++ SchiffeVersenken/src/BrokenSchiff.java | 4 +- SchiffeVersenken/src/Feld.java | 42 ++++++--- SchiffeVersenken/src/NoSchiff.java | 4 +- SchiffeVersenken/src/RealSchiff.java | 4 +- SchiffeVersenken/src/Schiff.java | 42 ++++----- SchiffeVersenken/src/SchiffeVersenken.java | 50 ++++++++-- .../SchiffeVersenken/BrokenSchiff.class | Bin 569 -> 569 bytes out/production/SchiffeVersenken/Feld.class | Bin 3186 -> 0 bytes .../SchiffeVersenken/NoSchiff.class | Bin 525 -> 525 bytes .../SchiffeVersenken/RealSchiff.class | Bin 493 -> 493 bytes out/production/SchiffeVersenken/Schiff.class | Bin 1081 -> 1081 bytes .../SchiffeVersenken/SchiffeVersenken.class | Bin 1094 -> 0 bytes 15 files changed, 172 insertions(+), 72 deletions(-) create mode 100644 SchiffeVersenken/SchiffeVersenken.iml delete mode 100644 out/production/SchiffeVersenken/Feld.class delete mode 100644 out/production/SchiffeVersenken/SchiffeVersenken.class 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 @@ - - - - - - - - - + - - - + + + + + + + + + + + + - { + "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" } -}]]> - +} + + + + @@ -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 a7eecdc35fbe93b532a7ed22ae0e2be13d041031..b73fced7b33869b2a1861574234fc0128b72d8ac 100644 GIT binary patch delta 20 acmdnVvXf=PG0sXx21ZXHuGn}!ixB`qY6b%U delta 20 acmdnVvXf=PG0qA`21ZXHuH1M&ixB`qVg>^M diff --git a/out/production/SchiffeVersenken/Feld.class b/out/production/SchiffeVersenken/Feld.class deleted file mode 100644 index 442210b998097e3127e839daaf43f1939723c203..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 3186 zcmc&$-%}G;6#kaoWRuMzOC$o4&}da4v7$j~B}!G4ibk=Qp1)(2T?ye+cFlIjCCGzAGb$nyB3J9h@{+x>x&9IN4Y zlqST&13C`k5b>CehE*f{LaS4|h<^yKa3s$|> zJa0G6`KrZZmTs%KBB5t%_p>dl3yxEFG!!65a7{vXlgpCr{y&i;Dy}nwuUlrdRVFv^ ziHaE=H*rfs$CkH=Dp3)C#cJA)RoQf2bWL}aFIaN7C4`#x*A^E_Qxe)INWe8~?u;NW zvxtqrpd==&PGzEAVdZrc?3#6Tb!FaiZV0v#;)QzItj?H@E#`qzahGg53KvA#=2_cb z;$0CnsEDMZrd6?o9jhrf1gYKgh>#I)LnzRL#s6cx}#F6D3V+#WdzZWIkhJ;zp+VCmo zpKY6ej!w~__>S>LH{;eLGQ(R*hX_;u1)!LS@Ciyb^9)kv1=P|rM4v*-7?GDSO0r?R zM7$(teuh>oDZU8#BJ2y*7ZG0=fz}ArMoS+J-fMx31yT=WdmwiOvcr?~x#I{zJB}c{ z;|SCpM-bU@1jf!EgR$f1@DyD?dJSieV;l;{PL2v&O<2iLV^X90Ed}!^b0(qEYB!P& z85(tSFVItp?|p`3fpy+>UJ%Lfi9LGb5a!k4|j$<^0c(Dz*YPBrC%hw5ZBKITr|vJ2{t+1p>`a~2Dc8Wh^gpN zu`IEbZgBJHHjN5{Kk?>oq&66vHvc8pOQ>RnBO5XPWQ;^Km&qVLqP#>~NzN9Z27JF2H*uPc^m*ix9=RmR^*uzpAU93oK99a! zGWR<^{tn?s2+5D-E!=wuJCteD*K*gA?2pem1`$UouYR`!ces5nEB2_dxUXZT4+lan^M{{mEZ Bm>&QD diff --git a/out/production/SchiffeVersenken/NoSchiff.class b/out/production/SchiffeVersenken/NoSchiff.class index 85452cc1ff5d9098bd0b8ab845db410765f7ffeb..ac040851563063ab98824e6d0e25e2725885d921 100644 GIT binary patch delta 16 XcmeBW>1CPlinWrFfw5xadpSk`E_(%3 delta 16 XcmeBW>1CPlinW4~fw6MqdpSk`E_Ve~ diff --git a/out/production/SchiffeVersenken/RealSchiff.class b/out/production/SchiffeVersenken/RealSchiff.class index dc6e44ed4ddb0bceed72c2bfb0f13df84611dd7d..78a8920dc003e6624635da86d50a9a3af2d0c85b 100644 GIT binary patch delta 20 acmaFM{FZsbVa`fM21ZXHuGn}+kr4n%HU=61 delta 20 acmaFM{FZsbVa^Ih21ZXHuH1M=kr4n%E(RI^ diff --git a/out/production/SchiffeVersenken/Schiff.class b/out/production/SchiffeVersenken/Schiff.class index 03cc92e67e64103cecbca244e23dbf0fc4d4dbaf..3d432c7977dd2dc6072dfa74b3b592197de7be3d 100644 GIT binary patch delta 54 zcmdnVv6Ewi5+iFRBLic_WOc@9Muy4FjC&?eW)hwp#3Vj>FOaTb5@)oVJcUVfawe1T J<_k=ji~yuq503x< delta 54 zcmdnVv6Ewi5+iE`BLidQWOc@9M#jm_jC&?eW)hwp#3Vj>FOaTb5@#%%JcUVfawe1T J<_k=ji~y&I56A!j diff --git a/out/production/SchiffeVersenken/SchiffeVersenken.class b/out/production/SchiffeVersenken/SchiffeVersenken.class deleted file mode 100644 index fef057f08e144a41d6a51eaa4dabe5c8f2206c07..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 1094 zcmZuw>rN9v6#k~&GVQWJX(>0aC`wC#iXtjSw_G8pkp##BV=26{SYs-#1%{ z(>fmDA;*}3I1(4sbn<>vRt{gbm9usawOs;CO9Sy zOd-V({f{yR-mI`}ll1t%6siH!q^TuU=A>JbHr*NB_KH)K3_Yf0%dL7PFWqgCFB1|m zoq{OmglnmD8`Qi*iwcRfYrZ5(6H`T4MA(0=s&)y99NC3iqS)xwqT6N~mR3s{n4o(y zFDHYDS)QTWyxL>}gC$Xs&WUu%zQ@p&^+e&A8r&4hF-NUgeP57O&vNV<#~Q=!%Ms1k zo^;)M)sw|PP85)J>TW@ti~lXlJws#?Qb$HqB3H zRc4?EGqf5l2Ry|rokDnq=R~4{e-KfC_KkD!-7|#DbLhML494b!7D$Fq8tBZ^@y}{8?F0If4Get3aPl+me!*x1<0`)&D*re=BOrnxJ&0cP(eTGGihdfT z0Ze0%lnh}G!&s;1pft|3bl?S{pr}Qf^)%`3CAIVP?0pnHiUqtP{%PV^#1b?tlkWh0 ezX%F&tZ;A+og+%M$MBkr8d0p$f1SR4*!T??{pqj(