Created
April 6, 2016 21:05
-
-
Save chadfennell/3287ee666f7c9bac29324d81ad96571c to your computer and use it in GitHub Desktop.
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 ChessCell | |
attr_reader :row, :column, :coordinates | |
def initialize(coordinates) | |
@row, @column = coordinates | |
@coordinates = coordinates | |
end | |
end | |
class Queen | |
attr_reader :attackable, :cell | |
def initialize(coordinates, cell: ChessCell) | |
@attackable = [coordinates] | |
@cell = cell.new(coordinates) | |
end | |
def attackables | |
[row_columns, | |
column_rows, | |
right_up, | |
left_up, | |
right_down, | |
left_down].reduce(:concat) | |
end | |
def row_columns | |
(0..7).map { |col| [cell.row, col] } | |
end | |
def column_rows | |
(0..7).map { |row| [row, cell.column] } | |
end | |
def right_up | |
row = cell.row | |
increment(cell.column).upto(7).map do |column| | |
[row = decrement(row), column] | |
end | |
end | |
def left_up | |
row = cell.row | |
decrement(cell.column).downto(0).map do |column| | |
[row = decrement(row), column] | |
end | |
end | |
def right_down | |
column = cell.column | |
increment(cell.row).upto(7).map do |row| | |
[row, column = increment(column)] | |
end | |
end | |
def left_down | |
column = cell.column | |
increment(cell.row).upto(7).map do |row| | |
[row, column = decrement(column)] | |
end | |
end | |
def decrement(cell) | |
cell - 1 | |
end | |
def increment(cell) | |
cell + 1 | |
end | |
end | |
class Queens | |
attr_reader :white, :black | |
def initialize(white: [0, 3], black: [7, 3]) | |
@white = white | |
@black = black | |
validate_coordinates | |
end | |
def validate_coordinates | |
raise ArgumentError if white == black | |
end | |
def attack? | |
Queen.new(black).attackables.include?(white) | |
end | |
def to_s | |
rows = [] | |
0.upto(7).each do |row| | |
cols = [] | |
0.upto(7).each do |column| | |
cols << draw_cell([row, column]) | |
end | |
rows << cols.join(' ') | |
end | |
rows.join("\n") | |
end | |
def draw_cell(coordinates) | |
case coordinates | |
when white | |
"W" | |
when black | |
"B" | |
else | |
"_" | |
end | |
end | |
end | |
#!/usr/bin/env ruby | |
gem 'minitest', '>= 5.0.0' | |
require 'minitest/autorun' | |
require_relative 'queen_attack' | |
class QueensTest < Minitest::Test | |
def test_default_positions | |
queens = Queens.new | |
assert_equal [0, 3], queens.white | |
assert_equal [7, 3], queens.black | |
end | |
def test_specific_placement | |
queens = Queens.new(white: [3, 7], black: [6, 1]) | |
assert_equal [3, 7], queens.white | |
assert_equal [6, 1], queens.black | |
end | |
def test_multiple_boards_simultaneously | |
queens1 = Queens.new(white: [3, 7], black: [6, 1]) | |
queens2 = Queens.new(white: [5, 4], black: [7, 7]) | |
assert_equal [3, 7], queens1.white | |
assert_equal [6, 1], queens1.black | |
assert_equal [5, 4], queens2.white | |
assert_equal [7, 7], queens2.black | |
end | |
def test_cannot_occupy_same_space | |
assert_raises ArgumentError do | |
Queens.new(white: [2, 4], black: [2, 4]) | |
end | |
end | |
def test_string_representation # rubocop:disable Metrics/MethodLength | |
queens = Queens.new(white: [2, 4], black: [6, 6]) | |
board = <<-BOARD.chomp | |
_ _ _ _ _ _ _ _ | |
_ _ _ _ _ _ _ _ | |
_ _ _ _ W _ _ _ | |
_ _ _ _ _ _ _ _ | |
_ _ _ _ _ _ _ _ | |
_ _ _ _ _ _ _ _ | |
_ _ _ _ _ _ B _ | |
_ _ _ _ _ _ _ _ | |
BOARD | |
assert_equal board, queens.to_s | |
end | |
def test_another_string_representation # rubocop:disable Metrics/MethodLength | |
queens = Queens.new(white: [7, 1], black: [0, 0]) | |
board = <<-BOARD.chomp | |
B _ _ _ _ _ _ _ | |
_ _ _ _ _ _ _ _ | |
_ _ _ _ _ _ _ _ | |
_ _ _ _ _ _ _ _ | |
_ _ _ _ _ _ _ _ | |
_ _ _ _ _ _ _ _ | |
_ _ _ _ _ _ _ _ | |
_ W _ _ _ _ _ _ | |
BOARD | |
assert_equal board, queens.to_s | |
end | |
# rubocop:disable Metrics/MethodLength | |
def test_yet_another_string_representation | |
queens = Queens.new(white: [4, 3], black: [3, 4]) | |
board = <<-BOARD.chomp | |
_ _ _ _ _ _ _ _ | |
_ _ _ _ _ _ _ _ | |
_ _ _ _ _ _ _ _ | |
_ _ _ _ B _ _ _ | |
_ _ _ W _ _ _ _ | |
_ _ _ _ _ _ _ _ | |
_ _ _ _ _ _ _ _ | |
_ _ _ _ _ _ _ _ | |
BOARD | |
assert_equal board, queens.to_s | |
end | |
def test_cannot_attack | |
queens = Queens.new(white: [2, 3], black: [4, 7]) | |
assert !queens.attack? | |
end | |
# rubocop:enable Metrics/MethodLength | |
def test_can_attack_on_same_row | |
queens = Queens.new(white: [2, 4], black: [2, 7]) | |
assert queens.attack? | |
end | |
def test_can_attack_on_same_row_reverse | |
queens = Queens.new(white: [2, 7], black: [2, 4]) | |
assert queens.attack? | |
end | |
def test_can_attack_on_same_column | |
queens = Queens.new(white: [5, 4], black: [2, 4]) | |
assert queens.attack? | |
end | |
def test_can_attack_on_diagonal | |
queens = Queens.new(white: [1, 1], black: [6, 6]) | |
assert queens.attack? | |
end | |
def test_can_attack_on_other_diagonal | |
queens = Queens.new(white: [0, 6], black: [1, 7]) | |
assert queens.attack? | |
end | |
def test_can_attack_on_yet_another_diagonal | |
queens = Queens.new(white: [4, 1], black: [6, 3]) | |
assert queens.attack? | |
end | |
def test_can_attack_on_a_diagonal_slanted_the_other_way | |
queens = Queens.new(white: [6, 1], black: [1, 6]) | |
assert queens.attack? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment