Skip to content

Commit 8b046ca

Browse files
* docs: kata description * feat: kata/battleship-field-validator * refactor: remove unnecessary condition * refactor: use boolean orientation * refactor: remove unnecessary condition statement --------- Co-authored-by: ParanoidUser <5120290+ParanoidUser@users.noreply.github.com>
1 parent 6610a42 commit 8b046ca

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# [Battleship field validator](https://www.codewars.com/kata/battleship-field-validator "https://www.codewars.com/kata/52bb6539a4cf1b12d90005b7")
2+
3+
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
4+
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
5+
1 if occupied by ship.
6+
7+
**Battleship** (also Battleships or Sea Battle) is a guessing game for two players.
8+
Each player has a 10x10 grid containing several "ships" and objective is to destroy enemy's forces by targeting individual cells on his
9+
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
10+
use Soviet/Russian version of the game.
11+
12+
![](https://i.imgur.com/IWxeRBV.png)
13+
14+
Before the game begins, players set up the board and place the ships accordingly to the following rules:
15+
16+
- There must be single battleship (size of 4 cells), 2 cruisers (size 3), 3 destroyers (size 2) and 4 submarines (size 1). Any additional
17+
ships are not allowed, as well as missing ships.
18+
- Each ship must be a straight line, except for submarines, which are just single cell.
19+
![](https://i.imgur.com/FleBpT9.png)
20+
- The ship cannot overlap or be in contact with any other ship, neither by edge nor by corner.
21+
![](https://i.imgur.com/MuLvnug.png)
22+
23+
This is all you need to solve this kata. If you're interested in more information about the game,
24+
visit [this link](https://en.wikipedia.org/wiki/Battleship_(game)).
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import static java.util.stream.IntStream.range;
2+
3+
interface BattleField {
4+
static boolean fieldValidator(int[][] field) {
5+
return range(0, 10).flatMap(r -> range(0, 10).map(c -> size(field, r, c))).reduce(0, Integer::sum) == 1234;
6+
}
7+
8+
private static int size(int[][] field, int row, int col) {
9+
int size = 1;
10+
while (col + size < 10 && field[row][col + 1] == 1 && field[row][col + size] == 1 ||
11+
row + size < 10 && field[row + 1][col] == 1 && field[row + size][col] == 1) {
12+
size++;
13+
}
14+
return isValid(field, row, col, row < 9 && field[row + 1][col] == 1, size) ? (int) Math.pow(10, size - 1.) : 0;
15+
}
16+
17+
private static boolean isValid(int[][] field, int row, int col, boolean vertical, int size) {
18+
for (int r = Math.max(row - 1, 0); r <= Math.min(vertical ? row + size : row + 1, 9); r++) {
19+
for (int c = Math.max(col - 1, 0); c <= Math.min(vertical ? col + 1 : col + size, 9); c++) {
20+
if (field[r][c] == 1 && !(r == row && c == col ||
21+
col + size > c && r == row && c >= col ||
22+
row + size > r && c == col && r >= row)) {
23+
return false;
24+
}
25+
}
26+
}
27+
return field[row][col] == 1;
28+
}
29+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import static org.junit.jupiter.api.Assertions.assertFalse;
2+
import static org.junit.jupiter.api.Assertions.assertTrue;
3+
4+
import org.junit.jupiter.api.Test;
5+
6+
class SolutionTest {
7+
@Test
8+
void sample() {
9+
assertTrue(BattleField.fieldValidator(new int[][]{
10+
{1, 0, 0, 0, 0, 1, 1, 0, 0, 0},
11+
{1, 0, 1, 0, 0, 0, 0, 0, 1, 0},
12+
{1, 0, 1, 0, 1, 1, 1, 0, 1, 0},
13+
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
14+
{0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
15+
{0, 0, 0, 0, 1, 1, 1, 0, 0, 0},
16+
{0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
17+
{0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
18+
{0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
19+
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}}));
20+
assertFalse(BattleField.fieldValidator(new int[][]{
21+
{1, 0, 0, 0, 0, 0, 0, 0, 0, 0},
22+
{1, 0, 1, 0, 0, 0, 0, 0, 1, 0},
23+
{1, 0, 1, 0, 1, 1, 1, 0, 1, 0},
24+
{1, 0, 1, 0, 0, 0, 0, 0, 0, 0},
25+
{0, 0, 1, 0, 0, 0, 0, 0, 1, 0},
26+
{0, 0, 0, 0, 1, 1, 1, 0, 0, 0},
27+
{0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
28+
{0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
29+
{0, 0, 0, 0, 0, 0, 0, 1, 0, 0},
30+
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
31+
}));
32+
}
33+
}

kata/3-kyu/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
1+
- [Battleship field validator](battleship-field-validator "52bb6539a4cf1b12d90005b7")
12
- [Make a spiral](make-a-spiral "534e01fbbb17187c7e0000c6")
23
- [Path Finder #3: the Alpinist](path-finder-number-3-the-alpinist "576986639772456f6f00030c")

0 commit comments

Comments
 (0)