From ba218b5315ea6b009a1be236f794db48c05e51ba Mon Sep 17 00:00:00 2001 From: Thoumi Ngonga Brice Date: Mon, 6 Feb 2023 22:19:42 +0100 Subject: [PATCH] Update Implementation Methode AIGridGUI --- src/main/java/BattleShip/AIGridGUI.java | 31 ++++++++++++++++++++++++- 1 file changed, 30 insertions(+), 1 deletion(-) diff --git a/src/main/java/BattleShip/AIGridGUI.java b/src/main/java/BattleShip/AIGridGUI.java index 5afe8d4..3f059a4 100644 --- a/src/main/java/BattleShip/AIGridGUI.java +++ b/src/main/java/BattleShip/AIGridGUI.java @@ -156,7 +156,6 @@ public class AIGridGUI extends JPanel { return endGame; } - public void go() { //Play a turn by making a guess at a cell. @@ -190,6 +189,36 @@ public class AIGridGUI extends JPanel { guesses[randInt] = guesses[i]; guesses[i] = randGuess; } + + int numCellsTried = 0; + + //Test potential guesses and mark clear if criteria met. + while(!isClear && numCellsTried < guesses.length) { + guessLocation = guesses[numCellsTried]; + numCellsTried++; + + //If the cell has not already been guessed, test whether there are enough clear spaces in at least one direction. + if(!cellsGuessed[guessLocation]) { + + int u = guessLocation; + int upCount = -1; + while(u >= 0 && !cellsHit[u]) { + u = moveUp(u); + upCount++; + } + } + } + } + } + + //Return the location of a cell one space in the given direction, or return -1 if out of bounds. + + public int moveUp(int u) { + int dirUp = u - columns; + if(dirUp < 0) { + return -1; + } else { + return dirUp; } } }