| 1 | #!/bin/bash |
| 2 | # msquare.sh |
| 3 | # Magic Square generator (odd-order squares only!) |
| 4 | |
| 5 | # Author: mendel cooper |
| 6 | # reldate: 19 Jan. 2009 |
| 7 | # License: Public Domain |
| 8 | # A C-program by the very talented Kwon Young Shin inspired this script. |
| 9 | # http://user.chollian.net/~brainstm/MagicSquare.htm |
| 10 | |
| 11 | # Definition: A "magic square" is a two-dimensional array |
| 12 | # of integers in which all the rows, columns, |
| 13 | # and *long* diagonals add up to the same number. |
| 14 | # Being "square," the array has the same number |
| 15 | # of rows and columns. That number is the "order." |
| 16 | # An example of a magic square of order 3 is: |
| 17 | # 8 1 6 |
| 18 | # 3 5 7 |
| 19 | # 4 9 2 |
| 20 | # All the rows, columns, and the two long diagonals add up to 15. |
| 21 | |
| 22 | |
| 23 | # Globals |
| 24 | EVEN=2 |
| 25 | MAXSIZE=31 # 31 rows x 31 cols. |
| 26 | E_usage=90 # Invocation error. |
| 27 | dimension= |
| 28 | declare -i square |
| 29 | |
| 30 | usage_message () |
| 31 | { |
| 32 | echo "Usage: $0 order" |
| 33 | echo " ... where \"order\" (square size) is an ODD integer" |
| 34 | echo " in the range 3 - 31." |
| 35 | # Actually works for squares up to order 159, |
| 36 | #+ but large squares will not display pretty-printed in a term window. |
| 37 | # Try increasing MAXSIZE, above. |
| 38 | exit $E_usage |
| 39 | } |
| 40 | |
| 41 | |
| 42 | calculate () # Here's where the actual work gets done. |
| 43 | { |
| 44 | local row col index dimadj j k cell_val=1 |
| 45 | dimension=$1 |
| 46 | |
| 47 | let "dimadj = $dimension * 3"; let "dimadj /= 2" # x 1.5, then truncate. |
| 48 | |
| 49 | for ((j=0; j < dimension; j++)) |
| 50 | do |
| 51 | for ((k=0; k < dimension; k++)) |
| 52 | do # Calculate indices, then convert to 1-dim. array index. |
| 53 | # Bash doesn't support multidimensional arrays. Pity. |
| 54 | let "col = $k - $j + $dimadj"; let "col %= $dimension" |
| 55 | let "row = $j * 2 - $k + $dimension"; let "row %= $dimension" |
| 56 | let "index = $row*($dimension) + $col" |
| 57 | square[$index]=cell_val; ((cell_val++)) |
| 58 | done |
| 59 | done |
| 60 | } # Plain math, visualization not required. |
| 61 | |
| 62 | |
| 63 | print_square () # Output square, one row at a time. |
| 64 | { |
| 65 | local row col idx d1 |
| 66 | let "d1 = $dimension - 1" # Adjust for zero-indexed array. |
| 67 | |
| 68 | for row in $(seq 0 $d1) |
| 69 | do |
| 70 | |
| 71 | for col in $(seq 0 $d1) |
| 72 | do |
| 73 | let "idx = $row * $dimension + $col" |
| 74 | printf "%3d " "${square[idx]}"; echo -n " " |
| 75 | done # Displays up to 13th order neatly in 80-column term window. |
| 76 | |
| 77 | echo # Newline after each row. |
| 78 | done |
| 79 | } |
| 80 | |
| 81 | |
| 82 | ################################################# |
| 83 | if [[ -z "$1" ]] || [[ "$1" -gt $MAXSIZE ]] |
| 84 | then |
| 85 | usage_message |
| 86 | fi |
| 87 | |
| 88 | let "test_even = $1 % $EVEN" |
| 89 | if [ $test_even -eq 0 ] |
| 90 | then # Can't handle even-order squares. |
| 91 | usage_message |
| 92 | fi |
| 93 | |
| 94 | calculate $1 |
| 95 | print_square # echo "${square[@]}" # DEBUG |
| 96 | |
| 97 | exit $? |
| 98 | ################################################# |
| 99 | |
| 100 | |
| 101 | # Exercises: |
| 102 | # --------- |
| 103 | # 1) Add a function to calculate the sum of each row, column, |
| 104 | # and *long* diagonal. The sums must match. |
| 105 | # This is the "magic constant" of that particular order square. |
| 106 | # 2) Have the print_square function auto-calculate how much space |
| 107 | # to allot between square elements for optimized display. |
| 108 | # This might require parameterizing the "printf" line. |
| 109 | # 3) Add appropriate functions for generating magic squares |
| 110 | # with an *even* number of rows/columns. |
| 111 | # This is non-trivial(!). |
| 112 | # See the URL for Kwon Young Shin, above, for help. |