def initiate(self): self.append_piece(piece.Rook('b'), 0, 0) self.append_piece(piece.Knight('b'), 0, 1) self.append_piece(piece.Bishop('b'), 0, 2) self.append_piece(piece.Queen('b'), 0, 3) self.append_piece(piece.King('b'), 0, 4) self.append_piece(piece.Bishop('b'), 0, 5) self.append_piece(piece.Knight('b'), 0, 6) self.append_piece(piece.Rook('b'), 0, 7) self.append_piece(piece.Pawn('b'), 1, 0) self.append_piece(piece.Pawn('b'), 1, 1) self.append_piece(piece.Pawn('b'), 1, 2) self.append_piece(piece.Pawn('b'), 1, 3) self.append_piece(piece.Pawn('b'), 1, 4) self.append_piece(piece.Pawn('b'), 1, 5) self.append_piece(piece.Pawn('b'), 1, 6) self.append_piece(piece.Pawn('b'), 1, 7) self.append_piece(piece.Rook('w'), 7, 0) self.append_piece(piece.Knight('w'), 7, 1) self.append_piece(piece.Bishop('w'), 7, 2) self.append_piece(piece.Queen('w'), 7, 3) self.append_piece(piece.King('w'), 7, 4) self.append_piece(piece.Bishop('w'), 7, 5) self.append_piece(piece.Knight('w'), 7, 6) self.append_piece(piece.Rook('w'), 7, 7) self.append_piece(piece.Pawn('w'), 6, 0) self.append_piece(piece.Pawn('w'), 6, 1) self.append_piece(piece.Pawn('w'), 6, 2) self.append_piece(piece.Pawn('w'), 6, 3) self.append_piece(piece.Pawn('w'), 6, 4) self.append_piece(piece.Pawn('w'), 6, 5) self.append_piece(piece.Pawn('w'), 6, 6) self.append_piece(piece.Pawn('w'), 6, 7)
def _built_board(self): colors = ["white", "black"] * (self.cols + 2) self.board = [[ square.Square(i, j, colors[j + i % 2]) for j in range(self.cols) ] for i in range(self.rows)] # Black pieces # Rook, Knight, Bishop, King, Queen, Pawns self.board[0][0].set_piece(piece.Rook(self.board[0][0], Board.black)) self.board[0][7].set_piece(piece.Rook(self.board[0][7], Board.black)) self.board[0][1].set_piece(piece.Knight(self.board[0][1], Board.black)) self.board[0][6].set_piece(piece.Knight(self.board[0][6], Board.black)) self.board[0][2].set_piece(piece.Bishop(self.board[0][2], Board.black)) self.board[0][5].set_piece(piece.Bishop(self.board[0][5], Board.black)) self.board[0][3].set_piece(piece.King(self.board[0][3], Board.black)) self.board[0][4].set_piece(piece.Queen(self.board[0][4], Board.black)) for j in xrange(0, self.cols): self.board[1][j].set_piece( piece.Pawn(self.board[1][j], Board.black)) # White pieces # Rook, Knight, Bishop, King, Queen, Pawns self.board[7][0].set_piece(piece.Rook(self.board[7][0], Board.white)) self.board[7][7].set_piece(piece.Rook(self.board[7][7], Board.white)) self.board[7][1].set_piece(piece.Knight(self.board[7][1], Board.white)) self.board[7][6].set_piece(piece.Knight(self.board[7][6], Board.white)) self.board[7][2].set_piece(piece.Bishop(self.board[7][2], Board.white)) self.board[7][5].set_piece(piece.Bishop(self.board[7][5], Board.white)) self.board[7][4].set_piece(piece.King(self.board[7][4], Board.white)) self.board[7][3].set_piece(piece.Queen(self.board[7][3], Board.white)) for j in xrange(0, self.cols): self.board[6][j].set_piece( piece.Pawn(self.board[6][j], Board.white)) self.kings = [self.board[0][3], self.board[7][4]]
def makeMove(self, initialX, initialY, posX, posY): posY = int(posY) - 1 initialY = int(initialY) - 1 #Get the piece in question and update its knowledge of its location, make the move. If there was a piece in the new spot, update the piece tracker. current_soldier = self.positions[initialX][initialY] current_soldier.posX = posX current_soldier.posY = posY old_soldier = self.positions[posX][posY] self.positions[posX][posY] = current_soldier self.positions[initialX][initialY] = ' ' if type(old_soldier) != str: self.update_pieces() #Update logic for individual pieces with special cases if type(current_soldier) == piece.King or type(current_soldier) == piece.Rook: current_soldier.hasNotMovedYet = False if type(current_soldier) == piece.Pawn: if current_soldier.first_turn: current_soldier.first_turn = False #Handle promotion logic for pawns and update piece tracker to reflect this. if current_soldier.posY == self.white_pawn_goal and current_soldier.side == "White": picked = False while not picked: promotion = input("Enter what kind of piece you want your pawn to be promoted to - your options are Q, Kn, R or B: ") if promotion == "Kn": self.positions[current_soldier.posX][current_soldier.posY] = piece.Knight('wKn', 'White', current_soldier.posX, current_soldier.posY) picked = True elif promotion == "Q": self.positions[current_soldier.posX][current_soldier.posY] = piece.Queen('wQ', 'White', current_soldier.posX, current_soldier.posY) picked = True elif promotion == "R": self.positions[current_soldier.posX][current_soldier.posY] = piece.Rook('wR', 'White', current_soldier.posX, current_soldier.posY) picked = True elif promotion == "B": self.positions[current_soldier.posX][current_soldier.posY] = piece.Bishop('wB', 'White', current_soldier.posX, current_soldier.posY) picked = True else: self.printer() print("Issue with the piece name you're inputting") self.update_pieces() elif current_soldier.posY == self.black_pawn_goal and current_soldier.side == "Black": picked = False while not picked: promotion = input("Enter what kind of piece you want your pawn to be promoted to - your options are Q, Kn, R or B: ") if promotion == "Kn": self.positions[current_soldier.posX][current_soldier.posY] = piece.Knight('bKn', 'Black', current_soldier.posX, current_soldier.posY) picked = True elif promotion == "Q": self.positions[current_soldier.posX][current_soldier.posY] = piece.Queen('bQ', 'Black', current_soldier.posX, current_soldier.posY) picked = True elif promotion == "R": self.positions[current_soldier.posX][current_soldier.posY] = piece.Rook('bR', 'Black', current_soldier.posX, current_soldier.posY) picked = True elif promotion == "B": self.positions[current_soldier.posX][current_soldier.posY] = piece.Bishop('bB', 'Black', current_soldier.posX, current_soldier.posY) picked = True else: print("Issue with the piece name you're inputting") self.update_pieces()
def __init__ (self): self.board = [[None for _ in range(8)] for _ in range(8)] self.board[0][0] = ColoredChessPiece("W", piece.Rook(1,1)) self.board[0][7] = ColoredChessPiece("W", piece.Rook(8,1)) self.board[0][1] = ColoredChessPiece("W", piece.Knight(2,1)) self.board[0][6] = ColoredChessPiece("W", piece.Knight(7,1)) self.board[0][2] = ColoredChessPiece("W", piece.Bishop(3,1)) self.board[0][5] = ColoredChessPiece("W", piece.Bishop(6,1)) self.board[0][3] = ColoredChessPiece("W", piece.Queen(4,1)) self.board[0][4] = ColoredChessPiece("W", piece.King(5,1,5,1)) self.board[1][0] = ColoredChessPiece("W", piece.Pawn(1,2,2)) self.board[1][1] = ColoredChessPiece("W", piece.Pawn(2,2,2)) self.board[1][2] = ColoredChessPiece("W", piece.Pawn(3,2,2)) self.board[1][3] = ColoredChessPiece("W", piece.Pawn(4,2,2)) self.board[1][4] = ColoredChessPiece("W", piece.Pawn(5,2,2)) self.board[1][5] = ColoredChessPiece("W", piece.Pawn(6,2,2)) self.board[1][6] = ColoredChessPiece("W", piece.Pawn(7,2,2)) self.board[1][7] = ColoredChessPiece("W", piece.Pawn(8,2,2)) self.board[7][0] = ColoredChessPiece("B", piece.Rook(1,8)) self.board[7][7] = ColoredChessPiece("B", piece.Rook(8,8)) self.board[7][1] = ColoredChessPiece("B", piece.Knight(2,8)) self.board[7][6] = ColoredChessPiece("B", piece.Knight(7,8)) self.board[7][2] = ColoredChessPiece("B", piece.Bishop(3,8)) self.board[7][5] = ColoredChessPiece("B", piece.Bishop(6,8)) self.board[7][3] = ColoredChessPiece("B", piece.Queen(4,8)) self.board[7][4] = ColoredChessPiece("B", piece.King(5,8,5,8)) self.board[6][0] = ColoredChessPiece("B", piece.Pawn(1,7,7)) self.board[6][1] = ColoredChessPiece("B", piece.Pawn(2,7,7)) self.board[6][2] = ColoredChessPiece("B", piece.Pawn(3,7,7)) self.board[6][3] = ColoredChessPiece("B", piece.Pawn(4,7,7)) self.board[6][4] = ColoredChessPiece("B", piece.Pawn(5,7,7)) self.board[6][5] = ColoredChessPiece("B", piece.Pawn(6,7,7)) self.board[6][6] = ColoredChessPiece("B", piece.Pawn(7,7,7)) self.board[6][7] = ColoredChessPiece("B", piece.Pawn(8,7,7)) self.__white_king_moved__ = False self.__black_king_moved__ = False self.__white_a_rook_moved__ = False self.__white_e_rook_moved__ = False self.__black_a_rook_moved__ = False self.__black_e_rook_moved__ = False self.__en_passant_possible__ = -1
def spawnPiece(self, piecetype, team, x, y, z, d): col = color.white if (team == 1): col = color.red if (piecetype == 'pawn'): self.addPiece(x, y, z, d, piece.Pawn(vector(x, y, z), col, self.spots)) if (piecetype == 'knight'): self.addPiece(x, y, z, d, piece.Knight(vector(x, y, z), col, self.spots)) if (piecetype == 'rook'): self.addPiece(x, y, z, d, piece.Rook(vector(x, y, z), col, self.spots)) if (piecetype == 'bishop'): self.addPiece(x, y, z, d, piece.Bishop(vector(x, y, z), col, self.spots)) if (piecetype == 'king'): self.addPiece(x, y, z, d, piece.King(vector(x, y, z), col, self.spots)) if (col == color.red): self.redkingpos = fvec.Fvector(x, y, z, d) else: self.whitekingpos = fvec.Fvector(x, y, z, d) if (piecetype == 'queen'): self.addPiece(x, y, z, d, piece.Queen(vector(x, y, z), col, self.spots))
def promote(self, piec): posvec = piec.getpos() c = 0 if piec.ogcolor == color.red else 3 if (posvec.z == c and posvec.d == c): picked = False while (not picked): scene.caption = 'Please choose a piece to upgrade your pawn to q/k/r/b' ev = scene.waitfor('keydown') if ev.event == 'keydown': picked = True if ev.key == 'q': col = self.spots[posvec.d][posvec.z][posvec.y][ posvec.x].ogcolor self.spots[posvec.d][posvec.z][posvec.y][ posvec.x].setvisible(False) self.spots[posvec.d][posvec.z][posvec.y][ posvec.x] = None self.addPiece( posvec.x, posvec.y, posvec.z, posvec.d, piece.Queen(vector(posvec.x, posvec.y, posvec.z), col, self.spots)) elif ev.key == 'k': col = self.spots[posvec.d][posvec.z][posvec.y][ posvec.x].ogcolor self.spots[posvec.d][posvec.z][posvec.y][ posvec.x].setvisible(False) self.spots[posvec.d][posvec.z][posvec.y][ posvec.x] = None self.addPiece( posvec.x, posvec.y, posvec.z, posvec.d, piece.Knight(vector(posvec.x, posvec.y, posvec.z), col, self.spots)) elif ev.key == 'b': col = self.spots[posvec.d][posvec.z][posvec.y][ posvec.x].ogcolor self.spots[posvec.d][posvec.z][posvec.y][ posvec.x].setvisible(False) self.spots[posvec.d][posvec.z][posvec.y][ posvec.x] = None self.addPiece( posvec.x, posvec.y, posvec.z, posvec.d, piece.Bishop(vector(posvec.x, posvec.y, posvec.z), col, self.spots)) elif ev.key == 'r': col = self.spots[posvec.d][posvec.z][posvec.y][ posvec.x].ogcolor self.spots[posvec.d][posvec.z][posvec.y][ posvec.x].setvisible(False) self.spots[posvec.d][posvec.z][posvec.y][ posvec.x] = None self.addPiece( posvec.x, posvec.y, posvec.z, posvec.d, piece.Rook(vector(posvec.x, posvec.y, posvec.z), col, self.spots)) else: picked = False else: return False
def init_board_array(self): temp = [[ piece.Rook((0, 0), False, 'r'), piece.Knight((0, 1), False, 'n'), piece.Bishop((0, 2), False, 'b'), piece.Queen((0, 3), False, 'q'), piece.King((0, 4), False, 'k'), piece.Bishop((0, 5), False, 'b'), piece.Knight((0, 6), False, 'n'), piece.Rook((0, 7), False, 'r') ], [ piece.Pawn((1, 0), False, 'p'), piece.Pawn((1, 1), False, 'p'), piece.Pawn((1, 2), False, 'p'), piece.Pawn((1, 3), False, 'p'), piece.Pawn((1, 4), False, 'p'), piece.Pawn((1, 5), False, 'p'), piece.Pawn((1, 6), False, 'p'), piece.Pawn((1, 7), False, 'p') ], [None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None], [None, None, None, None, None, None, None, None], [ piece.Pawn((6, 0), True, 'P'), piece.Pawn((6, 1), True, 'P'), piece.Pawn((6, 2), True, 'P'), piece.Pawn((6, 3), True, 'P'), piece.Pawn((6, 4), True, 'P'), piece.Pawn((6, 5), True, 'P'), piece.Pawn((6, 6), True, 'P'), piece.Pawn((6, 7), True, 'P') ], [ piece.Rook((7, 0), 1, 'R'), piece.Knight((7, 1), True, 'N'), piece.Bishop((7, 2), True, 'B'), piece.Queen((7, 3), True, 'Q'), piece.King((7, 4), True, 'K'), piece.Bishop((7, 5), True, 'B'), piece.Knight((7, 6), True, 'N'), piece.Rook((7, 7), True, 'R') ]] return temp
def update(self, m): self.execute(m) if self.promotion: p = piece.Queen(m.finish.p.color) self.last_move.append(p.color) self.append_piece(p, *m.finish.coord) self.promotion = False print(self) print(len(self.last_move))
def check_pawn_prom(self, piece, pos_end): if isinstance(piece, p.Pawn): if (piece.color is True) & (piece.pos[1] == self.ul): piece = p.Queen( (pos_end[0], pos_end[1]), color=piece.color, value=9, image=load("../images/w_queen.png"), ) self.place_piece(piece) elif (piece.color is False) & (piece.pos[1] == self.ll): piece = p.Queen( (pos_end[0], pos_end[1]), color=piece.color, value=-9, image=load("../images/b_queen.png"), ) self.place_piece(piece) return piece
def _built_board(self): colors = ["white", "black"] * (self.cols + 2) self.board = [[ square.Square(i, j, colors[j + i % 2]) for j in range(self.cols) ] for i in range(self.rows)] # Black pieces self.board[0][0].set_piece(piece.King(self.board[0][0], Board.black)) # White pieces self.board[7][7].set_piece(piece.King(self.board[7][7], Board.white)) self.board[0][2].set_piece(piece.Rook(self.board[0][2], Board.white)) self.board[2][0].set_piece(piece.Rook(self.board[2][0], Board.white)) self.board[4][0].set_piece(piece.Queen(self.board[4][0], Board.white)) self.kings = [self.board[7][7], self.board[0][0]]
def move_piece(self, ipoint, fpoint): if ipoint == "0-0-0": return False elif ipoint == "0-0": return False elif ipoint and fpoint: ipiece = self.board_array[ipoint[0]][ipoint[1]] fpiece = self.board_array[fpoint[0]][fpoint[1]] if fpiece: if fpiece.get_symbol() in ('k', 'K'): return False else: self.cap_piece(fpiece) self.board_array[fpoint[0]][fpoint[1]] = ipiece ipiece.set_pos(fpoint) ipiece.inc_move_count() self.board_array[ipoint[0]][ipoint[1]] = None symbol = ipiece.get_symbol() if symbol in ('p', 'P') and fpoint[0] in (0, 8): if symbol.isupper(): self.board_array[fpoint[0]][fpoint[1]] = piece.Queen( fpoint, ipiece.get_color(), 'Q') elif symbol.islower(): self.board_array[fpoint[0]][fpoint[1]] = piece.Queen( fpoint, ipiece.get_color(), 'q') elif symbol in ('k', 'K'): if symbol.isupper(): self.white_king = ipiece else: self.black_king = ipiece self.inc_turn() return True else: return False
def populate(self): self.positions['a'][7] = piece.Rook('bR', 'Black', 'a', '7') self.positions['b'][7] = piece.Knight('bKn', 'Black', 'b', '7') self.positions['c'][7] = piece.Bishop('bB', 'Black', 'c', '7') self.positions['d'][7] = piece.Queen('bQ', 'Black', 'd', '7') self.positions['e'][7] = piece.King('bK', 'Black', 'e', '7') self.positions['f'][7] = piece.Bishop('bB', 'Black', 'f', '7') self.positions['g'][7] = piece.Knight('bKn', 'Black', 'g', '7') self.positions['h'][7] = piece.Rook('bR', 'Black', 'h', '7') self.positions['a'][0] = piece.Rook('wR', 'White', 'a', '0') self.positions['b'][0] = piece.Knight('wKn', 'White', 'b', '0') self.positions['c'][0] = piece.Bishop('wB', 'White', 'c', '0') self.positions['d'][0] = piece.Queen('wQ', 'White', 'd', '0') self.positions['e'][0] = piece.King('wK', 'White', 'e', '0') self.positions['f'][0] = piece.Bishop('wB', 'White', 'f', '0') self.positions['g'][0] = piece.Knight('wKn', 'White', 'g', '0') self.positions['h'][0] = piece.Rook('wR', 'White', 'h', '0') for letter in self.letters: self.positions[letter][6] = piece.Pawn('bP', 'Black', letter, '6') self.positions[letter][1] = piece.Pawn('wP', 'White', letter, '1') for letter in self.letters: for num in self.positions[letter]: if type(num) != str: self.pieces.append(num)
if __name__ == '__main__': chess = chessBoard.ChessBoard() parser = argparse.ArgumentParser(description=( "Find a list of all the potential board positions the given piece could advance to, \ with one move, from the given position, with the assumption there are no other pieces on the board" )) parser.add_argument('-piece', metavar='piece', type=str, choices=['KNIGHT', 'ROOK', 'QUEEN'], help="Only support KNIGHT|ROOK|QUEEN.") parser.add_argument( '-position', metavar = 'position', type = str, choices=chess.getCells(), \ help = "position in the board that piece is on") res = parser.parse_args() (row, col), ok = chess.toIndex(res.position) if res.piece == "KNIGHT": p = piece.Knight(row, col, chess) elif res.piece == "QUEEN": p = piece.Queen(row, col, chess) else: p = piece.Rook(row, col, chess) s = ", ".join(p.possibleMoves()) print(f'"{s}"')
def __init__(self, master): self._turn = 1 self.n = 8 # length checkers on board self.wking_loc = [0] * 2 self.wking_loc[0] = 7 self.wking_loc[1] = 4 self.bking_loc = [0, 4] # self.game_board = [[piece.EmptyPiece()]*8]*8 WRONG, duplicates the 1D list 8 times (same lists) # self.game_board = { (pp,qq):0 for pp in range(8) for qq in range(8) } if were using a dictionary self.game_board = [[piece.EmptyPiece() for pp in range(8)] for qq in range(8)] for s in range(0, 8): self.game_board[6][s] = piece.Pawn(defines.white_piece, defines.pawn) self.game_board[1][s] = piece.Pawn(defines.black_piece, defines.pawn) self.game_board[7][4] = piece.King(defines.white_piece, defines.king) self.game_board[0][4] = piece.King(defines.black_piece, defines.king) self.game_board[7][3] = piece.Queen(defines.white_piece, defines.queen) self.game_board[0][3] = piece.Queen(defines.black_piece, defines.queen) self.game_board[7][0] = piece.Rook(defines.white_piece, defines.rook) self.game_board[0][0] = piece.Rook(defines.black_piece, defines.rook) self.game_board[7][7] = piece.Rook(defines.white_piece, defines.rook) self.game_board[0][7] = piece.Rook(defines.black_piece, defines.rook) self.game_board[7][2] = piece.Bishop(defines.white_piece, defines.bishop) self.game_board[0][2] = piece.Bishop(defines.black_piece, defines.bishop) self.game_board[7][5] = piece.Bishop(defines.white_piece, defines.bishop) self.game_board[0][5] = piece.Bishop(defines.black_piece, defines.bishop) self.game_board[7][6] = piece.Knight(defines.white_piece, defines.knight) self.game_board[0][6] = piece.Knight(defines.black_piece, defines.knight) self.game_board[7][1] = piece.Knight(defines.white_piece, defines.knight) self.game_board[0][1] = piece.Knight(defines.black_piece, defines.knight) # Graphics self.master = master self.master.title('ChessPy') self.master.configure(background='#e1d8b9') self.master.minsize(400, 470) self.master.resizable(True, True) self.master.bind('<Configure>', lambda e: self.draw_board( )) # call draw_board() in the event of new Hight and Width window size self.style = ttk.Style() self.style.configure('TFrame', background='#e1d8b9') self.style.configure('TButton', background='#e1d8b9') self.style.configure('TLabel', background='#e1d8b9') self.board_canvas = Canvas( self.master) # used to draw shapes: line, oval,... self.board_canvas.pack() self.controls_frame = ttk.Frame(self.master) self.controls_frame.pack(side=TOP, pady=20) self.n_var = StringVar() self.n_var.set(self.n) # Spinbox(self.controls_frame, from_ = 4, to = 99, width = 2, # font = 'Verdana 10 bold', textvariable = self.n_var).grid(row = 0, column = 1) self.from_row_var = StringVar() self.from_col_var = StringVar() self.to_row_var = StringVar() self.to_col_var = StringVar() #self.to_col_var.set('0') """ location entry widgets """ ttk.Label(self.controls_frame, text='Move From Row:', font='Verdana 10 bold').grid(row=0, column=0, sticky=(W), padx=10) # , sticky= (E) self.from_row_entry = ttk.Entry( self.controls_frame, width=10, #textvariable = self.from_row_var, font='Verdana 10') #, sticky = (W) self.from_row_entry.grid(row=0, column=1, sticky=(W)) ttk.Label(self.controls_frame, text='Move From Column:', font='Verdana 10 bold').grid(row=1, column=0, sticky=(W), padx=10) self.from_col_entry = ttk.Entry( self.controls_frame, width=10, #textvariable = self.from_col_var, font='Verdana 10') self.from_col_entry.grid(row=1, column=1, sticky=(W)) ttk.Label(self.controls_frame, text='Move To Row:', font='Verdana 10 bold').grid(row=2, column=0, sticky=(W), padx=10) self.to_row_entry = ttk.Entry( self.controls_frame, width=10, #textvariable = self.to_row_var, font='Verdana 10') self.to_row_entry.grid(row=2, column=1, sticky=(W)) ttk.Label(self.controls_frame, text='Move To Column:', font='Verdana 10 bold').grid(row=3, column=0, sticky=(W), padx=10) self.to_col_entry = ttk.Entry( self.controls_frame, width=10, #textvariable = self.to_col_var, font='Verdana 10') self.to_col_entry.grid(row=3, column=1, sticky=(W)) # self.from_row_entry.pack() # self.from_col_entry.pack() # self.to_row_entry.pack() # self.to_col_entry.pack() # self.from_row_entry["textvariable"] = self.from_row_var self.from_col_entry["textvariable"] = self.from_col_var self.to_row_entry["textvariable"] = self.to_row_var self.to_col_entry["textvariable"] = self.to_col_var # and here we get a callback when the user hits return. #self.to_col_entry.bind('<Key-Return>',self.move_callback) #self.to_col_var.get() """ Move button """ ttk.Button(self.controls_frame, text='Enter Move', command=self.move_callback).grid(row=4, column=1, columnspan=2, sticky=(W), padx=10, pady=10) ttk.Label(self.controls_frame, text='to exit type q', font='Verdana 8').grid(row=7, column=0, sticky=(W), pady=10) # , sticky= (E) # ttk.Label(self.controls_frame).grid(row = 0, column = 2, padx = 10) # spacer turn_color = "WHITE" if self._turn == 1 else "BLACK" ttk.Label(self.controls_frame, text=turn_color + ' turn', font='Verdana 8').grid(row=6, column=0, sticky=(W), pady=10) self.draw_board()
poss.resetPiece() else: take = False gameboard.movePiece(poss, nextMove) if gameboard.checkCheck(currentPlayer): ## Check if the move is legal if take: gameboard.piecesGroup.add(temp) gameboard.movePiece(poss, nextMove) else: gameboard.movePiece(poss, move.Move(originalCoords)) else: ## Check for pawn promotion if poss.piece == "Pawn" and poss.coords[1] in [0,7]: gameboard.piecesGroup.remove(poss) gameboard.piecesGroup.add(piece.Queen(poss.colour, [poss.coords[0], poss.coords[1]])) moved = True elif inCheck and poss.colour == currentPlayer and (mousex // SQUAREWIDTH, mousey // SQUAREWIDTH) in [x.coords for x in possibleMoves]: currentCoords = (poss.coords[0], poss.coords[1]) if not gameboard.isSquareEmpty((moveX, moveY)): for target in gameboard.piecesGroup.sprites(): if target.coords[0] == moveX and target.coords[1] == moveY: if target.colour != poss.colour: temp = target piecesGroup.remove(target) gameboard.movePiece(poss, nextMove) if not gameboard.checkCheck(currentPlayer): gameboard.takePiece(poss, target, nextMove) moved = True
def create_pieces(self): ps = [ p.Rook( (0, 7), color=True, value=5, image=load("../images/w_rook.png"), ), p.Rook( (7, 7), color=True, value=5, image=load("../images/w_rook.png"), ), p.Bishop( (2, 7), color=True, value=3, image=load("../images/w_bishop.png"), ), p.Bishop( (5, 7), color=True, value=3, image=load("../images/w_bishop.png"), ), p.Knight( (1, 7), color=True, value=3, image=load("../images/w_knight.png"), ), p.Knight( (6, 7), color=True, value=3, image=load("../images/w_knight.png"), ), p.Rook( (0, 0), color=False, value=-5, image=load("../images/b_rook.png"), ), p.Rook( (7, 0), color=False, value=-5, image=load("../images/b_rook.png"), ), p.Knight( (1, 0), color=False, value=-3, image=load("../images/b_knight.png"), ), p.Knight( (6, 0), color=False, value=-3, image=load("../images/b_knight.png"), ), p.Bishop( (2, 0), color=False, value=-3, image=load("../images/b_bishop.png"), ), p.Bishop( (5, 0), color=False, value=-3, image=load("../images/b_bishop.png"), ), p.King( (4, 7), color=True, value=1000, image=load("../images/w_king.png"), ), p.Queen( (3, 7), color=True, value=9, image=load("../images/w_queen.png"), ), p.King( (4, 0), color=False, value=-1000, image=load("../images/b_king.png"), ), p.Queen( (3, 0), color=False, value=-9, image=load("../images/b_queen.png"), ), ] for i in range(8): ps += [ p.Pawn( (i, 6), color=True, value=1, image=pygame.image.load("../images/w_pawn.png"), ), p.Pawn( (i, 1), color=False, value=-1, image=pygame.image.load("../images/b_pawn.png"), ), ] return ps
import board import piece import copy #these are the pieces that each player will initially start each game with p1InitializedPieces = [piece.Rook("R2",0,0,False),piece.Knight("K2",0,1,False),piece.Bishop("B2",0,2,False),piece.Queen("Q1",0,3,False),\ piece.King("KG",0,4,False),piece.Bishop("B1",0,5,False), piece.Knight("K1",0,6,False),piece.Rook("R1",0,7,False), \ piece.Pawn("P8",1,0,False),piece.Pawn("P7",1,1,False),piece.Pawn("P6",1,2,False),piece.Pawn("P5",1,3,False),piece.Pawn("P4",1,4,False), \ piece.Pawn("P3",1,5,False),piece.Pawn("P2",1,6,False),piece.Pawn("P1",1,7,False)] p2InitializedPieces = [piece.Pawn("P1",6,0,False),piece.Pawn("P2",6,1,False),piece.Pawn("P3",6,2,False),piece.Pawn("P4",6,3,False),piece.Pawn("P5",6,4,False), \ piece.Pawn("P6",6,5,False),piece.Pawn("P7",6,6,False),piece.Pawn("P8",6,7,False), \ piece.Rook("R1",7,0,False),piece.Knight("K1",7,1,False),piece.Bishop("B1",7,2,False),piece.King("KG",7,3,False),\ piece.Queen("Q1",7,4,False),piece.Bishop("B2",7,5,False), piece.Knight("K2",7,6,False),piece.Rook("R2",7,7,False)] class Player: '''Player objects will have a list of Piece objects that are owned by the player ''' def __init__(self, name, isPlayer1): #constructor self.name = name self.isPlayer1 = isPlayer1 playerId = name[0] if isPlayer1 == True: self.myPieces = p1InitializedPieces else: self.myPieces = p2InitializedPieces listLength = len(self.myPieces) counter = 0 while counter != listLength: #add a identifier(playerName[0]) to each piece