Avatar of Tyresius92

Tyresius92's solution

to Queen Attack in the PHP Track

Published at May 12 2019 · 0 comments
Instructions
Test suite
Solution

Note:

This exercise has changed since this solution was written.

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

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.
     *
     * @expectedException InvalidArgumentException
     * @expectedExceptionMessage The rank and file numbers must be positive.
     */
    public function testQueenHasPositiveRank()
    {
        placeQueen(-2, 2);
    }

    /**
     * Test the queen has a rank on the board.
     *
     * @expectedException InvalidArgumentException
     * @expectedExceptionMessage The position must be on a standard size chess board.
     */
    public function testQueenHasRankOnBoard()
    {
        placeQueen(8, 4);
    }

    /**
     * Test the queen is placed on a positive file.
     *
     * @expectedException InvalidArgumentException
     * @expectedExceptionMessage The rank and file numbers must be positive.
     */
    public function testQueenHasPositiveFile()
    {
        placeQueen(2, -2);
    }

    /**
     * Test the queen has a file on the board.
     *
     * @expectedException InvalidArgumentException
     * @expectedExceptionMessage The position must be on a standard size chess board.
     */
    public function testQueenHasFileOnBoard()
    {
        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

// this is not a very good name for this function
// I would not expect it to return a bool
function placeQueen($rank, $file) {
    if ($rank < 0 || $file < 0) {
        throw new InvalidArgumentException("The rank and file numbers must be positive.");
    }

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

    return true;
}

function canAttack($whitePos, $blackPos) {
    placeQueen($whitePos[0], $whitePos[1]);
    placeQueen($blackPos[0], $blackPos[1]);

    // horizontal or vertical attack
    if ($whitePos[0] === $blackPos[0] || $whitePos[1] === $blackPos[1]) {
        return true;
    }

    // diagonal attack
    if (abs($whitePos[0] - $blackPos[0]) === abs($whitePos[1] - $blackPos[1])) {
        return true; 
    }

    return false;
}

Community comments

Find this solution interesting? Ask the author a question to learn more.

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?
  • Are there new concepts here that you could read more about to improve your understanding?