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

Method placeColor

java/Chapter 8/Question8_8/Board.java:27–56  ·  view source on GitHub ↗
(int row, int column, Color color)

Source from the content-addressed store, hash-verified

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

Callers 1

playPieceMethod · 0.80

Calls 2

flipSectionMethod · 0.95
updateScoreMethod · 0.95

Tested by

no test coverage detected