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 | 1 | ||
| Bugs | 0 | Features | 1 |
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); |
||
| 41 | public function getPossibleMoves(array &$board, array &$hasMoved, int $forAccountID = null, bool $attackingCheck = false) : array { |
||
| 42 | $moves = array(); |
||
| 43 | if ($forAccountID === null || $this->accountID == $forAccountID) { |
||
| 44 | if ($this->pieceID == self::PAWN) { |
||
| 45 | $dirY = $this->colour == ChessGame::PLAYER_BLACK ? 1 : -1; |
||
| 46 | $moveY = $this->y + $dirY; |
||
| 47 | //Pawn forward movement is not attacking - so don't check it if doing an attacking check. |
||
| 48 | if (!$attackingCheck) { |
||
| 49 | if (ChessGame::isValidCoord($this->x, $moveY, $board) && $board[$moveY][$this->x] === null && $this->isSafeMove($board, $hasMoved, $this->x, $moveY)) { |
||
| 50 | $moves[] = array($this->x, $moveY); |
||
| 51 | } |
||
| 52 | $doubleMoveY = $moveY + $dirY; |
||
| 53 | if ($this->y - $dirY == 0 || $this->y - $dirY * 2 == count($board)) { //Double move first move |
||
| 54 | if ($board[$moveY][$this->x] === null && $board[$doubleMoveY][$this->x] === null && $this->isSafeMove($board, $hasMoved, $this->x, $doubleMoveY)) { |
||
| 55 | $moves[] = array($this->x, $doubleMoveY); |
||
| 56 | } |
||
| 57 | } |
||
| 58 | } |
||
| 59 | for ($i = -1; $i < 2; $i += 2) { |
||
| 60 | $moveX = $this->x + $i; |
||
| 61 | if (ChessGame::isValidCoord($moveX, $moveY, $board)) { |
||
| 62 | if ($attackingCheck || |
||
| 63 | ((($hasMoved[ChessPiece::PAWN][0] == $moveX && $hasMoved[ChessPiece::PAWN][1] == $this->y) || |
||
| 64 | ($board[$moveY][$moveX] != null && $board[$moveY][$moveX]->colour != $this->colour)) |
||
| 65 | && $this->isSafeMove($board, $hasMoved, $moveX, $moveY))) { |
||
| 66 | $moves[] = array($moveX, $moveY); |
||
| 67 | } |
||
| 68 | } |
||
| 69 | } |
||
| 70 | } elseif ($this->pieceID == self::KING) { |
||
| 71 | for ($i = -1; $i < 2; $i++) { |
||
| 72 | for ($j = -1; $j < 2; $j++) { |
||
| 73 | if ($i != 0 || $j != 0) { |
||
| 74 | $this->addMove($this->x + $i, $this->y + $j, $board, $moves, $hasMoved, $attackingCheck); |
||
| 75 | } |
||
| 76 | } |
||
| 77 | } |
||
| 78 | //Castling is not attacking - so don't check it if doing an attacking check. |
||
| 79 | if (!$attackingCheck && !$hasMoved[$this->colour][ChessPiece::KING] && !ChessGame::isPlayerChecked($board, $hasMoved, $this->colour)) { |
||
| 80 | if (!$hasMoved[$this->colour][ChessPiece::ROOK]['Queen'] && |
||
| 81 | ChessGame::isValidCoord($this->x - 1, $this->y, $board) && $board[$this->y][$this->x - 1] === null && |
||
| 82 | ChessGame::isValidCoord($this->x - 3, $this->y, $board) && $board[$this->y][$this->x - 3] === null && |
||
| 83 | $this->isSafeMove($board, $hasMoved, $this->x - 1, $this->y)) { |
||
| 84 | $this->addMove($this->x - 2, $this->y, $board, $moves, $hasMoved, $attackingCheck); |
||
| 85 | } |
||
| 86 | if (!$hasMoved[$this->colour][ChessPiece::ROOK]['King'] && |
||
| 87 | ChessGame::isValidCoord($this->x + 1, $this->y, $board) && $board[$this->y][$this->x + 1] === null && |
||
| 88 | $this->isSafeMove($board, $hasMoved, $this->x + 1, $this->y)) { |
||
| 89 | $this->addMove($this->x + 2, $this->y, $board, $moves, $hasMoved, $attackingCheck); |
||
| 90 | } |
||
| 91 | } |
||
| 92 | } elseif ($this->pieceID == self::QUEEN) { |
||
| 93 | $moveX = $this->x; |
||
| 94 | $moveY = $this->y; |
||
| 95 | while ($this->addMove(--$moveX, $moveY, $board, $moves, $hasMoved, $attackingCheck) && $board[$moveY][$moveX] === null); //Left |
||
| 96 | $moveX = $this->x; |
||
| 97 | $moveY = $this->y; |
||
| 98 | while ($this->addMove(++$moveX, $moveY, $board, $moves, $hasMoved, $attackingCheck) && $board[$moveY][$moveX] === null); //Right |
||
| 99 | $moveX = $this->x; |
||
| 100 | $moveY = $this->y; |
||
| 101 | while ($this->addMove($moveX, ++$moveY, $board, $moves, $hasMoved, $attackingCheck) && $board[$moveY][$moveX] === null); //Up |
||
| 102 | $moveX = $this->x; |
||
| 103 | $moveY = $this->y; |
||
| 104 | while ($this->addMove($moveX, --$moveY, $board, $moves, $hasMoved, $attackingCheck) && $board[$moveY][$moveX] === null); //Down |
||
| 105 | $moveX = $this->x; |
||
| 106 | $moveY = $this->y; |
||
| 107 | while ($this->addMove(--$moveX, --$moveY, $board, $moves, $hasMoved, $attackingCheck) && $board[$moveY][$moveX] === null); //Up-Left |
||
| 108 | $moveX = $this->x; |
||
| 109 | $moveY = $this->y; |
||
| 110 | while ($this->addMove(++$moveX, --$moveY, $board, $moves, $hasMoved, $attackingCheck) && $board[$moveY][$moveX] === null); //Up-Right |
||
| 111 | $moveX = $this->x; |
||
| 112 | $moveY = $this->y; |
||
| 113 | while ($this->addMove(--$moveX, ++$moveY, $board, $moves, $hasMoved, $attackingCheck) && $board[$moveY][$moveX] === null); //Down-Left |
||
| 114 | $moveX = $this->x; |
||
| 115 | $moveY = $this->y; |
||
| 116 | while ($this->addMove(++$moveX, ++$moveY, $board, $moves, $hasMoved, $attackingCheck) && $board[$moveY][$moveX] === null); //Up-Left |
||
| 117 | } elseif ($this->pieceID == self::ROOK) { |
||
| 118 | $moveX = $this->x; |
||
| 119 | $moveY = $this->y; |
||
| 120 | while ($this->addMove(--$moveX, $moveY, $board, $moves, $hasMoved, $attackingCheck) && $board[$moveY][$moveX] === null); //Left |
||
| 121 | $moveX = $this->x; |
||
| 122 | $moveY = $this->y; |
||
| 123 | while ($this->addMove(++$moveX, $moveY, $board, $moves, $hasMoved, $attackingCheck) && $board[$moveY][$moveX] === null); //Right |
||
| 124 | $moveX = $this->x; |
||
| 125 | $moveY = $this->y; |
||
| 126 | while ($this->addMove($moveX, ++$moveY, $board, $moves, $hasMoved, $attackingCheck) && $board[$moveY][$moveX] === null); //Up |
||
| 127 | $moveX = $this->x; |
||
| 128 | $moveY = $this->y; |
||
| 129 | while ($this->addMove($moveX, --$moveY, $board, $moves, $hasMoved, $attackingCheck) && $board[$moveY][$moveX] === null); //Down |
||
| 130 | } elseif ($this->pieceID == self::BISHOP) { |
||
| 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::KNIGHT) { |
||
| 144 | $moveX = $this->x - 1; |
||
| 145 | $moveY = $this->y - 2; |
||
| 146 | $this->addMove($moveX, $moveY, $board, $moves, $hasMoved, $attackingCheck); //2up-left |
||
| 147 | $moveX += 2; |
||
| 148 | $this->addMove($moveX, $moveY, $board, $moves, $hasMoved, $attackingCheck); //2up-right |
||
| 149 | $moveY = $this->y + 2; |
||
| 150 | $this->addMove($moveX, $moveY, $board, $moves, $hasMoved, $attackingCheck); //2down-right |
||
| 151 | $moveX -= 2; |
||
| 152 | $this->addMove($moveX, $moveY, $board, $moves, $hasMoved, $attackingCheck); //2down-left |
||
| 153 | $moveX = $this->x - 2; |
||
| 154 | $moveY = $this->y - 1; |
||
| 155 | $this->addMove($moveX, $moveY, $board, $moves, $hasMoved, $attackingCheck); //2left-up |
||
| 156 | $moveY += 2; |
||
| 157 | $this->addMove($moveX, $moveY, $board, $moves, $hasMoved, $attackingCheck); //2left-down |
||
| 158 | $moveX = $this->x + 2; |
||
| 159 | $this->addMove($moveX, $moveY, $board, $moves, $hasMoved, $attackingCheck); //2right-down |
||
| 160 | $moveY -= 2; |
||
| 161 | $this->addMove($moveX, $moveY, $board, $moves, $hasMoved, $attackingCheck); //2right-up |
||
| 162 | } |
||
| 163 | } |
||
| 164 | |||
| 165 | return $moves; |
||
| 166 | } |
||
| 235 |