-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpawnMove.py
101 lines (88 loc) · 3.56 KB
/
pawnMove.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
def move(board, pos, target):
posx=int(ord(pos[0].lower())-97)
posy=8-int(pos[1])
targetx=int(ord(target[0].lower())-97)
targety=8-int(target[1])
pawn = board[posy][posx]
board[posy][posx]=0
board[targety][targetx]=pawn
return board
def getPawnPositions(board):
pawnPositions = []
for i,row in enumerate(board):
for index,column in enumerate(row):
if column==1 or column==3:
pawnPositions.append([i,index])
return pawnPositions
def checkPromotions(board):
pawnPositions = getPawnPositions(board)
for pawn in pawnPositions:
if pawn[0]==0:
board[pawn[0]][pawn[1]]=3
return board
def checkCaptures(board):
pawnPositions = getPawnPositions(board)
possibleCaptures = set()
for pawn in pawnPositions:
p = board[pawn[0]][pawn[1]]
if pawn[0]>1:
if pawn[1]>1:
if (board[pawn[0]-1][pawn[1]-1] == 2 or board[pawn[0]-1][pawn[1]-1] == 4) and board[pawn[0]-2][pawn[1]-2] == 0:
capture =(str(chr(pawn[1]+97))+ str(8-pawn[0]),str(chr(pawn[1]-2+97))+ str(8-pawn[0]+2))
possibleCaptures.add(capture)
if pawn[1]<6:
if (board[pawn[0]-1][pawn[1]+1] == 2 or board[pawn[0]-1][pawn[1]+1] == 4) and board[pawn[0]-2][pawn[1]+2] == 0:
capture =(str(chr(pawn[1]+97))+ str(8-pawn[0]), str(chr(pawn[1]+2+97))+ str(8-pawn[0]+2))
possibleCaptures.add(capture)
if p == 3:
if pawn[0]<6:
if pawn[1]>1:
if (board[pawn[0]+1][pawn[1]-1] == 2 or board[pawn[0]+1][pawn[1]-1] == 4) and board[pawn[0]+2][pawn[1]-2] == 0:
capture =(str(chr(pawn[1]+97))+ str(8-pawn[0]),str(chr(pawn[1]-2+97))+ str(8-pawn[0]-2))
possibleCaptures.add(capture)
if pawn[1]<6:
if (board[pawn[0]+1][pawn[1]+1] == 2 or board[pawn[0]+1][pawn[1]+1] == 4) and board[pawn[0]+2][pawn[1]+2] == 0:
capture =(str(chr(pawn[1]+97))+ str(8-pawn[0]), str(chr(pawn[1]+2+97))+ str(8-pawn[0]-2))
possibleCaptures.add(capture)
return possibleCaptures
def capture(board,pos,target):
posx=int(ord(pos[0].lower())-97)
posy=8-int(pos[1])
targetx=int(ord(target[0].lower())-97)
targety=8-int(target[1])
capturex=int((posx+targetx)/2)
capturey=int((posy+targety)/2)
board[capturey][capturex]=0
return board
def checkPos(board,pos):
posx=int(ord(pos[0].lower())-97)
posy=8-int(pos[1])
if board[posy][posx]==1 or board[posy][posx]==3:
return True
else:
return False
def checkMovesWhitePawn(board,pos):
possibleMoves = set()
posx=int(ord(pos[0].lower())-97)
posy=8-int(pos[1])
pawn = board[posy][posx]
if posy>0:
if posx>0 and board[posy-1][posx-1]==0:
targetx=chr(posx-1+97)
targety=8-posy+1
possibleMoves.add(targetx+str(targety))
if posx<7 and board[posy-1][posx+1]==0:
targetx=chr(posx+1+97)
targety=8-posy+1
possibleMoves.add(targetx+str(targety))
if pawn == 3:
if posy<7:
if posx>0 and board[posy+1][posx-1]==0:
targetx=chr(posx-1+97)
targety=8-posy-1
possibleMoves.add(targetx+str(targety))
if posx<7 and board[posy+1][posx+1]==0:
targetx=chr(posx+1+97)
targety=8-posy-1
possibleMoves.add(targetx+str(targety))
return possibleMoves