-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQueensProblemAlpha.py
More file actions
193 lines (127 loc) · 5.05 KB
/
Copy pathQueensProblemAlpha.py
File metadata and controls
193 lines (127 loc) · 5.05 KB
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
class QueensProblem():
def __init__(self, numSquares):
self.board = []
squares = numSquares
while squares > 0:
tempsquares = numSquares
temp = []
while tempsquares > 0:
temp.append("*")
tempsquares -= 1
self.board.append(temp)
squares -= 1
def __str__(self):
repre = ""
for i in self.board:
temp = ""
for j in i:
temp += str(j) + " "
repre += temp + "\n"
return repre
def isValidPlace(self, row, col):
valid = False
rowCount = 0
hasQ = False
print("Testing for a row of:", row, "and column of:", col)
for i in range (len(self.board)):
colCount = 0
if "Q" in self.board[i]:
#print(i)
hasQ = True
if i == row:
return False
else:
for j in range (len(self.board[i])):
if self.board[i][j] == "Q":
if j == col:
return False
if hasQ:
diagRow = row
diagCol = col
#print(diagRow, diagCol)
#South east check (+, +)
print("South east check")
while diagRow < (len(self.board) - 1) and diagCol < (len(self.board) -1):
diagRow += 1
diagCol += 1
#print(diagRow, diagCol)
if self.board[diagRow][diagCol] == "Q":
return False
diagRow = row
diagCol = col
#print(diagRow, diagCol)
#North East Check (-, +)
print("North East Check")
while diagRow > 0 and diagCol < (len(self.board) -1):
diagRow -= 1
diagCol += 1
#print(diagRow, diagCol)
if self.board[diagRow][diagCol] == "Q":
return False
diagRow = row
diagCol = col
#print(diagRow, diagCol)
#North West Check (-, -)
print("North West Check")
while diagRow > 0 and diagCol > 0:
diagRow -= 1
diagCol -= 1
#print(diagRow, diagCol)
if self.board[diagRow][diagCol] == "Q":
return False
diagRow = row
diagCol = col
#print(diagRow, diagCol)
#South West Check (+, -)
print("South West Check")
while diagRow < (len(self.board) - 1) and diagCol > 0:
diagRow += 1
diagCol -= 1
#print(diagRow, diagCol)
if self.board[diagRow][diagCol] == "Q":
return False
print("Success")
return True
def solve(self, n):
if n == 0:
return self
failedValid = False
valid = False
if "Q" in self.board[n - 1]:
print("Failed Valid")
failedValid = True
for i in range(len(self.board[n - 1])):
if failedValid:
if self.board[n - 1][i] == "Q":
self.board[n - 1][i] = "*"
print(self)
if i + 1 <= (len(self.board[n - 1]) - 1) :
#print ("i", i)
valid = self.isValidPlace(n - 1, i + 1)
if valid:
self.board[n - 1][i + 1] = "Q"
return self.solve(n - 1)
else:
#print ("i", i)
valid = self.isValidPlace(n - 1, 0)
if valid:
self.board[n - 1][0] = "Q"
return self.solve(n - 1)
else:
valid = self.isValidPlace(n - 1, i)
#print ("i", i)
if valid:
self.board[n - 1][i] = "Q"
print(self)
return self.solve(n - 1)
if not valid:
return self.solve(n + 1)
return False
def main():
squares = int(input("Input a number above 4: "))
while squares < 4:
squares = int(input("Input a number above 4: "))
qProblem = QueensProblem(squares)
print(qProblem)
print(qProblem.solve(squares))
main()