From 5d52b6b876b89a19c535fa7bc6d13d08b0b6e0aa Mon Sep 17 00:00:00 2001 From: Julius Dewender Date: Sun, 31 Jan 2021 11:22:28 +0100 Subject: [PATCH] refactored moveAllowed() --- .../src/main/java/de/fd/fh/Figure.java | 4 +++- .../src/main/java/de/fd/fh/FigureFarmer.java | 6 ++++- .../src/main/java/de/fd/fh/FigureRook.java | 5 +++- .../test/java/de/fd/fh/FigureFarmerTest.java | 20 ++++++++++++---- .../test/java/de/fd/fh/FigureRookTest.java | 24 ++++++++++++++----- .../src/test/java/de/fd/fh/FigureTest.java | 16 +++++++++---- 6 files changed, 57 insertions(+), 18 deletions(-) diff --git a/fh.fd.ci.client/src/main/java/de/fd/fh/Figure.java b/fh.fd.ci.client/src/main/java/de/fd/fh/Figure.java index d592c94..745d961 100644 --- a/fh.fd.ci.client/src/main/java/de/fd/fh/Figure.java +++ b/fh.fd.ci.client/src/main/java/de/fd/fh/Figure.java @@ -4,7 +4,7 @@ public class Figure { public static final int fieldLength = 8; - public boolean moveAllowed(int src, int dst, int[] field) + public boolean moveAllowed(int src, int dst, Figure[] field) { if (src == dst) { @@ -16,6 +16,8 @@ public class Figure return false; } + // TODO: im Falle von Schach muss Zug Situation auflösen, sonst nicht möglich + return true; } } diff --git a/fh.fd.ci.client/src/main/java/de/fd/fh/FigureFarmer.java b/fh.fd.ci.client/src/main/java/de/fd/fh/FigureFarmer.java index 76ecf78..49ec0be 100644 --- a/fh.fd.ci.client/src/main/java/de/fd/fh/FigureFarmer.java +++ b/fh.fd.ci.client/src/main/java/de/fd/fh/FigureFarmer.java @@ -3,7 +3,7 @@ package de.fd.fh; public class FigureFarmer extends Figure { @Override - public boolean moveAllowed(int src, int dst, int[] field) + public boolean moveAllowed(int src, int dst, Figure[] field) { int rowDiff = src / fieldLength - dst / fieldLength; // hoch/runter @@ -17,6 +17,10 @@ public class FigureFarmer extends Figure return false; } + // TODO: nur ein Schritt nach oben, wenn auf Ziel keine Figur + // TODO: links, rechts verboten + // TODO: Diagonal links/rechts hoch, wenn Figur geschlagen werden kann (Figur auf Ziel) + return super.moveAllowed(src, dst, field); } } diff --git a/fh.fd.ci.client/src/main/java/de/fd/fh/FigureRook.java b/fh.fd.ci.client/src/main/java/de/fd/fh/FigureRook.java index 968180f..545eccc 100644 --- a/fh.fd.ci.client/src/main/java/de/fd/fh/FigureRook.java +++ b/fh.fd.ci.client/src/main/java/de/fd/fh/FigureRook.java @@ -3,7 +3,7 @@ package de.fd.fh; public class FigureRook extends Figure { @Override - public boolean moveAllowed(int src, int dst, int[] field) + public boolean moveAllowed(int src, int dst, Figure[] field) { int rowDiff = src / fieldLength - dst / fieldLength; // hoch/runter int relStep = src % fieldLength - dst % fieldLength; // links//rechts @@ -28,6 +28,9 @@ public class FigureRook extends Figure return false; } + // TODO: keine Figuren auf dem Weg "überspringen" + // TODO: falls Figur auf Ziel und keine Figuren auf Weg -> Figur schlagen + return super.moveAllowed(src, dst, field); } } diff --git a/fh.fd.ci.client/src/test/java/de/fd/fh/FigureFarmerTest.java b/fh.fd.ci.client/src/test/java/de/fd/fh/FigureFarmerTest.java index 1405b85..9e17ae4 100644 --- a/fh.fd.ci.client/src/test/java/de/fd/fh/FigureFarmerTest.java +++ b/fh.fd.ci.client/src/test/java/de/fd/fh/FigureFarmerTest.java @@ -11,34 +11,44 @@ class FigureFarmerTest void whiteFarmerValidDestination() { Figure f = new FigureFarmer(); - assertTrue(f.moveAllowed(Figure.fieldLength * 6 + 0, Figure.fieldLength * 5 + 0, new int[Figure.fieldLength * Figure.fieldLength])); + assertTrue(f.moveAllowed(Figure.fieldLength * 6 + 0, + Figure.fieldLength * 5 + 0, + new Figure[Figure.fieldLength * Figure.fieldLength])); } @Test void whiteFarmerInvalidDestinationDown() // statt nach "oben" wird nach "unten" gegangen { Figure f = new FigureFarmer(); - assertFalse(f.moveAllowed(Figure.fieldLength * 6 + 0, Figure.fieldLength * 7 + 0, new int[Figure.fieldLength * Figure.fieldLength])); + assertFalse(f.moveAllowed(Figure.fieldLength * 6 + 0, + Figure.fieldLength * 7 + 0, + new Figure[Figure.fieldLength * Figure.fieldLength])); } @Test void whiteFarmerInvalidDestinationUp() // mehr als 1 Schritt nach "oben" { Figure f = new FigureFarmer(); - assertFalse(f.moveAllowed(Figure.fieldLength * 6 + 0, Figure.fieldLength * 4 + 0, new int[Figure.fieldLength * Figure.fieldLength])); + assertFalse(f.moveAllowed(Figure.fieldLength * 6 + 0, + Figure.fieldLength * 4 + 0, + new Figure[Figure.fieldLength * Figure.fieldLength])); } @Test void checkMoveWithValidDestinationVar2() // Bauer (w) steht in der Mitte vom Feld und geht 1 nach oben { Figure f = new FigureFarmer(); - assertTrue(f.moveAllowed(Figure.fieldLength * 4 + 4, Figure.fieldLength * 3 + 4, new int[Figure.fieldLength * Figure.fieldLength])); + assertTrue(f.moveAllowed(Figure.fieldLength * 4 + 4, + Figure.fieldLength * 3 + 4, + new Figure[Figure.fieldLength * Figure.fieldLength])); } @Test void whiteFarmerInvalidDestinationDownVar2() // Bauer (w) steht in der Mitte vom Feld und geht 1 nach unten { Figure f = new FigureFarmer(); - assertFalse(f.moveAllowed(Figure.fieldLength * 4 + 4, Figure.fieldLength * 5 + 4, new int[Figure.fieldLength * Figure.fieldLength])); + assertFalse(f.moveAllowed(Figure.fieldLength * 4 + 4, + Figure.fieldLength * 5 + 4, + new Figure[Figure.fieldLength * Figure.fieldLength])); } } \ No newline at end of file diff --git a/fh.fd.ci.client/src/test/java/de/fd/fh/FigureRookTest.java b/fh.fd.ci.client/src/test/java/de/fd/fh/FigureRookTest.java index b2f73f1..526664e 100644 --- a/fh.fd.ci.client/src/test/java/de/fd/fh/FigureRookTest.java +++ b/fh.fd.ci.client/src/test/java/de/fd/fh/FigureRookTest.java @@ -11,41 +11,53 @@ class FigureRookTest void whiteRookValidMoveUp() { Figure f = new FigureRook(); - assertTrue(f.moveAllowed(Figure.fieldLength * 4 + 4, Figure.fieldLength * 0 + 4, new int[Figure.fieldLength * Figure.fieldLength])); + assertTrue(f.moveAllowed(Figure.fieldLength * 4 + 4, + Figure.fieldLength * 0 + 4, + new Figure[Figure.fieldLength * Figure.fieldLength])); } @Test void whiteRookValidMoveDown() { Figure f = new FigureRook(); - assertTrue(f.moveAllowed(Figure.fieldLength * 4 + 4, Figure.fieldLength * 7 + 4, new int[Figure.fieldLength * Figure.fieldLength])); + assertTrue(f.moveAllowed(Figure.fieldLength * 4 + 4, + Figure.fieldLength * 7 + 4, + new Figure[Figure.fieldLength * Figure.fieldLength])); } @Test void whiteRookInvalidMoveLeftUp() // diagonal nach links oben { Figure f = new FigureRook(); - assertFalse(f.moveAllowed(Figure.fieldLength * 4 + 4, Figure.fieldLength * 3 + 3, new int[Figure.fieldLength * Figure.fieldLength])); + assertFalse(f.moveAllowed(Figure.fieldLength * 4 + 4, + Figure.fieldLength * 3 + 3, + new Figure[Figure.fieldLength * Figure.fieldLength])); } @Test void whiteRookInvalidMoveRightUp() // diagonal nach rechts oben { Figure f = new FigureRook(); - assertFalse(f.moveAllowed(Figure.fieldLength * 4 + 4, Figure.fieldLength * 3 + 5, new int[Figure.fieldLength * Figure.fieldLength])); + assertFalse(f.moveAllowed(Figure.fieldLength * 4 + 4, + Figure.fieldLength * 3 + 5, + new Figure[Figure.fieldLength * Figure.fieldLength])); } @Test void whiteRookInvalidMoveLeftDown() // diagonal nach links unten { Figure f = new FigureRook(); - assertFalse(f.moveAllowed(Figure.fieldLength * 4 + 4, Figure.fieldLength * 5 + 3, new int[Figure.fieldLength * Figure.fieldLength])); + assertFalse(f.moveAllowed(Figure.fieldLength * 4 + 4, + Figure.fieldLength * 5 + 3, + new Figure[Figure.fieldLength * Figure.fieldLength])); } @Test void whiteRookInvalidMoveRightDown() // diagonal nach rechts unten { Figure f = new FigureRook(); - assertFalse(f.moveAllowed(Figure.fieldLength * 4 + 4, Figure.fieldLength * 5 + 5, new int[Figure.fieldLength * Figure.fieldLength])); + assertFalse(f.moveAllowed(Figure.fieldLength * 4 + 4, + Figure.fieldLength * 5 + 5, + new Figure[Figure.fieldLength * Figure.fieldLength])); } } \ No newline at end of file diff --git a/fh.fd.ci.client/src/test/java/de/fd/fh/FigureTest.java b/fh.fd.ci.client/src/test/java/de/fd/fh/FigureTest.java index 60271c1..49ea43c 100644 --- a/fh.fd.ci.client/src/test/java/de/fd/fh/FigureTest.java +++ b/fh.fd.ci.client/src/test/java/de/fd/fh/FigureTest.java @@ -10,27 +10,35 @@ class FigureTest void checkMoveWithInvalidFieldSmall() // Feldgröße sollte 8*8 betragen { Figure f = new Figure(); - assertFalse(f.moveAllowed(Figure.fieldLength * 6 + 0, Figure.fieldLength * 5 + 0, new int[0])); + assertFalse(f.moveAllowed(Figure.fieldLength * 6 + 0, + Figure.fieldLength * 5 + 0, + new Figure[0])); } @Test void checkMoveWithInvalidFieldBigger() // Feldgröße sollte 8*8 betragen { Figure f = new Figure(); - assertFalse(f.moveAllowed(Figure.fieldLength * 6 + 0, Figure.fieldLength * 5 + 0, new int[8*9])); + assertFalse(f.moveAllowed(Figure.fieldLength * 6 + 0, + Figure.fieldLength * 5 + 0, + new Figure[8*9])); } @Test void checkMoveWithInvalidFieldCorrect() // Feldgröße sollte 8*8 betragen { Figure f = new Figure(); - assertTrue(f.moveAllowed(Figure.fieldLength * 6 + 0, Figure.fieldLength * 5 + 0, new int[Figure.fieldLength * Figure.fieldLength])); + assertTrue(f.moveAllowed(Figure.fieldLength * 6 + 0, + Figure.fieldLength * 5 + 0, + new Figure[Figure.fieldLength * Figure.fieldLength])); } @Test void checkMoveWithoutNewDestination() { Figure f = new Figure(); - assertFalse(f.moveAllowed(Figure.fieldLength * 6 + 0, Figure.fieldLength * 6 + 0, new int[Figure.fieldLength * Figure.fieldLength])); + assertFalse(f.moveAllowed(Figure.fieldLength * 6 + 0, + Figure.fieldLength * 6 + 0, + new Figure[Figure.fieldLength * Figure.fieldLength])); } } \ No newline at end of file