Last active
July 16, 2018 17:59
-
-
Save Brandongoodman615/c31625581f9ce2adefc0c4e72d82a84a to your computer and use it in GitHub Desktop.
is_obstructed? method
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Piece | |
attr_accessor :x, :y | |
def initialize(x, y) # Every instance of self.x or self.y will need to be self.x_coord or self.y_coord | |
@x = x | |
@y = y | |
end | |
# Need function to automoatically keep up with current pieces coords at all times. | |
def current_pieces_coords # Will need to reference the db of all the current coords. | |
[[0,7],[3,7],[4,7],[5,7],[6,7],[7,7], | |
[2,6],[3,6],[4,6],[5,6],[6,6],[7,6], | |
[0,5],[1,5],[7,5], | |
[2,3],[3,3], | |
[0,1],[1,1],[4,1],[5,1],[6,1],[7,1], | |
[0,0],[1,0],[3,0],[5,0],[6,0],[7,0]] # Represents all the coordinates of pieces in play | |
end | |
def is_vertically_obstructed?(x, y) | |
y_range = [] | |
if self.y < y | |
(self.y..y).each { |n| y_range << n } # If statement catches if numbers go high to low | |
else | |
(y..self.y).each { |n| y_range << n } # Range doesn't work backwards | |
y_range = y_range.reverse # This line reverses it back to the correct order | |
end | |
y_range.pop # removes first number | |
y_range.shift # removes last number | |
coords_to_check = [] | |
y_range.each do |y| #creates the array of coords between points | |
coords_to_check << [x,y] | |
end | |
duplicates = coords_to_check & current_pieces_coords # Creates a variable storing any common coords in both arrays | |
if duplicates.empty? # If empty returns false, It's not obstructed | |
return false | |
else | |
return true | |
end | |
end | |
def is_horizontally_obstructed?(x, y) | |
x_range = [] | |
if self.x < x | |
(self.x..x).each { |n| x_range << n } # If statement catches if numbers go high to low | |
else | |
(x..self.x).each { |n| x_range << n } # Range doesn't work backwards | |
x_range = x_range.reverse # This line reverses it back to the correct order | |
end | |
x_range.pop # removes first number | |
x_range.shift # removes last number | |
coords_to_check = [] | |
x_range.each do |x| #creates the array of coords between points | |
coords_to_check << [x,y] | |
end | |
duplicates = coords_to_check & current_pieces_coords # Creates a variable storing any common coords in both arrays | |
if duplicates.empty? # If empty returns false, It's not obstructed | |
return false | |
else | |
return true | |
end | |
end | |
def is_diagonally_obstructed?(x, y) | |
x_range = [] | |
if self.x < x | |
(self.x..x).each { |n| x_range << n } # If statement catches if numbers go high to low | |
else | |
(x..self.x).each { |n| x_range << n } # Range doesn't work backwards | |
x_range = x_range.reverse # This line reverses it back to the correct order | |
end | |
x_range.pop # removes first number | |
x_range.shift # removes last number | |
y_range = [] | |
if self.y < y | |
(self.y..y).each { |n| y_range << n } | |
else | |
(y..self.y).each { |n| y_range << n } | |
y_range = y_range.reverse | |
end | |
y_range.pop | |
y_range.shift | |
coords_to_check = x_range.zip(y_range) # Combines x and y ranges to make each coord | |
duplicates = coords_to_check & current_pieces_coords # Creates a variable storing any common coords in both arrays | |
if duplicates.empty? # If empty returns false, It's not obstructed | |
return false | |
else | |
return true | |
end | |
end | |
def invalid_input?(x, y) | |
x_range = [] | |
if self.x < x | |
(self.x..x).each { |n| x_range << n } # If statement catches if numbers go high to low | |
else | |
(x..self.x).each { |n| x_range << n } # Range doesn't work backwards | |
x_range = x_range.reverse # This line reverses it back to the correct order | |
end | |
y_range = [] | |
if self.y < y | |
(self.y..y).each { |n| y_range << n } | |
else | |
(y..self.y).each { |n| y_range << n } | |
y_range = y_range.reverse | |
end | |
if x_range.length != y_range.length # If the lengths are different the move can't be diagonal! | |
return "Invalid Input" | |
else | |
return false | |
end | |
end | |
def is_obstructed?(x, y) | |
if self.x == x | |
is_vertically_obstructed?(x,y) | |
elsif self.y == y | |
is_horizontally_obstructed?(x,y) | |
elsif invalid_input?(x,y) == false | |
is_diagonally_obstructed?(x,y) | |
else | |
invalid_input?(x,y) | |
end | |
end | |
end | |
b_rook = Piece.new(0,0) # In console: b_rook = Piece.new | |
w_rook = Piece.new(0,7) # b_rook.x_coord = 0 | |
w_queen = Piece.new(0,5) # b_rook.y_coord = 0 | |
w_knight = Piece.new(3,3) #works the same b_rook = Piece.new(:x_coord => 0, :y_coord => 0) | |
# b_rook.is_obstructed?(0,4) | |
p b_rook.is_obstructed?(0,4) # true | |
p w_rook.is_obstructed?(2,7) # false | |
p w_rook.is_obstructed?(0,5) # false | |
p w_queen.is_obstructed?(2,3) # false | |
p w_queen.is_obstructed?(1,5) # "Invalid Input" | |
p w_knight.is_obstructed?(1,4) # "Invalid Input" | |
p w_knight | |
p w_queen | |
# Pull out coords for positions between start and end point 0,1 0,2 0,3 | |
# Compare those coords to other pieces coords | |
# Return True if there is a match |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment