Skip to content

Commit 60b42f2

Browse files
committedSep 1, 2021
last commit
1 parent b5e912d commit 60b42f2

15 files changed

+223
-39
lines changed
 

‎Bishop.py

+11-2
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,18 @@
33

44

55
class Bishop(Piece):
6+
'''
7+
Bishop Class inherited from Piece
8+
'''
69

7-
def canMove(self,board,x,y): #weather bishop can move to x,y or not
8-
if abs(self.x-x)==abs(self.y-y): #diagonal move check
10+
def canMove(self,board,x,y):
11+
'''
12+
This func will return weather a bishop can move to provided x,y or not
13+
Input: Board and targer co-ordinates
14+
Output: Boolean result
15+
'''
16+
#diagonal move check
17+
if abs(self.x-x)==abs(self.y-y):
918
return True
1019
else:
1120
return False

‎Board.py

+90-6
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,25 @@
11

2+
from Knight import Knight
3+
from Rock import Rock
4+
from Pawn import Pawn
5+
from King import King
6+
from Queen import Queen
7+
from Bishop import Bishop
28
from Box import Box
39
from Piece import Piece
410
from termcolor import colored
511

612

713
class Board:
14+
'''
15+
This class has board object consisting of boxes and pieces
16+
'''
817
grid=[]
918
def __init__(self):
19+
'''
20+
This func will initialize all black and white boxes and place all pieces on it
21+
'''
22+
#initialize board with alternate box color
1023
color=1
1124
for i in range(8):
1225
templist=[]
@@ -20,19 +33,87 @@ def __init__(self):
2033
if j!=7:
2134
color=3-color
2235
self.grid.append(templist)
36+
37+
#insert all black pieces to board
38+
self.grid[0][0].piece=Rock('BR','Black',0,0)
39+
self.grid[0][1].piece=Knight('BK','Black',0,1)
40+
self.grid[0][2].piece=Bishop('BB','Black',0,2)
41+
self.grid[0][3].piece=Queen('QB','Black',0,3)
42+
self.grid[0][4].piece=King('KB','Black',0,4)
43+
self.grid[0][5].piece=Bishop('BB','Black',0,5)
44+
self.grid[0][6].piece=Knight('BK','Black',0,6)
45+
self.grid[0][7].piece=Rock('BR','Black',0,7)
46+
for i in range(8):
47+
self.grid[1][i].piece=Pawn('BP','Black',1,i)
2348

49+
#insert all white pieces to board
50+
self.grid[7][0].piece=Rock('WR','White',7,0)
51+
self.grid[7][1].piece=Knight('WK','White',7,1)
52+
self.grid[7][2].piece=Bishop('WB','White',7,2)
53+
self.grid[7][3].piece=Queen('QW','White',7,3)
54+
self.grid[7][4].piece=King('KW','White',7,4)
55+
self.grid[7][5].piece=Bishop('WB','White',7,5)
56+
self.grid[7][6].piece=Knight('WK','White',7,6)
57+
self.grid[7][7].piece=Rock('WR','White',7,7)
58+
for i in range(8):
59+
self.grid[6][i].piece=Pawn('WP','White',6,i)
60+
2461

2562

2663
def putPiece(self,piece):
64+
'''
65+
This func will place given piece on a box in board
66+
Input:Piece Object
67+
'''
68+
#creating box object using given piece
2769
toPut=Box(self.grid[piece.x][piece.y].color,piece)
2870
self.grid[piece.x][piece.y]=toPut
2971

3072
def getPiece(self,x,y):
31-
return self.grid[x][y].piece
73+
'''
74+
This func will return piece object based on given co-ordinates
75+
Input: X and Y co-ordinates
76+
Output: Piece object placed on given X,Y
77+
'''
78+
return self.grid[x][y].piece
79+
80+
def isKingKilled(self,turn):
81+
'''
82+
This func will return weather opponent king is killed or not
83+
Input: Opponent Player's turn
84+
Output: Boolean result
85+
'''
86+
87+
#initialize assuming kings are killed
88+
whiteKing=True
89+
blackKing=True
90+
#loop to check all board pieces for existence of kings
91+
for i in range(8):
92+
for j in range(8):
93+
if self.grid[i][j].piece!='':
94+
if self.grid[i][j].piece.namee=='KB':
95+
blackKing=False
96+
if self.grid[i][j].piece.namee=='KW':
97+
whiteKing=False
98+
#output
99+
if turn==1:
100+
return whiteKing
101+
elif turn==2:
102+
return blackKing
103+
32104

33105
def movePiece(self,piece,x,y):
34-
color=self.grid[piece.x][piece.y].color #maintain box color
35-
self.grid[piece.x][piece.y]=Box(color,'') #place empty box
106+
'''
107+
This func will move given piece to target co-ordinate killing any piece along way
108+
Input: Piece object,X and Y co-ordinates
109+
'''
110+
#maintain box color
111+
color=self.grid[piece.x][piece.y].color
112+
113+
#place empty box
114+
self.grid[piece.x][piece.y]=Box(color,'')
115+
116+
#place given piece to provided co-ordinates and kill
36117
color=self.grid[x][y].color
37118
piece.x=x
38119
piece.y=y
@@ -44,14 +125,17 @@ def movePiece(self,piece,x,y):
44125

45126

46127
def printBoard(self):
128+
'''
129+
This func will print board and show all pieces on their positions
130+
'''
47131
for i in range(8):
48132
for j in range(8):
49133
if self.grid[i][j].piece!='':
50-
print(self.grid[i][j].piece.namee,end='')
134+
print(' '+self.grid[i][j].piece.namee + ' ',end='')
51135
elif self.grid[i][j].color=='B':
52-
print(colored('__','grey'),end='')
136+
print(colored(' __ ','grey'),end='')
53137
elif self.grid[i][j].color=='W':
54-
print(colored('__','blue'),end='')
138+
print(colored(' __ ','blue'),end='')
55139
#print(self.grid[i][j].color + ' ',end='')
56140
print('')
57141

‎Game.py

+74-17
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11

2+
from Pawn import Pawn
23
from Queen import Queen
34
from Player import Player
45
from Board import Board
56
class Game:
7+
'''
8+
Main Class having all game logic
9+
'''
610

711
def canQueenKill(self):
812
print('Welcome to Chess by Mujahid')
@@ -22,33 +26,86 @@ def canQueenKill(self):
2226
print('No They can not attack')
2327

2428
def play(self):
29+
'''
30+
Main func creating game flow and having all game objects,rules etc
31+
'''
2532
print('Welcome to Chess by Mujahid')
33+
34+
#get user names
2635
print('Player 1 Please Enter your name')
2736
name1=input()
2837
print('Player 2 Please Enter your name')
2938
name2=input()
39+
40+
#initialze player objects
3041
player1=Player(name1,0)
3142
player2=Player(name2,0)
3243
print(player1.name + ' and ' +player2.name +' are playing now')
33-
board=Board() #initialize Board
34-
board.putPiece(Queen('WQ','White',2,2))#just for testing
44+
45+
#initialize Board
46+
board=Board()
47+
#set turn for white player
3548
turn=1
36-
#game started
49+
50+
#game loop started
3751
while True:
38-
if turn==1:
39-
print(player1.name + "'s Turn")
40-
elif turn==2:
41-
print(player2.name + "'s Turn")
42-
board.printBoard()
43-
print('Please enter "X1,Y1,X2,Y2" ')
44-
x1,y1,x2,y2=input().split(',')
45-
x1,y1,x2,y2=int(x1),int(y1),int(x2),int(y2)
46-
currentPiece=board.getPiece(x1,y1)
47-
if currentPiece.canMove(board,x2,y2):
48-
board.movePiece(currentPiece,x2,y2)
49-
turn=3-turn#changing turn
50-
else:
51-
print('Not a valid move. Try again')
52+
try:
53+
if turn==1:
54+
print(player1.name + "'s Turn (white)")
55+
elif turn==2:
56+
print(player2.name + "'s Turn (black)")
57+
58+
#show current board with all pieces
59+
board.printBoard()
60+
61+
#gettig user input for source and target position
62+
print('Please enter "X1,Y1,X2,Y2" ')
63+
x1,y1,x2,y2=input().split(',')
64+
x1,y1,x2,y2=int(x1),int(y1),int(x2),int(y2)
65+
66+
#get current and target piece from board based on given x,y
67+
currentPiece=board.getPiece(x1,y1)
68+
targetPiece=board.getPiece(x2,y2)
69+
70+
#check if target box has same color piece
71+
if targetPiece!='' and currentPiece.color==targetPiece.color:
72+
print('Cant kill your own soldier')
73+
continue
74+
75+
#getting type of Piece
76+
pieceType=type(currentPiece)
77+
78+
#pawn spcific check as pawn can only move forward
79+
if pieceType==Pawn:
80+
if currentPiece.canMove(board,x2,y2,turn):
81+
board.movePiece(currentPiece,x2,y2)
82+
#game end condition check
83+
if board.isKingKilled(3-turn)==True:
84+
break
85+
#changing turn
86+
turn=3-turn
87+
else:
88+
print('Not a valid move. Try again')
89+
90+
#for all other pieces
91+
elif currentPiece.canMove(board,x2,y2):
92+
board.movePiece(currentPiece,x2,y2)
93+
#game end condition check
94+
if board.isKingKilled(3-turn)==True:
95+
break
96+
#changing turn
97+
turn=3-turn
98+
else:
99+
print('Not a valid move. Try again')
100+
101+
except Exception as e:
102+
print(e)
103+
104+
#ending game and show winner
105+
if turn==1:
106+
print(player1.name + " Won (white)")
107+
elif turn==2:
108+
print(player2.name + " Won (black)")
52109

53110

54111

‎King.py

+13-3
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,22 @@
33

44

55
class King(Piece):
6+
'''
7+
King Class inherited from Piece
8+
'''
69

7-
def canMove(self,board,x,y): #weather a king can move to x,y or not
8-
pointDffrnc=abs(self.x-x-self.y-y) #diff of src(x,y) and trgt(x,y)
10+
def canMove(self,board,x,y):
11+
'''
12+
This func will return weather a king can move to provided x,y or not
13+
Input: Board and targer co-ordinates
14+
Output: Boolean result
15+
'''
16+
#diff of src(x,y) and trgt(x,y)
17+
pointDffrnc=abs(self.x-x)+abs(self.y-y)
918
if pointDffrnc==1:
1019
return True
11-
elif abs(self.x-x)==abs(self.y-y)==1: #diagonal difference of 1
20+
#diagonal difference of 1
21+
elif abs(self.x-x)==abs(self.y-y)==1:
1222
return True
1323
else:
1424
return False

‎Pawn.py

+14-6
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,18 @@
44

55
class Pawn(Piece):
66

7-
def canMove(self,board,x,y):
7+
def canMove(self,board,x,y,turn):
88
sourceX,sourceY,targetX,targetY=self.x,self.y,x,y
9-
if sourceX == targetX and sourceX < targetX and ((sourceX - targetX) == 1 or (sourceX - targetX) == 2):
10-
print("true")
11-
return True
12-
else:
13-
return False
9+
10+
if turn==1:
11+
if sourceX > targetX and (abs(sourceX - targetX) == 1 or abs(sourceX - targetX) == 2):
12+
return True
13+
else:
14+
return False
15+
16+
elif turn==2:
17+
if sourceX < targetX and (abs(sourceX - targetX) == 1 or abs(sourceX - targetX) == 2):
18+
return True
19+
else:
20+
return False
21+

‎Queen.py

+19-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,13 @@
33

44

55
class Queen(Piece):
6+
'''
7+
Queen Class inherited from Piece
8+
'''
69
def canAttack(self,board,piece):
10+
'''
11+
Bonus func to check weather a queen can kill other queen
12+
'''
713
sourceX,sourceY,targetX,targetY=self.x,self.y,piece.x,piece.y
814
if sourceX==targetX or sourceY==targetY:
915
return True
@@ -13,13 +19,21 @@ def canAttack(self,board,piece):
1319
return False
1420

1521

16-
def canMove(self,board,x,y): #weather a queen can move to x,y or not
22+
def canMove(self,board,x,y):
23+
'''
24+
This func will return weather a queen can move to provided x,y or not
25+
Input: Board and targer co-ordinates
26+
Output: Boolean result
27+
'''
1728
sourceX,sourceY,targetX,targetY=self.x,self.y,x,y
18-
if sourceX==targetX and sourceY!=targetY: #move in same row
29+
#move in same row
30+
if sourceX==targetX and sourceY!=targetY:
1931
return True
20-
elif sourceX!=targetX and sourceY==targetY: #move in same col
21-
return True
22-
elif abs(sourceX-targetX)==abs(sourceY-targetY): #diagnonal move
32+
#move in same col
33+
elif sourceX!=targetX and sourceY==targetY:
34+
return True
35+
#diagnonal move
36+
elif abs(sourceX-targetX)==abs(sourceY-targetY):
2337
return True
2438
else:
2539
return False

‎__pycache__/Bishop.cpython-39.pyc

743 Bytes
Binary file not shown.

‎__pycache__/Board.cpython-39.pyc

1.93 KB
Binary file not shown.

‎__pycache__/Game.cpython-39.pyc

588 Bytes
Binary file not shown.

‎__pycache__/King.cpython-39.pyc

813 Bytes
Binary file not shown.

‎__pycache__/Knight.cpython-39.pyc

876 Bytes
Binary file not shown.

‎__pycache__/Pawn.cpython-39.pyc

679 Bytes
Binary file not shown.

‎__pycache__/Queen.cpython-39.pyc

291 Bytes
Binary file not shown.

‎__pycache__/Rock.cpython-39.pyc

558 Bytes
Binary file not shown.

‎main.py

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
from Game import Game
22
def main():
3+
#creating game object
34
game=Game()
5+
#starting game
46
game.play()
57

68
if __name__ == "__main__":

0 commit comments

Comments
 (0)
Please sign in to comment.