Skip to content

Instantly share code, notes, and snippets.

@mkweick
Last active January 19, 2016 02:51
Show Gist options
  • Save mkweick/247089ae8fc494d38351 to your computer and use it in GitHub Desktop.
Save mkweick/247089ae8fc494d38351 to your computer and use it in GitHub Desktop.
Queen Attack
class Queens
attr_reader :white, :black
def initialize(queens = { white: [0, 3], black: [7, 3] })
fail ArgumentError if queens[:white] == queens[:black]
@white = queens[:white]
@black = queens[:black]
end
def to_s
board = 8.times.map { '________' }
board[white[0]][white[1]] = 'W'
board[black[0]][black[1]] = 'B'
board.map! { |row| row.split("").join(" ") }
board.join("\n")
end
def attack?
delta_y = black[0] - white[0]
delta_x = black[1] - white[1]
delta_y == delta_x || delta_y == (-1) * delta_x || delta_y == 0 || delta_x == 0
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment