Skip to content

Commit b5e912d

Browse files
committed
second commit
1 parent 6930f32 commit b5e912d

14 files changed

+79
-21
lines changed

.DS_Store

0 Bytes
Binary file not shown.

Bishop.py

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
2+
from Piece import Piece
3+
4+
5+
class Bishop(Piece):
6+
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
9+
return True
10+
else:
11+
return False
12+
13+

Board.py

+14
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,20 @@ def putPiece(self,piece):
2727
toPut=Box(self.grid[piece.x][piece.y].color,piece)
2828
self.grid[piece.x][piece.y]=toPut
2929

30+
def getPiece(self,x,y):
31+
return self.grid[x][y].piece
32+
33+
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
36+
color=self.grid[x][y].color
37+
piece.x=x
38+
piece.y=y
39+
self.grid[x][y]=Box(color,piece)
40+
41+
42+
43+
3044

3145

3246
def printBoard(self):

Box.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
class Box:
2-
def __init__(self,color,piece):
2+
def __init__(self,color,piece):
33
self.color=color
44
self.piece=piece

Game.py

+35-13
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
from Board import Board
55
class Game:
66

7-
def canQueenKill(self):
7+
def canQueenKill(self):
88
print('Welcome to Chess by Mujahid')
99
board=Board()
1010
print('Enter position(x,y) of black queen')
11-
bQ=input()
12-
bQ=bQ.split(',')
11+
bQ=input().split(',')
1312
print('Enter position(x,y) of white queen')
14-
wQ=input()
15-
wQ=wQ.split(',')
13+
wQ=input().split(',')
1614
blackQueen=Queen('BQ','Black',int(bQ[0]),int(bQ[1]))
1715
whiteQueen=Queen('WQ','White',int(wQ[0]),int(wQ[1]))
1816
board.putPiece(blackQueen)
@@ -25,12 +23,36 @@ def canQueenKill(self):
2523

2624
def play(self):
2725
print('Welcome to Chess by Mujahid')
28-
# print('Player 1 Please Enter your name')
29-
# name1=input()
30-
# print('Player 2 Please Enter your name')
31-
# name2=input()
32-
# player1=Player(name1,0)
33-
# player2=Player(name2,0)
34-
# print(player1.name + ' and ' +player2.name +' are playing now')
35-
self.canQueenKill()
26+
print('Player 1 Please Enter your name')
27+
name1=input()
28+
print('Player 2 Please Enter your name')
29+
name2=input()
30+
player1=Player(name1,0)
31+
player2=Player(name2,0)
32+
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
35+
turn=1
36+
#game started
37+
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+
53+
54+
55+
56+
57+
3658

King.py

+10-2
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,13 @@
44

55
class King(Piece):
66

7-
def canMove(self,board,x,y):
8-
pass
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)
9+
if pointDffrnc==1:
10+
return True
11+
elif abs(self.x-x)==abs(self.y-y)==1: #diagonal difference of 1
12+
return True
13+
else:
14+
return False
15+
16+

Player.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
class Player:
2-
def __init__(self,name,score):
2+
def __init__(self,name,score):
33
self.name=name
44
self.score=score
55

Queen.py

+4-4
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ def canAttack(self,board,piece):
1313
return False
1414

1515

16-
def canMove(self,board,x,y):
16+
def canMove(self,board,x,y): #weather a queen can move to x,y or not
1717
sourceX,sourceY,targetX,targetY=self.x,self.y,x,y
18-
if sourceX==targetX and sourceY!=targetY:
18+
if sourceX==targetX and sourceY!=targetY: #move in same row
1919
return True
20-
elif sourceX!=targetX and sourceY==targetY:
20+
elif sourceX!=targetX and sourceY==targetY: #move in same col
2121
return True
22-
elif abs(sourceX-targetX)==abs(sourceY-targetY):
22+
elif abs(sourceX-targetX)==abs(sourceY-targetY): #diagnonal move
2323
return True
2424
else:
2525
return False

README.md

+1
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
# Python-django-chess-game
2+
#This is a sample project for python/django learning purpose at Invozone

__pycache__/Board.cpython-39.pyc

435 Bytes
Binary file not shown.

__pycache__/Box.cpython-39.pyc

21 Bytes
Binary file not shown.

__pycache__/Game.cpython-39.pyc

664 Bytes
Binary file not shown.

__pycache__/Player.cpython-39.pyc

21 Bytes
Binary file not shown.

__pycache__/Queen.cpython-39.pyc

21 Bytes
Binary file not shown.

0 commit comments

Comments
 (0)