Exemple #1
0
    def _piecesNewGame( self ):
        """Setus up the pieces for a new game."""
        mypieces = {}

        # black pieces
        ###############
        color = BLACK

        # pawns
        y = 7
        for x in letterCoords:
            mypieces[(x,y)] = pieces.pawn(color,(x,y))

        y = 8 # for everything else
        # rooks
        mypieces[(a,y)] = pieces.rook(color,(a,y))
        mypieces[(h,y)] = pieces.rook(color,(h,y))
        # knights
        mypieces[(b,y)] = pieces.knight(color,(b,y))
        mypieces[(g,y)] = pieces.knight(color,(g,y))
        # bishops
        mypieces[(c,y)] = pieces.bishop(color,(c,y))
        mypieces[(f,y)] = pieces.bishop(color,(f,y))
        # queen
        mypieces[(d,y)] = pieces.queen(color,(d,y))
        # king
        mypieces[(e,y)] = pieces.king(color,(e,y))

        # white pieces
        ###############
        color = WHITE

        # pawns
        y = 2
        for x in letterCoords:
            mypieces[(x,y)] = pieces.pawn(color,(x,y))

        y = 1 # for everything else
        # rooks
        mypieces[(a,y)] = pieces.rook(color,(a,y))
        mypieces[(h,y)] = pieces.rook(color,(h,y))
        # knights
        mypieces[(b,y)] = pieces.knight(color,(b,y))
        mypieces[(g,y)] = pieces.knight(color,(g,y))
        # bishops
        mypieces[(c,y)] = pieces.bishop(color,(c,y))
        mypieces[(f,y)] = pieces.bishop(color,(f,y))
        # queen
        mypieces[(d,y)] = pieces.queen(color,(d,y))
        # king
        mypieces[(e,y)] = pieces.king(color,(e,y))

        return mypieces
Exemple #2
0
    def __init__(self):
        all_pieces = {
            pieces.empty((x - 1, y - 1))
            for x in range(1, 9) for y in range(3, 7)
        }

        for x in range(1, 9):
            all_pieces.add(pieces.pawn("white", (x - 1, 2 - 1)))
            all_pieces.add(pieces.pawn("black", (x - 1, 7 - 1)))
        for y in [1, 8]:
            for r in [1, 8]:
                all_pieces.add(
                    pieces.rook("white" * (y == 1) + "black" * (y == 8),
                                (r - 1, y - 1)))
            for n in [2, 7]:
                all_pieces.add(
                    pieces.knight("white" * (y == 1) + "black" * (y == 8),
                                  (n - 1, y - 1)))
            for b in [3, 6]:
                all_pieces.add(
                    pieces.bishop("white" * (y == 1) + "black" * (y == 8),
                                  (b - 1, y - 1)))
            all_pieces.add(
                pieces.king("white" * (y == 1) + "black" * (y == 8),
                            (5 - 1, y - 1)))
            all_pieces.add(
                pieces.queen("white" * (y == 1) + "black" * (y == 8),
                             (4 - 1, y - 1)))

        self.all_pieces = all_pieces
Exemple #3
0
 def board_setup(self):
     for x in range(8):
         if x in (0, 7):
             self.board[0][x][1] = pc.castle(self.black, (0, x))
             self.board[7][x][1] = pc.castle(self.white, (7, x))
         if x in (1, 6):
             self.board[0][x][1] = pc.knight(self.black, (0, x))
             self.board[7][x][1] = pc.knight(self.white, (7, x))
         if x in (2, 5):
             self.board[0][x][1] = pc.bishop(self.black, (0, x))
             self.board[7][x][1] = pc.bishop(self.white, (7, x))
         if x == 3:
             self.board[0][x][1] = pc.queen(self.black, (0, x))
             self.board[7][x][1] = pc.queen(self.white, (7, x))
         if x == 4:
             self.board[0][x][1] = pc.king(self.black, (0, x))
             self.board[7][x][1] = pc.king(self.white, (7, x))
         for y in (2, 3, 4, 5):
             self.board[y][x][1] = None
         self.board[1][x][1] = pc.pawn(self.black, (1, x))
         self.board[6][x][1] = pc.pawn(self.white, (6, x))
Exemple #4
0
 def __init__(self, colour):
     self.pawn = 8
     self.knight = 2
     self.bishop = 2
     self.rook = 2
     self.queen = 1
     self.king = 1
     self.colour = colour
     self.pieces = [pieces.pawn(self.colour)] * self.pawn
     self.pieces += [pieces.knight(self.colour)] * self.knight
     self.pieces += [pieces.bishop(self.colour)] * self.bishop
     self.pieces += [pieces.rook(self.colour)] * self.rook
     self.pieces += [pieces.queen(self.colour)] * self.queen
     self.pieces += [pieces.king(self.colour)] * self.king
Exemple #5
0
def makeBoard():

    color = brown
    for i in range(8):
        if color == brown:
            color = beige
        else:
            color = brown
        for j in range(8):
            board[i].append(square(j * w / 8, i * w / 8, color, None))
            if color == brown:
                color = beige
            else:
                color = brown
    for i in board[1]:
        p = pieces.pawn(black)
        i.setPiece(p)
    for i in board[6]:
        p = pieces.pawn(white)
        i.setPiece(p)

    board[0][0].setPiece(pieces.rook(black))
    board[0][7].setPiece(pieces.rook(black))
    board[0][1].setPiece(pieces.knight(black))
    board[0][6].setPiece(pieces.knight(black))
    board[0][2].setPiece(pieces.bishop(black))
    board[0][5].setPiece(pieces.bishop(black))
    board[0][3].setPiece(pieces.queen(black))
    board[0][4].setPiece(pieces.king(black))
    board[7][0].setPiece(pieces.rook(white))
    board[7][7].setPiece(pieces.rook(white))
    board[7][1].setPiece(pieces.knight(white))
    board[7][6].setPiece(pieces.knight(white))
    board[7][2].setPiece(pieces.bishop(white))
    board[7][5].setPiece(pieces.bishop(white))
    board[7][3].setPiece(pieces.queen(white))
    board[7][4].setPiece(pieces.king(white))
Exemple #6
0
 def promote( self, pieceType ):
     """This function will change a pawn to the type provided in the
         parameter. It should be called only if  a pawn is eligible and
         right after the pawn becomes eligible. If no pawn is eligible,
         this will return False. Parameter is a string from chessLocals
         indicating the piece desired."""
     color = None
     newPiece = None
     y = 0
     if( self.whitesTurn ):
         # the move that made the pawn eligible happened already, so
         #   if its white's turn, black can promote
         color = BLACK
         y = 1
     else:
         color = WHITE
         y = 8
     # find eligible pawn
     for pos,piece in self.pieces.iteritems():
         if( piece.iAm == PAWN )and( piece.color == color )\
             and( piece.pos[1] == y ):
             # create the requested piece
             if( pieceType == ROOK ):
                 newPiece = pieces.rook(color,pos)
             if( pieceType == KNIGHT ):
                 newPiece = pieces.knight(color,pos)
             if( pieceType == BISHOP ):
                 newPiece = pieces.bishop(color,pos)
             if( pieceType == QUEEN ):
                 newPiece = pieces.queen(color,pos)
             break
     if( newPiece ):
         self.pieces[newPiece.pos] = newPiece
         self._updateSquares()
         return True
     else:
         # either an invalid string was provided or no pawn is eligible
         return False
Exemple #7
0
def main():
    global board
    global previous
    global whiteTurn
    global taken
    whiteTurn = True
    taken = None
    board = [[], [], [], [], [], [], [], []]
    makeBoard()
    square = None
    run = True
    selected = False
    pressed = False
    over = False
    promoted = False
    whiteKing = board[7][4].piece
    blackKing = board[0][4].piece
    checking = None
    king = None
    while run:

        for i in board:
            for j in i:
                if j.piece is not None:

                    if j.piece is blackKing or j.piece is whiteKing:
                        j.piece.inCheck = False

                    pieces.reachable(j, board)

        win.fill((0, 0, 0, 0))
        draw(over, king)

        for event in pygame.event.get():

            if event.type == pygame.QUIT:
                run = False

            if event.type == pygame.KEYDOWN:

                #reset
                if event.key == pygame.K_c:
                    board = [[], [], [], [], [], [], [], []]
                    makeBoard()
                    whiteTurn = True
                    selected, pressed, over = (False, False, False)

                #undo previous move
                if event.key == pygame.K_z:
                    if previous != (None, None):

                        whiteTurn = not whiteTurn
                        endSquare, square = previous
                        if not promoted:
                            endSquare.setPiece(square.piece)
                            if str(endSquare.piece) == "P":
                                for i in board[1]:
                                    if str(i.piece) == "P":
                                        i.piece.start = False
                                for i in board[6]:
                                    if str(i.piece) == "P":
                                        i.piece.start = False

                        else:
                            endSquare.setPiece(pieces.pawn(square.piece.color))

                        square.piece = None
                        square.reset()
                        if taken is not None:
                            square.setPiece(taken)
                        previous = (None, None)
                        selected = False
                        for i in board:
                            for j in i:
                                j.reset()

            if event.type == pygame.MOUSEBUTTONDOWN and not over:

                pos = pygame.mouse.get_pos()
                col, row = getClicked(pos, 8, w)
                pygame.time.delay(50)

                if not selected:
                    if board[row][col].piece is not None:
                        square = board[row][col]
                        square.setColor(yellow)
                        if square.piece is not None:
                            for i in square.piece.reachable:
                                if i.piece is None:
                                    i.setColor(red)
                                else:
                                    if i.piece.color != square.piece.color:
                                        i.setColor(red)
                        selected = True
                else:
                    endSquare = board[row][col]

                    if isValid(square, endSquare, whiteTurn, board) == 1:

                        if str(square.piece) == "P":
                            square.piece.start = True
                        whiteTurn = not whiteTurn
                        taken = endSquare.piece
                        move(square, endSquare)
                        previous = (square, endSquare)
                        for i in board:
                            for j in i:
                                j.reset()
                        selected = False

                    elif isValid(square, endSquare, whiteTurn, board) == 2:

                        taken = endSquare.piece
                        endSquare.setPiece(pieces.queen(square.piece.color))
                        square.piece = None
                        whiteTurn = not whiteTurn
                        for i in board:
                            for j in i:
                                j.reset()
                        selected = False
                        previous = (square, endSquare)
                        promoted = True

                    elif isValid(square, endSquare, whiteTurn, board) == 3:
                        taken = endSquare.piece
                        move(square, endSquare)
                        if endSquare.piece.color == white:
                            board[7][5].setPiece(pieces.rook(white))
                            board[7][7].piece = None
                            endSquare.piece.moved = True
                        else:
                            board[0][5].setPiece(pieces.rook(black))
                            board[0][7].piece = None
                            endSquare.piece.moved = True
                        whiteTurn = not whiteTurn
                        for i in board:
                            for j in i:
                                j.reset()
                        selected = False

                    elif isValid(square, endSquare, whiteTurn, board) == 4:
                        taken = endSquare.piece
                        move(square, endSquare)
                        if endSquare.piece.color == white:
                            board[7][3].setPiece(pieces.rook(white))
                            board[7][0].piece = None
                            endSquare.piece.moved = True
                        else:
                            board[0][3].setPiece(pieces.rook(black))
                            board[0][0].piece = None
                            endSquare.piece.moved = True
                        whiteTurn = not whiteTurn
                        for i in board:
                            for j in i:
                                j.reset()
                        selected = False

                    else:
                        if endSquare.piece is not None:
                            for i in board:
                                for j in i:
                                    j.reset()
                            endSquare.setColor(yellow)
                            square = board[row][col]
                            for i in square.piece.reachable:
                                if i.piece is None:
                                    i.setColor(red)
                                else:
                                    if i.piece.color != square.piece.color:
                                        i.setColor(red)

                        else:
                            square.reset()
                            for i in board:
                                for j in i:
                                    j.reset()
                            selected = False

        for i in board:
            for j in i:
                if j.piece is not None:

                    if j.piece is blackKing or j.piece is whiteKing:
                        j.piece.inCheck = False

                    pieces.reachable(j, board)

        for i in board:
            for j in i:
                if j.piece is not None:
                    for k in j.piece.reachable:

                        if k.piece is whiteKing and j.piece.color == black:
                            whiteKing.inCheck = True
                            checking = j
                            k.setColor(red)

                        if k.piece is blackKing and j.piece.color == white:
                            blackKing.inCheck = True
                            checking = j
                            k.setColor(red)

        if checkmate(blackKing, board, checking) and not whiteTurn:
            over = True
            king = blackKing

        if checkmate(whiteKing, board, checking) and whiteTurn:
            over = True
            king = whiteKing
Exemple #8
0
    def move(self, color, type, start, dest):
        if type not in ["p", "r", "n", "b", "Q", "K"]:
            return type + " is not a valid piece"
        if len(start) != 2 or ord(start[0]) not in range(97, 105) or int(
                start[1]) not in range(1, 9):
            return start + " not on chess board"
        if len(dest) != 2 or ord(dest[0]) not in range(97, 105) or int(
                dest[1]) not in range(1, 9):
            return dest + " not on chess board"

        start_x = ord(start[0]) - 97
        start_y = int(start[1]) - 1

        active_piece = None
        for p in self.all_pieces:
            if p.get_type() == type and p.get_color() == color and p.get_x(
            ) == start_x and p.get_y() == start_y:
                active_piece = p
        if active_piece == None:
            return "No " + str(color) + " " + str(type) + " at " + str(start)

        dest_x = ord(dest[0]) - 97
        dest_y = int(dest[1]) - 1

        target_piece = None
        for p in self.all_pieces:
            if p.get_x() == dest_x and p.get_y() == dest_y:
                target_piece = p

        if (dest_x, dest_y) not in active_piece.get_valid_squares(
                self.get_graph()):
            return "Illegal move"

        self.all_pieces.remove(target_piece)
        self.all_pieces.remove(active_piece)
        active_piece.set_pos(dest_x, dest_y)
        self.all_pieces.add(active_piece)
        empty_piece = pieces.empty((start_x, start_y))
        self.all_pieces.add(empty_piece)

        this_king = None
        opponent_king = None
        for p in self.all_pieces:
            if p.get_type() == "K" and p.get_color() == color:
                this_king = p
            if p.get_type() == "K" and p.get_color() != color:
                opponent_king = p

        if this_king.check_attackers(self.get_graph()):
            self.all_pieces.remove(empty_piece)
            self.all_pieces.remove(active_piece)
            active_piece.set_pos(start_x, start_y)
            self.all_pieces.add(active_piece)
            self.all_pieces.add(target_piece)
            return "This move leads to check"

        # check for promotion
        if active_piece.get_type() == "p" and (dest_y == 7 or dest_y == 0):
            while True:
                type = input(
                    "What would you like to promote to (Q, r, n, or b)?")
                if type in ["r", "n", "b", "Q"]:
                    break
            self.all_pieces.remove(active_piece)
            promoted = None
            if type == "Q":
                promoted = pieces.queen(active_piece.get_color(),
                                        (dest_x, dest_y))
            if type == "r":
                promoted = pieces.rook(active_piece.get_color(),
                                       (dest_x, dest_y))
            if type == "n":
                promoted = pieces.knight(active_piece.get_color(),
                                         (dest_x, dest_y))
            if type == "b":
                promoted = pieces.bishop(active_piece.get_color(),
                                         (dest_x, dest_y))
            self.all_pieces.add(promoted)

        # check castle flag
        if active_piece.get_type() in ["r", "K"]:
            active_piece.toggle_castle_flag()

        if opponent_king.check_attackers(self.get_graph()):
            return "Check"
# positionY = {"A": 1, "B": 2, "C": 3, "D": 4, "E": 5, "F": 6, "G": 7, "H": 8}
#
# print(board_Game[7][positionY["A"]])
#
# print(positionY.len())
#
#
# print(black["pawn"][1].pos)

# black["pawn"][1].pos = [1, 4]

# pos1 = "A1"
# pos1 = [positionY[pos1[0]], int(pos1[1])]
# print(pos1)

queen1 = pieces.queen("white", [1, 8], "queen")
king1 = pieces.king("black",[1,4], "king")
pawn1 = pieces.pawn("black",[2,3], "pawn")
pawn2 = pieces.pawn("black",[2,4], "pawn")
pawn3 = pieces.pawn("black",[2,5], "pawn")

pieces.board.game_board[1][4] = king1
pieces.board.game_board[1][8] = queen1
pieces.board.game_board[2][3] = pawn1
pieces.board.game_board[2][4] = pawn2
pieces.board.game_board[2][5] = pawn3

pieces.board.show()

print(pieces.board.game_board[1][4].possible_move())