From d64957ee58f24a401f9159cd523f763083e5436c Mon Sep 17 00:00:00 2001 From: Thoumi Ngonga Brice Date: Tue, 7 Feb 2023 01:06:52 +0100 Subject: [PATCH] Update Implementation AIGridGUI class --- src/main/java/BattleShip/AIGridGUI.java | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/src/main/java/BattleShip/AIGridGUI.java b/src/main/java/BattleShip/AIGridGUI.java index 57aaab0..042c5ba 100644 --- a/src/main/java/BattleShip/AIGridGUI.java +++ b/src/main/java/BattleShip/AIGridGUI.java @@ -333,6 +333,22 @@ public class AIGridGUI extends JPanel { if(killed) { text = "Your " + s.getName() + " was sunk. Your turn."; boolean unkilledCells = false; + for(BSButton bu : buttons) { + //Mark killed cells. + if(bu.getCellContents() == s) { + bu.setBackground(Red); + cellsKilled[bu.getGridLocation()] = true; + } + //Mark if any cell remains that has been hit but not yet killed. If so, lock onto that cell. + if(cellsHit[bu.getGridLocation()] && !cellsKilled[bu.getGridLocation()]) { + f = bu.getGridLocation(); + unkilledCells = true; + } + } + //If all hit cells have been killed, return to random guessing. + if(!unkilledCells) { + randomGuess = true; + } } } }