Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
95.45% covered (success)
95.45%
21 / 22
92.86% covered (success)
92.86%
13 / 14
CRAP
0.00% covered (danger)
0.00%
0 / 1
BoardPosition
95.45% covered (success)
95.45%
21 / 22
92.86% covered (success)
92.86%
13 / 14
14
0.00% covered (danger)
0.00%
0 / 1
 pack
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 fromLetters
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 unpackX
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 unpackY
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 toLetters
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 flipX
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 flipY
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 mirror
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 mirrorAround
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 horizontallyMirroredAround
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 verticallyMirroredAround
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 shift
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 diff
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 min
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2
3// the current range of board position is -31 to +31
4// the negative values is to support different transformations related to board comparisons
5class BoardPosition
6{
7    public const ZERO = (32 << 6) + 32;
8
9    public static function pack($x, $y): int
10    {
11        return ($x + 32) << 6 | ($y + 32);
12    }
13
14    public static function fromLetters($x, $y): int
15    {
16        return BoardPosition::pack(ord($x) - ord('a'), ord($y) - ord('a'));
17    }
18
19    public static function unpackX(int $packed): int
20    {
21        return ($packed >> 6) - 32;
22    }
23    public static function unpackY(int $packed): int
24    {
25        return ($packed & 63) - 32;
26    }
27
28    public static function toLetters(int $packed): string
29    {
30        return chr(self::unpackX($packed) + ord('a')) . chr(self::unpackY($packed) + ord('a'));
31    }
32
33    public static function flipX(int $packed, int $size): int
34    {
35        return self::pack($size - 1 - self::unpackX($packed), self::unpackY($packed));
36    }
37
38    public static function flipY(int $packed, int $size): int
39    {
40        return self::pack(self::unpackX($packed), $size - 1 - self::unpackY($packed));
41    }
42
43    public static function mirror(int $packed): int
44    {
45        return self::pack(self::unpackY($packed), self::unpackX($packed));
46    }
47
48    public static function mirrorAround(int $packed, $pivot): int
49    {
50        $pivotX = self::unpackX($pivot);
51        $pivotY = self::unpackY($pivot);
52        $x = BoardPosition::unpackX($packed) - $pivotX + $pivotY;
53        $y = BoardPosition::unpackY($packed) - $pivotY + $pivotX;
54        return BoardPosition::pack($y, $x);
55    }
56
57    public static function horizontallyMirroredAround(int $packed, $pivot): int
58    {
59        $pivotX = self::unpackX($pivot);
60        $x = $pivotX + $pivotX - BoardPosition::unpackX($packed);
61        return BoardPosition::pack($x, BoardPosition::unpackY($packed));
62    }
63
64    public static function verticallyMirroredAround(int $packed, $pivot): int
65    {
66        $pivotY = self::unpackY($pivot);
67        $y = $pivotY + $pivotY - BoardPosition::unpackY($packed);
68        return BoardPosition::pack(BoardPosition::unpackX($packed), $y);
69    }
70
71    public static function shift(int $packed, int $shift): int
72    {
73        return $packed - $shift + BoardPosition::ZERO;
74    }
75
76    public static function diff(int $a, int $b): int
77    {
78        return $a - $b + BoardPosition::ZERO;
79    }
80
81    public static function min(int $packed, int $other): int
82    {
83        return self::pack(min(self::unpackX($packed), self::unpackX($other)), min(self::unpackY($packed), self::unpackY($other)));
84    }
85}