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.
Go to the root of your PHP exercise directory, which is <EXERCISM_WORKSPACE>/php
.
To find the Exercism workspace run
% exercism debug | grep Workspace
Get PHPUnit if you don't have it already.
% wget --no-check-certificate https://phar.phpunit.de/phpunit.phar
% chmod +x phpunit.phar
Execute the tests:
% ./phpunit.phar queen-attack/queen-attack_test.php
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.
<?php
require "queen-attack.php";
class QueenAttackTest extends PHPUnit\Framework\TestCase
{
/**
* Test a queen is placed in a valid position.
*/
public function testCreateQueenWithValidPosition()
{
$this->assertTrue(placeQueen(2, 2));
}
/**
* Test the queen is placed on a positive rank.
*/
public function testQueenHasPositiveRank()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The rank and file numbers must be positive.');
placeQueen(-2, 2);
}
/**
* Test the queen has a rank on the board.
*/
public function testQueenHasRankOnBoard()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The position must be on a standard size chess board.');
placeQueen(8, 4);
}
/**
* Test the queen is placed on a positive file.
*/
public function testQueenHasPositiveFile()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The rank and file numbers must be positive.');
placeQueen(2, -2);
}
/**
* Test the queen has a file on the board.
*/
public function testQueenHasFileOnBoard()
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The position must be on a standard size chess board.');
placeQueen(4, 8);
}
/**
* Test if queens can attack each other.
*/
public function testQueensCanAttack()
{
$this->assertFalse(canAttack([2, 4], [6, 6]));
}
/**
* Test if queens can attack each other on the same rank.
*/
public function testQueensCanAttackOnSameRank()
{
$this->assertTrue(canAttack([2, 4], [2, 6]));
}
/**
* Test if queens can attack each other on the same file.
*/
public function testQueensCanAttackOnSameFile()
{
$this->assertTrue(canAttack([4, 5], [2, 5]));
}
/**
* Test if queens can attack each other on the first diagonal.
*/
public function testQueensCanAttackOnFirstDiagonal()
{
$this->assertTrue(canAttack([2, 2], [0, 4]));
}
/**
* Test if queens can attack each other on the second diagonal.
*/
public function testQueensCanAttackOnSecondDiagonal()
{
$this->assertTrue(canAttack([2, 2], [3, 1]));
}
/**
* Test if queens can attack each other on the third diagonal.
*/
public function testQueensCanAttackOnThirdDiagonal()
{
$this->assertTrue(canAttack([2, 2], [1, 1]));
}
/**
* Test if queens can attack each other on the fourth diagonal.
*/
public function testQueensCanAttackOnFourthDiagonal()
{
$this->assertTrue(canAttack([2, 2], [5, 5]));
}
}
<?php
function placeQueen(int $file, int $rank): bool
{
if ($file < 0 || $rank < 0) {
throw new InvalidArgumentException('The rank and file numbers must be positive.');
}
if ($file > 7 || $rank > 7) {
throw new InvalidArgumentException('The position must be on a standard size chess board.');
}
return true;
}
function canAttack(array $coords1, array $coords2): bool
{
[$x1, $y1] = $coords1;
[$x2, $y2] = $coords2;
return ($x1 === $x2 || $y1 === $y2 || abs($x1 - $x2) === abs($y1 - $y2));
}
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,091 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