def valid_move_list(self, b): valid_moves = [] if isinstance(b, Board) is False: return valid_moves # Check if another piece is blocking path if b.square_has_piece(self.x, self.y + 1) is False: valid_moves.append((self.x, self.y + 1)) # Check if piece can be taken diagonally if b.square_has_piece(self.x + 1, self.y + 1, opposite_team(self.team)): valid_moves.append((self.x + 1, self.y + 1)) if b.square_has_piece(self.x - 1, self.y + 1, opposite_team(self.team)): valid_moves.append((self.x - 1, self.y + 1)) # Double Moves if self.has_moved is False and \ b.square_has_piece(self.x, self.y+1) is False and \ b.square_has_piece(self.x, self.y+2) is False: valid_moves.append((self.x, self.y + 2)) for i in valid_moves: if i[0] < 0 or i[1] < 0: del valid_moves[i] return valid_moves
def valid_move_list(self, b): valid_moves = [] if isinstance(b, Board) is False: return valid_moves # Check for valid moves in up direction. for y in range(self.y - 1, -1, -1): if b.square_has_piece(self.x, y) is False: valid_moves.append((self.x, y)) elif b.square_has_piece(self.x, y, opposite_team(self.team)): valid_moves.append((self.x, y)) break else: break # Check for valid moves in down direction. for y in range(self.y + 1, 8): if b.square_has_piece(self.x, y) is False: valid_moves.append((self.x, y)) elif b.square_has_piece(self.x, y, opposite_team(self.team)): valid_moves.append((self.x, y)) break else: break # Check for valid moves in left direction. for x in range(self.x - 1, -1, -1): if b.square_has_piece(x, self.y) is False: valid_moves.append((x, self.y)) elif b.square_has_piece(x, self.y, opposite_team(self.team)): valid_moves.append((x, self.y)) break else: break # Check for valid moves in right direction. for x in range(self.x + 1, 8): if b.square_has_piece(x, self.y) is False: valid_moves.append((x, self.y)) elif b.square_has_piece(x, self.y, opposite_team(self.team)): valid_moves.append((x, self.y)) break else: break for i in valid_moves: if i[0] < 0 or i[1] < 0: del valid_moves[i] return valid_moves
def valid_move_list(self, b): valid_moves = [] if isinstance(b, Board) is False: return valid_moves for x, y in [ (self.x + 1, self.y - 2), # Up-Up-R (self.x + 2, self.y - 1), # Up-R-R (self.x + 2, self.y + 1), # Down-R-R (self.x + 1, self.y + 2), # Down-Down-R (self.x - 1, self.y + 2), # Down-Down-L (self.x - 2, self.y + 1), # Down-L-L (self.x - 2, self.y - 1), # Up-L-L (self.x - 1, self.y - 2) ]: # Up-Up-L if b.square_has_piece(x, y) is False or \ b.square_has_piece(x, y, opposite_team(self.team)): valid_moves.append((x, y)) return valid_moves
def valid_move_list(self, b): valid_moves = [] if isinstance(b, Board) is False: return valid_moves for x, y in [ (self.x, self.y - 1), # Up (self.x + 1, self.y - 1), # Up-R (self.x + 1, self.y), # R (self.x + 1, self.y + 1), # Down-R (self.x, self.y + 1), # Down (self.x - 1, self.y + 1), # Down-L (self.x - 1, self.y), # L (self.x - 1, self.y - 1) ]: # Up-L if b.square_has_piece(x, y) is False or \ b.square_has_piece(x, y, opposite_team(self.team)): valid_moves.append((x, y)) # TODO: Castling return valid_moves
def main(stdscr): # Enable Mouse curses.mousemask(True) # Disable Cursor curses.curs_set(0) # Initialize Color Pairs. # White piece curses.init_pair(1, curses.COLOR_WHITE, curses.COLOR_CYAN) curses.init_pair(2, curses.COLOR_WHITE, curses.COLOR_BLUE) # Black piece curses.init_pair(3, curses.COLOR_BLACK, curses.COLOR_CYAN) curses.init_pair(4, curses.COLOR_BLACK, curses.COLOR_BLUE) # Board highlighting curses.init_pair(5, curses.COLOR_WHITE, curses.COLOR_GREEN) curses.init_pair(6, curses.COLOR_BLACK, curses.COLOR_GREEN) # Off-board curses.init_pair(7, curses.COLOR_YELLOW, curses.COLOR_BLACK) # Setup pieces board = StandardBoard() board_x = 2 board_y = 2 turn = "white" turn_count = 0 stdscr.addstr(0, 1, "Turn:", curses.color_pair(7)) # Main rendering loop while True: # Render board and pieces render_board(board_x, board_y, board, stdscr) stdscr.addstr(0, 6, str(turn), curses.color_pair(7)) stdscr.refresh() piece_moved = False # Select a piece while True: event = stdscr.getch() if event == curses.KEY_MOUSE: try: _, click_x, click_y, _, _ = curses.getmouse() except: continue # If click was a valid piece if board.square_has_piece(click_x - board_x, click_y - board_y, turn): selected_piece = board.grid[click_y - board_y][click_x - board_x] avail_moves = selected_piece.valid_move_list(board) # Update render render_board(board_x, board_y, board, stdscr, avail_moves) stdscr.refresh() break # Select a target square while True: event = stdscr.getch() if event == curses.KEY_MOUSE: try: _, click_x, click_y, _, _ = curses.getmouse() except: continue try: avail_moves.index((click_x - board_x, click_y - board_y)) except ValueError: break else: board.move_piece(selected_piece, click_x - board_x, click_y - board_y) piece_moved = True break if piece_moved: turn = game.opposite_team(turn) turn_count = turn_count + 1 stdscr.refresh()