-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodels.py
81 lines (63 loc) · 2.41 KB
/
models.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
74
75
76
77
78
79
80
81
from collections import Set
class Queen:
def __init__(self, id):
self.id = id
self.uniqueAttackableNode = []
self.location = NodeLocation(-1, -1)
def setLocation(self, r, c):
self.location = NodeLocation(r,c)
def __str__(self):
return 'Q {0} at: ({1},{2}).'.format(self.id, self.location[0], self.location[1])
def printQueen(self):
print('Queen {0} at: ({1},{2}). unique controlled nodes:'.format(self.id, self.location[0], self.location[1]))
print(*self.uniqueAttackableNode)
# q.location[]
def printBorad(self):
occupiedNodes = {}
for i in range(0, self.n):
q = self.queens[i]
if q.location[0] != -1:
occupiedNodes[q.location] = q.id
print('-', end='')
for i in range(0, self.n):
print('--'+str(i)+'-', end='')
print('-')
for r in range(0, self.n):
print(str(r)+'|', end='')
for c in range(0, self.n):
if (r, c) in occupiedNodes:
print(' X |', end='')
else:
print(' |', end='')
print()
class BoardNode:
def __init__(self, rowId, colId):
self.location = NodeLocation(rowId, colId)
self.attackableBy = []
def addAttacker(self, queen):
if len(self.attackableBy) == 0:
queen.uniqueAttackableNode.append(self)
if len(self.attackableBy) == 1:
self.attackableBy[0].uniqueAttackableNode.remove(self)
self.attackableBy.append(queen)
def removeAttacker(self, queen):
self.attackableBy.remove(queen)
if len(self.attackableBy) == 1:
self.attackableBy[0].uniqueAttackableNode.append(self)
def printNode(self):
print('Node: ({0},{1}) controled by queens:'.format(self.location.rowId, self.location.colId))
print(*self.attackableBy)
def __str__(self):
return ('({0},{1}) '.format(self.rowId, self.colId))
class NodeLocation:
def __init__(self, rowId, colId):
self.rowId = rowId
self.colId = colId
a = self.rowId
b = self.colId
self.cantorPairing =int((a + b) * (a + b + 1) / 2 + a);
def __hash__(self):
return self.cantorPairing
def __eq__(self, other):
"""Overrides the default implementation"""
return (self.colId==other.colId) and (self.rowId==other.rowId)