diff --git a/src/main/java/BattleShip/AIGridGUI.java b/src/main/java/BattleShip/AIGridGUI.java index 3ae73be..d802f03 100644 --- a/src/main/java/BattleShip/AIGridGUI.java +++ b/src/main/java/BattleShip/AIGridGUI.java @@ -286,6 +286,32 @@ public class AIGridGUI extends JPanel { Collections.sort(directions, dc); guessLocation = directions.get(0).getCell(); } + + //If first guess is not clear or is out of bounds, continue trying other directions until one is found that works. + + if(attempts == 1) { + guessLocation = directions.get(1).getCell(); + } + + if(attempts == 2) { + guessLocation = directions.get(2).getCell(); + } + + if(attempts == 3) { + guessLocation = directions.get(3).getCell(); + } + + if(attempts > 4) { + guessLocation = new Random().nextInt(cellsGuessed.length); + } + + //Test whether the guess is valid and in an unguessed space. + + if(guessLocation >= 0) { + if(!cellsGuessed[guessLocation]) { + isClear = true; + } + } } }