|
@ -1,6 +1,7 @@ |
|
|
package BattleShip; |
|
|
package BattleShip; |
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
import java.util.ArrayList; |
|
|
|
|
|
import java.util.Random; |
|
|
|
|
|
|
|
|
public class GridGUI { |
|
|
public class GridGUI { |
|
|
|
|
|
|
|
@ -29,6 +30,37 @@ public class GridGUI { |
|
|
int shipLength = s.getLength(); |
|
|
int shipLength = s.getLength(); |
|
|
int clearSpace = 0; |
|
|
int clearSpace = 0; |
|
|
testLocations = new int[shipLength]; |
|
|
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++; |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |