Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class Board {
- /*
- 1 2 3 4 5 6 7 8 9 10
- A . . . . . . . . . .
- B . . . . . . . . . .
- C . . . . . . . . . .
- D . . . . . . . . . .
- E . . . . . . . . . .
- F . . . . . . . . . .
- G . . . . . . . . . .
- H . . . . . . . . . .
- I . . . . . . . . . .
- J . . . . . . . . . .
- cells[x][y]
- x is number, y is letter
- */
- Cell[][] cells = new Cell[10][10];
- public Board() {
- //place cells
- for (int i = 0; i < 10; i++) {
- for (int j = 0; j < 10; j++) {
- cells[i][j] = new Cell();
- }
- }
- }
- private static int convertLetterIndex(String letter) throws InvalidPositionException {
- return switch (letter.toLowerCase()) {
- case "a" -> 0;
- case "b" -> 1;
- case "c" -> 2;
- case "d" -> 3;
- case "e" -> 4;
- case "f" -> 5;
- case "g" -> 6;
- case "h" -> 7;
- case "i" -> 8;
- case "j" -> 9;
- default -> {
- throw new InvalidPositionException();
- }
- };
- }
- private static int convertNumberIndex(String letter) throws InvalidPositionException {
- return switch (letter.toLowerCase()) {
- case "1" -> 0;
- case "2" -> 1;
- case "3" -> 2;
- case "4" -> 3;
- case "5" -> 4;
- case "6" -> 5;
- case "7" -> 6;
- case "8" -> 7;
- case "9" -> 8;
- case "10" -> 9;
- default -> {
- throw new InvalidPositionException();
- }
- };
- }
- private static String indexConvert(int index) {
- return switch (index) {
- case 0 -> "A";
- case 1 -> "B";
- case 2 -> "C";
- case 3 -> "D";
- case 4 -> "E";
- case 5 -> "F";
- case 6 -> "G";
- case 7 -> "H";
- case 8 -> "I";
- case 9 -> "J";
- default -> null;
- };
- }
- private static String[] readPos(String position) {
- String[] splitPosition = new String[2];
- String stripPos = position.strip();
- splitPosition[0] = String.valueOf(stripPos.charAt(0));
- splitPosition[1] = stripPos.substring(1);
- return splitPosition;
- }
- public void placeShip(Ship target, String position, String direction) throws InvalidPlacementException, InvalidPositionException, InvalidShipTypeException{
- try {
- if (target == null) {
- throw new InvalidShipTypeException();
- }
- String[] posArray = new String[2];
- posArray = readPos(position);
- int y = convertNumberIndex(posArray[1]);
- int x = convertLetterIndex(posArray[0]);
- switch (direction) {
- case "down":
- if (x > (10 - target.length())) {
- throw new InvalidPlacementException();
- }
- for (int i = 0; i < target.length(); i++) {
- int j = i + x;
- if (cells[j][y].isOccupied()) {
- throw new InvalidPlacementException();
- };
- cells[j][y].placeSegment(target.getSegment(i + 1));
- }
- break;
- case "across":
- if (y > (10 - target.length())) {
- System.err.println(y);
- throw new InvalidPlacementException();
- }
- for (int i = 0; i < target.length(); i++) {
- int j = i + y;
- if (cells[x][j].isOccupied()) {
- throw new InvalidPlacementException();
- };
- cells[x][j].placeSegment(target.getSegment(i + 1));
- }
- break;
- default:
- throw new InvalidPlacementException();
- }
- }
- catch (StringIndexOutOfBoundsException e) {
- throw new InvalidPositionException();
- }
- }
- public void attack(String position) throws InvalidPositionException{
- try {
- String[] posArray = new String[2];
- posArray = readPos(position);
- int y = convertNumberIndex(posArray[1]);
- int x = convertLetterIndex(posArray[0]);
- cells[x][y].attack();
- }
- catch (StringIndexOutOfBoundsException e) {
- throw new InvalidPositionException();
- }
- }
- public boolean hasBeenHit(String position) throws InvalidPositionException{
- try {
- String[] posArray = new String[2];
- posArray = readPos(position);
- int y = convertNumberIndex(posArray[1]);
- int x = convertLetterIndex(posArray[0]);
- return cells[x][y].hasBeenHit();
- }
- catch (StringIndexOutOfBoundsException e) {
- throw new InvalidPositionException();
- }
- }
- public String displaySetup() {
- StringBuilder sb = new StringBuilder(" 1 2 3 4 5 6 7 8 9 10\n");
- for (int i = 0; i < 10; i++) {
- sb.append(indexConvert(i));
- for (int j = 0; j < 10; j++) {
- sb.append(" "+this.cells[i][j].displaySetup());
- }
- sb.append("\n");
- }
- return sb.toString();
- }
- @Override
- public String toString() {
- StringBuilder sb = new StringBuilder(" 1 2 3 4 5 6 7 8 9 10\n");
- for (int i = 0; i < 10; i++) {
- sb.append(indexConvert(i));
- for (int j = 0; j < 10; j++) {
- sb.append(" "+this.cells[i][j]);
- }
- sb.append("\n");
- }
- return sb.toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment