Avatar of HadiModarres

HadiModarres's solution

to Queen Attack in the PHP Track

Published at Aug 01 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

function canAttack(array $q1,array $q2){
   if ($q1[0]===$q2[0] || $q1[1]===$q2[1]){
       return true;
   }
   if (abs($q1[0]-$q2[0]) === abs($q1[1]-$q2[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?