Given the position of two queens on a chess board, indicate whether or not they are positioned so that they can attack each other.
In the game of chess, a queen can attack pieces which are on the same row, column, or diagonal.
A chessboard can be represented by an 8 by 8 array.
So if you're told the white queen is at (2, 3) and the black queen at (5, 6), then you'd know you've got a set-up like so:
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ W _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ B _
_ _ _ _ _ _ _ _
_ _ _ _ _ _ _ _
You'd also be able to answer whether the queens can attack each other. In this case, that answer would be yes, they can, because both pieces share a diagonal.
For installation and learning resources, refer to the exercism help page.
For running the tests provided, you will need the Minitest gem. Open a terminal window and run the following command to install minitest:
gem install minitest
If you would like color output, you can require 'minitest/pride'
in
the test file, or note the alternative instruction, below, for running
the test file.
Run the tests from the exercise directory using the following command:
ruby queen_attack_test.rb
To include color from the command line:
ruby -r minitest/pride queen_attack_test.rb
J Dalbey's Programming Practice problems http://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html
It's possible to submit an incomplete solution so you can see how others have completed the exercise.
require 'minitest/autorun'
require_relative 'queen_attack'
# Common test data version: 2.0.0 44a1e12
class QueenAttackTest < Minitest::Test
def test_queen_with_a_valid_position
# skip
assert Queens.new(white: [2, 2])
end
def test_queen_must_have_positive_row
skip
assert_raises ArgumentError do
Queens.new(white: [-2, 2])
end
end
def test_queen_must_have_row_on_board
skip
assert_raises ArgumentError do
Queens.new(white: [8, 4])
end
end
def test_queen_must_have_positive_column
skip
assert_raises ArgumentError do
Queens.new(white: [2, -2])
end
end
def test_queen_must_have_column_on_board
skip
assert_raises ArgumentError do
Queens.new(white: [4, 8])
end
end
def test_can_not_attack
skip
queens = Queens.new(white: [2, 4], black: [6, 6])
refute queens.attack?
end
def test_can_attack_on_same_row
skip
queens = Queens.new(white: [2, 4], black: [2, 6])
assert queens.attack?
end
def test_can_attack_on_same_column
skip
queens = Queens.new(white: [4, 5], black: [2, 5])
assert queens.attack?
end
def test_can_attack_on_first_diagonal
skip
queens = Queens.new(white: [2, 2], black: [0, 4])
assert queens.attack?
end
def test_can_attack_on_second_diagonal
skip
queens = Queens.new(white: [2, 2], black: [3, 1])
assert queens.attack?
end
def test_can_attack_on_third_diagonal
skip
queens = Queens.new(white: [2, 2], black: [1, 1])
assert queens.attack?
end
def test_can_attack_on_fourth_diagonal
skip
queens = Queens.new(white: [2, 2], black: [5, 5])
assert queens.attack?
end
# Problems in exercism evolve over time, as we find better ways to ask
# questions.
# The version number refers to the version of the problem you solved,
# not your solution.
#
# Define a constant named VERSION inside of the top level BookKeeping
# module, which may be placed near the end of your file.
#
# In your file, it will look like this:
#
# module BookKeeping
# VERSION = 1 # Where the version number matches the one in the test.
# end
#
# If you are curious, read more about constants on RubyDoc:
# http://ruby-doc.org/docs/ruby-doc-bundle/UsersGuide/rg/constants.html
def test_bookkeeping
skip
assert_equal 2, BookKeeping::VERSION
end
end
class Queens
def initialize(white: nil, black: nil)
@white = WhiteQueen.new
@black = BlackQueen.new
@chessboard = Chessboard.new({@white => white, @black => black})
end
def to_s
@chessboard.to_s
end
def white
@chessboard.pieces[@white]
end
def black
@chessboard.pieces[@black]
end
def attack?
@chessboard.attack?(@white, @chessboard.pieces[@black])
end
end
class Queen
def starting_position
[0, 0]
end
def symbol
'Q'
end
def attack?(chessboard, my_position, other_position)
conditions = %w(same_column? same_row? diagonal?)
conditions.any? { |condition| chessboard.send(condition, my_position, other_position) }
end
end
class WhiteQueen < Queen
def starting_position
[0, 3]
end
def symbol
'W'
end
end
class BlackQueen < Queen
def starting_position
[7, 3]
end
def symbol
'B'
end
end
class Chessboard
BOARD_SIZE = 8
attr_reader :pieces
def initialize(pieces = {})
@pieces = {}
pieces.each do |piece, piece_position|
if @pieces.values.include?(piece_position)
fail ArgumentError, "Position #{piece_position} already taken"
end
@pieces[piece] = piece_position || piece.starting_position
end
end
def to_s
board = Array.new(BOARD_SIZE) { Array.new(BOARD_SIZE) { '_' } }
@pieces.each do |piece, piece_position|
board[piece_position[0]][piece_position[1]] = piece.symbol
end
board.map { |row| row.join(' ') }.join("\n")
end
def attack?(piece, position)
piece.attack?(self, @pieces[piece], position)
end
def same_column?(position1, position2)
position1[0] == position2[0]
end
def same_row?(position1, position2)
position1[1] == position2[1]
end
def diagonal?(position1, position2)
(position1[0] - position2[0]).abs == (position1[1] - position2[1]).abs
end
end
A huge amount can be learned from reading other people’s code. This is why we wanted to give exercism users the option of making their solutions public.
Here are some questions to help you reflect on this solution and learn the most from it.
Level up your programming skills with 3,450 exercises across 52 languages, and insightful discussion with our volunteer team of welcoming mentors. Exercism is 100% free forever.
Sign up Learn More
Community comments
Am I overengineering this? None of the other solutions have multiple classes.
At first I liked how you made BOARD_SIZE a constant, but now that I think about it more, I might consider having it be a parameter at initialization in the Chessboard Class so that Chessboard can be used to generate a board of any size. It's really the Queens class that requires a board of size 8, so you could set it as a constant there.
Also, I like how you use Array.new to create the empty board. Did you know you can use the array index as a parameter in the block? (I didn't until I did this exercise). So instead of creating the array and then iterating over it again, you could set the piece symbol as you create it