지난법 까지 했던 마방진 풀이법들을 종합하여 내가 짠 소스를 개시하려 한다.
서버환경이 php를 지원하지 못할 경우를 위하여 스크립트 소스를 공개 한다.
다음의 코드가 스크립트 소스이다.
<html>
<head>
<title>
</title>
<SCRIPT LANGUAGE="JavaScript">
function magicsquare()
{
var square = Number(document.magic.square.value);
var i = 0;
var j = 0;
var k = 0;
var sq = new Array(square);
for ( k = 0 ; k < square ; k++ )
{
sq[k] = new Array(square);
}
function table()
{
var sum = 0;
for ( i=0 ; i < square ; i++ )
{
sum = sum + sq[i][square-i-1] ;
}
document.write('<p align="center"><table border=1 style="border-collapse:collapse;"><tr><td colspan='+square+' align="center">'+square+'차</td><td align="center">'+sum+'</td></tr><tr><td rowspan='+square+' colspan='+square+' align="center"><table width=100% border=1 style="border-collapse:collapse;">');
for ( i=0 ; i < square ; i++ )
{
document.write('<tr>');
for ( j=0 ; j < square ; j++ )
{
document.write('<td align="center" width='+(100/square)+'%>'+sq[i][j]+'</td>');
}
document.write('</tr>');
}
document.write('</table></td>');
for ( i=0 ; i < square ; i++ )
{
document.write('<td align="center">');
sum = 0 ;
for ( j=0 ; j < square ; j++ )
{
sum = sum + sq[i][j] ;
}
document.write(sum+'</td></tr><tr>');
}
for ( i=0 ; i < square ; i++ )
{
document.write('<td align="center">');
sum = 0 ;
for ( j=0 ; j < square ; j++ )
{
sum = sum + sq[j][i] ;
}
document.write(sum+'</td>');
}
document.write('<td align="center">');
sum = 0 ;
for ( i=0 ; i < square ; i++ )
{
sum = sum + sq[i][i] ;
}
document.write(sum+'</td></tr></table></p>');
}
if( square <= 2 )
{
document.getElementById("inhere").innerHTML = '<p align="center">3이상의 숫자를 입력하여 주세요!</p>';
}
else
{
if ( square%2 == 1 )
{
i = (square-1)/2 + 1 ;
j = (square-1)/2 ;
for( k = 1 ; k <= (square*square) ; k++ )
{
sq[i][j] = k ;
if ( k % square == 0 )
{
i = i + 2 ;
}
else
{
i++ ;
j++ ;
}
if ( i >= square )
{
i = i - square ;
}
else
{
if ( j >= square )
{
j = j - square ;
}
else
{
}
}
}
}
else
{
if ( square%4 == 0 )
{
k = 1 ;
for ( i=0 ; i < square ; i++ )
{
for ( j=(square-1) ; j >= 0 ; j-- )
{
if (((i%4 == 1) || (i%4 == 2)) ^ ((j%4 == 1) || (j%4 == 2)))
{
sq[i][j] = square*square-k+1 ;
}
else
{
sq[i][j] = k ;
}
k++ ;
}
}
}
else
{
k = 1 ;
for ( i=0 ; i < square ; i++ )
{
for ( j=(square-1) ; j >= 0 ; j-- )
{
if ((i == j ) || (i+j+1 == square))
{
sq[i][j] = k ;
}
else
{
if ((((i+j)%2 == 0) && (((i+j >= square) && (j < square/2)) || ((i-j > 0) && (i < square/2) && (i > 2)) || ((j-i > 0) && (i >= square/2)) || ((i+j < square) && (j >= square/2) && (i > 1)))) || (((i+j)%2 == 1) && (((i+j < square) && (i >= square/2)) || ((j-i > 0) && (j < square/2) && (i > 1)) || ((i-j > 0) && (j >= square/2)) || ((i+j > square) && (i < square/2) && (i > 2)))))
{
sq[i][j] = square*square-k+square-j*2 ;
}
else
{
if ((((i+j)%2 == 0) && (((j-i > 0) && (j < square/2)) || ((i+j >= square) && (i < square/2) && (j < square-2)) || ((i+j < square) && (i >= square/2)) || ((i-j > 0) && (j >= square/2) && (j < square-3)))) || (((i+j)%2 == 1) && (((i-j > 0) && (i < square/2)) || ((i+j < square) && (j >= square/2) && (j < square-3)) || ((i+j >= square) && (j < square/2)) || ((j-i > 0) && (i >= square/2) && (j < square-2)))))
{
sq[i][j] = (i*2+1)*square-k+1 ;
}
else
{
sq[i][j] = square*square-k+1 ;
}
}
}
k++ ;
}
}
}
}
document.write('<p align="center">제 '+square+'차 완전마방진!</p><p align="center">한 구역의 합은 '+((square*square+1)*square/2)+'입니다.')
table();
}
}
</SCRIPT>
</head>
<body>
<form name="magic">
<p align="center">
<table>
<tr>
<td align="center">
제 <input type="text" name="square" size="10" maxlength="10" onkeydown="javascript:if(event.keyCode ==13) {magicsquare();return false;}" />차 완전마방진 <input type="button" id="btn" value="만들기" onClick="javascript:magicsquare();" />
</td>
</tr>
</table>
</p>
</form>
</body>
</html>
|