From 7830143d4b1660bfe633a641388ca503142b645f Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Wed, 2 Feb 2022 16:14:26 +0100 Subject: [PATCH 1/8] Added isFreePosition() with first test case when position is filled --- .../tims/fleetstorm/matchfield/Matchfield.java | 18 ++++++++++++++++++ .../matchfield/MatchfieldShipTest.java | 10 ++++++++++ 2 files changed, 28 insertions(+) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java index cca91c9..1a4d4b8 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java @@ -110,4 +110,22 @@ public class Matchfield { return shipCounter == 0; } + public boolean isFreePosition(Coordinate next, int length, int direction) { + + for (int i = 0; i < length; i++) { + + if (this.getState(next.getX(), next.getY()) != Coordinate.EMPTY) + return false; + + if (direction == 0) { + + next = new Coordinate(next.getX() + i, next.getY()); + } else if (direction == 1) { + next = new Coordinate(next.getX(), next.getY() + i); + } + } + + return true; + } + } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java index 665f367..565cd98 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java @@ -130,4 +130,14 @@ class MatchfieldShipTest { boolean calulatedResult = matchfield.areAllShipsHit(); assertFalse(calulatedResult); } + + @Test + void testMatchfieldIsFreePositionWithNonFreeField() { + Matchfield matchfield = new Matchfield(10); + matchfield.createMatchfield(); + matchfield.setState(new Coordinate(0, 0), Coordinate.SHIP); + + boolean calculatedResult = matchfield.isFreePosition(new Coordinate(0, 0), 2, 1); + assertFalse(calculatedResult); + } } From 0419b89aaa0168286d202ea9074cf56d1afe75b0 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Wed, 2 Feb 2022 16:24:16 +0100 Subject: [PATCH 2/8] Refactoring: MatchfieldShipTest class --- .../fleetstorm/matchfield/Matchfield.java | 1 - .../matchfield/MatchfieldShipTest.java | 71 ++++++++++--------- 2 files changed, 39 insertions(+), 33 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java index 1a4d4b8..9190895 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java @@ -118,7 +118,6 @@ public class Matchfield { return false; if (direction == 0) { - next = new Coordinate(next.getX() + i, next.getY()); } else if (direction == 1) { next = new Coordinate(next.getX(), next.getY() + i); diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java index 565cd98..019ebba 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java @@ -6,6 +6,7 @@ import static org.assertj.core.api.Assertions.assertThat; import java.util.stream.Stream; +import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.provider.Arguments; @@ -13,19 +14,27 @@ import org.junit.jupiter.params.provider.MethodSource; class MatchfieldShipTest { + private Matchfield matchfield; + private int matchfieldSize = 10; + + @BeforeEach + void setup() { + matchfield = new Matchfield(matchfieldSize); + matchfield.createMatchfield(); + } + @ParameterizedTest(name = "ship was set on the correct positions") @MethodSource("testShipPositioning") - void testMatchfieldShipSetHasCorrectPositions(String testName, int matchfieldSize, Coordinate coordinate, - int direction, int length, Coordinate[] coordinatesWithShip) { - Matchfield matchfield = new Matchfield(matchfieldSize); - matchfield.createMatchfield(); + void testMatchfieldShipSetHasCorrectPositions(String testName, Coordinate coordinate, int direction, int length, + Coordinate[] coordinatesWithShip) { matchfield.setShip(coordinate, length, direction); int shipSetCounter = 0; for (Coordinate toCheckCoordinate : coordinatesWithShip) { - if (matchfield.getField(toCheckCoordinate).getState() == Coordinate.SHIP) + if (matchfield.getField(toCheckCoordinate).getState() == Coordinate.SHIP) { shipSetCounter++; + } } assertThat(shipSetCounter).describedAs(testName).isEqualTo(length); @@ -39,20 +48,18 @@ class MatchfieldShipTest { new Coordinate(0, 4), new Coordinate(0, 5) }; return Stream.of( - Arguments.of("set ship from 0:0, length 2, direction 0", 10, new Coordinate(0, 0), 0, 2, + Arguments.of("set ship from 0:0, length 2, direction 0", new Coordinate(0, 0), 0, 2, coordinatesWithShip00_2_0), - Arguments.of("set ship from 0:1, length 3, direction 0", 10, new Coordinate(0, 1), 0, 3, + Arguments.of("set ship from 0:1, length 3, direction 0", new Coordinate(0, 1), 0, 3, coordinatesWithShip01_3_0), - Arguments.of("set ship from 0:2, length 4, direction 1", 10, new Coordinate(0, 2), 1, 4, + Arguments.of("set ship from 0:2, length 4, direction 1", new Coordinate(0, 2), 1, 4, coordinatesWithShip02_4_1)); } @ParameterizedTest(name = "ship positioning on invalid or valid coordinates") @MethodSource("testShipPositioningFailed") - void testMatchfieldShipSetWithInvalidOrValidCoordinates(String testName, int matchfieldSize, Coordinate coordinate, - int direction, int length, boolean expectedResult) { - Matchfield matchfield = new Matchfield(matchfieldSize); - matchfield.createMatchfield(); + void testMatchfieldShipSetWithInvalidOrValidCoordinates(String testName, Coordinate coordinate, int direction, + int length, boolean expectedResult) { boolean calculatedResult = matchfield.setShip(coordinate, length, direction); @@ -65,16 +72,13 @@ class MatchfieldShipTest { static Stream testShipPositioningFailed() { return Stream.of( - Arguments.of("invalid ship position from 9:0, length 2, direction 0", 10, new Coordinate(9, 0), 0, 2, + Arguments.of("invalid ship position from 9:0, length 2, direction 0", new Coordinate(9, 0), 0, 2, false), - Arguments.of("valid ship position from 8:0, length 5, direction 1", 10, new Coordinate(9, 0), 1, 5, - true)); + Arguments.of("valid ship position from 8:0, length 5, direction 1", new Coordinate(9, 0), 1, 5, true)); } @Test void testShipPositionAlreadySetWithShip() { - Matchfield matchfield = new Matchfield(10); - matchfield.createMatchfield(); matchfield.setShip(new Coordinate(0, 0), 5, 1); assertThat(matchfield.setShip(new Coordinate(0, 1), 5, 1)).describedAs("set ship on coordinate with ship") @@ -88,9 +92,6 @@ class MatchfieldShipTest { @Test void testIfAllShipsHitReturnsFalseWhenNoShipIsHit() { - Matchfield matchfield = new Matchfield(10); - matchfield.createMatchfield(); - matchfield.setShip(new Coordinate(0, 0), 5, 1); boolean calulatedResult = matchfield.areAllShipsHit(); assertFalse(calulatedResult); @@ -98,9 +99,6 @@ class MatchfieldShipTest { @Test void testIfAllShipsHitReturnsTrueWhenAllShipsAreHit() { - Matchfield matchfield = new Matchfield(10); - matchfield.createMatchfield(); - matchfield.setShip(new Coordinate(0, 0), 5, 1); matchfield.getField(0, 0).setState(Coordinate.HIT); matchfield.getField(0, 1).setState(Coordinate.HIT); @@ -114,9 +112,6 @@ class MatchfieldShipTest { @Test void testIfAllShipsHitReturnsFalseWhenTwoShipsAreNotFullyHit() { - Matchfield matchfield = new Matchfield(10); - matchfield.createMatchfield(); - matchfield.setShip(new Coordinate(0, 0), 5, 1); matchfield.getField(0, 0).setState(Coordinate.HIT); matchfield.getField(0, 1).setState(Coordinate.HIT); @@ -131,13 +126,25 @@ class MatchfieldShipTest { assertFalse(calulatedResult); } - @Test - void testMatchfieldIsFreePositionWithNonFreeField() { - Matchfield matchfield = new Matchfield(10); + @ParameterizedTest(name = "matchfield returns correct state if a position is free or not") + @MethodSource("testIsFreeMethod") + void testMatchfieldReturnsCorrectValueAboutFreeCoorinateSlots(String testName, Coordinate originCoordinate, + int direction, int length, Coordinate[] coordinatesWithShip, boolean expectedResult) { + + Matchfield matchfield = new Matchfield(matchfieldSize); matchfield.createMatchfield(); - matchfield.setState(new Coordinate(0, 0), Coordinate.SHIP); - boolean calculatedResult = matchfield.isFreePosition(new Coordinate(0, 0), 2, 1); - assertFalse(calculatedResult); + for (Coordinate coordinate : coordinatesWithShip) { + matchfield.getField(coordinate).setState(Coordinate.SHIP); + } + + boolean calculatedResult = matchfield.isFreePosition(originCoordinate, length, direction); + assertThat(calculatedResult).describedAs(testName).isEqualTo(expectedResult); + + } + + static Stream testIsFreeMethod() { + return Stream.of(Arguments.of("one ship is within the coordinates", new Coordinate(0, 0), 1, 2, + new Coordinate[] { new Coordinate(0, 0) }, false)); } } From 8915964ed0ea21de591eb45afca797cfaf01be38 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Wed, 2 Feb 2022 16:27:26 +0100 Subject: [PATCH 3/8] Added Test case when no ship is within the coordinates (isFree() method) --- .../tims/fleetstorm/matchfield/MatchfieldShipTest.java | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java index 019ebba..72a94c8 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java @@ -128,7 +128,7 @@ class MatchfieldShipTest { @ParameterizedTest(name = "matchfield returns correct state if a position is free or not") @MethodSource("testIsFreeMethod") - void testMatchfieldReturnsCorrectValueAboutFreeCoorinateSlots(String testName, Coordinate originCoordinate, + void testMatchfieldReturnsCorrectValueAboutFreeCoordinateSlots(String testName, Coordinate originCoordinate, int direction, int length, Coordinate[] coordinatesWithShip, boolean expectedResult) { Matchfield matchfield = new Matchfield(matchfieldSize); @@ -144,7 +144,10 @@ class MatchfieldShipTest { } static Stream testIsFreeMethod() { - return Stream.of(Arguments.of("one ship is within the coordinates", new Coordinate(0, 0), 1, 2, - new Coordinate[] { new Coordinate(0, 0) }, false)); + return Stream.of( + Arguments.of("one ship is within the coordinates", new Coordinate(0, 0), 1, 2, + new Coordinate[] { new Coordinate(0, 0) }, false), + Arguments.of("no ship is within the coordinates", new Coordinate(1, 1), 0, 5, new Coordinate[] {}, + true)); } } From a05c6c09b9024c80035afdea548bf0c3ce311f6a Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Wed, 2 Feb 2022 16:34:42 +0100 Subject: [PATCH 4/8] Added Test case when one coordinate is hit, but others on free field (isFreePosition() method) --- src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java | 4 ++-- .../de/tims/fleetstorm/matchfield/MatchfieldShipTest.java | 4 +++- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java index 9190895..ade8efb 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java @@ -118,9 +118,9 @@ public class Matchfield { return false; if (direction == 0) { - next = new Coordinate(next.getX() + i, next.getY()); + next = new Coordinate(next.getX() + 1, next.getY()); } else if (direction == 1) { - next = new Coordinate(next.getX(), next.getY() + i); + next = new Coordinate(next.getX(), next.getY() + 1); } } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java index 72a94c8..b3f1205 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java @@ -148,6 +148,8 @@ class MatchfieldShipTest { Arguments.of("one ship is within the coordinates", new Coordinate(0, 0), 1, 2, new Coordinate[] { new Coordinate(0, 0) }, false), Arguments.of("no ship is within the coordinates", new Coordinate(1, 1), 0, 5, new Coordinate[] {}, - true)); + true), + Arguments.of("one of the coordinates is hit, others on free fields", new Coordinate(5, 5), 1, 3, + new Coordinate[] { new Coordinate(5, 7), new Coordinate(5, 8), new Coordinate(5, 9) }, false)); } } From 3ff4b665f600a6230696daa5ee9d0ceecf9d5c78 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Wed, 2 Feb 2022 16:53:42 +0100 Subject: [PATCH 5/8] Implemented method to random position ships on matchfield --- .../fleetstorm/matchfield/Matchfield.java | 25 +++++++++++++++++++ .../matchfield/MatchfieldShipTest.java | 8 ++++++ 2 files changed, 33 insertions(+) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java index ade8efb..77ee171 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java @@ -1,5 +1,7 @@ package de.tims.fleetstorm.matchfield; +import java.util.Random; + public class Matchfield { private Coordinate[][] matchfield; @@ -127,4 +129,27 @@ public class Matchfield { return true; } + public boolean setShipOnRandomPosition(int length) { + boolean success = false; + Coordinate origin = null; + int randomDirection; + + do { + + Random random = new Random(); + int randomX = random.nextInt(this.size - length); + int randomY = random.nextInt(this.size - length); + origin = new Coordinate(randomX, randomY); + + randomDirection = random.nextInt(2); + + if (this.isFreePosition(origin, length, randomDirection)) { + success = this.setShip(new Coordinate(randomX, randomY), length, randomDirection); + } + + } while (!success); + + return true; + } + } diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java index b3f1205..d12852e 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java @@ -152,4 +152,12 @@ class MatchfieldShipTest { Arguments.of("one of the coordinates is hit, others on free fields", new Coordinate(5, 5), 1, 3, new Coordinate[] { new Coordinate(5, 7), new Coordinate(5, 8), new Coordinate(5, 9) }, false)); } + + @Test + void testIfRandomShipPositioningIsWorking() { + for (int shipLength = 2; shipLength <= 5; shipLength++) { + boolean calculatedResult = matchfield.setShipOnRandomPosition(shipLength); + assertTrue(calculatedResult); + } + } } From f753e07ac127d035ce0e5eae612c4b6cda44aae4 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Wed, 2 Feb 2022 17:01:06 +0100 Subject: [PATCH 6/8] Refactoring MatchfieldShipTest class --- .../matchfield/MatchfieldShipTest.java | 36 ++++++++----------- 1 file changed, 15 insertions(+), 21 deletions(-) diff --git a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java index d12852e..8a8814c 100644 --- a/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java +++ b/src/test/java/de/tims/fleetstorm/matchfield/MatchfieldShipTest.java @@ -3,9 +3,7 @@ package de.tims.fleetstorm.matchfield; import static org.junit.jupiter.api.Assertions.*; import static org.assertj.core.api.Assertions.assertThat; - import java.util.stream.Stream; - import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest; @@ -62,19 +60,15 @@ class MatchfieldShipTest { int length, boolean expectedResult) { boolean calculatedResult = matchfield.setShip(coordinate, length, direction); - - if (expectedResult) - assertThat(calculatedResult).describedAs(testName).isEqualTo(true); - - if (!expectedResult) - assertThat(calculatedResult).describedAs(testName).isEqualTo(false); + assertThat(calculatedResult).describedAs(testName).isEqualTo(expectedResult); } static Stream testShipPositioningFailed() { return Stream.of( - Arguments.of("invalid ship position from 9:0, length 2, direction 0", new Coordinate(9, 0), 0, 2, - false), - Arguments.of("valid ship position from 8:0, length 5, direction 1", new Coordinate(9, 0), 1, 5, true)); + Arguments.of("invalid ship position from 9:0, length 2, direction 0 (x-axis)", new Coordinate(9, 0), 0, + 2, false), + Arguments.of("valid ship position from 8:0, length 5, direction 1 (y-axis)", new Coordinate(9, 0), 1, 5, + true)); } @Test @@ -100,11 +94,8 @@ class MatchfieldShipTest { @Test void testIfAllShipsHitReturnsTrueWhenAllShipsAreHit() { matchfield.setShip(new Coordinate(0, 0), 5, 1); - matchfield.getField(0, 0).setState(Coordinate.HIT); - matchfield.getField(0, 1).setState(Coordinate.HIT); - matchfield.getField(0, 2).setState(Coordinate.HIT); - matchfield.getField(0, 3).setState(Coordinate.HIT); - matchfield.getField(0, 4).setState(Coordinate.HIT); + + setFieldRangeHit(matchfield); boolean calulatedResult = matchfield.areAllShipsHit(); assertTrue(calulatedResult); @@ -113,11 +104,8 @@ class MatchfieldShipTest { @Test void testIfAllShipsHitReturnsFalseWhenTwoShipsAreNotFullyHit() { matchfield.setShip(new Coordinate(0, 0), 5, 1); - matchfield.getField(0, 0).setState(Coordinate.HIT); - matchfield.getField(0, 1).setState(Coordinate.HIT); - matchfield.getField(0, 2).setState(Coordinate.HIT); - matchfield.getField(0, 3).setState(Coordinate.HIT); - matchfield.getField(0, 4).setState(Coordinate.HIT); + + setFieldRangeHit(matchfield); matchfield.setShip(new Coordinate(3, 4), 2, 0); matchfield.getField(4, 4).setState(Coordinate.HIT); @@ -126,6 +114,12 @@ class MatchfieldShipTest { assertFalse(calulatedResult); } + void setFieldRangeHit(Matchfield matchfield) { + for (int i = 0; i <= 4; i++) { + matchfield.getField(0, i).setState(Coordinate.HIT); + } + } + @ParameterizedTest(name = "matchfield returns correct state if a position is free or not") @MethodSource("testIsFreeMethod") void testMatchfieldReturnsCorrectValueAboutFreeCoordinateSlots(String testName, Coordinate originCoordinate, From a32541b39f2ed1eadf2189b31d05416b992e46d3 Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Thu, 3 Feb 2022 08:30:43 +0100 Subject: [PATCH 7/8] Implemented finished random ship positionig method into gui --- .../java/de/tims/fleetstorm/gui/GameLogic.java | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java index 2bc34d5..c61cafa 100644 --- a/src/main/java/de/tims/fleetstorm/gui/GameLogic.java +++ b/src/main/java/de/tims/fleetstorm/gui/GameLogic.java @@ -194,10 +194,10 @@ public class GameLogic extends JPanel { // create player matchfield and set ships this.matchfield = new Matchfield(matchfieldSize); this.matchfield.createMatchfield(); - this.matchfield.setShip(new Coordinate(4, 6), 2, 0); - this.matchfield.setShip(new Coordinate(1, 3), 3, 0); - this.matchfield.setShip(new Coordinate(9, 1), 4, 1); - this.matchfield.setShip(new Coordinate(0, 0), 5, 0); + this.matchfield.setShipOnRandomPosition(2); + this.matchfield.setShipOnRandomPosition(3); + this.matchfield.setShipOnRandomPosition(4); + this.matchfield.setShipOnRandomPosition(5); this.aiLogic = new Logic(); this.aiLogic.setMatchfield(this.matchfield); @@ -206,10 +206,10 @@ public class GameLogic extends JPanel { // matchfield) this.enemyMatchfield = new Matchfield(matchfieldSize); this.enemyMatchfield.createMatchfield(); - this.enemyMatchfield.setShip(new Coordinate(8, 9), 2, 0); - this.enemyMatchfield.setShip(new Coordinate(0, 2), 3, 1); - this.enemyMatchfield.setShip(new Coordinate(7, 0), 4, 1); - this.enemyMatchfield.setShip(new Coordinate(3, 6), 5, 0); + this.enemyMatchfield.setShipOnRandomPosition(2); + this.enemyMatchfield.setShipOnRandomPosition(3); + this.enemyMatchfield.setShipOnRandomPosition(4); + this.enemyMatchfield.setShipOnRandomPosition(5); // enter game loop this.gameState = GameLogic.RUNNING; From af28ea8e341701e1c68ba88541fd934ec15c97ec Mon Sep 17 00:00:00 2001 From: Lorenz Hohmann Date: Thu, 3 Feb 2022 08:32:14 +0100 Subject: [PATCH 8/8] Improved random ship positioning --- .../de/tims/fleetstorm/matchfield/Matchfield.java | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java index 77ee171..54eb146 100644 --- a/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java +++ b/src/main/java/de/tims/fleetstorm/matchfield/Matchfield.java @@ -137,12 +137,21 @@ public class Matchfield { do { Random random = new Random(); - int randomX = random.nextInt(this.size - length); - int randomY = random.nextInt(this.size - length); - origin = new Coordinate(randomX, randomY); randomDirection = random.nextInt(2); + int randomX = 0, randomY = 0; + + if (randomDirection == 0) { + randomX = random.nextInt(this.size - length); + randomY = random.nextInt(this.size); + } else if (randomDirection == 1) { + randomX = random.nextInt(this.size); + randomY = random.nextInt(this.size - length); + } + + origin = new Coordinate(randomX, randomY); + if (this.isFreePosition(origin, length, randomDirection)) { success = this.setShip(new Coordinate(randomX, randomY), length, randomDirection); }