DELPHI/Sample Source

[델파이 / 마방진] Delphi 로 마방진 (Magic Square) 코딩 하기

Kim.Sung 2021. 1. 21. 09:09
728x90

※ 이것도 한참 Delphi 공부 할때 예제로 코딩하였던 예제이다.

※ 마방진이라고 왜 했는지 기억이.. 함수 공부하려고 했던 것 같다.

※ 사용 언어는 Delphi7

 

1. 실행 화면

※ button Click 시 저렇게 마방진 숫자가 나오는 예제 코드 였다.

2. Sample Source

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
unit Unit1;
 
interface
 
uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;
 
type
  Tnumber = class
    private
        XCoord  : Integer;  //가로 줄
        YCoord  : Integer;  //세로 줄
        arr1    : array of array of Integer;       //동적 배열
        num     : Integer;
 
    public
        constructor Create(XCoord_MAX : Integer);
        procedure   NumberSet(XCoord_MAX : Integer; Change : Integer);
        procedure   Print(XCoord_MAX : Integer);
 
    end;
type
  TForm1 = class(TForm)
    Button1: TButton;
    procedure Button1Click(Sender: TObject);
 
  private
    { Private declarations }
  public
    { Public declarations }
  end;
 
var
  Form1: TForm1;
 
implementation
 
{$R *.dfm}
 
 
constructor Tnumber.Create(XCoord_MAX : Integer);
var
    i : Integer;
begin
    XCoord  := -1;
    YCoord :=  0;
    num     := 0;
 
    SetLength(arr1,XCoord_MAX);
    for i := 0 to 4 do
    begin
        SetLength(arr1[i],XCoord_MAX);    //동적배열 사용시 임의로 메모리 할당
    end;
    NumberSet(XCoord_MAX,1);
    Print(XCoord_MAX);
 
end;
 
procedure TForm1.Button1Click(Sender: TObject);
var
    Number    : Tnumber;
begin
    Number := Tnumber.Create(5);
    Number.Free;
end;
 
// XCoord_max  : 가로 max 갯수
// Change : X,Y 증가,감소 치
// 가로 값 세팅 & 마지막 열 세로값 세팅
procedure Tnumber.NumberSet(XCoord_MAX : Integer; Change : Integer);
var
    i: integer;
begin
    while True do
    begin
        if XCoord_MAX <= 0 then Exit;
 
        for i := 0 to (2*XCoord_MAX-1) -1 do
        begin
            if i < XCoord_MAX then
                XCoord := XCoord + Change
            else
                YCoord := YCoord + Change;
 
 
            Inc(num);
            arr1[XCoord][YCoord] := num;
        end;
        XCoord_MAX := XCoord_MAX-1;
        Change := Change*(-1);
   end;
end;
 
procedure Tnumber.Print(XCoord_MAX : Integer);
var
    iti, itj: Integer;
    itx, ity: Integer;
begin
    itx := 150; ity := 10;
    Application.MainForm.Canvas.TextOut(itx, ity, '');
    itx := 150; Inc(ity,20);
 
    for iti := 0 to (XCoord_MAX-1) do
    begin
        for itj := 0 to (XCoord_MAX-1) do
        begin
            Application.MainForm.Canvas.TextOut(itx, ity, Format('%2d ', [arr1[iti][itj]]));
            Inc(itx,20);
        end;
        Application.MainForm.Canvas.TextOut(itx, ity, '');
        itx := 150; Inc(ity,20);
    end;
end;
end.
cs
728x90