Send Close Add comments: (status displays here)
Got it!  This site uses cookies. You consent to this by clicking on "Got it!" or by continuing to use this website.nbsp; Note: This appears on each machine/browser from which this site is accessed.
Magic square grid


1. Magic square grid
This page looks at a magic square grid as a simple application of permutation processing.

2. Problem
The problem is to fill in a 3 by 3 grid with the numbers 1 through 9 such that the sum of each row, column, and diagonal is the same.
1 2 3 4 5 6 7 8 9

Note: This problem can be solved manually by trial and error or by clever use of mathematical deduction principles.

The approach used here is that of constraint logic permutation processing using a small computer program.

3. Warning
Warning: When working with permutations, one can generate a lot of output of one is not careful.

Use of debugging output and limit variables can help in getting the program started and working.

4. 2D array approach
A simple way to model the logical 3 by 3 grid is with a physical 3 by 3 2D array.

A change in "point of view" can often lead to a better and easier solution.

5. Alan Kay: Point of view
Talk at Creative Think seminar, 20 July 1982. Alan Kay (originator/developer of object-oriented concepts)

6. 1D array approach
Another way to model the logical 3 by 3 grid is with a physical 1D array of 9 elements. To use our permutation module, the permutation starts at position 1.

Since digits from 1 to 9 are being permuted, we can use the array square1 as the permutation array.

7. 2D array approach
There are times when a 2D array approach may be useful.

But in either case, when there is a wall to be avoided, sentinel data values can be added around the grid to make it easier to determine if the wall has been hit. The sentinel data values are added all round so that a ROWMAX by COLMAX 2D array becomes a ROWMAX+2 by COLMAX+2 array.

8. Brute force approach
The simple brute force approach used here is that of generating every permutation of the numbers 1 to 9 in the 3 by 3 grid and checking to see if the constraints are met.

9. Show the grid
The following procedure showDo1 will show the grid.

In many games, the positions and contents of the grid needs to be displayed on a regular basis.

10. Data driven approach
A data driven approach is used to check that the sum of the rows, columns, and diagonals match.


11. Off by one issues
Note: The criteria here uses a zero-based numbering scheme. The square1 array uses a one-based numbering scheme (to fit the permutation code previously developed). This is not a problem as long as the programmer accounts for the differences.

Such issues, if not handled properly, result it the common "off by one" errors in programming.

12. Sum a row
The following function sumDo1 will sum three cells of the permutation and return the result.

Think of indirection array cells1 as being the diagonal in the match1 list (position 6) from upper left to center to lower right in the square1 grid (as a 1D array).
{ 0, 4, 8}


13. Action
The action procedure actionDo1 checks if the permutation meets the match criteria. If so, the square is displayed using showDo1.


14. Main function
Here then is the main function.

The function actionDo1is passed to function permute0 as a "callback" function.

15. Program
Here is the C code.


16. Output
Here is the output. Notice the symmetry of the solutions. There is only one unique solution after rotations, reflections, etc.

Here is the output of the C code.


17. Future additions
Here are some useful future additions to the above program, for development purposes.

18. End of page