From 90fb04ca002e7745dfc7cdf5481b15c72e50c5ca Mon Sep 17 00:00:00 2001 From: Thoumi Ngonga Brice Date: Tue, 7 Feb 2023 00:41:49 +0100 Subject: [PATCH] Update Implementation AIGridGUI class --- src/main/java/BattleShip/AIGridGUI.java | 26 +++++++++++++++++++++++++ 1 file changed, 26 insertions(+) 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; + } + } } }