Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Ocean {
- // private instance variables
- private Boat[] boats;
- private int[][] ocean;
- private int boatIndex;
- private int totalBoats;
- // constructor
- public Ocean(int numberOfBoats, in oceanSize) {
- boatIndex = 0
- totalBoats = numberOfBoats;
- boats = new Boat[totalBoats];
- ocean = new int[oceanSize][oceanSize];
- // initialize ocean to having no boats (-1)
- for (int r = 0; r < ocean.length; r++) {
- for (int c = 0; c < ocean[r].length; c++) {
- ocean[r][c] = -1;
- }
- }
- }
- public void placeBoat(String boatName, String direction, Position pos) throws Exception {
- // create temp boat
- Boat tempBoat = new Boat(boatNmae, pos, direction)
- int posRow = pos.rowIndex()
- int posCol = pos.columnIndex();
- int maxCol = ocean[0].length - 1;
- int maxRow = ocean.length - 1;
- boolean horizontal = (direction.substring(0,1).toUpperCase().equals("H"));
- // check if starting position outside of ocean
- if (posRow < 0 || posRow > maxRow || posCol < 0 || posCol > maxCol)
- throw(new Exception("Starting position of Boat out of ocean"));
- // check if ending position outside of ocean
- if (horizontal) {
- // horizontal placement
- if (posCol + tempBoat.size() - 1 > maxCol)
- throw(new Exception("Boat placement goes of ocean grid (col)"));
- } else {
- //vertical placement
- if (posRow + tempBoat.size() - 1 > maxRow)
- throw(new Exception("Boat placement goes of ocean grid (row)"));
- }
- // check if boat placement overlaps another boat
- if (horizontal {
- // check columns for overlap
- for (int c = posCol; c < posCol + tempBoat.size(); c++) {
- if (ocean[posRow][c] != -1) {
- throw(new Exception("Boat placement overlaps existing boat (" +
- posRow + "," + posCol));
- }
- }
- } else {
- for (int r = posRow; r < posRow + tempBoat.size(); r++) {
- if (ocean[r][posCol]) != -1) {
- throw (new Exception("Boat placememnt overlaps exisiting boat (" +
- posRow + "," + posCol + ")"));
- }
- // create my boat in the ocean - if boat to place
- if (boatIndex < totalBoats) {
- boats[boatsIndex] = new Boat(boaName, pos, direction);
- if (horizontal) {
- // mark ocean for horizontal boat
- for (int c = posCol; c < posCol + boats[boatIndex].size(); c++) {
- ocean[posRow][c] = boatIndex;
- }
- } else {
- // mark ocean for vertical boat
- for (int r = posRow; r < posRow + boats[boatIndx].size(); r++) {
- ocean[r][posCol] = boatIndex;
- }
- }
- // increment number of boats placed
- boatIndex++;
- } else {
- throw(new Exception("too many boats!"));
- }
- public char boatInitial(Position pos) {
- if (ocean[pos.rowIndex)][pos.columnIndex()] != -1)
- return boats[ocean[pos.rowIndex()][pos.columnIndex()]].abbreviation()
- else
- return '?';
- }
- public String boatName(Position pos) {
- if (ocean[pos.rowIndex()][pos.columnIndex()] != -1
- return boats[ocean[pos.rowIndex()][pos.columnIndex()]].name();
- else
- return "??????"
- }
- public boolean allSunk() {
- for (int i = 0; i < boats.length; i++)
- if (!boats [i].sunk())
- return false;
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement