コード例 #1
0
 def init_pieces(self, color):
     # TODO: Should I adopt the recommendation of PyCharm (PEP8?) to take init_pieces out of class player_pieces?
     # TODO: assert color is either Color.White or Color.Black Enum. I may write it as base = 48 * get_color(color)
     #  and make the assertion inside get_color(color)
     base = 48 * color.value
     return {
         base + k: Tile(base + k, Piece())
         for k in [0, 2, 4, 6, 11, 13, 15]
     }
コード例 #2
0
    def inCheckmate(self, board):
        moves = self.legalMoves(board)
        for move in moves:
            copyBoard = copy.deepcopy(board)
            copyBoard.tiles[move] = Tile(move, NullPiece())
            allEnemyAttacks = copyBoard.allEnemyAttacks()
            if move in allEnemyAttacks:
                moves.remove(move)

        allFriendlyMoves = []
        allFriendlyPieces = []
        for tile in board.tiles.values():
            if tile.pieceOnTile.toString(
            ) != "-" and tile.pieceOnTile.color == self.color:
                if tile.pieceOnTile.toString().lower() != "k":
                    allFriendlyPieces.append(tile.pieceOnTile)
                if tile.pieceOnTile.legalMoves(board) is not None:
                    allFriendlyMoves = allFriendlyMoves + tile.pieceOnTile.legalMoves(
                        board)

        for piece in allFriendlyPieces:
            legalMoves = piece.legalMoves(board)
            if legalMoves is not None:
                for move in legalMoves:
                    copyBoard = copy.deepcopy(board)
                    copyBoard.tiles[piece.position] = Tile(
                        piece.position, NullPiece())
                    copyBoard.tiles[move] = Tile(move, piece)
                    if not self.inCheck(copyBoard):
                        return False

        copyBoard = copy.deepcopy(board)

        for number in allFriendlyMoves:
            if copyBoard.tiles[number].pieceOnTile.color != self.color:
                copyBoard.tiles[number] = Tile(number, NullPiece())

        return not moves and self.inCheck(
            copyBoard)  # and self.inCheck(copyBoard, )
コード例 #3
0
    def __init__(self, size, square_size, win_len):
        """Initialise a tile and create rectangles"""
        self.size = size
        self.win_len = win_len
        self.checked_count = 0
        self.tiles = [size[0] * [None] for i in range(size[1])]

        # Lines contain the winning streaks
        self.lines = []

        # Initialise all tiles as empty tiles
        for x in range(0, size[0]):
            for y in range(0, size[1]):
                self.tiles[x][y] = Tile((x, y), square_size, (0, 0))

        # The tile that was last played is used for checking of the game state - win,..
        self.last_played_tile = None
コード例 #4
0
ファイル: Player.py プロジェクト: chiefchiefon/checkers
 def init_pieces(self, color):
     base = 48 * color.value
     return {
         base + k: Tile(base + k, Piece())
         for k in [0, 2, 4, 6, 11, 13, 15]
     }
コード例 #5
0
    def makeMove(self):
        specialMove = False
        boardCopy = copy.deepcopy(self.board)
        prevPosition = self.piece.position
        boardCopy.tiles[self.piece.position] = Tile(self.piece.position,
                                                    NullPiece())
        enPassantHold = self.piece.position

        #after first move, castling isn't allowed
        if self.piece.toString().lower() == "k" or self.piece.toString().lower(
        ) == "r":
            self.piece.moved = True
        # deal with a queened pawn
        if self.piece.toString().lower() == "p":
            if self.piece.color == "White" and 56 <= self.coordinate < 64:
                boardCopy.tiles[self.coordinate] = Tile(
                    self.coordinate, Queen("White", self.coordinate))
                specialMove = True
            elif self.piece.color == "Black" and 0 <= self.coordinate < 7:
                boardCopy.tiles[self.coordinate] = Tile(
                    self.coordinate, Queen("Black", self.coordinate))
                specialMove = True

        #deal with en passant
        if self.piece.toString().lower() == "p":
            if self.piece.color == "White" and 32 <= enPassantHold < 39:
                if self.coordinate - enPassantHold != 8:
                    if boardCopy.tiles[
                            self.coordinate].pieceOnTile.toString() == "-":
                        boardCopy.tiles[self.coordinate - 8] = Tile(
                            self.coordinate, NullPiece())
                        boardCopy.tiles[self.coordinate] = Tile(
                            self.coordinate, self.piece)
                        specialMove = True
            elif self.piece.color == "Black" and 24 <= enPassantHold < 31:
                if self.coordinate - enPassantHold != -8:
                    if boardCopy.tiles[
                            self.coordinate].pieceOnTile.toString() == "-":
                        boardCopy.tiles[self.coordinate + 8] = Tile(
                            self.coordinate, NullPiece())
                        boardCopy.tiles[self.coordinate] = Tile(
                            self.coordinate, self.piece)
                        specialMove = True

        #deal with castling
        if self.piece.toString() == "k":
            if prevPosition == 4 and self.coordinate == 2:
                boardCopy.tiles[self.coordinate] = Tile(
                    self.coordinate, King("White", self.coordinate))
                boardCopy.tiles[0] = Tile(self.coordinate, NullPiece())
                boardCopy.tiles[3] = Tile(self.coordinate, Rook("White", 3))
                specialMove = True
            if prevPosition == 4 and self.coordinate == 6:
                boardCopy.tiles[self.coordinate] = Tile(
                    self.coordinate, King("White", self.coordinate))
                boardCopy.tiles[7] = Tile(self.coordinate, NullPiece())
                boardCopy.tiles[5] = Tile(self.coordinate, Rook("White", 5))
                specialMove = True

        if self.piece.toString() == "K":
            if prevPosition == 60 and self.coordinate == 58:
                boardCopy.tiles[self.coordinate] = Tile(
                    self.coordinate, King("Black", self.coordinate))
                boardCopy.tiles[56] = Tile(self.coordinate, NullPiece())
                boardCopy.tiles[59] = Tile(self.coordinate, Rook("Black", 59))
                specialMove = True
            if prevPosition == 60 and self.coordinate == 62:
                boardCopy.tiles[self.coordinate] = Tile(
                    self.coordinate, King("Black", self.coordinate))
                boardCopy.tiles[63] = Tile(self.coordinate, NullPiece())
                boardCopy.tiles[61] = Tile(self.coordinate, Rook("Black", 61))
                specialMove = True

        if not specialMove:
            self.piece.position = self.coordinate
            boardCopy.tiles[self.coordinate] = Tile(self.coordinate,
                                                    self.piece)

        friendlyKing = None
        for tile in boardCopy.tiles.values():
            if (tile.pieceOnTile.toString() == "K" and boardCopy.currentPlayer
                    == "Black") or (tile.pieceOnTile.toString() == "k"
                                    and boardCopy.currentPlayer == "White"):
                friendlyKing = tile.pieceOnTile
                break
        if friendlyKing.inCheck(boardCopy):
            return False
        return boardCopy
コード例 #6
0
ファイル: Board.py プロジェクト: Ben-Foxman/ChessEngine
    def __init__(self):
        self.currentPlayer = "White"
        self.tiles = dict()
        self.moveCounter = 1
        self.prevBoard = None
        for x in range(64):
            self.tiles[x] = (Tile(x, NullPiece()))
        for x in range(8, 16):
            pass
            self.tiles[x] = (Tile(x, Pawn("White", x)))
        for x in range(48, 56):
            self.tiles[x] = (Tile(x, Pawn("Black", x)))

        self.tiles[0] = (Tile(0, Rook("White", 0)))
        self.tiles[1] = (Tile(1, Knight("White", 1)))
        self.tiles[2] = (Tile(2, Bishop("White", 2)))
        self.tiles[3] = (Tile(3, Queen("White", 3)))
        self.tiles[4] = (Tile(4, King("White", 4)))
        self.tiles[5] = (Tile(5, Bishop("White", 5)))
        self.tiles[6] = (Tile(6, Knight("White", 6)))
        self.tiles[7] = (Tile(7, Rook("White", 7)))
        self.tiles[56] = (Tile(56, Rook("Black", 56)))
        self.tiles[57] = (Tile(57, Knight("Black", 57)))
        self.tiles[58] = (Tile(58, Bishop("Black", 58)))
        self.tiles[59] = (Tile(59, Queen("Black", 59)))
        self.tiles[60] = (Tile(60, King("Black", 60)))
        self.tiles[61] = (Tile(61, Bishop("Black", 61)))
        self.tiles[62] = (Tile(62, Knight("Black", 62)))
        self.tiles[63] = (Tile(63, Rook("Black", 63)))
コード例 #7
0
ファイル: Positions.py プロジェクト: Ben-Foxman/ChessEngine
    # node.toString()
    x = getBestMove(node, 2)
    newBoard = x[0]
    if newBoard.currentPlayer == "White":
        newBoard.currentPlayer = "Black"
        newBoard.prevBoard.currentPlayer = "White"
    else:
        newBoard.currentPlayer = "White"
        newBoard.prevBoard.currentPlayer = "Black"
    return newBoard

#testing

# b = BoardEvaluator(b)
b = Board()
b.tiles[39] = (Tile(39, Queen("White", 39)))
b.tiles[36] = (Tile(36, Pawn("Black", 36)))
b.tiles[52] = (Tile(52, NullPiece()))
b.tiles[53] = (Tile(53, NullPiece()))
b.tiles[21] = (Tile(21, Knight("White", 21)))
b.tiles[6] = (Tile(6, NullPiece()))
b.currentPlayer = "Black"

        # newBoard.printBoard()

#resultNode = Node(b, True)
#a = getBestMove(resultNode, 1)

#x = 0
#for node in nodes.keys():
    #node.position.printBoard()
コード例 #8
0
    def legalMoves(self, board):
        destinations = []
        startingCoordinate = self.position
        # Up Left
        while 0 <= startingCoordinate < 56:
            if startingCoordinate % 8 == 0:
                break
            else:
                startingCoordinate += 7
                if board.tiles[startingCoordinate].pieceOnTile.toString(
                ) == "-":
                    destinations.append(startingCoordinate)
                    break
                elif board.tiles[
                        startingCoordinate].pieceOnTile.color != self.color:
                    destinations.append(startingCoordinate)
                    break
                else:
                    break

        startingCoordinate = self.position
        # Down Left
        while 9 <= startingCoordinate < 64:
            if startingCoordinate % 8 == 0:
                break
            else:
                startingCoordinate -= 9
                if board.tiles[startingCoordinate].pieceOnTile.toString(
                ) == "-":
                    destinations.append(startingCoordinate)
                    break
                elif board.tiles[
                        startingCoordinate].pieceOnTile.color != self.color:
                    destinations.append(startingCoordinate)
                    break
                else:
                    break

        startingCoordinate = self.position
        # Up Right
        while 0 <= startingCoordinate < 55:
            if startingCoordinate % 8 == 7:
                break
            else:
                startingCoordinate += 9
                if board.tiles[startingCoordinate].pieceOnTile.toString(
                ) == "-":
                    destinations.append(startingCoordinate)
                    break
                elif board.tiles[
                        startingCoordinate].pieceOnTile.color != self.color:
                    destinations.append(startingCoordinate)
                    break
                else:
                    break

        startingCoordinate = self.position
        # Down Right
        while 8 <= startingCoordinate < 64:
            if startingCoordinate % 8 == 7:
                break
            else:
                startingCoordinate -= 7
                if board.tiles[startingCoordinate].pieceOnTile.toString(
                ) == "-":
                    destinations.append(startingCoordinate)
                    break
                elif board.tiles[
                        startingCoordinate].pieceOnTile.color != self.color:
                    destinations.append(startingCoordinate)
                    break
                else:
                    break

        startingCoordinate = self.position
        # Up
        while 0 <= startingCoordinate < 56:
            startingCoordinate += 8
            if board.tiles[startingCoordinate].pieceOnTile.toString() == "-":
                destinations.append(startingCoordinate)
                break
            elif board.tiles[
                    startingCoordinate].pieceOnTile.color != self.color:
                destinations.append(startingCoordinate)
                break
            else:
                break

        startingCoordinate = self.position
        # Down
        while 8 <= startingCoordinate < 64:
            startingCoordinate -= 8
            if board.tiles[startingCoordinate].pieceOnTile.toString() == "-":
                destinations.append(startingCoordinate)
                break
            elif board.tiles[
                    startingCoordinate].pieceOnTile.color != self.color:
                destinations.append(startingCoordinate)
                break
            else:
                break

        startingCoordinate = self.position
        # Right
        while 0 <= startingCoordinate < 63:
            if startingCoordinate % 8 == 7:
                break
            else:
                startingCoordinate += 1
                if board.tiles[startingCoordinate].pieceOnTile.toString(
                ) == "-":
                    destinations.append(startingCoordinate)
                    break
                elif board.tiles[
                        startingCoordinate].pieceOnTile.color != self.color:
                    destinations.append(startingCoordinate)
                    break
                else:
                    break

        startingCoordinate = self.position
        # Left
        while 1 <= startingCoordinate < 64:
            if startingCoordinate % 8 == 0:
                break
            else:
                startingCoordinate -= 1
                if board.tiles[startingCoordinate].pieceOnTile.toString(
                ) == "-":
                    destinations.append(startingCoordinate)
                    break
                elif board.tiles[
                        startingCoordinate].pieceOnTile.color != self.color:
                    destinations.append(startingCoordinate)
                    break
                else:
                    break

        startingCoordinate = self.position
        destinations = list(
            filter(lambda x: x != startingCoordinate, destinations))
        allEnemyAttacks = board.allEnemyAttacks()

        # deal with castling
        if self.moved == False:
            if self.color == "White":
                if self.position == 4 and board.tiles[0].pieceOnTile.toString(
                ) == "r" and board.tiles[0].pieceOnTile.moved == False:
                    if not 2 in allEnemyAttacks and not 3 in allEnemyAttacks and not 4 in allEnemyAttacks:
                        if board.tiles[1].pieceOnTile.toString() == "-":
                            if board.tiles[2].pieceOnTile.toString() == "-":
                                if board.tiles[3].pieceOnTile.toString(
                                ) == "-":
                                    destinations.append(2)
                if self.position == 4 and board.tiles[7].pieceOnTile.toString(
                ) == "r" and board.tiles[7].pieceOnTile.moved == False:
                    if not 5 in allEnemyAttacks and not 4 in allEnemyAttacks and not 6 in allEnemyAttacks:
                        if board.tiles[6].pieceOnTile.toString() == "-":
                            if board.tiles[5].pieceOnTile.toString() == "-":
                                destinations.append(6)
            if self.color == "Black":
                if self.position == 60 and board.tiles[56].pieceOnTile.toString(
                ) == "R" and board.tiles[56].pieceOnTile.moved == False:
                    if not 58 in allEnemyAttacks and not 59 in allEnemyAttacks and not 60 in allEnemyAttacks:
                        if board.tiles[58].pieceOnTile.toString() == "-":
                            if board.tiles[59].pieceOnTile.toString() == "-":
                                if board.tiles[57].pieceOnTile.toString(
                                ) == "-":
                                    destinations.append(58)
                if self.position == 60 and board.tiles[63].pieceOnTile.toString(
                ) == "R" and board.tiles[63].pieceOnTile.moved == False:
                    if not 60 in allEnemyAttacks and not 61 in allEnemyAttacks and not 62 in allEnemyAttacks:
                        if board.tiles[61].pieceOnTile.toString() == "-":
                            if board.tiles[62].pieceOnTile.toString() == "-":
                                destinations.append(62)
        ######
        finalMoves = []
        for move in destinations:
            if move not in allEnemyAttacks:
                finalMoves.append(move)

        moves = finalMoves
        for move in moves:
            copyBoard = copy.deepcopy(board)
            copyBoard.tiles[move] = Tile(move, NullPiece())
            allEnemyAttacks = copyBoard.allEnemyAttacks()
            if move in allEnemyAttacks:
                moves.remove(move)
        return moves