Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions kata/3-kyu/battleship-field-validator/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# [Battleship field validator](https://www.codewars.com/kata/battleship-field-validator "https://www.codewars.com/kata/52bb6539a4cf1b12d90005b7")

Write a method that takes a field for well-known board game "Battleship" as an argument and returns true if it has a valid disposition of
ships, false otherwise. Argument is guaranteed to be 10*10 two-dimension array. Elements in the array are numbers, 0 if the cell is free and
1 if occupied by ship.

**Battleship** (also Battleships or Sea Battle) is a guessing game for two players.
Each player has a 10x10 grid containing several "ships" and objective is to destroy enemy's forces by targeting individual cells on his
field. The ship occupies one or more cells in the grid. Size and number of ships may differ from version to version. In this kata we will
use Soviet/Russian version of the game.

![](https://i.imgur.com/IWxeRBV.png)

Before the game begins, players set up the board and place the ships accordingly to the following rules:

- There must be single battleship (size of 4 cells), 2 cruisers (size 3), 3 destroyers (size 2) and 4 submarines (size 1). Any additional
ships are not allowed, as well as missing ships.
- Each ship must be a straight line, except for submarines, which are just single cell.
![](https://i.imgur.com/FleBpT9.png)
- The ship cannot overlap or be in contact with any other ship, neither by edge nor by corner.
![](https://i.imgur.com/MuLvnug.png)

This is all you need to solve this kata. If you're interested in more information about the game,
visit [this link](https://en.wikipedia.org/wiki/Battleship_(game)).
29 changes: 29 additions & 0 deletions kata/3-kyu/battleship-field-validator/main/BattleField.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import static java.util.stream.IntStream.range;

interface BattleField {
static boolean fieldValidator(int[][] field) {
return range(0, 10).flatMap(r -> range(0, 10).map(c -> size(field, r, c))).reduce(0, Integer::sum) == 1234;
}

private static int size(int[][] field, int row, int col) {
int size = 1;
while (col + size < 10 && field[row][col + 1] == 1 && field[row][col + size] == 1 ||
row + size < 10 && field[row + 1][col] == 1 && field[row + size][col] == 1) {
size++;
}
return isValid(field, row, col, row < 9 && field[row + 1][col] == 1, size) ? (int) Math.pow(10, size - 1.) : 0;
}

private static boolean isValid(int[][] field, int row, int col, boolean vertical, int size) {
for (int r = Math.max(row - 1, 0); r <= Math.min(vertical ? row + size : row + 1, 9); r++) {
for (int c = Math.max(col - 1, 0); c <= Math.min(vertical ? col + 1 : col + size, 9); c++) {
if (field[r][c] == 1 && !(r == row && c == col ||
col + size > c && r == row && c >= col ||
row + size > r && c == col && r >= row)) {
return false;
}
}
}
return field[row][col] == 1;
}
}
33 changes: 33 additions & 0 deletions kata/3-kyu/battleship-field-validator/test/SolutionTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertTrue;

import org.junit.jupiter.api.Test;

class SolutionTest {
@Test
void sample() {
assertTrue(BattleField.fieldValidator(new int[][]{
{1, 0, 0, 0, 0, 1, 1, 0, 0, 0},
{1, 0, 1, 0, 0, 0, 0, 0, 1, 0},
{1, 0, 1, 0, 1, 1, 1, 0, 1, 0},
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
{0, 0, 0, 0, 1, 1, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
{0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}));
assertFalse(BattleField.fieldValidator(new int[][]{
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{1, 0, 1, 0, 0, 0, 0, 0, 1, 0},
{1, 0, 1, 0, 1, 1, 1, 0, 1, 0},
{1, 0, 1, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 1, 0, 0, 0, 0, 0, 1, 0},
{0, 0, 0, 0, 1, 1, 1, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
{0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
}));
}
}
1 change: 1 addition & 0 deletions kata/3-kyu/index.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
- [Battleship field validator](battleship-field-validator "52bb6539a4cf1b12d90005b7")
- [Make a spiral](make-a-spiral "534e01fbbb17187c7e0000c6")
- [Path Finder #3: the Alpinist](path-finder-number-3-the-alpinist "576986639772456f6f00030c")