package lect78_impl.model; import java.util.ArrayList; import java.util.List; /** * @overview Represents the 8x8 battlefield grid * * @attributes * cells Cell[][] 2D array of cells * size Integer Size of the board (8) */ public class Board { // Constants public static final int CELL_SIZE = 75; // Size of each grid cell in pixels public static final int GRID_SIZE = 8; // Default 8x8 grid size private Cell[][] cells; private int size; /** * @effects Initializes this as a Board with the specified size */ public Board(int size) { this.size = size; this.cells = new Cell[size][size]; // Initialize all cells for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { cells[y][x] = new Cell(new Position(x, y)); } } } /** * @effects Returns the size of this board */ public int getSize() { return size; } /** * @effects Returns the cell at the specified position * @return The cell, or null if position is invalid */ public Cell getCell(Position position) { if (!isValidPosition(position)) { return null; } return cells[position.getY()][position.getX()]; } /** * @effects Checks if a position is within the board bounds */ public boolean isValidPosition(Position position) { int x = position.getX(); int y = position.getY(); return x >= 0 && x < size && y >= 0 && y < size; } /** * @effects Places a tank on the board at the specified position * @return true if successful, false if position is invalid or occupied */ public boolean placeTank(Tank tank, Position position) { if (!isValidPosition(position)) { return false; } Cell cell = getCell(position); if (!cell.isEmpty()) { return false; } cell.setTank(tank); tank.setPosition(position); return true; } /** * @effects Moves a tank from its current position to a new position * @return true if successful, false if new position is invalid or occupied */ public boolean moveTank(Tank tank, Position newPosition) { if (!isValidPosition(newPosition)) { return false; } // Get target cell Cell targetCell = getCell(newPosition); if (!targetCell.isEmpty()) { return false; } // Get current cell Position currentPosition = tank.getPosition(); Cell currentCell = getCell(currentPosition); // Move tank currentCell.setTank(null); targetCell.setTank(tank); tank.setPosition(newPosition); return true; } /** * @effects Gets all tanks within a radius of a center position * @return List of tanks within the radius */ public List getTanksInRadius(Position center, int radius) { List tanksInRadius = new ArrayList<>(); for (int y = 0; y < size; y++) { for (int x = 0; x < size; x++) { Position pos = new Position(x, y); if (center.getDistance(pos) <= radius) { Cell cell = getCell(pos); if (!cell.isEmpty()) { tanksInRadius.add(cell.getTank()); } } } } return tanksInRadius; } /** * @effects Gets all cells in this board */ public Cell[][] getCells() { return cells; } }