-
Notifications
You must be signed in to change notification settings - Fork 0
/
magic_Square.py
73 lines (60 loc) · 2.31 KB
/
magic_Square.py
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
import numpy as np
import random as rd
import sys
class latin_square:
def grid(self):
self.size = int(input("enter the dimensions for the magic square: "))
self.grid = np.zeros((self.size, self.size), dtype = int)
self.count = 0
def possible(self, y, x, new_entery):
self.enteries_x = []
self.enteries_y = []
for i in range(self.size):
entry = self.grid[y][i]
self.enteries_x.append(entry)
self.possibilities_x = (set(range(1, self.size + 1)).difference(self.enteries_x))
self.count +=1
for j in range(self.size):
entry = self.grid[j][x]
self.enteries_y.append(entry)
self.possibilities_y = set(range(1, self.size + 1)).difference(self.enteries_y)
self.count +=1
self.enteries = list(set(self.enteries_x).union(self.enteries_y))
if new_entery in self.enteries:
self.possibilities = list(set(self.possibilities_x).intersection(self.possibilities_y))
self.count +=1
if self.possibilities == []:
self.count +=1
self.possibilities = list(self.possibilities_x)
return False
self.possibilities_x = []
self.possibilities_y = []
self.enteries_x = []
self.enteries_y = []
return True
def find_0(self):
for y in range(self.size) :
for x in range(1,self.size):
if self.grid[y][x] == 0:
return True
return False
def improve(self):
for y in range(self.size):
for x in range(self.size):
a = rd.randint(1, self.size)
if latin_square.possible(self, y,x,a):
self.grid[y][x] = a
self.count +=1
else:
self.count +=1
a = rd.choice(self.possibilities)
self.grid[y][x] = a
def get_latinSquare(self):
np.set_printoptions(threshold = sys.maxsize)
print(np.matrix(self.grid))
print(self.count)
v = self.grid[:][0].sort
a = latin_square()
a.grid()
a.improve()
a.get_latinSquare()