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
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
def fill(self): # entra las piezas(objetos) al tablero. # Para las piezas negras for fila in range(1, 3): for columna in range(1, 9): if (fila == 1): if (columna == 1 or columna == 8): board.board.game_board[fila][columna] = rook.rook( "black", [fila, columna], "rook") # torres if (columna == 2 or columna == 7): board.board.game_board[fila][columna] = knight.knight( "black", [fila, columna], "knight") # caballo de trolla if (columna == 3 or columna == 6): board.board.game_board[fila][ columna] = bishoop.bishoop("black", [fila, columna], "bishoop") # alfil if (columna == 4): board.board.game_board[fila][columna] = queen.queen( "black", [fila, columna], "queen") # reina if (columna == 5): board.board.game_board[fila][columna] = pieces.king( "black", [fila, columna], "king") # rey. if (fila == 2): board.board.game_board[fila][columna] = pieces.pawn( "black", [fila, columna], "pawn") # peones # Para las piezas blancas for fila in range(7, 9): for columna in range(1, 9): if (fila == 8): if (columna == 1 or columna == 8): board.board.game_board[fila][columna] = rook.rook( "white", [fila, columna], "rook") # torres if (columna == 2 or columna == 7): board.board.game_board[fila][columna] = knight.knight( "white", [fila, columna], "knight") # caballo if (columna == 3 or columna == 6): board.board.game_board[fila][ columna] = bishoop.bishoop("white", [fila, columna], "bishoop") # alfil if (columna == 4): board.board.game_board[fila][columna] = queen.queen( "white", [fila, columna], "queen") # reina if (columna == 5): board.board.game_board[fila][columna] = pieces.king( "white", [fila, columna], "king") # rey if (fila == 7): board.board.game_board[fila][columna] = pieces.pawn( "white", [fila, columna], "pawn") # peones
def test_setup(self): for x in range(8): for y in range(8): self.board[x][y][1] = None self.board[2][4][1] = pc.king(self.black, (2, 4)) self.board[6][4][1] = pc.castle(self.white, (6, 4)) self.board[6][1][1] = pc.bishop(self.white, (6, 1)) self.board[3][3][1] = pc.pawn(self.white, (3, 3))
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))
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
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))
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
# 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())
def __init__(self): self.gameId = uuid4() self.gBoard = board.board() for i in range(3): self.gBoard.setPos(0, i, pieces.pawn()) self.gBoard.setPos(2, i, pieces.pawn())