Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
Total | |
70.00% |
7 / 10 |
|
60.00% |
3 / 5 |
CRAP | |
0.00% |
0 / 1 |
SquareRelations | |
70.00% |
7 / 10 |
|
60.00% |
3 / 5 |
5.68 | |
0.00% |
0 / 1 |
__construct | |
0.00% |
0 / 2 |
|
0.00% |
0 / 1 |
2 | |||
new | |
0.00% |
0 / 1 |
|
0.00% |
0 / 1 |
2 | |||
getDistance | |
100.00% |
5 / 5 |
|
100.00% |
1 / 1 |
1 | |||
haveSameRank | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 | |||
haveSameFile | |
100.00% |
1 / 1 |
|
100.00% |
1 / 1 |
1 |
1 | <?php |
2 | /** |
3 | * This file is a part of ChessBrowser. |
4 | * |
5 | * ChessBrowser is free software: you can redistribute it and/or modify |
6 | * it under the terms of the GNU General Public License as published by |
7 | * the Free Software Foundation, either version 3 of the License, or |
8 | * (at your option) any later version. |
9 | * |
10 | * This program is distributed in the hope that it will be useful, |
11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
13 | * GNU General Public License for more details. |
14 | * |
15 | * You should have received a copy of the GNU General Public License |
16 | * along with this program. If not, see <https://www.gnu.org/licenses/>. |
17 | * |
18 | * @file SquareRelations |
19 | * @ingroup ChessBrowser |
20 | * @author DannyS712 |
21 | */ |
22 | |
23 | namespace MediaWiki\Extension\ChessBrowser; |
24 | |
25 | class SquareRelations { |
26 | |
27 | /** @var int */ |
28 | private $square1; |
29 | |
30 | /** @var int */ |
31 | private $square2; |
32 | |
33 | /** |
34 | * Can be used, but for chaining ::new is probably better |
35 | * |
36 | * @param int $square1 Square as byte number where in Hex notation |
37 | * the first four bits (0xF0) are rank [1-8] |
38 | * and the second 4 bits (0x0F) are file [a-h] |
39 | * @param int $square2 Square as byte number where in Hex notation |
40 | * the first four bits (0xF0) are rank [1-8] |
41 | * and the second 4 bits (0x0F) are file [a-h] |
42 | */ |
43 | public function __construct( int $square1, int $square2 ) { |
44 | $this->square1 = $square1; |
45 | $this->square2 = $square2; |
46 | } |
47 | |
48 | /** |
49 | * For chaining |
50 | * |
51 | * @param int $square1 |
52 | * @param int $square2 |
53 | * @return SquareRelations |
54 | */ |
55 | public static function new( int $square1, int $square2 ): SquareRelations { |
56 | return new SquareRelations( $square1, $square2 ); |
57 | } |
58 | |
59 | /** |
60 | * Get the distance between 2 squares |
61 | * @return int |
62 | */ |
63 | public function getDistance(): int { |
64 | $sq1 = $this->square1; |
65 | $sq2 = $this->square2; |
66 | |
67 | $rankDiff = abs( ( $sq1 & 0xF0 ) - ( $sq2 & 0XF0 ) ) >> 4; |
68 | $fileDiff = abs( ( $sq1 & 0x07 ) - ( $sq2 & 0x07 ) ); |
69 | |
70 | return max( $rankDiff, $fileDiff ); |
71 | } |
72 | |
73 | /** |
74 | * Returns whether two squares are on the same rank. |
75 | * @return bool |
76 | */ |
77 | public function haveSameRank(): bool { |
78 | return ( $this->square1 & 0xF0 ) === ( $this->square2 & 0xF0 ); |
79 | } |
80 | |
81 | /** |
82 | * Returns whether two squares are on the same file |
83 | * @return bool |
84 | */ |
85 | public function haveSameFile(): bool { |
86 | return ( $this->square1 & 0x0F ) === ( $this->square2 & 0x0F ); |
87 | } |
88 | |
89 | } |