def get_all_moves(board, color, is_sorted=False): """ Write something about this function here. """ moves = [] piece = Piece(color) pieceK = Piece(color, True) for r in range(8): for c in range(8): if not board.is_free(r, c): if str(board.get(r, c)) is str(piece): valid = tools.get_moves(board, r, c, True) for move in valid: tup = (deindexify(r, c), move) moves.append(tup) elif str(board.get(r, c)) is str(pieceK): valid = tools.get_moves(board, r, c, True) for move in valid: tup = (deindexify(r, c), move) moves.append(tup) if is_sorted: moves.sort() return moves
def get_all_moves(board, color, is_sorted=False): """ This function will give all the possible moves Values: int, string , boolean Returns: list """ moves_c = [] # Gets the dimentions of the board and #the location of the pieces on the board # checks the color of the piece, row and col # checks the type of the returnd piece, and # if it is a allowed type the creates a tuple and appends it to a list. b_length = board.get_length() for row in range(b_length): for col in range(b_length): piece = board.get(row, col) if piece != None: if piece.color() == color: moves = tools.get_moves(board, row, col, is_sorted) position = deindexify(row, col) if moves: for p in moves: m_availabel = (position, p) moves_c.append(m_availabel) if is_sorted: moves_c.sort() return moves_c
def apply_move(board, move): """ Apply a given move to a board object. Raise this exception below: raise RuntimeError("Invalid move, please type" \ + " \'hints\' to get suggestions.") If, a. there is no move from move[0], i.e. use tools.get_moves() function to get all the moves from move[0] b. the destination position move[1] is not in the moves list found from tools.get_moves() function. """ row, col = indexify(move[0])[0], indexify(move[0])[1] moves_list = tools.get_moves(board, row, col) if not move[1] in moves_list: raise RuntimeError("Invalid move, please type" \ + " \'hints\' to get suggestions.") board.place( indexify(move[1])[0], indexify(move[1])[1], board.get(indexify(move[0])[0], indexify(move[0])[1])) board.remove(indexify(move[0])[0], indexify(move[0])[1]) if board.get(indexify(move[1])[0], indexify(move[1])[1]).is_black( ) and move[1][0] == "h": #check if need turn king. board.get(indexify(move[1])[0], indexify(move[1])[1]).turn_king() if board.get(indexify(move[1])[0], indexify(move[1])[1]).is_white() and move[1][0] == "a": board.get(indexify(move[1])[0], indexify(move[1])[1]).turn_king()
def apply_move(board, move): """ Performs actual operations and moves that move the specified pieces. use the if and piece and board class No return Raise this exception below: raise RuntimeError("Invalid move, please type" \ + " \'hints\' to get suggestions.") If, a. there is no move from move[0], i.e. use tools.get_moves() function to get all the moves from move[0] b. the destination position move[1] is not in the moves list found from tools.get_moves() function. """ row, col = indexify(move[0]) row_end, col_end = indexify(move[1]) path_list = tools.get_moves(board, row, col, is_sorted=False) if move[1] in path_list: piece = board.get(row, col) if piece.is_black() and row_end == board.get_length()-1 \ or piece.is_white() and row_end == 0: piece.turn_king() board.remove(row, col) board.place(row_end, col_end, piece) else: raise RuntimeError("Invalid move, please type" \ + " \'hints\' to get suggestions.")
def apply_move(board, move): """ Write something about this function here. Raise this exception below: raise RuntimeError("Invalid move, please type" \ + " \'hints\' to get suggestions.") If, a. there is no move from move[0], i.e. use tools.get_moves() function to get all the moves from move[0] b. the destination position move[1] is not in the moves list found from tools.get_moves() function. """ ''' get all the moves using the get_moves -> list if this list is not empty if moves and move[1] in moves: then you just do the t if not then raise an error raise RunTimeError("Invalid mcove, please type" \ + " \'hints\' to get suggestions.") ''' initial_position = indexify(move[0]) my_move_lst = tools.get_moves(board, initial_position[0], initial_position[1]) next_pos = indexify(move[1]) piece = board.get(initial_position[0], initial_position[1]) if my_move_lst and move[1] in my_move_lst: board.place(next_pos[0], next_pos[1], piece) if piece.color() == 'black' and next_pos[0] == board.get_length( ) - 1 or piece.color() == 'white' and next_pos[0] == 0: piece.turn_king() return board.remove(initial_position[0], initial_position[1]) else: raise Exception("Invalid move, please type" \ + " \'hints\' to get suggestions.")
def get_all_moves(board, color, is_sorted=False): """ Get all moves a player can do. :param board: A board object. :param color: A string of the color of the player. :param is_sorted: A boolean of if sort. :return: A list of all moves. """ all_moves = [] length = board.get_length() for i in range(length): for j in range(length): if board.get(i, j) and board.get(i, j).color() == color: moves = tools.get_moves(board, i, j, is_sorted) for move in moves: all_moves.append((deindexify(i, j), move)) return all_moves
def get_all_moves(board, color, is_sorted=False): """ Get all moves a player can do. :param board: A board object. :param color: A string of the color of the player. :param is_sorted: A boolean of if sort. :return: A list of all moves. """ length_int = board.get_length() all_moves_list = [] for row in range(length_int): for col in range(length_int): if board.get(row, col): if board.get(row, col).color() == color: moves_tuple = tools.get_moves(board, row, col, is_sorted) for move in moves_tuple: all_moves_list.append((deindexify(row, col), move)) return all_moves_list
def get_all_moves(board, color, is_sorted=False): """ Get all the positions of all the pieces on the board that can be moved. use three for loop and three if return list of all move """ row = col = board.get_length() final_list = [] for r in range(row): for c in range(col): piece = board.get(r, c) if piece: if piece.color() == color: path_list = tools.get_moves(board, r, c, is_sorted) path_start = deindexify(r, c) for path in path_list: final_list.append((path_start, path)) if is_sorted == True: final_list.sort() return final_list
def apply_move(board, move): """ This function will try to apply the inputed move by checking if it is a valid move, placing a piece at the new place and removing it from the old place. Raise this exception below: raise RuntimeError("Invalid move, please type" \ + " \'hints\' to get suggestions.") If, a. there is no move from move[0], i.e. use tools.get_moves() function to get all the moves from move[0] b. the destination position move[1] is not in the moves list found from tools.get_moves() function. """ #Get the row and col of my first move row, col = indexify(move[0]) #Get the possible moves from the get_moves function move_list = tools.get_moves(board, row, col) #Create an if statement to check that move_list is not empty and that move[1] is in my list if move_list and move[1] in move_list: #Grab the piece from the board piece = board.get(row, col) #Remove that piece board.remove(row, col) #Get the index of the location where we are putting the piece row1, col1 = indexify(move[1]) #Place the piece we grabbed at the new location board.place(row1, col1, piece) #Create an if statement that if the piece has reached the last row to turn it into a king if (piece.color() == "black" and row1 == board.get_length() - 1) or ( piece.color() == "white" and row1 == 0): piece.turn_king() else: #If the move is not valid, we raise an error raise RuntimeError( "Invalid move, please type 'hints' to get suggestions.")
def get_all_moves(board, color, is_sorted=False): """ Write something about this function here. """ #Create an empty list end_list = [] #loop over every single spot on the board for r in range(board.get_length()): for c in range(board.get_length()): #If that spot is not free, we execute the following statements if not board.is_free(r, c): #We get the position of the coordinates position = deindexify(r, c) #We grab the piece at the coordinates p = board.get(r, c) #We check that the color of the piece is the desired color if p.color() == color: #We get moves from get_moves function moves = tools.get_moves(board, r, c, is_sorted) #Create a for every position in my list for pos in moves: #I append a tuple to my end list end_list.append((position, pos)) return (end_list)