-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode_1.py
More file actions
244 lines (220 loc) · 10.6 KB
/
Copy pathcode_1.py
File metadata and controls
244 lines (220 loc) · 10.6 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
class piece:
def __init__(self,name,colour,cols,rows,board):
self.name=name
self.colour=colour
self.cols=cols
self.rows=rows
self.board=board
def move(self,name,board,trow,tcols,rows,cols):
if self.valid(board, name, rows,cols, trow, tcols):
self.board[self.rows][self.cols]='.'
self.board[trow][tcols]=self.name
self.rows=trow
self.cols=tcols
print("move successful")
else:
print("invalid move")
def capture(self,name,fcols,trows,tcols,frow):
if self.valid(self.board, name, frow, fcols, trows, tcols) and (self.board[trows][tcols]!= '.' and (self.board[trows][tcols][0].isupper() != self.board[frow][fcols][0].isupper())):
self.board[frow][fcols]='.'
self.board[trows][tcols]=name
self.rows=trows
self.cols=tcols
else:
self.move(name,self.board,trows,tcols,frow,fcols)
def valid(self, board, name, frow, fcols, trows, tcols):
if self.name.startswith("P") or self.name.startswith("p"):
#use cordinates cheecking captures by using if statements
if self.colour=="white":
if (trows == frow-1 or (trows == frow-2 and frow==6)) and fcols == tcols:
if board[trows][tcols]=="." and board[frow-1][fcols]==".":
return True
else:
return False
if trows==frow-1 and abs(tcols-fcols)==1:
if board[trows][tcols]!="." and board[trows][tcols].islower():
return True
return False
if self.colour=="black":
if( trows==frow+1 or( trows==frow+2 and frow==1)) and fcols==tcols:
if board[trows][tcols]=="." and board[frow+1][fcols]==".":
return True
else:
return False
if trows==frow+1 and abs(tcols-fcols)==1:
if board[trows][tcols]!="." and board[trows][tcols].isupper():
return True
return False
elif self.name.startswith("R") or self.name.startswith("r"):
if self.colour=="white" or self.colour=="black":#rook
if trows==frow or tcols== fcols:
if trows==frow:
for i in range(min(fcols,tcols)+1,max(tcols,fcols)):
if board[frow][i]!=".":
return False
return True
if tcols==fcols:
for i in range (min(frow,trows)+1,max(trows,frow)):
if board[i][fcols]!=".":
return False
return True
elif self.name.startswith("B") or self.name.startswith("b"):#bishop
if self.colour=="white" or self.colour=="black":
if abs(trows-frow)==abs(tcols-fcols):
if trows>frow and tcols>fcols:
for i in range(1,abs(trows-frow)):#check squares
if board[frow+i][fcols+i]!=".":
return False
return True
if trows>frow and tcols<fcols:
for i in range(1,abs(trows-frow)):
if board[frow+i][fcols-i]!=".":
return False
return True
if trows<frow and tcols>fcols:
for i in range(1,abs(trows-frow)):
if board[frow-i][fcols+i]!=".":
return False
return True
if trows<frow and tcols<fcols:
for i in range(1,abs(trows-frow)):
if board[frow-i][fcols-i]!=".":
return False
return True
elif self.name.startswith("Q") or self.name.startswith("q"):
if self.colour=="white" or self.colour=="black":#straight movt
if trows==frow or tcols== fcols:
if trows==frow:
if tcols>fcols:
for i in range(fcols+1,tcols):
if board[frow][i]!=".":
return False
return True
if tcols<fcols:
for i in range(tcols+1,fcols):
if board[frow][i]!=".":
return False
return True
if tcols==fcols:
for i in range (min(frow,trows)+1,max(trows,frow)):
if board[i][fcols]!=".":
return False
return True
elif abs(trows-frow)==abs(tcols-fcols):#diagoanal movt
if trows>frow and tcols>fcols:
for i in range(1,abs(trows-frow)):#check squares
if board[frow+i][fcols+i]!=".":
return False
return True
if trows>frow and tcols<fcols:
for i in range(1,abs(trows-frow)):
if board[frow+i][fcols-i]!=".":
return False
return True
if trows<frow and tcols>fcols:
for i in range(1,abs(trows-frow)):
if board[frow-i][fcols+i]!=".":
return False
return True
if trows<frow and tcols<fcols:
for i in range(1,abs(trows-frow)):
if board[frow-i][fcols-i]!=".":
return False
return True
elif self.name.startswith("N") or self.name.startswith("n"):
if self.colour=='white' or self.colour=='black':
if (abs(trows- frow)==2 and abs(tcols-fcols)==1 )or( abs((trows-frow))==1 and abs(tcols- fcols)==2):
return True
return False
elif self.name.startswith("K") or self.name.startswith('k'):
if self.colour=='white' or self.colour=='black':
if abs(trows-frow)<=1 and abs(tcols-fcols)<=1:
return True
return False
class chess:
def __init__(self):
self.board = [
["r1","n1","b1","q","k","b2","n2","r2"],
["p1","p2","p3","p4","p5","p6","p7","p8"],
[".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".","."],
["P1","P2","P3","P4","P5","P6","P7","P8"],
["R1","N1","B1","Q","K","B2","N2","R2"]
]
self.white_positions = {
"P1":(6,0), "P2":(6,1), "P3":(6,2), "P4":(6,3),
"P5":(6,4), "P6":(6,5), "P7":(6,6), "P8":(6,7),
"R1":(7,0), "N1":(7,1), "B1":(7,2), "Q":(7,3),
"K":(7,4), "B2":(7,5), "N2":(7,6), "R2":(7,7)
}
self.black_positions = {
"r1":(0,0), "n1":(0,1), "b1":(0,2), "q":(0,3),
"k":(0,4), "b2":(0,5), "n2":(0,6), "r2":(0,7),
"p1":(1,0), "p2":(1,1), "p3":(1,2), "p4":(1,3),
"p5":(1,4), "p6":(1,5), "p7":(1,6), "p8":(1,7)
}
self.pieces = {}
for name, (row, col) in self.white_positions.items():
self.pieces[name] = piece(name, "white", col, row, self.board)
for name, (row, col) in self.black_positions.items():
self.pieces[name] = piece(name, "black", col, row, self.board)
# '''capitals are white pieces, lowercase are black pieces'''
def new_board(self):
self.board = [
["r1","n1","b1","q","k","b2","n2","r2"],
["p1","p2","p3","p4","p5","p6","p7","p8"],
[".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".","."],
[".",".",".",".",".",".",".","."],
["P1","P2","P3","P4","P5","P6","P7","P8"],
["R1","N1","B1","Q","K","B2","N2","R2"]
]
self.white_positions = {
"P1":(6,0), "P2":(6,1), "P3":(6,2), "P4":(6,3),
"P5":(6,4), "P6":(6,5), "P7":(6,6), "P8":(6,7),
"R1":(7,0), "N1":(7,1), "B1":(7,2), "Q":(7,3),
"K":(7,4), "B2":(7,5), "N2":(7,6), "R2":(7,7)
}
self.black_positions = {
"r1":(0,0), "n1":(0,1), "b1":(0,2), "q":(0,3),
"k":(0,4), "b2":(0,5), "n2":(0,6), "r2":(0,7),
"p1":(1,0), "p2":(1,1), "p3":(1,2), "p4":(1,3),
"p5":(1,4), "p6":(1,5), "p7":(1,6), "p8":(1,7)
}
def display(self):
print(" 0 1 2 3 4 5 6 7")
for r, row in enumerate(self.board):
row_str = f"{r} "
for cell in row:
row_str += ". " if cell == "." else cell[0] + " "
print(row_str)
print()
game=chess()
game.display()
turn="white"
while True:
cpiece=input("enter your piece you want to move:")
if turn=="white":
if cpiece not in ["P1","P2","P3","P4","P5","P6","P7","P8","R1","N1","B1","Q","K","B2","N2","R2"]or (trow<0 or trow>7 or tcols<0 or tcols>7):
print("invalid piece")
continue
trow=int(input("enter the row you want to move to:"))
tcols=int(input("enter the column you want to move to:"))
elif turn =="black":
if cpiece not in ["p1","p2","p3","p4","p5","p6","p7","p8","r1","n1","b1","q","k","b2","n2","r2"]or (trow<0 or trow>7 or tcols<0 or tcols>7):
print("invalid input")
continue
print("invalid piece")
continue
trow=int(input("enter the row you want to move to:"))
tcols=int(input("enter the column you want to move to:"))
current_piece.capture(cpiece, current_piece.cols, trow, tcols, current_piece.rows)
positions = game.white_positions if turn=="white" else game.black_positions
positions[cpiece] = (current_piece.rows, current_piece.cols)
if turn=="black":
turn="white"
else:
turn="black"