Advertisement
richarduie

MagicSquare.html

Oct 6th, 2013
182
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 5 2.94 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4.     <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  5.     <title>Magic Squares</title>
  6.  
  7.     <style type="text/css">
  8.         h1 { font-size: 1.5em; }
  9.         table, td { border: 1px solid black; }
  10.         td { text-align: center; padding: 4px 6px; }
  11.     </style>
  12.  
  13.     <script type="text/javascript">
  14.         function get( eid ) {return document.getElementById( eid ); };
  15.  
  16.         // create, format, and display magic square for valid input
  17.         function doSquare() {
  18.             // get user input and try to convert to integer
  19.             var n = parseInt( get( "N" ).value );
  20.             // test for valid input
  21.             if ( isNaN( n ) ) {
  22.                 alert( "square size must be an integer" );
  23.                 return;
  24.             }
  25.             if ( 0 > n ) {
  26.                 alert( "square size must be positive" );
  27.                 return;
  28.             }
  29.             if ( 0 == n%2 ) {
  30.                 alert( "square size must be odd" );
  31.                 return;
  32.             }
  33.             // for valid (odd, positive) integer, create and display square
  34.             showSquare( getSquare( n ) );
  35.         };
  36.  
  37.         // apply Loubère's method to fill a magic square
  38.         function getSquare( n ) {
  39.             // create NxN array
  40.             var values = new Array();
  41.             for (var i = 0; i < n; i++) {
  42.                 values[i] = new Array();
  43.             }
  44.             var row = 0;            // current row
  45.             var prevRow = row;      // previous row
  46.             var col = Math.floor( n/2 );    // current column
  47.             var prevCol = col;      // previous column
  48.             var last = n*n;         // largest value (number of values)
  49.             // for each value in {1, 2, 3,..., n^2}...
  50.             for (var i = 1; i <= last; i++) {
  51.                 // if value already in array position given by row and col,
  52.                 // place current value directly below the last one placed
  53.                 if ("undefined" != typeof values[ row ][ col ]) {
  54.                     row = prevRow + 1// one row lower than last
  55.                     col = prevCol;      // same column as last
  56.                 }
  57.                 values[ row ][ col ] = i;   // assign value to position
  58.                 var prevRow = row;      // record for next iteration
  59.                 var prevCol = col;      // record for next iteration
  60.                 // default next position is one row up and one column to right
  61.                 // - wrap to bottom row and leftmost column for out of bounds
  62.                 // index positions
  63.                 row -= 1;           // update for next iteration
  64.                 if (0 > row) row += n;      // ...correct, if out of bounds
  65.                 col += 1;           // update for next iteration
  66.                 if (col == n) col = 0;      // ...correct, if out of bounds
  67.             }
  68.             return values;
  69.         };
  70.  
  71.         // format as HTLM table for display
  72.         function showSquare( values ) {
  73.             var n = values.length;
  74.             var square = "<table>";
  75.             for (var i = 0; i < n; i++) {
  76.                 square += "<tr>";
  77.                 for (var j = 0; j < n; j++) {
  78.                     square += "<td>" + values[i][j] + "</td>";
  79.                 }
  80.                 square += "</tr>";
  81.             }
  82.             square += "</table>";
  83.             // add to page
  84.             get( "square" ).innerHTML = square;
  85.         };
  86.     </script>
  87. </head>
  88. <body>
  89.     <form>
  90.         <h1>Magic Squares Generator</h1>
  91.         <p>
  92.             square size (odd integer): <input type="text" id="N" />
  93.             <input type="button" value="draw square" onclick="doSquare();" />
  94.         </p>
  95.     </form>
  96.     <p id="square"></p>
  97. </body>
  98. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement