From 397d45776b585009cf7b25e882f4eff1c1ef2f79 Mon Sep 17 00:00:00 2001 From: fdai4581 Date: Tue, 7 Feb 2023 21:04:26 +0100 Subject: [PATCH] update --- src/main/java/BattleShip/GridGUI.java | 32 +++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) diff --git a/src/main/java/BattleShip/GridGUI.java b/src/main/java/BattleShip/GridGUI.java index d537acd..7756d31 100644 --- a/src/main/java/BattleShip/GridGUI.java +++ b/src/main/java/BattleShip/GridGUI.java @@ -1,6 +1,7 @@ package BattleShip; import java.util.ArrayList; +import java.util.Random; public class GridGUI { @@ -29,6 +30,37 @@ public class GridGUI { int shipLength = s.getLength(); int clearSpace = 0; testLocations = new int[shipLength]; + + while(clearSpace < shipLength) { + + + boolean vert = new Random().nextBoolean(); + int x; + int y; + + if(vert) { + + x = (int) (Math.random() * (columns)); + y = (int) (Math.random() * (rows - shipLength)); + for(int i = 0; i < shipLength; i++) { + testLocations[i] = x + (columns*(y+i)); + } + } else { + x = (int) (Math.random() * (columns - shipLength)); + y = (int) (Math.random() * (rows)); + for(int i = 0; i < shipLength; i++) { + testLocations[i] = x + i + (columns*y); + } + } + + + clearSpace = 0; + for(int i = 0; i < shipLength; i++) { + if(buttons.get(testLocations[i]).getCellContents() == null) { + clearSpace++; + } + } + } } } } \ No newline at end of file