Last active
January 19, 2016 02:51
-
-
Save mkweick/247089ae8fc494d38351 to your computer and use it in GitHub Desktop.
Queen Attack
This file contains hidden or 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 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