-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
298 lines (255 loc) · 8.78 KB
/
Copy pathmain.py
File metadata and controls
298 lines (255 loc) · 8.78 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
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
de = [['⬛', '⬜', '⬛', '⬜', '⬛', '⬜', '⬛', '⬜'],
['⬜', '⬛', '⬜', '⬛', '⬜', '⬛', '⬜', '⬛'],
['⬛', '⬜', '⬛', '⬜', '⬛', '⬜', '⬛', '⬜'],
['⬜', '⬛', '⬜', '⬛', '⬜', '⬛', '⬜', '⬛'],
['⬛', '⬜', '⬛', '⬜', '⬛', '⬜', '⬛', '⬜'],
['⬜', '⬛', '⬜', '⬛', '⬜', '⬛', '⬜', '⬛'],
['⬛', '⬜', '⬛', '⬜', '⬛', '⬜', '⬛', '⬜'],
['⬜', '⬛', '⬜', '⬛', '⬜', '⬛', '⬜', '⬛']]
#♔♕♖♗♘♙♚♛♜♝♞♟
def movement(
x, xo, y, yo,
once): #=-=-THE HOLY GRAIL-=-= (fyi touching this function is treason)
if x < 0 or x > 7 or y < 0 or y > 7: # checks if out of index
return
if a[x][y] == de[x][y]: # add and a[x][y] == enemy
a[x][y] = '❌'
print(f'you can move to {x},{y}')
if once == True:
return
else:
return
return movement(x + xo, xo, y + yo, yo, once)
class Pieces(object):
def __init__(self, x, y, color, board, emoji):
self.x, self.y = x, y
self.color = color
self.a = board
a[self.x][self.y] = self
self.emoji = emoji
def __str__(self):
return f'{self.emoji}'
def long_poss_moves(self): # long moves
global x1
global y1
if isinstance(self, Rook) or isinstance(self, Queen):
#right 0 +1
movement(self.x + 0, 0, self.y + 1, 1, False)
#left 0 -1
movement(self.x + 0, 0, self.y + -1, -1, False)
#up -1 0
movement(self.x + -1, -1, self.y + 0, 0, False)
#down +1 0
movement(self.x + 1, 1, self.y + 0, 0, False)
if isinstance(self, Bishop) or isinstance(self, Queen):
#upright -1 +1
movement(self.x + -1, -1, self.y + 1, 1, False)
#dwnleft +1 -1
movement(self.x + 1, 1, self.y + -1, -1, False)
#upleft -1 -1
movement(self.x + -1, -1, self.y + -1, -1, False)
#downright +1 +1
movement(self.x + +1, +1, self.y + 1, 1, False)
board()
player = input("Choose a coordinate: x1,y1 ").split(',')
x1, y1 = int(player[0]), int(player[1])
#ALL THE KING NEEDS IS IN HERE
def short_poss_moves(self):
global x1
global y1
movement(self.x + -1, -1, self.y + 0, 0, True)
# up -1 0
#eat only or king:
movement(self.x + -1, -1, self.y + 1, 1, True)
# upright -1 +1
movement(self.x + -1, -1, self.y + -1, -1, True)
# upleft -1 -1
if self.moved == False and isinstance(self, Pawn):
movement(x - 2, -2, y, 0, True)
# double up
##### KING:
if isinstance(self, King):
movement(self.x + -1, -1, self.y + 1, 1, True)
# dwnleft +1 -1
movement(self.x + -1, -1, self.y + -1, -1, True)
# downright -1 +1
movement(self.x + -1, -1, self.y + 1, 1, True)
# right 0 +1
movement(self.x + 0, 0, self.y + 1, 1, True)
# left 0 -1
movement(self.x + -1, -1, self.y + 0, 0, True)
# down +1 0
movement(self.x + 1, 1, self.y + 0, 0, True)
board()
player = input("Choose a coordinate: x1,y1 ").split(',')
x1, y1 = int(player[0]), int(player[1])
def move(self, inpx, inpy):
if self.a[inpx][inpy] == '❌':
self.moved = True
self.a[self.x][self.y] = de[self.x][
self.y] #previous location becomes empty
a[self.x][self.y] = de[self.x][
self.y] #previous location becomes empty in main board
self.x, self.y = inpx, inpy #object's new coords are inputs
self.a[inpx][inpy] = self
a[inpx][inpy] = self
for i in range(8):
for j in range(8):
if self.a[i][j] == '❌': #after movement return all Xs to normal
self.a[i][j] = de[i][j]
else:
print("invalid input") #Invalid input, return all Xs to normal
for i in range(8):
for j in range(8):
if self.a[i][j] == '❌':
self.a[i][j] = de[i][j]
def check_piece(x, y):
if a[x][y] not in ['⬜', '⬛']: #Needs optimization
if isinstance(a[x][y], Pawn):
#idea:
#if white or black:
#cycle through all pawns from 0 to 7
if a[x][y] == pawn0:
pawn0.short_poss_moves()
pawn0.move(x1, y1)
elif a[x][y] == pawn1:
pawn1.short_poss_moves()
pawn1.move(x1, y1)
elif a[x][y] == pawn2:
pawn2.short_poss_moves()
pawn2.move(x1, y1)
elif a[x][y] == pawn3:
pawn3.short_poss_moves()
pawn3.move(x1, y1)
elif a[x][y] == pawn4:
pawn4.short_poss_moves()
pawn4.move(x1, y1)
elif a[x][y] == pawn5:
pawn5.short_poss_moves()
pawn5.move(x1, y1)
elif a[x][y] == pawn6:
pawn6.short_poss_moves()
pawn6.move(x1, y1)
elif a[x][y] == pawn7:
pawn7.short_poss_moves()
pawn7.move(x1, y1)
elif isinstance(a[x][y], Rook):
if a[x][y] == rook0:
rook0.long_poss_moves()
rook0.move(x1, y1)
elif a[x][y] == rook1:
rook1.long_poss_moves()
rook1.move(x1, y1)
elif isinstance(a[x][y], Bishop):
if a[x][y] == bishop0:
bishop0.long_poss_moves()
bishop0.move(x1, y1)
elif a[x][y] == bishop1:
bishop1.long_poss_moves()
bishop1.move(x1, y1)
elif isinstance(a[x][y], Queen):
if a[x][y] == queen:
queen.long_poss_moves()
queen.move(x1, y1)
elif isinstance(a[x][y], Horse):
if a[x][y] == hrus:
hrus.poss_moves()
hrus.move(x1, y1)
elif a[x][y] == hrus1:
hrus1.poss_moves()
hrus1.move(x1, y1)
class Pawn(Pieces):
def __init__(self, x: int, y: int, color: str, board: list, emoji):
super().__init__(x, y, color, board, emoji)
self.moved = False
#once moved can only go up in ones
def short_poss_moves(self):
super().short_poss_moves()
if self.moved == False:
self.moved = True
movement(x - 2, -2, y, 0, True)
class Rook(Pieces):
def __init__(self, x: int, y: int, color: str, board: list, emoji):
super().__init__(x, y, color, board, emoji)
class Bishop(Pieces):
def __init__(self, x: int, y: int, color: str, board: list, emoji):
super().__init__(x, y, color, board, emoji)
class Queen(Pieces):
def __init__(self, x: int, y: int, color: str, board: list, emoji):
super().__init__(x, y, color, board, emoji)
class King(Pieces):
def __init__(self, x: int, y: int, color: str, board: list, emoji):
super().__init__(x, y, color, board, emoji)
self.moved = False
#once moved, cannot castle
class Horse(Pieces):
def __init__(self, x: int, y: int, color: str, board: list, emoji):
super().__init__(x, y, color, board, emoji)
def poss_moves(self): # Horse
global x1
global y1
#upright -2 1
movement(self.x + -2, -2, self.y + 1, 1, True)
#up left -2 -1
movement(self.x + -2, -2, self.y + -1, -1, True)
#down right 2 1
movement(self.x + 2, 2, self.y + 1, 1, True)
#down left 2 -1
movement(self.x + 2, 2, self.y + -1, -1, True)
#right up -1 2
movement(self.x + -1, -1, self.y + 2, 2, True)
#right down 1 +1
movement(self.x + 1, 1, self.y + 1, 1, True)
#left up -1 -2
movement(self.x + -1, -1, self.y + -2, -2, True)
#left down +1 -2
movement(self.x + 1, 1, self.y + -2, -2, True)
board()
player = input("Choose a coordinate: x1,y1 ").split(',')
x1, y1 = int(player[0]), int(player[1])
def board():
x = 0
print(" 0 12 34 5 67")
for i in range(8):
x += 1
print(i, end=" ")
for j in range(8):
print(a[i][j], end="")
print()
print('-✖')
a = [['⬛', '⬜', '⬛', '⬜', '⬛', '⬜', '⬛', '⬜'],
['⬜', '⬛', '⬜', '⬛', '⬜', '⬛', '⬜', '⬛'],
['⬛', '⬜', '⬛', '⬜', '⬛', '⬜', '⬛', '⬜'],
['⬜', '⬛', '⬜', '⬛', '⬜', '⬛', '⬜', '⬛'],
['⬛', '⬜', '⬛', '⬜', '⬛', '⬜', '⬛', '⬜'],
['⬜', '⬛', '⬜', '⬛', '⬜', '⬛', '⬜', '⬛'],
['⬛', '⬜', '⬛', '⬜', '⬛', '⬜', '⬛', '⬜'],
['⬜', '⬛', '⬜', '⬛', '⬜', '⬛', '⬜', '⬛']]
# test cases:
pawn0 = Pawn(5, 0, 'w', a, "♟")
pawn1 = Pawn(5, 1, 'w', a, "♟")
pawn2 = Pawn(5, 2, 'w', a, "♟")
pawn3 = Pawn(5, 3, 'w', a, "♟")
pawn4 = Pawn(5, 4, 'w', a, "♟")
pawn5 = Pawn(5, 5, 'w', a, "♟")
pawn6 = Pawn(5, 6, 'w', a, "♟")
pawn7 = Pawn(5, 7, 'w', a, "♟")
rook0 = Rook(7, 0, 'w', a, "♜")
rook1 = Rook(7, 7, 'w', a, "♜")
bishop0 = Bishop(7, 1, "w", a, "♗")
bishop1 = Bishop(7, 6, "w", a, "♗")
queen = Queen(2, 3, "w", a, "♕")
king = King(7, 4, "w ", a, "♔")
hrus = Horse(7, 2, 'w', a, "♞")
hrus1 = Horse(7, 5, 'w', a, "♞")
board()
print()
while True:
player = input("Choose a coordinate: x,y ").split(',')
x, y = int(player[0]), int(player[1])
while 0 > x or x > 7 or y > 7 or 0 > y:
player = input("Choose a coordinate: x,y maximum 7").split(',')
x, y = int(player[0]), int(player[1])
#Check piece func
check_piece(x, y)
board()