-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmain.pl
197 lines (183 loc) · 5.7 KB
/
main.pl
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
%---------------------Validations---------------------
diabolic([1,8,13,12,14,11,2,7,4,5,16,9,15,10,3,6]).
diabolic([1,12,7,14,8,13,2,11,10,3,16,5,15,6,9,4]).
diabolic([1,8,11,14,12,13,2,7,6,3,16,9,15,10,5,4]).
diabolic([A1,A2,A3,A4,B1,B2,B3,B4,C1,C2,C3,C4,D1,D2,D3,D4]):- %Checking any 4x4 Square.
/* [ A1,A2,A3,A4,
B1,B2,B3,B4,
C1,C2,C3,C4,
D1,D2,D3,D4 ]*/
%37 Validations!
%_______Rows(4)_______
A1+A2+A3+A4=:=34,
B1+B2+B3+B4=:=34,
C1+C2+C3+C4=:=34,
D1+D2+D3+D4=:=34,
%______Columns(4)_____
A1+B1+C1+D1=:=34,
A2+B2+C2+D2=:=34,
A3+B3+C3+D3=:=34,
A4+B4+C4+D4=:=34,
%___Diagonals \ (8)___
A1+B2+C3+D4=:=34,
B1+C2+D3+A4=:=34,
C1+D2+A3+B4=:=34,
D1+A2+B3+C4=:=34,
%___Diagonals / (8)___
A4+B3+C2+D1=:=34,
B4+C3+D2+A1=:=34,
C4+D3+A2+B1=:=34,
D4+A3+B2+C1=:=34,
%_______2x2(16)_______
A1+A2+B1+B2=:=34,
A2+A3+B2+B3=:=34,
A3+A4+B3+B4=:=34,
A4+A1+B4+B1=:=34,
B1+B2+C1+C2=:=34,
B2+B3+C2+C3=:=34,
B3+B4+C3+C4=:=34,
B4+B1+C4+C1=:=34,
C1+C2+D1+D2=:=34,
C2+C3+D2+D3=:=34,
C3+C4+D3+D4=:=34,
C4+C1+D4+D1=:=34,
D1+D2+A1+A2=:=34,
D2+D3+A2+A3=:=34,
D3+D4+A3+A4=:=34,
D4+D1+A4+A1=:=34,
%_____3x3(corners)____
A1+A3+C1+C3=:=34,
A2+A4+C2+C4=:=34,
B1+B3+D1+D3=:=34,
B2+B4+D2+D4=:=34,
%_____4x4(corners)____
A1+A4+D1+D4=:=34.
%recursevily obtains one diabolic square, and calls for one less argument, curry like.
diabolic(A,B):- diabolic(A), reflection(A,B).
diabolic(A,B,C):- diabolic(A,B),rotationCenter(A,C).
diabolic(A,B,C,D):-diabolic(A,B,C),rotationColumns(A,D).
diabolic(A,B,C,D,E):-diabolic(A,B,C,D),rotationRows(A,E).
diabolic(A,B,C,D,E,F):-diabolic(A,B,C,D,E),convolution(A,F).
diabolic(A,B,C,D,E,F,G):-diabolic(A,B,C,D,E,F),reflection(C,G).
diabolic(A,B,C,D,E,F,G,H):-diabolic(A,B,C,D,E,F,G),rotationCenter(D,H).
diabolic(A,B,C,D,E,F,G,H,I):-diabolic(A,B,C,D,E,F,G,H),rotationColumns(B,I).
diabolic(A,B,C,D,E,F,G,H,I,J):-diabolic(A,B,C,D,E,F,G,H,I),rotationRows(B,J).
%---------------------Showall---------------------
%It starts with the three basic forms of the magic squares,
showall:-showall( [1,8,13,12,14,11,2,7,4,5,16,9,15,10,3,6], [1,12,7,14,8,13,2,11,10,3,16,5,15,6,9,4], [1,8,11,14,12,13,2,7,6,3,16,9,15,10,5,4]).
showall(A,B,C):-
showall(A),
showall(B),
showall(C).
%Computes for each, the reflection and itself all the possible transformations for these two lists
showall(A):-
reflection(A,ReflectionOfA),
showallaux(A),
showallaux(ReflectionOfA).
%Have the first magic squares and call all other auxiliars.
%For A computes all the possible combinations, and writes the output, user friendly on console.
showallaux(A):-
rotationCenter(A,RotationOfA),
convolution(A,ConvolutionOfA),
rotationCenter(ConvolutionOfA,RotationCenterOfA),
convolution(RotationOfA,RotationOfAConv),
print([RotationOfA,ConvolutionOfA,RotationCenterOfA,RotationOfAConv]),
auxRotate3Rows(A),
auxRotate3Rows(RotationOfA),
auxRotate3Rows(ConvolutionOfA),
auxRotate3Rows(RotationCenterOfA),
auxRotate3Rows(RotationOfAConv).
%rotate a row and a column three times
auxRotate3Rows(A):-
rotationRows(A,A1),
auxRotate3Columns(A1),
rotationRows(A1,A2),
auxRotate3Columns(A2),
rotationRows(A2,A3),
auxRotate3Columns(A3),
print([A1,A2,A3]).
auxRotate3Columns(A):-
rotationColumns(A,A1),
rotationColumns(A1,A2),
rotationColumns(A2,A3),
print([A1,A2,A3]).
%-------------------------Transformations--------------
%just interchanges two columns
reflection([A1,A2,A3,A4,B1,B2,B3,B4,C1,C2,C3,C4,D1,D2,D3,D4],ANS):-
diabolic([A1,A2,A3,A4,B1,B2,B3,B4,C1,C2,C3,C4,D1,D2,D3,D4]),
reverse([A1,A2,A3,A4],LA),
reverse([B1,B2,B3,B4],LB),
reverse([C1,C2,C3,C4],LC),
reverse([D1,D2,D3,D4],LD),
append(LA,LB,X),
append(X,LC,Y),
append(Y,LD,ANS),
diabolic(ANS).
%interchanges in a rotation the middle values of the matrix counterwise
rotationCenter([A1,A2,A3,A4,B1,B2,B3,B4,C1,C2,C3,C4,D1,D2,D3,D4],ANS):-
diabolic([A1,A2,A3,A4,B1,B2,B3,B4,C1,C2,C3,C4,D1,D2,D3,D4]),
add(B1,[A1,A4],2,T),
add(B4,T,3,U),
add(A2,U,5,V),
add(C2,[D2,C3],2,T2),
add(D3,T2,4,U2),
add(D1,U2,5,V2),
append(V,[B2,B3],W),
append(W,[A3],Y),
append(V2,[C1,C4],W2),
append(W2,[D4],X),
append(Y,X,ANS),
diabolic(ANS).
%a simple movement to slice the colunms of position
rotationColumns([A1,A2,A3,A4,B1,B2,B3,B4,C1,C2,C3,C4,D1,D2,D3,D4],ANS):-
diabolic([A1,A2,A3,A4,B1,B2,B3,B4,C1,C2,C3,C4,D1,D2,D3,D4]),
rotatelist([A1,A2,A3,A4],L1),
rotatelist([B1,B2,B3,B4],L2),
rotatelist([C1,C2,C3,C4],L3),
rotatelist([D1,D2,D3,D4],L4),
append(L1,L2,X),
append(X,L3,Y),
append(Y,L4,ANS),
diabolic(ANS).
%simple rotation of the rows
rotationRows([A1,A2,A3,A4,B1,B2,B3,B4,C1,C2,C3,C4,D1,D2,D3,D4],ANS):-
diabolic([A1,A2,A3,A4,B1,B2,B3,B4,C1,C2,C3,C4,D1,D2,D3,D4]),
rotatelist([A1,A2,A3,A4,B1,B2,B3,B4,C1,C2,C3,C4,D1,D2,D3,D4],W),
rotatelist(W,X),
rotatelist(X,Y),
rotatelist(Y,ANS),
diabolic(ANS).
convolution([A1,A2,A3,A4,B1,B2,B3,B4,C1,C2,C3,C4,D1,D2,D3,D4],ANS):-
diabolic([A1,A2,A3,A4,B1,B2,B3,B4,C1,C2,C3,C4,D1,D2,D3,D4]),
append([A1,A4,B4,B1],[A2,A3,B3,B2],Y),
append([D2,D3,C3,C2],[D1,D4,C4,C1],X),
append(Y,X,ANS),
diabolic(ANS).
%-----------------------AUXILIARY ------------------
%simple action of reverse the list
reverse(list,list).
reverse(InList,Outlist):- reverseAux(InList,[],Outlist).
reverseAux(list,list,list).
reverseAux([],Outlist,Outlist).
reverseAux([Head|Tail],L1,L2):-reverseAux(Tail,[Head|L1],L2).
rotatelist([H|T], R) :- append(T, [H], R).
%Insert an element in the nth place
add(Val,[H|List],Pos,[H|Res]):- Pos > 1,!,Pos1 is Pos - 1,
add(Val,List,Pos1,Res).
add(Val, List, 1, [Val|List]).
%Friendly output in the console, for the matrix
print([]).
print( [ [A1,A2,A3,A4,B1,B2,B3,B4,C1,C2,C3,C4,D1,D2,D3,D4] |Xs]):-
write('------------'),
nl,
write('['),
write([A1,A2,A3,A4]),
nl,
write([B1,B2,B3,B4]),
nl,
write([C1,C2,C3,C4]),
nl,
write([D1,D2,D3,D4]),
write(']'),
nl,
print(Xs).