3 kyu

Battleship field validator

5,086 of 15,223romanzes

Description:

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 targetting 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.


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.
  • The ship cannot overlap or be in contact with any other ship, neither by edge nor by corner.
This is all you need to solve this kata. If you're interested in more information about the game, visit this link.
Games
Arrays
Algorithms

More By Author:

Check out these other kata created by romanzes

Stats:

CreatedDec 25, 2013
PublishedDec 26, 2013
Warriors Trained46852
Total Skips9851
Total Code Submissions168183
Total Times Completed15223
JavaScript Completions5086
CoffeeScript Completions69
Python Completions5805
PHP Completions396
C++ Completions1195
Java Completions1426
C# Completions1333
Ruby Completions201
Total Stars2359
% of votes with a positive feedback rating94% of 2208
Total "Very Satisfied" Votes1984
Total "Somewhat Satisfied" Votes191
Total "Not Satisfied" Votes33
Ad
Contributors
  • romanzes Avatar
  • adam-tokarski Avatar
  • donaldsebleung Avatar
  • Blind4Basics Avatar
  • Voile Avatar
  • Ching Ching Avatar
  • iNont Avatar
  • hobovsky Avatar
  • trashy_incel Avatar
  • avermakov Avatar
Ad