What is needed in this case is a simple approach with which we can determine if the point in question (ie. the ship), falls on one of our islands. We will probably also have the need to check this condition at later points in the program, not just in the initialization period. We could easily look at the board (screen) and determine if we had any ships on land (assuming we don't cheat!) and move them off. The computer however doesn't have any eyes (at least mine doesn't), so we have to develop some other method it can use to determine the same information. Which brings us to 'SETEMUP' and 'ISITIN'.
What SETEMUP does is to create some number of line equations describing the area(s) in question. This subroutine works for any regular or irregular shape. It works for any number of shapes, and set's Lip all for all those shapes simultaneously. As well as preparing all this information for the subsequent subroutine ISITIN. (Note: the subroutines are only limited by array sizes).
SETEMUP fills up five arrays with line equations representing the shapes I have defined, where does that leave us? It leaves us with ISITIN, which is a simple algorithm to determine if a point lies within the area's defined by the line equations from SETEMUP. If the given point lies on a line defining the area it is considered outside the area.
The pseudococle for the logic of placing the ships about would look something like this:
Step 1: Call SETEMUP for the islands def ined earlier.
Step 2: Generate random X,Y coordinate, in the range 0,0 to 319,159 (Gr. 8).
Call ISITIN for the point just generated if it's 'in' go back to Step 2.
store the ship location.
if we have more ships to place go to Step 2. END
We can allow the player to determine the position of his own vessels or we can randomly distribute his vessel's as well. If we are going to randomly distribute his ships as well then we repeat steps 1 & 2, but for his ships. If we are letting him deploy his ships himself then we still repeat steps 1 & 2, but we remove the random point generator and substitute an 'INPUT X,Y' statement in it's place.
Now that we have an idea of what we are trying to do let's take a look at the programs themselves. First we define the variables into three classes: 1) INPUT - data that must be present when the subroutine is called. 2) OUTPUT - data generated by the subroutine. 3) INTERNAL - data that has no significance outside the subroutine.
For SETEMUP:
INPUT:
A = Array containing X values
B = Array containing Y values
C = Array containing number of cornerpoints for each shape
D1 = Number of areas (islands in our case)
OUTPUT:
T = Array containing upper X boundaries
U = Array containing lower X boundaries
D, E, F = Array's that contain the line equation values
N1 = Number of line equations generated
The remaining variables used in SETEMUP are internal.
For ISITIN:
INPUT:
X =Input X coordinate
Y = Input Y coordinate
OUTPUT:
Z1 = Logical variables,
if Z1 = TRUE (Z1 = 1) otherwise Z1 = False (Z1 = 0)
The remaining variables in ISITIN are internal. Here is a quick example of how we would stuff data into SETEMUP, and subsequently call ISITIN to check a point. Let's assume we are only defining one area, it will have four corner points 1,1;4,1;4,6; and 2,5. We would set the following values in our program before we called SETEMUP:
5 D1 = 1