Skip to content

Commit a61730f

Browse files
authored
Create ChessGame.py
1 parent 2c9bdb1 commit a61730f

File tree

1 file changed

+166
-0
lines changed

1 file changed

+166
-0
lines changed

Diff for: ChessGame.py

+166
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,166 @@
1+
# This is a chess game made in Python CLI
2+
def check(ChessBoard):
3+
z=-1
4+
x=-1
5+
for i in range(8):
6+
chessboard=ChessBoard[i]
7+
try:
8+
z=chessboard.index('♚')
9+
except:
10+
continue
11+
for i in range(8):
12+
chessboard=ChessBoard[i]
13+
try:
14+
x=chessboard.index('♔')
15+
except:
16+
continue
17+
if(z>=0 and x>=0):
18+
return 1
19+
else:
20+
return 0
21+
def pawn(ChessBoard,a,b,c,d):
22+
if(a-c==b-d):
23+
ChessBoard[c][d]=ChessBoard[a][b]
24+
ChessBoard[a][b]=' '
25+
elif(a-c==1 and b==d):
26+
ChessBoard[c][d]=ChessBoard[a][b]
27+
ChessBoard[a][b]=' '
28+
elif(a-c==d-b):
29+
ChessBoard[c][d]=ChessBoard[a][b]
30+
ChessBoard[a][b]=' '
31+
def bishop(ChessBoard,a,b,c,d):
32+
if(a-c==b-d):
33+
ChessBoard[c][d]=ChessBoard[a][b]
34+
ChessBoard[a][b]=' '
35+
elif(a-c==d-b):
36+
ChessBoard[c][d]=ChessBoard[a][b]
37+
ChessBoard[a][b]=' '
38+
elif(c-a==b-d):
39+
ChessBoard[c][d]=ChessBoard[a][b]
40+
ChessBoard[a][b]=' '
41+
elif(c-a==d-b):
42+
ChessBoard[c][d]=ChessBoard[a][b]
43+
ChessBoard[a][b]=' '
44+
def rook(ChessBoard,a,b,c,d):
45+
if(a==c):
46+
ChessBoard[c][d]=ChessBoard[a][b]
47+
ChessBoard[a][b]=' '
48+
elif(b==d):
49+
ChessBoard[c][d]=ChessBoard[a][b]
50+
ChessBoard[a][b]=' '
51+
def king(ChessBoard,a,b,c,d):
52+
if(a==c and b-d==1):
53+
ChessBoard[c][d]=ChessBoard[a][b]
54+
ChessBoard[a][b]=' '
55+
elif(a==c and d-b==1):
56+
ChessBoard[c][d]=ChessBoard[a][b]
57+
ChessBoard[a][b]=' '
58+
elif(b==d and a-c==1):
59+
ChessBoard[c][d]=ChessBoard[a][b]
60+
ChessBoard[a][b]=' '
61+
elif(b==d and c-a==1):
62+
ChessBoard[c][d]=ChessBoard[a][b]
63+
ChessBoard[a][b]=' '
64+
elif(a-c==1 and b-d==1):
65+
ChessBoard[c][d]=ChessBoard[a][b]
66+
ChessBoard[a][b]=' '
67+
elif(a-c==1 and d-b==1):
68+
ChessBoard[c][d]=ChessBoard[a][b]
69+
ChessBoard[a][b]=' '
70+
elif(c-a==1 and b-d==1):
71+
ChessBoard[c][d]=ChessBoard[a][b]
72+
ChessBoard[a][b]=' '
73+
elif(c-a==1 and d-b==1):
74+
ChessBoard[c][d]=ChessBoard[a][b]
75+
ChessBoard[a][b]=' '
76+
def queen(ChessBoard,a,b,c,d):
77+
if(a==c):
78+
ChessBoard[c][d]=ChessBoard[a][b]
79+
ChessBoard[a][b]=' '
80+
elif(b==d):
81+
ChessBoard[c][d]=ChessBoard[a][b]
82+
ChessBoard[a][b]=' '
83+
elif(a-c==b-d):
84+
ChessBoard[c][d]=ChessBoard[a][b]
85+
ChessBoard[a][b]=' '
86+
elif(a-c==d-b):
87+
ChessBoard[c][d]=ChessBoard[a][b]
88+
ChessBoard[a][b]=' '
89+
elif(c-a==b-d):
90+
ChessBoard[c][d]=ChessBoard[a][b]
91+
ChessBoard[a][b]=' '
92+
elif(c-a==d-b):
93+
ChessBoard[c][d]=ChessBoard[a][b]
94+
ChessBoard[a][b]=' '
95+
def knight(ChessBoard,a,b,c,d):
96+
if(a-c==1 and d-b==2):
97+
ChessBoard[c][d]=ChessBoard[a][b]
98+
ChessBoard[a][b]=' '
99+
elif(c-a==1 and d-b==2):
100+
ChessBoard[c][d]=ChessBoard[a][b]
101+
ChessBoard[a][b]=' '
102+
elif(c-a==2 and b-d==1):
103+
ChessBoard[c][d]=ChessBoard[a][b]
104+
ChessBoard[a][b]=' '
105+
elif(c-a==2 and d-b==1):
106+
ChessBoard[c][d]=ChessBoard[a][b]
107+
ChessBoard[a][b]=' '
108+
elif(c-a==1 and b-d==2):
109+
ChessBoard[c][d]=ChessBoard[a][b]
110+
ChessBoard[a][b]=' '
111+
elif(a-c==1 and b-d==2):
112+
ChessBoard[c][d]=ChessBoard[a][b]
113+
ChessBoard[a][b]=' '
114+
elif(a-c==2 and b-d==1):
115+
ChessBoard[c][d]=ChessBoard[a][b]
116+
ChessBoard[a][b]=' '
117+
elif(a-c==2 and d-b==1):
118+
ChessBoard[c][d]=ChessBoard[a][b]
119+
ChessBoard[a][b]=' '
120+
def printchess(ChessBoard):
121+
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])
122+
print("--------------------------------")
123+
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])
124+
print("---------------------------------")
125+
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])
126+
print("---------------------------------")
127+
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])
128+
print("---------------------------------")
129+
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])
130+
print("---------------------------------")
131+
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])
132+
print("---------------------------------")
133+
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])
134+
print("---------------------------------")
135+
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])
136+
137+
138+
ChessBoard=[['♜','♞','♝','♛','♚','♝','♞','♜'],['♟','♟','♟','♟','♟','♟','♟','♟'],[' ',' ',' ',' ',' ',' ',' ',' '],[' ',' ',' ',' ',' ',' ',' ',' '],[' ',' ',' ',' ',' ',' ',' ',' '],[' ',' ',' ',' ',' ',' ',' ',' '],['♙','♙','♙','♙','♙','♙','♙','♙'],['♖','♘','♗','♕','♔','♗','♘','♖']]
139+
140+
printchess(ChessBoard)
141+
s=check(ChessBoard)
142+
print("White's turn is first and then Black's and then the game continues until any one of the kings is dead")
143+
while(s==1):
144+
a=int(input("enter initial row"))
145+
b=int(input("enter initial column "))
146+
c=int(input("enter final row "))
147+
d=int(input("enter final column "))
148+
if( ChessBoard[a][b]=='♙'):
149+
pawn(ChessBoard,a,b,c,d)
150+
elif(ChessBoard[a][b]=='♜' or ChessBoard[a][b]=='♖'):
151+
rook(ChessBoard,a,b,c,d)
152+
elif(ChessBoard[a][b]=='♞' or ChessBoard[a][b]=='♘'):
153+
knight(ChessBoard,a,b,c,d)
154+
elif(ChessBoard[a][b]=='♝' or ChessBoard[a][b]=='♗'):
155+
bishop(ChessBoard,a,b,c,d)
156+
elif(ChessBoard[a][b]=='♚' or ChessBoard[a][b]=='♔'):
157+
king(ChessBoard,a,b,c,d)
158+
elif(ChessBoard[a][b]=='♛' or ChessBoard[a][b]=='♕'):
159+
queen(ChessBoard,a,b,c,d)
160+
else:
161+
ChessBoard[c][d]=ChessBoard[a][b]
162+
ChessBoard[a][b]=' '
163+
printchess(ChessBoard)
164+
s=check(ChessBoard)
165+
if(s==0):
166+
print("Game ends")

0 commit comments

Comments
 (0)