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 __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 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 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
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"