15 puzzle solver: Difference between revisions

Content added Content deleted
Line 246: Line 246:


===The Code===
===The Code===
The source code started off as a mainline only, but then as facilities were added and service routines became helpful. This prompted the use of F90's MODULE facility so that the PARAMETER statement could be used to describe the shape of the board with this available to each routine without the need for passing the values as parameters or messing with COMMON storage, or repeating the PARAMETER statement in each routine. Otherwise, literal constants such as 4 would appear in various places. These appearances could now be documented by using the appropriate name such as <code>NR</code> and <code>NC</code> rather than just <code>4</code> and similar. However, inside FORMAT statements the use of <code><NR - 1></code> (and not <code><NC - 1></code>) rather than 3 will succeed only if the compiler accepts this usage, and not all do. More complex calculations involving the board size have not been attempted. The PARAMETER facility is useful only for simple calculations. Considerations such as a 4x4 board having 16 squares is easy, but the consequences of this count fitting into a four-bit binary field are not, thus the equivalences involving <code>BOARD, BORED and BOAR</code> are not general for different board shapes, nor are the column headings adjustable. Similarly, subroutine UNPACK does not attempt to use a loop but employs explicit code.
The source code started off as a mainline only, but then facilities were added and service routines became helpful. This prompted the use of F90's MODULE facility so that the PARAMETER statement could be used to describe the shape of the board with this information available to each routine without the need for passing the values as parameters or messing with COMMON storage, or repeating the PARAMETER statement in each routine. Otherwise, literal constants such as 4 would appear in various places. These appearances could now be documented by using the appropriate name such as <code>NR</code> and <code>NC</code> rather than just <code>4</code> and similar. However, inside FORMAT statements the use of <code><NR - 1></code> (and not <code><NC - 1></code>) rather than 3 will succeed only if the compiler accepts this usage, and not all do. More complex calculations involving the board size have not been attempted. The PARAMETER facility is useful only for simple calculations, and the compiler typically does not allow the use of many functions, even library functions, in expressions. Considerations such as a 4x4 board having 16 squares is easy, but the consequences of this count fitting into a four-bit binary field are not, thus the equivalences involving <code>BOARD, BORED and BOAR</code> are not general for different board shapes, nor are the column headings adjustable. Similarly, subroutine UNPACK does not attempt to use a loop but employs explicit code.


This approach is also followed in the calculation of the hash code. Not using a loop (for two items only) but, by writing out the product rather than using the compiler's built-in <code>PRODUCT</code> function. A startling difference results:
This approach is also followed in the calculation of the hash code. Not using a loop (for two items only) but, by writing out the product rather than using the compiler's built-in <code>PRODUCT</code> function. A startling difference results: