From 5a864fbea6108d3033124c046c112987c103244a Mon Sep 17 00:00:00 2001 From: Thoumi Ngonga Brice Date: Tue, 7 Feb 2023 21:33:16 +0100 Subject: [PATCH 1/3] Refratoring Color Red to Darkred --- src/main/java/BattleShip/AIGridGUI.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/BattleShip/AIGridGUI.java b/src/main/java/BattleShip/AIGridGUI.java index ddcae2f..8918acd 100644 --- a/src/main/java/BattleShip/AIGridGUI.java +++ b/src/main/java/BattleShip/AIGridGUI.java @@ -24,7 +24,7 @@ public class AIGridGUI extends JPanel { boolean[] cellsKilled; boolean randomGuess = true; int f; // it is the first - Color Red = new Color(100, 0, 0); // + Color darkRed = new Color(100, 0, 0); // Border loweredBevel = BorderFactory.createLoweredBevelBorder(); Border raisedbevel = BorderFactory.createRaisedBevelBorder(); Border defaultBorder; @@ -353,7 +353,7 @@ public class AIGridGUI extends JPanel { for(BSButton bu : buttons) { //Mark killed cells. if(bu.getCellContents() == s) { - bu.setBackground(Red); + bu.setBackground(darkRed); cellsKilled[bu.getGridLocation()] = true; } //Mark if any cell remains that has been hit but not yet killed. If so, lock onto that cell. From d475d3834d01841c42ba6241018f4af88f0db6da Mon Sep 17 00:00:00 2001 From: Thoumi Ngonga Brice Date: Tue, 7 Feb 2023 21:43:17 +0100 Subject: [PATCH 2/3] Refratoring int f to firstHit --- src/main/java/BattleShip/AIGridGUI.java | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/src/main/java/BattleShip/AIGridGUI.java b/src/main/java/BattleShip/AIGridGUI.java index 8918acd..bf9c8ac 100644 --- a/src/main/java/BattleShip/AIGridGUI.java +++ b/src/main/java/BattleShip/AIGridGUI.java @@ -23,7 +23,7 @@ public class AIGridGUI extends JPanel { boolean[] cellsHit; boolean[] cellsKilled; boolean randomGuess = true; - int f; // it is the first + int firstHit; Color darkRed = new Color(100, 0, 0); // Border loweredBevel = BorderFactory.createLoweredBevelBorder(); Border raisedbevel = BorderFactory.createRaisedBevelBorder(); @@ -261,7 +261,7 @@ public class AIGridGUI extends JPanel { //Starting from the location of the first hit on the ship, test each direction to determine how many consecutive hits have been made in that direction. - int u = f; + int u = firstHit; int upCount = -1; while(u >= 0 && cellsHit[u] && !cellsKilled[u]) { u = moveUp(u); @@ -270,7 +270,7 @@ public class AIGridGUI extends JPanel { up.setCell(u); up.setCount(upCount); - int d = f; + int d = firstHit; int downCount = -1; while(d >= 0 && cellsHit[d] && !cellsKilled[d]) { d = moveDown(d); @@ -279,7 +279,7 @@ public class AIGridGUI extends JPanel { down.setCell(d); down.setCount(downCount); - int r = f; + int r = firstHit; int rightCount = -1; while(r >= 0 && cellsHit[r] && !cellsKilled[r]) { r = moveRight(r); @@ -288,7 +288,7 @@ public class AIGridGUI extends JPanel { right.setCell(r); right.setCount(rightCount); - int l = f; + int l = firstHit; int leftCount = -1; while(l >= 0 && cellsHit[l] && !cellsKilled[l]) { l = moveLeft(l); @@ -358,7 +358,7 @@ public class AIGridGUI extends JPanel { } //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(); + firstHit = bu.getGridLocation(); unkilledCells = true; } } @@ -372,7 +372,7 @@ public class AIGridGUI extends JPanel { b.setBackground(Color.red); //If previously random guessing, switch to locking onto the hit cell. if(randomGuess) { - f = b.getGridLocation(); + firstHit = b.getGridLocation(); randomGuess = false; } cellsHit[guessLocation] = true; From 7f5ab5a893c2b8d397b7495803c4bd4b2c1cf5c6 Mon Sep 17 00:00:00 2001 From: Thoumi Ngonga Brice Date: Tue, 7 Feb 2023 21:50:00 +0100 Subject: [PATCH 3/3] Refratoring attemps --- src/main/java/BattleShip/AIGridGUI.java | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/main/java/BattleShip/AIGridGUI.java b/src/main/java/BattleShip/AIGridGUI.java index bf9c8ac..c012d19 100644 --- a/src/main/java/BattleShip/AIGridGUI.java +++ b/src/main/java/BattleShip/AIGridGUI.java @@ -252,7 +252,7 @@ public class AIGridGUI extends JPanel { } else { //If nonrandom guess (locked onto a particular ship that has been hit but not killed), determine where to guess. - int attempts = 1; + int attempts = 0; while(!isClear) { attempts++; @@ -306,15 +306,15 @@ public class AIGridGUI extends JPanel { //If first guess is not clear or is out of bounds, continue trying other directions until one is found that works. - if(attempts == 1) { + if(attempts == 2) { guessLocation = directions.get(1).getCell(); } - if(attempts == 2) { + if(attempts == 3) { guessLocation = directions.get(2).getCell(); } - if(attempts == 3) { + if(attempts == 4) { guessLocation = directions.get(3).getCell(); }