 # maisiec's solution

## to Queen Attack in the PHP Track

Published at Sep 10 2019 · 0 comments
Instructions
Test suite
Solution

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.

## Running the tests

1. Go to the root of your PHP exercise directory, which is `<EXERCISM_WORKSPACE>/php`. To find the Exercism workspace run

`````` % exercism debug | grep Workspace
``````
2. Get PHPUnit if you don't have it already.

`````` % wget --no-check-certificate https://phar.phpunit.de/phpunit.phar
% chmod +x phpunit.phar
``````
3. Execute the tests:

`````` % ./phpunit.phar queen-attack/queen-attack_test.php
``````

## Source

J Dalbey's Programming Practice problems http://users.csc.calpoly.edu/~jdalbey/103/Projects/ProgrammingPractice.html

## Submitting Incomplete Solutions

It's possible to submit an incomplete solution so you can see how others have completed the exercise.

### queen-attack_test.php

``````<?php

class QueenAttackTest extends PHPUnit\Framework\TestCase
{
public static function setUpBeforeClass() : void
{
require_once 'queen-attack.php';
}

/**
* Test a queen is placed in a valid position.
*/
public function testCreateQueenWithValidPosition() : void
{
\$this->assertTrue(placeQueen(2, 2));
}

/**
* Test the queen is placed on a positive rank.
*/
public function testQueenHasPositiveRank() : void
{
\$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() : void
{
\$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() : void
{
\$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() : void
{
\$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() : void
{
\$this->assertFalse(canAttack([2, 4], [6, 6]));
}

/**
* Test if queens can attack each other on the same rank.
*/
public function testQueensCanAttackOnSameRank() : void
{
\$this->assertTrue(canAttack([2, 4], [2, 6]));
}

/**
* Test if queens can attack each other on the same file.
*/
public function testQueensCanAttackOnSameFile() : void
{
\$this->assertTrue(canAttack([4, 5], [2, 5]));
}

/**
* Test if queens can attack each other on the first diagonal.
*/
public function testQueensCanAttackOnFirstDiagonal() : void
{
\$this->assertTrue(canAttack([2, 2], [0, 4]));
}

/**
* Test if queens can attack each other on the second diagonal.
*/
public function testQueensCanAttackOnSecondDiagonal() : void
{
\$this->assertTrue(canAttack([2, 2], [3, 1]));
}

/**
* Test if queens can attack each other on the third diagonal.
*/
public function testQueensCanAttackOnThirdDiagonal() : void
{
\$this->assertTrue(canAttack([2, 2], [1, 1]));
}

/**
* Test if queens can attack each other on the fourth diagonal.
*/
public function testQueensCanAttackOnFourthDiagonal() : void
{
\$this->assertTrue(canAttack([2, 2], [5, 5]));
}
}``````
``````<?php

/**
* 	Check queen is placed in a valid position.
*/
function placeQueen(int \$x, int \$y) : bool
{
if (\$x < 0 || \$y < 0) {
throw new InvalidArgumentException("The rank and file numbers must be positive.");
}

if (\$x >= 8 || \$y >= 8) {
throw new InvalidArgumentException("The position must be on a standard size chess board.");
}

return true;
}

/**
* 	Check if 2 queens can attack each other from their current position
*/
function canAttack(\$wQueen, \$bQueen)
{
placeQueen(\$wQueen, \$wQueen);
placeQueen(\$bQueen, \$bQueen);
if (onRankOrFile(\$wQueen, \$bQueen) ||
onRankOrFile(\$wQueen, \$bQueen) ||
onDiagonal(\$wQueen, \$wQueen, \$bQueen, \$bQueen)
) {
return true;
}

return false;
}

/**
* 	Check queen is placed on same line
*/
function onRankOrFile(int \$wXY, int \$bXY) : bool
{
if (\$wXY === \$bXY) {
return true;
}

return false;
}

/**
* 	Check queen is placed on same diagonal line
*/
function onDiagonal(\$wRank, \$wFile, \$bRank, \$bFile)
{
\$wPosition = abs((\$wRank - \$bRank));
\$bPosition = abs((\$wFile - \$bFile));

if (\$wPosition === \$bPosition) {
return true;
}

return false;
}``````

### What can you learn from this solution?

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.

• What compromises have been made?