We could not synchronize checks via GitHub's checks API since Scrutinizer's GitHub App is not installed for this repository.
Conditions | 76 |
Paths | 25 |
Total Lines | 125 |
Code Lines | 106 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php declare(strict_types=1); |
||
67 | public function getPossibleMoves(array $board, array $hasMoved, Colour $forColour = null, bool $attackingCheck = false): array { |
||
68 | $moves = []; |
||
69 | if ($forColour === null || $this->colour === $forColour) { |
||
70 | if ($this->pieceID == self::PAWN) { |
||
71 | $dirY = $this->colour == Colour::Black ? 1 : -1; |
||
72 | $moveY = $this->y + $dirY; |
||
73 | //Pawn forward movement is not attacking - so don't check it if doing an attacking check. |
||
74 | if (!$attackingCheck) { |
||
75 | if (ChessGame::isValidCoord($this->x, $moveY, $board) && $board[$moveY][$this->x] === null && $this->isSafeMove($board, $hasMoved, $this->x, $moveY)) { |
||
76 | $moves[] = [$this->x, $moveY]; |
||
77 | } |
||
78 | $doubleMoveY = $moveY + $dirY; |
||
79 | if ($this->y - $dirY == 0 || $this->y - $dirY * 2 == count($board)) { //Double move first move |
||
80 | if ($board[$moveY][$this->x] === null && $board[$doubleMoveY][$this->x] === null && $this->isSafeMove($board, $hasMoved, $this->x, $doubleMoveY)) { |
||
81 | $moves[] = [$this->x, $doubleMoveY]; |
||
82 | } |
||
83 | } |
||
84 | } |
||
85 | for ($i = -1; $i < 2; $i += 2) { |
||
86 | $moveX = $this->x + $i; |
||
87 | if (ChessGame::isValidCoord($moveX, $moveY, $board)) { |
||
88 | if ($attackingCheck || |
||
89 | ((($hasMoved[self::PAWN][0] == $moveX && $hasMoved[self::PAWN][1] == $this->y) || |
||
90 | ($board[$moveY][$moveX] != null && $board[$moveY][$moveX]->colour != $this->colour)) |
||
91 | && $this->isSafeMove($board, $hasMoved, $moveX, $moveY))) { |
||
92 | $moves[] = [$moveX, $moveY]; |
||
93 | } |
||
94 | } |
||
95 | } |
||
96 | } elseif ($this->pieceID == self::KING) { |
||
97 | for ($i = -1; $i < 2; $i++) { |
||
98 | for ($j = -1; $j < 2; $j++) { |
||
99 | if ($i != 0 || $j != 0) { |
||
100 | $this->addMove($this->x + $i, $this->y + $j, $board, $moves, $hasMoved, $attackingCheck); |
||
101 | } |
||
102 | } |
||
103 | } |
||
104 | //Castling is not attacking - so don't check it if doing an attacking check. |
||
105 | if (!$attackingCheck && !$hasMoved[$this->colour->value][self::KING] && !ChessGame::isPlayerChecked($board, $hasMoved, $this->colour)) { |
||
106 | if (!$hasMoved[$this->colour->value][self::ROOK]['Queen'] && |
||
107 | ChessGame::isValidCoord($this->x - 1, $this->y, $board) && $board[$this->y][$this->x - 1] === null && |
||
108 | ChessGame::isValidCoord($this->x - 3, $this->y, $board) && $board[$this->y][$this->x - 3] === null && |
||
109 | $this->isSafeMove($board, $hasMoved, $this->x - 1, $this->y)) { |
||
110 | $this->addMove($this->x - 2, $this->y, $board, $moves, $hasMoved, $attackingCheck); |
||
111 | } |
||
112 | if (!$hasMoved[$this->colour->value][self::ROOK]['King'] && |
||
113 | ChessGame::isValidCoord($this->x + 1, $this->y, $board) && $board[$this->y][$this->x + 1] === null && |
||
114 | $this->isSafeMove($board, $hasMoved, $this->x + 1, $this->y)) { |
||
115 | $this->addMove($this->x + 2, $this->y, $board, $moves, $hasMoved, $attackingCheck); |
||
116 | } |
||
117 | } |
||
118 | } elseif ($this->pieceID == self::QUEEN) { |
||
119 | $moveX = $this->x; |
||
120 | $moveY = $this->y; |
||
121 | while ($this->addMove(--$moveX, $moveY, $board, $moves, $hasMoved, $attackingCheck) && $board[$moveY][$moveX] === null); //Left |
||
122 | $moveX = $this->x; |
||
123 | $moveY = $this->y; |
||
124 | while ($this->addMove(++$moveX, $moveY, $board, $moves, $hasMoved, $attackingCheck) && $board[$moveY][$moveX] === null); //Right |
||
125 | $moveX = $this->x; |
||
126 | $moveY = $this->y; |
||
127 | while ($this->addMove($moveX, ++$moveY, $board, $moves, $hasMoved, $attackingCheck) && $board[$moveY][$moveX] === null); //Up |
||
128 | $moveX = $this->x; |
||
129 | $moveY = $this->y; |
||
130 | while ($this->addMove($moveX, --$moveY, $board, $moves, $hasMoved, $attackingCheck) && $board[$moveY][$moveX] === null); //Down |
||
131 | $moveX = $this->x; |
||
132 | $moveY = $this->y; |
||
133 | while ($this->addMove(--$moveX, --$moveY, $board, $moves, $hasMoved, $attackingCheck) && $board[$moveY][$moveX] === null); //Up-Left |
||
134 | $moveX = $this->x; |
||
135 | $moveY = $this->y; |
||
136 | while ($this->addMove(++$moveX, --$moveY, $board, $moves, $hasMoved, $attackingCheck) && $board[$moveY][$moveX] === null); //Up-Right |
||
137 | $moveX = $this->x; |
||
138 | $moveY = $this->y; |
||
139 | while ($this->addMove(--$moveX, ++$moveY, $board, $moves, $hasMoved, $attackingCheck) && $board[$moveY][$moveX] === null); //Down-Left |
||
140 | $moveX = $this->x; |
||
141 | $moveY = $this->y; |
||
142 | while ($this->addMove(++$moveX, ++$moveY, $board, $moves, $hasMoved, $attackingCheck) && $board[$moveY][$moveX] === null); //Up-Left |
||
143 | } elseif ($this->pieceID == self::ROOK) { |
||
144 | $moveX = $this->x; |
||
145 | $moveY = $this->y; |
||
146 | while ($this->addMove(--$moveX, $moveY, $board, $moves, $hasMoved, $attackingCheck) && $board[$moveY][$moveX] === null); //Left |
||
147 | $moveX = $this->x; |
||
148 | $moveY = $this->y; |
||
149 | while ($this->addMove(++$moveX, $moveY, $board, $moves, $hasMoved, $attackingCheck) && $board[$moveY][$moveX] === null); //Right |
||
150 | $moveX = $this->x; |
||
151 | $moveY = $this->y; |
||
152 | while ($this->addMove($moveX, ++$moveY, $board, $moves, $hasMoved, $attackingCheck) && $board[$moveY][$moveX] === null); //Up |
||
153 | $moveX = $this->x; |
||
154 | $moveY = $this->y; |
||
155 | while ($this->addMove($moveX, --$moveY, $board, $moves, $hasMoved, $attackingCheck) && $board[$moveY][$moveX] === null); //Down |
||
156 | } elseif ($this->pieceID == self::BISHOP) { |
||
157 | $moveX = $this->x; |
||
158 | $moveY = $this->y; |
||
159 | while ($this->addMove(--$moveX, --$moveY, $board, $moves, $hasMoved, $attackingCheck) && $board[$moveY][$moveX] === null); //Up-Left |
||
160 | $moveX = $this->x; |
||
161 | $moveY = $this->y; |
||
162 | while ($this->addMove(++$moveX, --$moveY, $board, $moves, $hasMoved, $attackingCheck) && $board[$moveY][$moveX] === null); //Up-Right |
||
163 | $moveX = $this->x; |
||
164 | $moveY = $this->y; |
||
165 | while ($this->addMove(--$moveX, ++$moveY, $board, $moves, $hasMoved, $attackingCheck) && $board[$moveY][$moveX] === null); //Down-Left |
||
166 | $moveX = $this->x; |
||
167 | $moveY = $this->y; |
||
168 | while ($this->addMove(++$moveX, ++$moveY, $board, $moves, $hasMoved, $attackingCheck) && $board[$moveY][$moveX] === null); //Up-Left |
||
169 | } elseif ($this->pieceID == self::KNIGHT) { |
||
170 | $moveX = $this->x - 1; |
||
171 | $moveY = $this->y - 2; |
||
172 | $this->addMove($moveX, $moveY, $board, $moves, $hasMoved, $attackingCheck); //2up-left |
||
173 | $moveX += 2; |
||
174 | $this->addMove($moveX, $moveY, $board, $moves, $hasMoved, $attackingCheck); //2up-right |
||
175 | $moveY = $this->y + 2; |
||
176 | $this->addMove($moveX, $moveY, $board, $moves, $hasMoved, $attackingCheck); //2down-right |
||
177 | $moveX -= 2; |
||
178 | $this->addMove($moveX, $moveY, $board, $moves, $hasMoved, $attackingCheck); //2down-left |
||
179 | $moveX = $this->x - 2; |
||
180 | $moveY = $this->y - 1; |
||
181 | $this->addMove($moveX, $moveY, $board, $moves, $hasMoved, $attackingCheck); //2left-up |
||
182 | $moveY += 2; |
||
183 | $this->addMove($moveX, $moveY, $board, $moves, $hasMoved, $attackingCheck); //2left-down |
||
184 | $moveX = $this->x + 2; |
||
185 | $this->addMove($moveX, $moveY, $board, $moves, $hasMoved, $attackingCheck); //2right-down |
||
186 | $moveY -= 2; |
||
187 | $this->addMove($moveX, $moveY, $board, $moves, $hasMoved, $attackingCheck); //2right-up |
||
188 | } |
||
189 | } |
||
190 | |||
191 | return $moves; |
||
192 | } |
||
276 |