MagicSquare.cs

-------------------------------------------------------------------------------

using System;

namespace MagicSqr
{
       /// <summary>
       /// 마방진(Magic Square)을 생성하는 클레스
       /// 단, 3이상의 홀수 차수의 경우만 가능함
       /// </summary>
       public class TMagicSquare
       {
              private int[,] FBoard;
              public readonly int size;

              //property
              public int[,] Board
              {
                     get { return FBoard; }
              }

              public TMagicSquare(int n)
              {
                     size = n;

                     // Check Size & Odd
                     if ( (size < 3) || (size % 2 == 0))
                            throw new Exception("size should be odd and larger than 2");

                     FBoard = new int[size, size];

                     //초기화
                     for (int x=0; x < size; x++)
                            for (int y=0; y < size; y++)
                                   FBoard[x,y] = 0;

                     MakeMagicSquare();
              }

              private void MakeMagicSquare()
              {
                     int x, y;
                     int count;

                     x = size / 2;
                     y = 0;
                     count = 1;

                     while (count <= (size * size))
                     {
                            if ( 0 == FBoard[x, y] )
                            {
                                   FBoard[x, y] = count;

                                   x = (--x < 0) ? size-1 : x;
                                   y = (--y < 0) ? size-1 : y;

                                   count++;
                            }
                            else
                            {
                                   x = (x + 1) % size;
                                   y = (y + 2) % size;
                            }
                     }//end of while
              }
       }//end of class MagicSquare
}

-------------------------------------------------------------------------------


MagicSqrTest.cs

-------------------------------------------------------------------------------

using System;

namespace MagicSqr
{
       class MagicSqrTest
       {
              [STAThread]
              static void Main(string[] args)
              {
                     int size;
                     TMagicSquare MagicSquare;

                     Console.Write("Input Size: ");

                     size = Int32.Parse(Console.ReadLine());

                     Console.WriteLine("");

                     try
                     {
                            MagicSquare = new TMagicSquare(size);

                            for (int y = 0; y < size; y++)
                            {
                                   for (int x = 0; x < size; x++)
                                          Console.Write("  {0:D2}", MagicSquare.Board[x, y]);

                                   Console.WriteLine("");
                            }//end of for y
                     }
                     catch
                     {
                            Console.WriteLine("Size Error !!");
                     }//end of try ~ catch  

                     Console.WriteLine("Press ENTER to End...");
                     Console.ReadLine();
              }
       }//end of class MagicSqrTest
}

-------------------------------------------------------------------------------

+ Recent posts