-
Notifications
You must be signed in to change notification settings - Fork 261
/
Copy pathChessGame.py
166 lines (163 loc) · 6.72 KB
/
ChessGame.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
# This is a chess game made in Python CLI
def check(ChessBoard):
z=-1
x=-1
for i in range(8):
chessboard=ChessBoard[i]
try:
z=chessboard.index('♚')
except:
continue
for i in range(8):
chessboard=ChessBoard[i]
try:
x=chessboard.index('♔')
except:
continue
if(z>=0 and x>=0):
return 1
else:
return 0
def pawn(ChessBoard,a,b,c,d):
if(a-c==b-d):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
elif(a-c==1 and b==d):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
elif(a-c==d-b):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
def bishop(ChessBoard,a,b,c,d):
if(a-c==b-d):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
elif(a-c==d-b):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
elif(c-a==b-d):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
elif(c-a==d-b):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
def rook(ChessBoard,a,b,c,d):
if(a==c):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
elif(b==d):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
def king(ChessBoard,a,b,c,d):
if(a==c and b-d==1):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
elif(a==c and d-b==1):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
elif(b==d and a-c==1):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
elif(b==d and c-a==1):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
elif(a-c==1 and b-d==1):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
elif(a-c==1 and d-b==1):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
elif(c-a==1 and b-d==1):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
elif(c-a==1 and d-b==1):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
def queen(ChessBoard,a,b,c,d):
if(a==c):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
elif(b==d):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
elif(a-c==b-d):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
elif(a-c==d-b):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
elif(c-a==b-d):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
elif(c-a==d-b):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
def knight(ChessBoard,a,b,c,d):
if(a-c==1 and d-b==2):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
elif(c-a==1 and d-b==2):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
elif(c-a==2 and b-d==1):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
elif(c-a==2 and d-b==1):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
elif(c-a==1 and b-d==2):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
elif(a-c==1 and b-d==2):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
elif(a-c==2 and b-d==1):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
elif(a-c==2 and d-b==1):
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
def printchess(ChessBoard):
print("",ChessBoard[0][0],"|",ChessBoard[0][1],"|",ChessBoard[0][2],"|",ChessBoard[0][3],"|",ChessBoard[0][4],"|",ChessBoard[0][5],"|",ChessBoard[0][6],"|",ChessBoard[0][7])
print("--------------------------------")
print("",ChessBoard[1][0],"|",ChessBoard[1][1],"|",ChessBoard[1][2],"|",ChessBoard[1][3],"|",ChessBoard[1][4],"|",ChessBoard[1][5],"|",ChessBoard[1][6],"|",ChessBoard[1][7])
print("---------------------------------")
print("",ChessBoard[2][0],"|",ChessBoard[2][1],"|",ChessBoard[2][2],"|",ChessBoard[2][3],"|",ChessBoard[2][4],"|",ChessBoard[2][5],"|",ChessBoard[2][6],"|",ChessBoard[2][7])
print("---------------------------------")
print("",ChessBoard[3][0],"|",ChessBoard[3][1],"|",ChessBoard[3][2],"|",ChessBoard[3][3],"|",ChessBoard[3][4],"|",ChessBoard[3][5],"|",ChessBoard[3][6],"|",ChessBoard[3][7])
print("---------------------------------")
print("",ChessBoard[4][0],"|",ChessBoard[4][1],"|",ChessBoard[4][2],"|",ChessBoard[4][3],"|",ChessBoard[4][4],"|",ChessBoard[4][5],"|",ChessBoard[4][6],"|",ChessBoard[4][7])
print("---------------------------------")
print("",ChessBoard[5][0],"|",ChessBoard[5][1],"|",ChessBoard[5][2],"|",ChessBoard[5][3],"|",ChessBoard[5][4],"|",ChessBoard[5][5],"|",ChessBoard[5][6],"|",ChessBoard[5][7])
print("---------------------------------")
print("",ChessBoard[6][0],"|",ChessBoard[6][1],"|",ChessBoard[6][2],"|",ChessBoard[6][3],"|",ChessBoard[6][4],"|",ChessBoard[6][5],"|",ChessBoard[6][6],"|",ChessBoard[6][7])
print("---------------------------------")
print("",ChessBoard[7][0],"|",ChessBoard[7][1],"|",ChessBoard[7][2],"|",ChessBoard[7][3],"|",ChessBoard[7][4],"|",ChessBoard[7][5],"|",ChessBoard[7][6],"|",ChessBoard[7][7])
ChessBoard=[['♜','♞','♝','♛','♚','♝','♞','♜'],['♟','♟','♟','♟','♟','♟','♟','♟'],[' ',' ',' ',' ',' ',' ',' ',' '],[' ',' ',' ',' ',' ',' ',' ',' '],[' ',' ',' ',' ',' ',' ',' ',' '],[' ',' ',' ',' ',' ',' ',' ',' '],['♙','♙','♙','♙','♙','♙','♙','♙'],['♖','♘','♗','♕','♔','♗','♘','♖']]
printchess(ChessBoard)
s=check(ChessBoard)
print("White's turn is first and then Black's and then the game continues until any one of the kings is dead")
while(s==1):
a=int(input("enter initial row"))
b=int(input("enter initial column "))
c=int(input("enter final row "))
d=int(input("enter final column "))
if( ChessBoard[a][b]=='♙'):
pawn(ChessBoard,a,b,c,d)
elif(ChessBoard[a][b]=='♜' or ChessBoard[a][b]=='♖'):
rook(ChessBoard,a,b,c,d)
elif(ChessBoard[a][b]=='♞' or ChessBoard[a][b]=='♘'):
knight(ChessBoard,a,b,c,d)
elif(ChessBoard[a][b]=='♝' or ChessBoard[a][b]=='♗'):
bishop(ChessBoard,a,b,c,d)
elif(ChessBoard[a][b]=='♚' or ChessBoard[a][b]=='♔'):
king(ChessBoard,a,b,c,d)
elif(ChessBoard[a][b]=='♛' or ChessBoard[a][b]=='♕'):
queen(ChessBoard,a,b,c,d)
else:
ChessBoard[c][d]=ChessBoard[a][b]
ChessBoard[a][b]=' '
printchess(ChessBoard)
s=check(ChessBoard)
if(s==0):
print("Game ends")