Since you have 10 max max and your data structure is a byte array, why not just imagine water fields using a value of 0 and ship fields using values from 1 to 10.
Then you can iterate over the fields and check the surrounding (you can optimize based on iteration, so as not to check the same combination twice).
If you encounter a value of 0, continue.
If you get a value> 0, then check if the surrounding fields contain values other than 0 and the field value (detection of moving ships).
In addition, if the surrounding fields contain the same value, but touch the corners, you have detected an illegal setting. L-shaped ships must also violate the off-diagonal rule.
The last thing to check is whether the ship can contain holes. In this case, just save the range of fields found on each ship, and check if the new field is near this range. This will also give you the number of ships, since you can request the length of each range of ships at the end.
Consider the following partial board:
ABCDEFGHIJ _____________________ 1 | 0 0 2 0 0 0 0 0 0 0 2 | 0 1 0 0 3 3 0 4 0 4 3 | 0 0 0 0 0 3 0 0 0 0
If you now check all the surrounding B2 fields (which has a value of 1), you will find a value of 2, which is an error.
If you check E2, you will find the field F3 diagonal, although with the same value 3 → error
If you check H2, you get the H2-H2 range (length 1), since H2 is surrounded by empty fields. If you check J2 later, you will see that the difference between H and J is 2 (1 will be normal), so you found a duplicate vessel (or one with a hole) -> error
In principle, these rules can be formulated as follows (as applied to non-empty cells, with ships):
- each cell diagonal of the cell must be empty (value 0)
- each cell of a cell must either have a value of 0 or the same value as the cell
- If the cell value is already indicated in the list, it must have at least one direct neighbor having the same value
These 3 rules should find any positional irregularities, if you keep the range of each vessel in the list specified for rule 3, you can also check the limit on the number of ships.