This is an object-oriented Chess game. So far, I have implemented the pawn
and board
functionalities and I would love to get a review early on before diving deep into the project.
piece.py
# piece.py
from abc import ABC, abstractmethod
class Piece(ABC):
def __init__(self, color):
if color.lower() != 'black' and color.lower() != 'white':
raise ValueError('Invalid color argument')
self.color = color.lower()
def can_move(self):
pass
pawn.py
# pawn.py
from piece import Piece
class Pawn(Piece):
def __init__(self, color, co_ordinates):
super().__init__(color)
self.co_ordinates = co_ordinates
self.first_move = True
def can_move(self, board_list, co_ordinates):
new_row, new_column = co_ordinates
if self.color == 'black':
return self.__can_move_black(board_list, new_row, new_column)
if self.color == 'white':
return self.__can_move_white(board_list, new_row, new_column)
def __can_move_black(self, board_list, row, column):
location_is_empty = board_list[row][column] == 0
if (self.co_ordinates[0] + 1 == row) and location_is_empty:
return True
if self.first_move and location_is_empty:
self.first_move = False
return self.co_ordinates[0] + 2 == row and self.co_ordinates[1] == column;
lhs_is_valid = self.co_ordinates[0] + 1 == row and self.co_ordinates[1] - 1 == column
rhs_is_valid = self.co_ordinates[0] + 1 == row and self.co_ordinates[1] + 1 == column
if rhs_is_valid or lhs_is_valid:
if not location_is_empty and board_list[row][column].color != self.color:
return True;
else:
return False
def __can_move_white(self, board_list, row, column):
location_is_empty = board_list[row][column] == 0
if (self.co_ordinates[0] - 1 == row) and location_is_empty:
return True
if self.first_move and location_is_empty:
self.first_move = False
return self.co_ordinates[0] - 2 == row and self.co_ordinates[1] == column
lhs_is_valid = self.co_ordinates[0] - 1 == row and self.co_ordinates[1] - 1 == column
rhs_is_valid = self.co_ordinates[0] - 1 == row and self.co_ordinates[1] + 1 == column
if rhs_is_valid or lhs_is_valid:
if not location_is_empty and (board_list[row][column].color != self.color):
return True;
else:
return False
def board_repr(self):
return self.color[0] + 'p'
def __repr__(self):
return 'Pawn(%s, %r)' % (self.color, self.co_ordinates)
board.py
# board.py
from pawn import Pawn
class Board:
def __init__(self):
self.board_list = [
[ 0 for i in range(8)] for i in range(8)
]
self.__init_pieces()
def __init_pieces(self):
self.__init_pawns()
def __init_pawns(self):
row = 1
for column in range(8):
self.board_list[row][column] = Pawn('Black', (row, column))
row = 6
for column in range(8):
self.board_list[row][column] = Pawn('White', (row, column))
def draw(self):
for row in self.board_list:
for column in row:
if column != 0:
print(column.board_repr(), end=' ')
else:
print(column, end=' ')
print()
def move(self, piece, co_ordinates):
if piece != 0 and piece.can_move(self.board_list, co_ordinates):
row, column = co_ordinates
if self.__can_capture(row, column):
self.board_list[row][column] = 0
piece_row, piece_column = piece.co_ordinates
piece.co_ordinates = co_ordinates
self.board_list[row][column], self.board_list[piece_row][piece_column] = self.board_list[piece_row][piece_column], self.board_list[row][column]
else:
print('Invalid move')
def __can_capture(self, row, column):
location_is_empty = self.board_list[row][column] == 0
if not location_is_empty:
return True
else:
return False
def __promote(self, piece, new_piece):
new_piece.co_ordinates = piece.co_ordinates
row, column = new_piece.co_ordinates
board_list[row][column] = new_piece
if __name__ == '__main__':
board = Board();
board.move(board.board_list[1][0], (3,0))
board.move(board.board_list[6][1], (4,1))
board.move(board.board_list[3][0], (4,1))
board.move(board.board_list[0][0], (4,1))
#board.move(pawn, (4,1))
board.draw();