from abc import ABCMeta, abstractmethod class Board(): def display(self): firstLine = " " for c in range(self.__columns): firstLine += ("| " + str(c+1) + " ") firstLine += "|" print(firstLine) print("-"*((5*self.__columns)+4)) for r in range(self.__rows): print(str(" " + chr(r+65)) + " ", end='') for x in self.__board[r]: if x == None: y = " " z = " " else: y = x.getType()[0] z = "(" + x.getColour()[0].lower() + ")" print("|" + y + z, end="") print("|") print("-"*((5*self.__columns)+4)) print() def movePiece(self, colour, startPos, endPos): startPiece = self.__board[startPos[0]][startPos[1]] endPiece = self.__board[endPos[0]][endPos[1]] if startPiece != None: if startPiece.getColour() == colour: if startPiece.validMove(self, startPos, endPos): print(colour + "'s " + startPiece.getType() + " was moved to " + str(chr(endPos[0]+65)) + str(endPos[1]+1) + ".") self.__takePiece(startPiece, endPiece) self.__board[startPos[0]][startPos[1]] = None self.__board[endPos[0]][endPos[1]] = startPiece self.__upgradePiece(startPiece, endPos) print() return True print("That is not a valid move! Please try again.") return False class BasicMovement(): def validMove(self, colour, board, startPos, endPos): if endPos[0] >= 0 and endPos[0] < board.getHeight(): if endPos[1] >= 0 and endPos[1] < board.getWidth(): if startPos != endPos: if board.pieceAt(endPos) != None: if board.pieceAt(endPos).getColour() != colour: return True else: return True return False class StraightMovement(): def validMove(self, colour, board, startPos, endPos, pieceRange): if startPos[0] == endPos[0]: if pieceRange >= abs(endPos[1] - startPos[1]): for i in range(1, endPos[1] - startPos[1]): tempPos = [startPos[0], startPos[1] + i] if board.pieceAt(tempPos) != None: return False return True if startPos[1] == endPos[1]: if pieceRange >= abs(endPos[0] - startPos[0]): for i in range(1, endPos[0] - startPos[0]): tempPos = [startPos[0] + i, startPos[1]] if board.pieceAt(tempPos) != None: return False return True return False class DiagonalMovement(): def validMove(self, colour, board, startPos, endPos, pieceRange): if abs(endPos[0] - startPos[0]) != abs(endPos[1] - startPos[1]): return False length = abs(endPos[0] - startPos[0]) columnChange = 1 rowChange = 1 if endPos[0] < startPos[0]: rowChange = -1 if endPos[1] < startPos[1]: columnChange = -1 if pieceRange >= length: if length == 1: return True for r in range(1, abs(endPos[0] - startPos[0])): tempPos = [startPos[0] + r * rowChange, startPos[1] + r * columnChange] if board.pieceAt(tempPos) != None: return False return True return False class Piece(): def validMove(self, board, startPos, endPos): if self._basic.validMove(self._colour, board, startPos, endPos): return True else: return False class Pawn(): def validMove(self, board, startPos, endPos): if not super().validMove(board, startPos, endPos): return False if self._colour == "White" and startPos[0] < endPos[0]: return False elif self._colour == "Black" and startPos[0] > endPos[0]: return False if startPos[1] == endPos[1]: if board.pieceAt(endPos) != None: return False else: if self._colour == "White" and startPos[0] == board.getHeight() - 2 or self._colour == "Black" and startPos[0] == 1: if self.__straight.validMove(self._colour, board, startPos, endPos, self._range + 1): return True elif self.__straight.validMove(self._colour, board, startPos, endPos, self._range): return True if startPos[1] != endPos[1]: if board.pieceAt(endPos) == None: return False else: if self.__diagonal.validMove(self._colour, board, startPos, endPos, self._range): return True return False class King(): class Queen(): class Rook(): class Bishop(): class Knight(): class Player(): class HumanPlayer(): def movePiece(self, board): end = False while end != True: posSet = False while not posSet: board.display() print() print(self._colour + "'s turn:") startDisplay = input("Enter the location of the piece you would like to move (e.g. A1):") print() startPos = self.__getPos(board, self._colour, startDisplay) if startPos != None and board.pieceAt(startPos) != None and board.pieceAt(startPos).getColour() == self._colour: posSet = True else: print("You don't have a piece at " + startDisplay + " to move! Please try again.") print() board.display() print() print(self._colour + "'s turn:") endPos = input("Where would you like to move the " + board.pieceAt(startPos).getType() + " from " + startDisplay.upper() + " to?:") print() end = self.__getPos(board, self._colour, startPos, endPos) print() print() return board.gameOver() def __getPos(self, board, colour, startPos, endPos = None): if endPos == None: checkPos = startPos else: checkPos = endPos if len(checkPos) > 2: return None try: if endPos == None: startRow = int(ord(startPos[0].upper())) - 65 startColumn = int(startPos[1]) - 1 if startRow < 0 or startRow >= board.getHeight() or startColumn < 0 or startColumn >= board.getHeight(): return None else: checkPos = [startRow, startColumn] else: endRow = int(ord(endPos[0].upper())) - 65 endColumn = int(endPos[1]) - 1 if endRow < 0 or endRow >= board.getHeight() or endColumn < 0 or endColumn >= board.getHeight(): return None else: checkPos = [endRow, endColumn] except: return None if endPos == None and board.pieceAt(checkPos) == None: return None elif endPos == None: return checkPos elif endPos != None: return board.movePiece(colour, startPos, checkPos) else: return None def main(): board = Board() white = HumanPlayer("White") black = HumanPlayer("Black") gameOver = False while not gameOver: print() print("It's White's turn to move:") gameOver = white.movePiece(board) if gameOver: break print() print("It's Black's turn to move:") gameOver = black.movePiece(board) board.display() input() if __name__ == '__main__': main()