(int row, int column, Color color, Direction d)
| 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 |
| 60 | * will have a delta, since we're only moving in one direction at a time. |
| 61 | */ |
| 62 | int r = 0; |
| 63 | int c = 0; |
| 64 | switch (d) { |
| 65 | case up: |
| 66 | r = -1; |
| 67 | break; |
| 68 | case down: |
| 69 | r = 1; |
| 70 | break; |
| 71 | case left: |
| 72 | c = -1; |
| 73 | break; |
| 74 | case right: |
| 75 | c = 1; |
| 76 | break; |
| 77 | } |
| 78 | |
| 79 | /* If out of bounds, or nothing to flip, return an error (-1) */ |
| 80 | if (row < 0 || row >= board.length || column < 0 || column >= board[row].length || board[row][column] == null) { |
| 81 | return -1; |
| 82 | } |
| 83 | |
| 84 | /* Found same color - return nothing flipped */ |
| 85 | if (board[row][column].getColor() == color) { |
| 86 | return 0; |
| 87 | } |
| 88 | |
| 89 | /* Recursively flip the remainder of the row. If -1 is returned, then we know we hit the boundary |
| 90 | * of the row (or a null piece) before we found our own color, so there's nothing to flip. Return |
| 91 | * the error code. |
| 92 | */ |
| 93 | int flipped = flipSection(row + r, column + c, color, d); |
| 94 | if (flipped < 0) { |
| 95 | return -1; |
| 96 | } |
| 97 | |
| 98 | /* flip our own color */ |
| 99 | board[row][column].flip(); |
| 100 | return flipped + 1; |
| 101 | } |
| 102 | |
| 103 | public int getScoreForColor(Color c) { |
| 104 | if (c == Color.Black) { |
no test coverage detected