MCPcopy Index your code
hub / github.com/careercup/ctci / flipSection

Method flipSection

java/Chapter 8/Question8_8/Board.java:58–101  ·  view source on GitHub ↗
(int row, int column, Color color, Direction d)

Source from the content-addressed store, hash-verified

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) {

Callers 1

placeColorMethod · 0.95

Calls 2

getColorMethod · 0.45
flipMethod · 0.45

Tested by

no test coverage detected