# Returns possible couples of moves/positions for a given start position# player: white | blackdefpossible_moves(player:string,position:array(array(string?))=@position):hash(string,array(array(string?)))possible_moves=hash(string,array(array(string?))).newifplayer=="white"position.each_with_indexdo|row,row_index|row.each_with_indexdo|square,column_index|nextunlesssquarenew_positions=ifsquare=="r"(0..7).mapdo|new_row|(0..7).mapdo|new_column|same_square=new_row==row_index&&new_column==column_indexnextifsame_squaredifferent_row=new_row!=row_indexdifferent_col=new_column!=column_indexnextifdifferent_col&&different_rownextifpiece_of_same_colour_on_destination?(position,player,new_row,new_column)# todo: if a piece on the "row", i can't reach squares beyond the piecepiece_in_between=falsenextifpiece_in_betweennew_position=position.clone# remove piece from current squarenew_position[row][column]=nil# move piece to new squarenew_position[new_row][new_column]=squareletters=('a'..'h').to_auci_move="#{letters[column_index]}#{row_index+1}#{letters[new_column]}#{new_row+1}"possible_moves[uci_move]=new_positionendendendendendelsifplayer=="black"endpossible_movesend
Comments (0)
HTTPSSSH
You can clone a snippet to your computer for local editing.
Learn more.