Assignment Report:
Assignment Report:
. Setup phase: In the program, players are informed to input a ship location
into a map of size 7x7 (7 rows and 7 columns), under a form as followed:
beginning row index beginning column index ending row index ending
column index
(e.g 1 1 1 2), with no comma and only space between each index needed, where
each index is ranged from 0 to 6 standing for each grid in a row or a column.
Next, the program will check for validity of the input in the following sequence:
● Firstly, it checks for correct space between indices. Each space is input
exactly at locations 1, 3, or 5, of total 7 locations (ranged from 0-6) in the
input (taken as a string). If at each locations 1, 3, 5 is a character different
from a space, it will fail the input validity.
● Secondly, it checks for correct input indices, if not in range 0-6 will fail the
validity. It does the method by subtracting each index by 48 and compare
the value with 6, if any index is greater than 6 will fail the validity.
● Third, it will check for correct size of the input ship. Each ship is allowed to
have a size of 2x1, 3x1, or 4x1, so each will only have a body of a straight
horizontal or vertical line. The program will check whether it lies completely
horizontally or vertically, then check the size of the body line with value 2,
3, or 4. If input location does not lie horizontally or vertically (e.g 1 2 3 4),
or its size is not 2, 3, or 4, it will fail the validity. The program does this by
checking for the same value of either 2 row indices or 2 column indices and
check size of the body line.
● Lastly, the program check for collisions in any existing ship on the map
(with saved locations of ships later on), if there is any collision, it will fail
validity of the input. Collisions can be checked by comparing whether any
index in the input line belongs to other lines existed in the map. First, the
program will check for parallel of the 2 compared lines, if they are but
differed from the same level (e.g line on row 4 // line on row 5) there is no
collision with this current ship in the map and the program moves onto the
next ship to check. If they are on the same level, the program checks
whether the beginning index of the input that is not fixed (horizontally or
vertically), considered a point, belongs to the compared line, under a form:
unfixed map index <= input index <= unfixed map index, where each map
index is unfixed, the map beginning or ending index. If the beginning or
ending input index is in the range of the map indices, or reversely, the
indices of the map is in the range of the input indices, the program check
for the next ship existed in the map. In the case of orthogonal lines, it will
find the indices that belong to both line, for example, 1 1 1 2 vs 1 2 3 2
results in indices (1,2). If this pair of indices belong to both line, namely
input index <= index on the same level of input <= input index, or similarly
for map indices, the program will move onto the next ship in the map. If
there’s not ship left, it will start to write input into the map.
Players will be informed which error is encountered during process and the
existed ship location map, as well as its left numbers allowed to input, where
1
2x1 has 3 ships, 3x1 has 2 ships, and 4x1 has 1 ship available.