-
Notifications
You must be signed in to change notification settings - Fork 0
/
Chess.py
227 lines (172 loc) · 7.05 KB
/
Chess.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# Chess Library
from Utility import *
class Chess:
# def __init__(self, board):
# self.board = board #create board
def setBoardPosition(self, board):
self.board = board
## This function is point of interaction for any type of chess movement
def makeMove(self, pieceType, posFrom, posTo):
_from = Utility.stringToIndexies(posFrom)
_to = Utility.stringToIndexies(posTo)
isValid = False
if pieceType[1] == 'P':
isValid = self.pawnMove(pieceType, _from, _to)
if pieceType[1] == 'Q':
isValid = self.queenMove(pieceType, _from, _to)
if pieceType[1] == 'R':
isValid = self.rookMove(pieceType, _from, _to)
if pieceType[1] == 'H':
isValid = self.knightMove(pieceType, _from, _to)
if pieceType[1] == 'K':
isValid = self.kingMove(pieceType, _from, _to)
if pieceType[1] == 'B':
isValid = self.bishopsMove(pieceType, _from, _to)
## If move is valid then make move
if isValid:
self.doMove(pieceType, _from, _to)
####################################
def knightMove(self, pieceType, posFrom, posTo):
if Utility.checkPosAreDiagonal(posFrom, posTo):
print(pieceType, "Cannot make diagonal position move")
return False
if Utility.isStraightMove(posFrom, posTo):
print(pieceType, "Cannot make straight position move")
return False
if not self.isValidMove(pieceType, posFrom, posTo):
return False
if not Utility.isKnightMove(posFrom, posTo):
print(pieceType, "movement is not valid")
return False
return True
######################## Queen Move ################################
def queenMove(self, pieceType, posFrom, posTo):
if not Utility.checkPosAreDiagonal(posFrom, posTo) or not Utility.isStraightMove(posFrom, posTo):
print(pieceType, "Cannot make move other than diagonal or straight position")
return False
if not self.isValidMove(pieceType, posFrom, posTo):
return False
return True
######################## Pawn ################################
def pawnMove(self, pieceType, posFrom, posTo):
if not self.isValidMove(pieceType, posFrom, posTo):
return False
if Utility.checkPosAreDiagonal(posFrom, posTo) and self.posIsEmpty(posTo):
print("Pawn Cannot make move on empty diagonal position")
return False
if not Utility.checkPosAreDiagonal(posFrom, posTo) and not self.posIsEmpty(posTo):
print("Pawn Cannot make straight move on non-empty position")
return False
## Conditon check for pawn back movement
isWhite = Utility.isWhite(pieceType)
if (isWhite and posTo[0] < posFrom[0]) or (not isWhite and posTo[0] > posFrom[0]):
print("Pawn cannot move back")
return False
if Utility.distanceBetweenTwoPositon(posFrom, posTo) != 1 and not self.isPeiceOnBaseLoc(pieceType, posFrom):
print("Pawn cannot move than one step from given position")
return False
if self.isPeiceOnBaseLoc(pieceType, posFrom):
if Utility.distanceBetweenTwoPositon(posFrom, posTo) == 2 and not self.posIsEmpty(posTo):
print("Pawn cannot move from given position")
return False
elif Utility.distanceBetweenTwoPositon(posFrom, posTo) > 2:
print("Pawn cannot move more than one or two step from base location")
return True
######################## Rook ################################
def rookMove(self, pieceType, posFrom, posTo):
if Utility.checkPosAreDiagonal(posFrom, posTo):
print(pieceType, "Cannot move random position")
return False
if not self.isValidMove(pieceType, posFrom, posTo):
return False
return True
######################## Bishops ################################
def bishopsMove(self, pieceType, posFrom, posTo):
if not Utility.checkPosAreDiagonal(posFrom, posTo):
print(pieceType, "Cannot move straight position")
return False
if not self.isValidMove(pieceType, posFrom, posTo):
return False
return True
######################## King ################################
def kingMove(pieceType, posFrom, posTo):
if not self.isValidMove(pieceType, posFrom, posTo):
return False
if Utility.distanceBetweenTwoPositon(posFrom, posTo) != 1:
return False
return True
def doMove(self, pieceType, fromPos, toPos):
self.board[fromPos[0]][fromPos[1]] = "--"
self.board[toPos[0]][toPos[1]] = pieceType
Utility.printBoard(self.board)
################################################
# Check is given peice at Base Location or not #
################################################
def isPeiceOnBaseLoc(self, pieceType, fromPos):
if pieceType == 'WP' and fromPos[0] == 1:
return True
elif pieceType[0] == 'W' and fromPos[0] == 0:
return True
if pieceType == 'BP' and fromPos[0] == 6:
return True
elif pieceType[0] == 'B' and fromPos[0] == 7:
return True
return False
##########################################
# Checking is empty space at given space #
##########################################
def posIsEmpty(self, pos):
if self.board[pos[0]][pos[1]] == "--":
return True
else:
return False
##########################################################
# Checking is some common move validation at given space #
##########################################################
def isValidMove(self, pieceType, fromPos, toPos):
if self.posIsEmpty(fromPos):
print("Cannot move empty peice")
return False
if toPos[0] < 0 or toPos[1] < 0:
print(pieceType, "cannot move since Destination position cannot be negative")
return False
if toPos[0] > 7 or toPos[1] > 7:
print(pieceType, "cannot move since Destination Position is out of board")
return False
if Utility.isWhite(pieceType) == Utility.isWhite(self.getPeiceAt(toPos)):
print(pieceType, "Cannot make move since same color peice at destination")
return False
if pieceType[1] != 'H' and ( self.IsPieceInBtwMoves(fromPos, toPos) or Utility.isKnightMove(fromPos, toPos)):
print(pieceType,"Cannot jump over the peice")
return False
return True
###################################################################
# In this method we checking that, is there any peice in between #
# the from and to position #
###################################################################
def IsPieceInBtwMoves(self, fromPos, toPos):
i = j = 0
if fromPos[0] > toPos[0]:
i = -1
if fromPos[0] < toPos[0]:
i = 1
if fromPos[1] > toPos[1]:
j = -1
if fromPos[1] < toPos[1]:
j = 1
x = fromPos[0] + i
y = fromPos[1] + j
# we travese each each postion till they reached in target position or till any non-empty peice found in between
while x != toPos[0] or y != toPos[1]:
if not self.posIsEmpty((x,y)):
return True
if x != toPos[0]:
x += i
if y != toPos[1]:
y += j
return False
######################################
# Get peice name at given position #
######################################
def getPeiceAt(self, pos):
return self.board[pos[0]][pos[1]]