|
@ -1,6 +1,70 @@ |
|
|
package BattleShip; |
|
|
package BattleShip; |
|
|
|
|
|
|
|
|
import javax.swing.*; |
|
|
import javax.swing.*; |
|
|
|
|
|
import javax.swing.border.Border; |
|
|
|
|
|
import java.awt.*; |
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
import java.util.*; |
|
|
|
|
|
|
|
|
public class AIGridGUI extends JPanel { |
|
|
public class AIGridGUI extends JPanel { |
|
|
|
|
|
ArrayList<BSButton> buttons = new ArrayList<BSButton>(); |
|
|
|
|
|
ArrayList<Ship> allShips = new ArrayList<Ship>(); |
|
|
|
|
|
// public ArrayList<SetShipsListener> listeners = new ArrayList<SetShipsListener>(); |
|
|
|
|
|
|
|
|
|
|
|
int[] testLocations; |
|
|
|
|
|
int numOfGuesses = 0; |
|
|
|
|
|
public String text; |
|
|
|
|
|
int rows; |
|
|
|
|
|
int columns; |
|
|
|
|
|
boolean endGame = false; |
|
|
|
|
|
boolean[] cellsGuessed; |
|
|
|
|
|
boolean[] cellsHit; |
|
|
|
|
|
boolean[] cellsKilled; |
|
|
|
|
|
boolean randomGuess = true; |
|
|
|
|
|
int f; // it is the first |
|
|
|
|
|
Color Red = new Color(100, 0, 0); // |
|
|
|
|
|
Border loweredBevel = BorderFactory.createLoweredBevelBorder(); |
|
|
|
|
|
Border defaultBorder; |
|
|
|
|
|
Ship shipToPlace; |
|
|
|
|
|
boolean vertical = false; |
|
|
|
|
|
boolean clear; |
|
|
|
|
|
boolean shipsPlaced = false; |
|
|
|
|
|
Ship destroyer = new Ship(2, "destroyer"); |
|
|
|
|
|
Ship cruiser = new Ship(3, "cruiser"); |
|
|
|
|
|
Ship submarine = new Ship(3, "submarine"); |
|
|
|
|
|
Ship battleship = new Ship(4, "battleship"); |
|
|
|
|
|
Ship aircraftCarrier = new Ship(5, "aircraft carrier"); |
|
|
|
|
|
ArrayList<Direction> directions = new ArrayList<Direction>(); |
|
|
|
|
|
Direction up = new Direction(); |
|
|
|
|
|
Direction down = new Direction(); |
|
|
|
|
|
Direction right = new Direction(); |
|
|
|
|
|
Direction left = new Direction(); |
|
|
|
|
|
|
|
|
|
|
|
public AIGridGUI(int r, int c) { |
|
|
|
|
|
|
|
|
|
|
|
rows = r; |
|
|
|
|
|
columns = c; |
|
|
|
|
|
|
|
|
|
|
|
//Create arrays to keep track of which cells have been guessed, hit, and killed/sunk. |
|
|
|
|
|
|
|
|
|
|
|
cellsGuessed = new boolean[(rows * columns)]; |
|
|
|
|
|
cellsHit = new boolean[(rows * columns)]; |
|
|
|
|
|
cellsKilled = new boolean[(rows * columns)]; |
|
|
|
|
|
for(int i = 0; i < (rows * columns); i++) { |
|
|
|
|
|
cellsGuessed[i] = false; |
|
|
|
|
|
cellsHit[i] = false; |
|
|
|
|
|
cellsKilled[i] = false; |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
//Add all ships to an ArrayList to allow for cycling through all ships. |
|
|
|
|
|
|
|
|
|
|
|
allShips.add(destroyer); |
|
|
|
|
|
allShips.add(cruiser); |
|
|
|
|
|
allShips.add(submarine); |
|
|
|
|
|
allShips.add(battleship); |
|
|
|
|
|
allShips.add(aircraftCarrier); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
} |
|
|
} |