Skip to content

Instantly share code, notes, and snippets.

@johnzondr
Created August 12, 2015 00:43
Show Gist options
  • Save johnzondr/80df01a54265c67a9ea8 to your computer and use it in GitHub Desktop.
Save johnzondr/80df01a54265c67a9ea8 to your computer and use it in GitHub Desktop.
valid move
x_position = 0
y_position = 0
possible_moves = []
#vertical
vert_x =Array.new(8, x_position)
vert_y =(0..7).to_a
ary_vert = vert_x.zip(vert_y)
#horizontal
horiz_y =Array.new(8,y_position)
horiz_x =(0..7).to_a
ary_horiz = horiz_x.zip(horiz_y)
for ary in ary_vert do
possible_moves << ary
end
for ary in ary_horiz do
possible_moves << ary
end
#diagonals
5.times do |n|
possible_moves << [x_position+(n+1), y_position+(n+1)]
possible_moves << [x_position+(-n-1), y_position+(n+1)]
possible_moves << [x_position+(-n-1), y_position+(-n-1)]
possible_moves << [x_position+(n+1), y_position+(-n-1)]
end
#filter out positions off the board
possible_moves.delete_if do |position|
position[0] < 0 || position[1] < 0
end
possible_moves.delete_if do |position|
position[0] > 7 || position[1] > 7
end
p possible_moves
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment