-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
214 lines (151 loc) · 5 KB
/
main.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
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
198
199
200
201
202
203
204
205
206
207
208
209
210
#!/usr/bin/env python
# coding: utf-8
# In[6]:
###########################################################################################################################
# Hassan Shahzad
# 18i-0441
# CS-D
# FAST-NUCES ISB
###########################################################################################################################
################################################## GLOBAL DECLARATIONS ####################################################
import numpy as np
import random
###########################################################################################################################
################################################# DEFINING THE BOARD ######################################################
def makeBoard(): #Creating an 8x8 chess board
arr=np.zeros((8,8))
return arr
###########################################################################################################################
################################################ PRINTING THE BOARD #######################################################
def printState(state):
print(state)
###########################################################################################################################
################################### RANDOM GENERATION OF QUEENS IMPLEMENTATION ############################################
def placeQueens(state, location): # This function will randomly place 8 queens in the maze
i=0
location=[]
while(i<=7):
index_x=random.randint(0,7)
index_y=random.randint(0,7)
while(1):
if state[index_x][index_y]!=1: # Checks if the queen is already placed in that location
state[index_x][index_y]=1 # The Queen is represented by "1"
location.append(index_x)
location.append(index_y)
break
else:
index_x=random.randint(0,7)
index_y=random.randint(0,7)
random.seed(random.randint(0,10))
random.seed(random.randint(0,10))
i=i+1
return location
###########################################################################################################################
####################################### OBJECTIVE FUNCTION IMPLEMENTATION #################################################
def objectiveFunction(state,location): # This function will calculate the number of attacking queens in each possibility
attackingQueens=0
q=0
while(q<=7):
y=location.pop()
x=location.pop()
index_x=x
index_y=y
i=0
count=0
while(i<=7): # Checking all possible combinations
if state[x][i]==1:
count=count+1
if count==2:
attackingQueens=attackingQueens+1 # Incrementing by 1 upon attack of two queens
i=i+1 # Will divide by 2 at the end
## We will keep on repeating these steps for each column and each row of that column
if count<2:
count=0
i=0
while(i<=7):
if state[i][y]==1:
count=count+1
if count==2:
attackingQueens=attackingQueens+1
i=i+1
if count<2:
count=0
x=index_x
y=index_y
while(y>0 & x>0 & x<7 & y<7):
if state[x][y]==1:
count=count+1
x=x+1
y=y+1
else:
x=x+1
y=y+1
if count==2:
attackingQueens=attackingQueens+1
if count<2:
count=0
x=index_x
y=index_y
while(y>0 & x>0 & x<7 & y<7):
if state[x][y]==1:
count=count+1
x=x-1
y=y-1
else:
x=x-1
y=y-1
if count==2:
attackingQueens=attackingQueens+1
if count<2:
count=0
x=index_x
y=index_y
while(y>0 & x>0 & x<7 & y<7):
if state[x][y]==1:
count=count+1
x=x-1
y=y+1
else:
x=x-1
y=y+1
if count==2:
attackingQueens=attackingQueens+1
if count<2:
count=0
x=index_x
y=index_y
while(y>0 & x>0 & x<7 & y<7):
if state[x][y]==1:
count=count+1
x=x+1
y=y-1
else:
x=x+1
y=y-1
if count==2:
attackingQueens=attackingQueens+1
q=q+1
print("Number of attacking queens = ")
print(int(attackingQueens/2))
###########################################################################################################################
################################################ MAIN IMPLEMENTATION ######################################################
def main():
board=makeBoard()
lis=[]
lis=placeQueens(board,lis)
print()
print("Board after placing queens = ")
print()
printState(board)
print()
print()
print("Queens are randomly palced at following locations : ")
print(lis)
print()
objectiveFunction(board,lis)
# Tell python to run main method
if __name__ == "__main__": main()
###########################################################################################################################
# In[ ]:
# In[ ]: