|
@ -23,10 +23,10 @@ public class AIGridGUI extends JPanel { |
|
|
boolean[] cellsHit; |
|
|
boolean[] cellsHit; |
|
|
boolean[] cellsKilled; |
|
|
boolean[] cellsKilled; |
|
|
boolean randomGuess = true; |
|
|
boolean randomGuess = true; |
|
|
int f; // it is the first |
|
|
|
|
|
Color Red = new Color(100, 0, 0); // |
|
|
|
|
|
|
|
|
int firstHit; |
|
|
|
|
|
Color darkRed = new Color(100, 0, 0); // |
|
|
Border loweredBevel = BorderFactory.createLoweredBevelBorder(); |
|
|
Border loweredBevel = BorderFactory.createLoweredBevelBorder(); |
|
|
Border raisedbevel = BorderFactory.createRaisedBevelBorder(); |
|
|
|
|
|
|
|
|
//Border raisedbevel = BorderFactory.createRaisedBevelBorder(); |
|
|
Border defaultBorder; |
|
|
Border defaultBorder; |
|
|
//empty = BorderFactory.createEmptyBorder(4, 4, 4, 4); |
|
|
//empty = BorderFactory.createEmptyBorder(4, 4, 4, 4); |
|
|
Border compound = BorderFactory.createCompoundBorder(); |
|
|
Border compound = BorderFactory.createCompoundBorder(); |
|
@ -252,7 +252,7 @@ public class AIGridGUI extends JPanel { |
|
|
} else { |
|
|
} else { |
|
|
//If nonrandom guess (locked onto a particular ship that has been hit but not killed), determine where to guess. |
|
|
//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) { |
|
|
while(!isClear) { |
|
|
attempts++; |
|
|
attempts++; |
|
@ -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. |
|
|
//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; |
|
|
int upCount = -1; |
|
|
while(u >= 0 && cellsHit[u] && !cellsKilled[u]) { |
|
|
while(u >= 0 && cellsHit[u] && !cellsKilled[u]) { |
|
|
u = moveUp(u); |
|
|
u = moveUp(u); |
|
@ -270,7 +270,7 @@ public class AIGridGUI extends JPanel { |
|
|
up.setCell(u); |
|
|
up.setCell(u); |
|
|
up.setCount(upCount); |
|
|
up.setCount(upCount); |
|
|
|
|
|
|
|
|
int d = f; |
|
|
|
|
|
|
|
|
int d = firstHit; |
|
|
int downCount = -1; |
|
|
int downCount = -1; |
|
|
while(d >= 0 && cellsHit[d] && !cellsKilled[d]) { |
|
|
while(d >= 0 && cellsHit[d] && !cellsKilled[d]) { |
|
|
d = moveDown(d); |
|
|
d = moveDown(d); |
|
@ -279,7 +279,7 @@ public class AIGridGUI extends JPanel { |
|
|
down.setCell(d); |
|
|
down.setCell(d); |
|
|
down.setCount(downCount); |
|
|
down.setCount(downCount); |
|
|
|
|
|
|
|
|
int r = f; |
|
|
|
|
|
|
|
|
int r = firstHit; |
|
|
int rightCount = -1; |
|
|
int rightCount = -1; |
|
|
while(r >= 0 && cellsHit[r] && !cellsKilled[r]) { |
|
|
while(r >= 0 && cellsHit[r] && !cellsKilled[r]) { |
|
|
r = moveRight(r); |
|
|
r = moveRight(r); |
|
@ -288,7 +288,7 @@ public class AIGridGUI extends JPanel { |
|
|
right.setCell(r); |
|
|
right.setCell(r); |
|
|
right.setCount(rightCount); |
|
|
right.setCount(rightCount); |
|
|
|
|
|
|
|
|
int l = f; |
|
|
|
|
|
|
|
|
int l = firstHit; |
|
|
int leftCount = -1; |
|
|
int leftCount = -1; |
|
|
while(l >= 0 && cellsHit[l] && !cellsKilled[l]) { |
|
|
while(l >= 0 && cellsHit[l] && !cellsKilled[l]) { |
|
|
l = moveLeft(l); |
|
|
l = moveLeft(l); |
|
@ -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 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(); |
|
|
guessLocation = directions.get(1).getCell(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if(attempts == 2) { |
|
|
|
|
|
|
|
|
if(attempts == 3) { |
|
|
guessLocation = directions.get(2).getCell(); |
|
|
guessLocation = directions.get(2).getCell(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if(attempts == 3) { |
|
|
|
|
|
|
|
|
if(attempts == 4) { |
|
|
guessLocation = directions.get(3).getCell(); |
|
|
guessLocation = directions.get(3).getCell(); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
@ -343,7 +343,7 @@ public class AIGridGUI extends JPanel { |
|
|
if(s == null) { |
|
|
if(s == null) { |
|
|
//If no ship in that cell, mark as a miss. |
|
|
//If no ship in that cell, mark as a miss. |
|
|
text = "Other player missed. Your turn."; |
|
|
text = "Other player missed. Your turn."; |
|
|
b.setBackground(Color.BLUE); |
|
|
|
|
|
|
|
|
b.setBackground(Color.lightGray); |
|
|
} else { |
|
|
} else { |
|
|
//Check if guess killed a ship. |
|
|
//Check if guess killed a ship. |
|
|
killed = s.counter(); |
|
|
killed = s.counter(); |
|
@ -353,12 +353,12 @@ public class AIGridGUI extends JPanel { |
|
|
for(BSButton bu : buttons) { |
|
|
for(BSButton bu : buttons) { |
|
|
//Mark killed cells. |
|
|
//Mark killed cells. |
|
|
if(bu.getCellContents() == s) { |
|
|
if(bu.getCellContents() == s) { |
|
|
bu.setBackground(Red); |
|
|
|
|
|
|
|
|
bu.setBackground(darkRed); |
|
|
cellsKilled[bu.getGridLocation()] = true; |
|
|
cellsKilled[bu.getGridLocation()] = true; |
|
|
} |
|
|
} |
|
|
//Mark if any cell remains that has been hit but not yet killed. If so, lock onto that cell. |
|
|
//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()]) { |
|
|
if(cellsHit[bu.getGridLocation()] && !cellsKilled[bu.getGridLocation()]) { |
|
|
f = bu.getGridLocation(); |
|
|
|
|
|
|
|
|
firstHit = bu.getGridLocation(); |
|
|
unkilledCells = true; |
|
|
unkilledCells = true; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
@ -372,7 +372,7 @@ public class AIGridGUI extends JPanel { |
|
|
b.setBackground(Color.red); |
|
|
b.setBackground(Color.red); |
|
|
//If previously random guessing, switch to locking onto the hit cell. |
|
|
//If previously random guessing, switch to locking onto the hit cell. |
|
|
if(randomGuess) { |
|
|
if(randomGuess) { |
|
|
f = b.getGridLocation(); |
|
|
|
|
|
|
|
|
firstHit = b.getGridLocation(); |
|
|
randomGuess = false; |
|
|
randomGuess = false; |
|
|
} |
|
|
} |
|
|
cellsHit[guessLocation] = true; |
|
|
cellsHit[guessLocation] = true; |
|
@ -502,7 +502,7 @@ public class AIGridGUI extends JPanel { |
|
|
|
|
|
|
|
|
public void highlightCells(BSButton b, int x) { |
|
|
public void highlightCells(BSButton b, int x) { |
|
|
BSButton cell = b; |
|
|
BSButton cell = b; |
|
|
int action = x; |
|
|
|
|
|
|
|
|
int actionToTake = x; |
|
|
clear = true; |
|
|
clear = true; |
|
|
|
|
|
|
|
|
//Check whether sufficient spaces are clear to place the ship. |
|
|
//Check whether sufficient spaces are clear to place the ship. |
|
@ -521,20 +521,39 @@ public class AIGridGUI extends JPanel { |
|
|
for(int i = 0; i < shipToPlace.getLength(); i++) { |
|
|
for(int i = 0; i < shipToPlace.getLength(); i++) { |
|
|
BSButton bsb = buttons.get(cell.getGridLocation() + (i * columns)); |
|
|
BSButton bsb = buttons.get(cell.getGridLocation() + (i * columns)); |
|
|
//If mouse entered, highlight cells via lowered bevel. |
|
|
//If mouse entered, highlight cells via lowered bevel. |
|
|
if(action == 1) { |
|
|
|
|
|
bsb.setBorder(raisedbevel); |
|
|
|
|
|
|
|
|
if(actionToTake == 0) { |
|
|
|
|
|
bsb.setBorder(loweredBevel); |
|
|
} else { |
|
|
} else { |
|
|
//If mouse released, place ship and color ship cells. |
|
|
//If mouse released, place ship and color ship cells. |
|
|
if(action == 2) { |
|
|
|
|
|
|
|
|
if(actionToTake == 1) { |
|
|
bsb.setCellContents(shipToPlace); |
|
|
bsb.setCellContents(shipToPlace); |
|
|
bsb.setBackground(Color.gray); |
|
|
|
|
|
bsb.setBorder(raisedbevel); |
|
|
|
|
|
|
|
|
bsb.setBackground(Color.blue); |
|
|
|
|
|
bsb.setBorder(loweredBevel); |
|
|
} else { |
|
|
} else { |
|
|
//If mouse exited, unhighlight cells. |
|
|
//If mouse exited, unhighlight cells. |
|
|
bsb.setBorder(compound); |
|
|
bsb.setBorder(compound); |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
}else { |
|
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < shipToPlace.getLength(); i++) { |
|
|
|
|
|
BSButton bsb = buttons.get(cell.getGridLocation() + i); |
|
|
|
|
|
if(actionToTake == 0) { |
|
|
|
|
|
//If mouse entered, highlight cells via lowered bevel. |
|
|
|
|
|
bsb.setBorder(loweredBevel); |
|
|
|
|
|
} else { |
|
|
|
|
|
//If mouse released, place ship and color ship cells. |
|
|
|
|
|
if(actionToTake == 1) { |
|
|
|
|
|
bsb.setCellContents(shipToPlace); |
|
|
|
|
|
bsb.setBackground(Color.blue); |
|
|
|
|
|
bsb.setBorder(loweredBevel); |
|
|
|
|
|
} else { |
|
|
|
|
|
//If mouse exited, unhighlight cells. |
|
|
|
|
|
bsb.setBorder(defaultBorder); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|