(int row, int column, Color color)
| 25 | } |
| 26 | |
| 27 | public boolean placeColor(int row, int column, Color color) { |
| 28 | if (board[row][column] != null) { |
| 29 | return false; |
| 30 | } |
| 31 | |
| 32 | /* attempt to flip each of the four directions */ |
| 33 | int[] results = new int[4]; |
| 34 | results[0] = flipSection(row - 1, column, color, Direction.up); |
| 35 | results[1] = flipSection(row + 1, column, color, Direction.down); |
| 36 | results[2] = flipSection(row, column + 1, color, Direction.right); |
| 37 | results[3] = flipSection(row, column - 1, color, Direction.left); |
| 38 | |
| 39 | /* compute how many pieces were flipped */ |
| 40 | int flipped = 0; |
| 41 | for (int result : results) { |
| 42 | if (result > 0) { |
| 43 | flipped += result; |
| 44 | } |
| 45 | } |
| 46 | |
| 47 | /* if nothing was flipped, then it's an invalid move */ |
| 48 | if (flipped < 0) { |
| 49 | return false; |
| 50 | } |
| 51 | |
| 52 | /* flip the piece, and update the score */ |
| 53 | board[row][column] = new Piece(color); |
| 54 | updateScore(color, flipped + 1); |
| 55 | return true; |
| 56 | } |
| 57 | |
| 58 | private int flipSection(int row, int column, Color color, Direction d) { |
| 59 | /* Compute the delta for the row and the column. At all times, only the row or the column |
no test coverage detected